RushHour program in C

profilerogtheman12345
rushhour_program_in_c.docx

Write a program to play the game of RushHour, such as shown on  this page . Running your program should look like the following, where user input is shown in bold:

Welcome to the traffic game!

Move the vehicles so that the Red car (RR) can

exit the board to the right. Each move should be

of the form:   CDN   where  C  is the vehicle to

be moved,  D  is the direction (u for up, d for down,

l for left or r for right), and  N  is the number of

squares to move it.  For example GR2  means move the

G vehicle to the right 2 squares.  Lower-case input

such as   gr2   is also accepted.  Enter x to exit the

program.

- - - - - - - -

| G G . . . Y |

| P . . B . Y |

| P R R B . Y >

| P . . B . . |

| O . . . T T |

| O . F F F . |

- - - - - - - -

Your move -> gr1

- - - - - - - -

| . G G . . Y |

| P . . B . Y |

| P R R B . Y >

| P . . B . . |

| O . . . T T |

| O . F F F . |

- - - - - - - -

Your move -> 

Thank you for playing.  Exiting...

Press any key to continue . . .

Notes

1. Information on placement of pieces to be used for the board is read in from a file called board.txt.  The format of information should be just like the board that is displayed initially, except without the extra spaces, so for the sample board shown above it would look like:

--------

|GG...Y|

|P..B.Y|

|PRRB.Y>

|P..B..|

|O...TT|

|O.FFF.|

--------

2. You may assume the user enters either upper-case or lower-case input, with or without intervening spaces.

3. The program ends when the red car touches the '>' character to the right of it.

When making a move for program 3, we want to accept input in lower-case, upper-case, and with various options of spacing.  The key here is to ignore leading space when reading character input.  This will skip over blanks, tabs, and even carriage-return characters.  The code for this is something like the following:

char vehicle;

char direction;

char distance;

char distanceValue;

printf("Enter your move (e.g. ar2): ");

// skip leading spaces by putting a space in front of the %c in the format string

scanf(" %c %c %c", &vehicle, &direction, &distance);

// Now convert the distance char into a numerical value

distanceValue = distance - '0';

I'm using a 2D array for this project (i.e.board[8][8]). I am not finished with my code, but I've made substantial progress. With that said, if you have a question on how to implement a 2D array (issues with printing the board and null overriding), feel free to ask.

 

I won't give code, but I will give hints and suggestions. One of my group's problems yesterday was that the printed version had shifted characters from board[0][0] through [0][8] over to the right most side of the printed outcome. It is important to know that in the initial printf statement of board (as a nested for loop), that the rows iterate through 8, and the columns iterate through 9.

 

Keep in mind that is only true if you treat those special border characters as part of the read-in. Another small hint those read through values will change for different functions (i.e. when you want to read through the file to determine valid car movement).