Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this questionFor a program I'm required to fill a bunch of variables relating to a grid. On the grid different char
s represent different cars. Each car can be represented by 2 or 3 char
s in a line (either vertical or horizontal).
/* Structure to hold information about a car */
typedef struct
{
/* The location of the front of the car. As all cars are assumed
to be facing either north or west, this location represents
the northwest segment of the car */
LOCATION front;
/* The orientation of the car */
ORIENTATION orientation;
/* The length of the car */
int length;
} CARINFO;
I want to go through a for loop cycling through all the different cars (type CARINFO
) and fill the variables such as length and front. I have all the logic for this for example:
if ((carpark.grid[i + 1][j] == car && carpark.grid[i - 1][j] == car) || // (left a开发者_如何学JAVAnd right) OR
(carpark.grid[i][j + 1] == car && carpark.grid[i][j - 1] == car)) // (up and down)
CAR.length = 3;
I have used car
as a char
and CAR
as a CARINFO
.
If I had struct CARINFO a
; how would I make car = a
and do the same for all the other cars?
I'm not going to provide you with code, but an idea. What you should do is, well first basically a loop on the rows, inside it a loop on the columns:
for (i = 0; i < grid_row_size; ++i)
for (j = 0; j < grid_col_size; ++j)
Then you should detect a car (like the code you provided, but make sure you take care of boundaries. So for example when i==0
you shouldn't check grid[i-1][j]
.
Then, whenever you found a car, you set it's length, position and orientation they way you like and add it to a list (array) of CARINFO.
Finally, to make sure you don't find a car twice, whenever you find a car on the grid, you go over it and mark it as car_already_seen
.
精彩评论