Write an ASCII graphics version of the Ricochet Robots board game created by Alex Randolph. Each move should consist of clearing the screen and redisplaying the board. Running the game should look like the following:
Author: Dale Reed Program 3: Robots in C TA: Billie Joe Armstrong, T 6:00 AM UIC CS 141, September 2015 Welcome to Ricochet Robots. This was originally a board game created by Alex Randolph. The object of the game is to get the designated numbered robot to the indicated letter position. The numbered squares are all robots. They unfortunately have no brakes, and so will continue in whatever direction they are moving until they encounter an obstacle. For each move enter the robot number and the desired direction. For instance entering 1 U would move the #1 robot as far up as it can go. The first letter of input is the robot number (1 - 4), and the second letter is the direction (L=left, U=up, R=right, D=down) Enter x to exit. Have fun! Move robot 2 to square M |- - - - - - - - - - - - - - - -| |. . . . .|. . . . . .|. . . . .| | | |. . .|. . . . . .|. . . . . . .| | - - - | |. . . . . . . . . . . . . . .|.| | | |.|. . . . . . . . . . . . . . .| | - - | |. . . . . .|. . . . .|. . . . .| |- - -| |. . . . . . . . . . . . . . . 4| | - - | |. . . . . .|. . . . . .|. . . .| | - - | |. . . .|. . .|. .|. . . . . . .| | - | |. . . . . . .|. .|. . . . . . .| | - - - - | |. .|. . .|. . . . . . . . . . .| | -| |. . . . . . . . .|. . . . . . .| |- - - | |. . . . . . . . . . . . .|3 . .| | | |. . . . . . M|. . . . . . . . .| | - | |. . . . . . . . .|. . . . . . .| | - - | |. .|. . . . . . . . . . . . 1|.| | - | |. . . . . .|. . . . . .|. . . 2| |- - - - - - - - - - - - - - - -| 1. Please enter the robot to move and the direction (e.g. 2 r): 2L Move robot 2 to square M |- - - - - - - - - - - - - - - -| |. . . . .|. . . . . .|. . . . .| | | |. . .|. . . . . .|. . . . . . .| | - - - | |. . . . . . . . . . . . . . .|.| | | |.|. . . . . . . . . . . . . . .| | - - | |. . . . . .|. . . . .|. . . . .| |- - -| |. . . . . . . . . . . . . . . 4| | - - | |. . . . . .|. . . . . .|. . . .| | - - | |. . . .|. . .|. .|. . . . . . .| | - | |. . . . . . .|. .|. . . . . . .| | - - - - | |. .|. . .|. . . . . . . . . . .| | -| |. . . . . . . . .|. . . . . . .| |- - - | |. . . . . . . . . . . . .|3 . .| | | |. . . . . . M|. . . . . . . . .| | - | |. . . . . . . . .|. . . . . . .| | - - | |. .|. . . . . . . . . . . . 1|.| | - | |. . . . . .|. . . . . .|2 . . .| |- - - - - - - - - - - - - - - -| 2. Please enter the robot to move and the direction (e.g. 2 r): 2 u Move robot 2 to square M |- - - - - - - - - - - - - - - -| |. . . . .|. . . . . .|. . . . .| | | |. . .|. . . . . .|. . . . . . .| | - - - | |. . . . . . . . . . . . . . .|.| | | |.|. . . . . . . . . . . . . . .| | - - | |. . . . . .|. . . . .|. . . . .| |- - -| |. . . . . . . . . . . . . . . 4| | - - | |. . . . . .|. . . . . .|2 . . .| | - - | |. . . .|. . .|. .|. . . . . . .| | - | |. . . . . . .|. .|. . . . . . .| | - - - - | |. .|. . .|. . . . . . . . . . .| | -| |. . . . . . . . .|. . . . . . .| |- - - | |. . . . . . . . . . . . .|3 . .| | | |. . . . . . M|. . . . . . . . .| | - | |. . . . . . . . .|. . . . . . .| | - - | |. .|. . . . . . . . . . . . 1|.| | - | |. . . . . .|. . . . . .|. . . .| |- - - - - - - - - - - - - - - -| 3. Please enter the robot to move and the direction (e.g. 2 r): 4 D ... and so on ...
See the longer example. Note that user input of robot number and direction must handle mixed case input as well as space or no space between the two.
Function parameters, 2-D arrays
I have created a sample program to get you started so you won't get bogged down in reading from a file into an array. It reads in the datafile and displays the top line of the board. You have to complete the displayBoard() function before you go on. Pay careful attention to how the board is stored. Both the board display characters and the walls are stored in the same 2-dimensional array, with constants used as indexes to references the different aspects of each square.
Once you complete the displayBoard() function in the above sample program, the output should look like:
Author: *** YOUR NAME HERE ***
Program 3: Robots in C
TA: Billie Joe Armstrong, T 6:00 AM
UIC CS 141, September 2015
Welcome to Ricochet Robots.
This was originally a board game created by Alex Randolph.
The object of the game is to get the designated numbered robot
to the indicated letter position.
The numbered squares are all robots. They unfortunately have
no brakes, and so will continue in whatever direction they are
moving until they encounter an obstacle.
For each move enter the robot number and the desired direction.
For instance entering
1 U
would move the #1 robot as far up as it can go.
The first letter of input is the robot number (1 - 4),
and the second letter is the direction (L=left, U=up, R=right, D=down)
Enter x to exit. Have fun!
Blank board is:
|- - - - - - - - - - - - - - - -|
|. . . . . . . . . . . . . . . .|
| |
|. . . . . . . . . . . . . . . .|
| |
|. . . . . . . . . . . . . . . .|
| |
|. . . . . . . . . . . . . . . .|
| |
|. . . . . . . . . . . . . . . .|
| |
|. . . . . . . . . . . . . . . .|
| |
|. . . . . . . . . . . . . . . .|
| |
|. . . . . . . . . . . . . . . .|
| |
|. . . . . . . . . . . . . . . .|
| |
|. . . . . . . . . . . . . . . .|
| |
|. . . . . . . . . . . . . . . .|
| |
|. . . . . . . . . . . . . . . .|
| |
|. . . . . . . . . . . . . . . .|
| |
|. . . . . . . . . . . . . . . .|
| |
|. . . . . . . . . . . . . . . .|
| |
|. . . . . . . . . . . . . . . .|
|- - - - - - - - - - - - - - - -|
Initialized board is:
|- - - - - - - - - - - - - - - -|
|. . . . .|. . . . . .|. . . . .|
| |
|. . A|. . . . . .|B . . . . . .|
| - - - |
|. . . . . . . . . . . . . . C|.|
| |
|.|D . . . . . . . . . . . . . .|
| - - |
|. . . . . .|E . . . F|. . . . .|
|- - -|
|. . . . . . . . . . . . . . . 4|
| - - |
|. . . . . G|. . . . . .|H . . .|
| - - |
|. . . I|. . .|. .|. . . . . . .|
| - |
|. . . . . . .|. .|. . . . . . .|
| - - - - |
|. J|. . .|K . . . . . . . . . .|
| -|
|. . . . . . . . L|. . . . . . .|
|- - - |
|. . . . . . . . . . . . .|3 . .|
| |
|. . . . . . M|. . . . . . . . .|
| - |
|. . . . . . . . .|N . . . . . .|
| - - |
|. .|O . . . . . . . . . . . 1|.|
| - |
|. . . . . .|. . . . . .|. . . 2|
|- - - - - - - - - - - - - - - -|
You may not use any global variables besides the board itself and the 4 robots, as illustrated in the above sample program. You may use global constants, however, which are different than global variables. There will be a 30 point deduction for any global variables besides the ones shown in the sample program. The reason for this is that I want you to practice passing reference parameters in C/C++.
As usual you may not uses any C++ classes. (This includes the C++ String class. For strings you must use C-style strings, which are null-terminated arrays of char, which we will study later.) You may use (and I encourage the use of) C functions available in the C standard libraries, documented at http://logos.cs.uic.edu/Examples And Notes/misc/CStdLib.html. The random number generator rand() is documented there (as well as in our Zyante textbook) as are string and character functions.
Your code must be written in a modular fashion. You should not have one huge playGame() function, but rather the functionality should be broken up into different functions that communicate with each other using parameters.
You should include the code to randomly select the key robot, and randomly select the destination square for playing the game. Before you turn it in, however, be sure to hard-code your program so that the goal is moving robot 2 to square M.
There is no real animation in this program. After the user enters a move, the screen is erased using system("cls") on windows or system("clear") on unix, and the screen is redisplayed with the updated positions. Be sure to update the version of the system command you use to the unix version when you turn in your program. (Once done with everything else in the program, if you are highly motivated see here for sample code for an automatic way to check which system you are on, but you don't need to do this.)
The 55 points for execution will be as follows:
5 Displays the board
15 Allows making moves
20 Robots stop at walls and don't run over each other
5 Displayed goal is to move robot 2 to square M
5 Game stops when robot 2 reaches square M
5 If some robot besides robot 2 goes to the destination square (square M in this case), the destination square should change to show the robot there. When this robot moves away, the square should go back to displaying the letter that was there previously (M in our case.)
Datafile format:
The first line in the datafile (which must be called board.txt) describes the number of possible destination squares (15 in the below example). The second line describes the number of "special" squares on the board, that is squares that have some combination of walls and display label (29 in the below example). Note that the side squares don't count for this as "special", as they are handled separately. Your program must work for any valid datafile. We will likely test your program with a different datafile than the one shown below. Because of the way the sample code reads from the datafile, make sure there are no extra blank characters at the end of the lines in the datafile.
15 29 5 1000 11 1000 18 0011 A 25 1001 B 46 0110 C 49 1001 D 64 1001 70 1100 E 74 0011 F 95 0110 101 0110 G 108 1100 H 115 0011 I 119 1100 120 0110 135 1001 136 0011 145 0110 J 149 1100 K 159 0011 160 1001 168 0011 L 189 1100 198 0011 M 217 1001 N 226 1001 O 238 0110 246 1001 252 1001 238 255 189 95
After the first two lines, most of the entries give the piece index (reflecting its position on the board), a set of 0's and 1's denoting the four walls, and an optional letter to be displayed in that position. The 0's and 1's going from left to right represent first the left wall, then the top, then the right, then the bottom wall. For example the value 0110 is a board square with no left wall (0), with a top wall (1), with a right wall (1), without a bottom wall (0).
Remember that the name of your project should be your netid. Once you are done with your program, rename main.cpp to be netid.cpp, where netid is your netid. Zip up only your netid.cpp file and turn it in to Blackboard into the Program 3 assignment. Be sure to review the grading criteria on the course syllabus, particularly regarding functional decomposition.
(5 points) Display the board using graphics built in to Qt Creator. Explore the many sample programs to see sample code that can help you.
(5 points) Implement user input using a mouse rather than from the command line.
If you are submitting the extra credit version, please submit only that version, into the separate Program3Extra assignment.