xx123
Before you do anything else, please REMOVE from int main() your block of code
1 2 3 4 5 6 7
|
while(currentChar <= 'Z' && i < M && j < N)
{ bool can_go_left();
bool can_go_right();
bool can_go_up();
bool can_go_down();
currentChar++;
}
|
It makes no sense and it's in the wrong place (before you have even initialised the grid).
Next, have a look at your functions can_go_left() etc.
- these return either 'true' or 'false': remember this;
- you will get everyone confused if the i and j indices in these functions are in order grid[j][i], whereas in print_grid and initialize_grid they are in the opposite order, grid[i][j]: I strongly recommend changing this. Also, check these functions carefully: they will be crucial later on. Note also that 'left' and 'right' will be OK, but 'up' and 'down' could refer to the actual index increasing and decreasing, which (given the perfectly reasonable order in which the grid is printed) will not be the same as the direction on the printed page: be aware of this, at least.
In int main() You have variables i and j to store current position and currentChar to store the character. These are correctly initialised, so all good here. Why don't you, at this stage, assign this starting point on the grid and print the grid; with your new values M=10 and N=6 this should look like the following:
A.....
......
......
......
......
......
......
......
......
...... |
If this is OK, then move on.
Try just ONE move first. You need to generate a random number - either amongst the values 0,1,2,3 say, or 1,2,3,4 say - entirely your choice. You must decide for yourself which of those numbers correspond to left, which to right, which to up, which to down. (Entirely free choice - but you must stick to it thereafter.) While testing, output this number to check that it is OK.
Now you can use either a switch or an 'if' block to respond to the particular number and its corresponding direction. Suppose it corresponded to moving left - you would then check the can_move_left(..) function. If it returns false then you do nothing; if it returns true then you can: increment i and j, update the grid, update currentChar.
After the first move the top left corner of your grid can only look like one of the following (the first corresponds to an impossible move which would have gone through left or top boundaries).
Once this is done, you can then try a SMALL, FIXED number of moves. While testing, print out any variables you need and the grid at the end of each step. Note that your results will be different with each run if you are using random numbers. (While developing, you could ask the user to put in prescribed numbers instead, so as to test and debug.)
Here is an example after a few moves (some of the projected moves not being possible).
ABC...
..DE..
...F..
......
......
......
......
......
......
...... |
Finally, you can write a loop that runs to completion. Your condition for stopping will be
currentChar has reached (or gone beyond) 'Z'
OR
you are stuck (i.e. can_go_left AND can_go_right AND can_go_down AND can_go_up all return false)
Think about the logic statement that encompasses this.
Some suggestions for code development:
- don't add too much code at once; compile regularly and don't move on if not compiling;
- print out the values of any variables to see what is going on; you can remove this in the final version;
- track the calculations and output with pen and paper.
Good luck!