Prog 3: Traffic
Additions to this description added 2/27 shown with gray background.
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:
Author: Dale Reed
Lab: Wed 5am
Program: #3, Traffic
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
( 0 points) 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.|
--------
( 15 points) Alternatively board.txt could hold a different set of pieces with a different number of vehicles, which should still work with your program, such as the board shown below:
--------
|GGO.CC|
|PPO..Y|
|LRR..Y>
|LBBB.Y|
|L..FDD|
|KK.FEE|
--------
Your program should handle board pieces for any letter of the alphabet, except 'X' which will never represent a vehicle since it is needed to exit the program. You may assume the board is always correctly formatted in the input file and that it will always be a 6x6 board with the border pieces around it.
( 15 points) Correct user input should result in moves being made and reflected when the board is redisplayed.
This functionality is essential! Your program will be given a grade of 0 if you do not allow actually making moves.
( 5 points) You may assume the user enters either upper-case or lower-case input, with or without intervening spaces.
( 5 points) The program ends when the red car touches the '>' character to the right of it.
You need to give an error message and allow the user to retry giving user input in the following situations:
( 5 points) The vehicle is not on the board, such as choosing vehicle N in the above examples
( 5 points) The vehicle is attempted to be moved into a position where there is already another vehicle.
( 5 points) The move attempts to move the vehicle in a direction it is not supposed to go, such as attempting to move G up or down in the above examples.
Having trouble making progress in writing this program? Consider breaking down the problem as follows:
How to read the board pieces from board.txt
See the code samples covered in class to read in characters from a file, such as MacbethCount.c In the case of this program, read the characters from the file into the board array.
The main loop
From within main you should have a loop with the main components of your program, each of which is in a function that gets called. It should look something like:
// display id info // display instructions // loop while program is not done // display board // prompt for and get user input for move // make move
Breaking down the problem into smaller, more easily solvable pieces.
If figuring out how to make a move is giving you trouble, break down that problem into successively simpler but related ideas, then build up your solution one step at a time. For instance you could solve that bigger problem by solving each of the following in turn:
Figure out how to "make a move" on a tiny board of only two squares, such as:
0 1 <-array index ---------| G | |---------
Think of the instructions required to move 'G' from square 0 to square 1. The instructions should be something like:
board[ 1] = board[ 0]; board[0] = ' ';
Next consider a larger board, where again there is only a single 'G' somewhere, to be moved to the right a single square. First you would have to write the code to find the 'G' in the array, and store that location. Then again make the move, as in the previous step.
Now go back to the simple case, but this time consider how you might "make a move" where the vehicle is either a single 'G' or two letters "GG" to be moved one position to the right:
0 1 2 <-array index -------------| G | G | |-------------
Here consider how to write the code to move both 'G' characters one square to the right. Merge this with your previous code so that it works for either case, where your code figures out if there is a single 'G' or two 'G' characters. While you are at it, consider handling vehicles up to 3 characters long, which is the maximum we need for this game.
Once again think about how to solve this so that it is again a one or two character vehicle, but this time found anywhere on the board, again thinking only about moving that vehicle one character to the right.
Once you have the above done, think about how to move a vehicle up to 4 characters to the right. Add the code to make sure there are no other vehicles in the way and that it doesn't go off the edge of the board.
Now think about how you can make your code general, so that instead of adding 1 to the position of a character, you add n to it, where n could be 1 (to move right) or -1 (to move left).
Now do something similar to the previous step, but handle vehicles that move up and down rather than left and right.
To move a vehicle n multiple squares, consider writing code to move it only 1 square, then repeat that n times. Each time you make the move you should make sure the move is valid, that is to say it is not off the end of the board and does not go into a square that is already occupied. One way to handle this is to make a copy of the board and then attempt to make the move. If the move is successful on the copy of the board, then copy that new board back on top of the original board. If the move is invalid, the original board is still there, unchanged.