We initialized a random city with any desired number of intersections, filling it with the marker ('-'). We then printed the city using the p_city
function. Initially, we used the srand()
function to randomly assign intersections ('#') in the city. We employed the validmove
function to check the correctness of the vehicle's movement in a given direction, considering the city's constraints and intersections. After confirming a valid move, we used the movecar
function to randomly relocate the vehicle to a new valid direction, placing it in the new location with the marker ('*') and removing the vehicle's previous markers from the old locations. We used the printcity
function to print the city at each stage of the simulation. The main
function takes input, creates vehicles at random locations in the city, and moves them in each step using the movecar
function. We also utilized the clearscreen
function to clear the screen before printing the city in each stage, and the sleep
function was used to pause the program for a short period to better observe the vehicle movements.
- define constants are defined with
values of 20
. These constants are used to define the dimensions of the city array.
- The
ClearScreen()
function is one that clears the console screen.
- The city_p() function initializes the city array with dashes ('-') and randomly places a specified number of intersections, denoted by '#', in the city array
- The validmove() function checks whether a movement is valid for a car, considering its current position, direction, and the city array. It verifies if the movement is within the city limits and not blocked by an intersection (denoted by '#').
- The movecar() function moves a car within the city array. It generates a random direction and continues to generate new directions until a valid movement is found. Then, it updates the car's position in the city array.
- The printcity() function prints the current state of the city array.
- "In the main() function, the program asks the user to enter the number of cars, taghato, and iterations. It then initializes the city array and places the taghato using the citytaghato_p() function. It also dynamically allocates a two-dimensional array, negah_car, to store the positions of the cars. In the loop, each car is moved using the movecar() function, and its position in the negah_car array is updated. Then, it prints the current state of the city using printcity(), clears the screen, and repeats for a specified number of iterations. Finally, it frees the memory dynamically allocated for the negah_car array before returning from the program."