Take your solution (or mine) to program 5, and add the ability to undo moves. Typing in 'b' goes back to the previous board configuration and move number. Attempting to undo at the beginning of the game gives an error message and allows retrying the move. Your program will not be accepted for grading unless the ability to undo moves is implemented using a linked list.
You must also modify your previous version of the program to selectively disable output of the boards, so that only the prompts for moves, linked list information and robot positions are shown. This option is selected (or not) with the new user input shown at the very beginning of running your program.
Running your program (with boards displayed) should look like the following:
Enter T for true or F for false to indicate if boards and instructions will be displayed: t Author: Dale Reed Program 4: RicoRobots in C++ TA: Billie Joe Armstrong, T 6:00 AM March 22, 2018 Welcome to Ricochet Robots ('Ricochet Robot' is a registered trademark of Hans im Glück Munich, Germany, 1999. The game was 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 encountar an obstacle. For each move enter the robot number and the desired direction. For instance entering 1 U would move the #1 robot up as far 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! Enter 'r' for random robot and goal, 'd' for default or 's' to select: d Move robot 2 to square M ---------------------------------------------------------------- | . . . . . | . . . . . . | . . . . . | | | | . . . | . . . . . . | . . . . . . . | | --- --- --- | | . . . . . . . . . . . . . . . | . | | | | . | . . . . . . . . . . . . . . . | | --- --- | | . . . . . . | . . . . . | . . . . . | |--- --- --- | | . . . . . . . . . . . . . . . 4 | | --- --- | | . . . . . . | . . . . . . | . . . . | | --- --- | | . . . . | . . . | . . | . . . . . . . | | --- | | . . . . . . . | . . | . . . . . . . | | --- --- --- --- | | . . | . . . | . . . . . . . . . . . | | --- | | . . . . . . . . . | . . . . . . . | |--- --- --- | | . . . . . . . . . . . . . | 3 . . | | | | . . . . . . M | . . . . . . . . . | | --- | | . . . . . . . . . | . . . . . . . | | --- --- | | . . | . . . . . . . . . . . . 1 | . | | --- | | . . . . . . | . . . . . . | . . . 2 | ---------------------------------------------------------------- 1. List: 1 Robots: 238 255 189 95 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 1d Move robot 2 to square M ---------------------------------------------------------------- | . . . . . | . . . . . . | . . . . . | | | | . . . | . . . . . . | . . . . . . . | | --- --- --- | | . . . . . . . . . . . . . . . | . | | | | . | . . . . . . . . . . . . . . . | | --- --- | | . . . . . . | . . . . . | . . . . . | |--- --- --- | | . . . . . . . . . . . . . . . 4 | | --- --- | | . . . . . . | . . . . . . | . . . . | | --- --- | | . . . . | . . . | . . | . . . . . . . | | --- | | . . . . . . . | . . | . . . . . . . | | --- --- --- --- | | . . | . . . | . . . . . . . . . . . | | --- | | . . . . . . . . . | . . . . . . . | |--- --- --- | | . . . . . . . . . . . . . | 3 . . | | | | . . . . . . M | . . . . . . . . . | | --- | | . . . . . . . . . | . . . . . . . | | --- --- | | . . | . . . . . . . . . . . . . | . | | --- | | . . . . . . | . . . . . . | . . 1 2 | ---------------------------------------------------------------- 2. List: 2->1 Robots: 254 255 189 95 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 2u Move robot 2 to square M ---------------------------------------------------------------- | . . . . . | . . . . . . | . . . . . | | | | . . . | . . . . . . | . . . . . . . | | --- --- --- | | . . . . . . . . . . . . . . . | . | | | | . | . . . . . . . . . . . . . . . | | --- --- | | . . . . . . | . . . . . | . . . . . | |--- --- --- | | . . . . . . . . . . . . . . . 4 | | --- --- | | . . . . . . | . . . . . . | . . . . | | --- --- | | . . . . | . . . | . . | . . . . . . . | | --- | | . . . . . . . | . . | . . . . . . . | | --- --- --- --- | | . . | . . . | . . . . . . . . . . . | | --- | | . . . . . . . . . | . . . . . . 2 | |--- --- --- | | . . . . . . . . . . . . . | 3 . . | | | | . . . . . . M | . . . . . . . . . | | --- | | . . . . . . . . . | . . . . . . . | | --- --- | | . . | . . . . . . . . . . . . . | . | | --- | | . . . . . . | . . . . . . | . . 1 . | ---------------------------------------------------------------- 3. List: 3->2->1 Robots: 254 175 189 95 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: b * Undoing move * ---------------------------------------------------------------- | . . . . . | . . . . . . | . . . . . | | | | . . . | . . . . . . | . . . . . . . | | --- --- --- | | . . . . . . . . . . . . . . . | . | | | | . | . . . . . . . . . . . . . . . | | --- --- | | . . . . . . | . . . . . | . . . . . | |--- --- --- | | . . . . . . . . . . . . . . . 4 | | --- --- | | . . . . . . | . . . . . . | . . . . | | --- --- | | . . . . | . . . | . . | . . . . . . . | | --- | | . . . . . . . | . . | . . . . . . . | | --- --- --- --- | | . . | . . . | . . . . . . . . . . . | | --- | | . . . . . . . . . | . . . . . . . | |--- --- --- | | . . . . . . . . . . . . . | 3 . . | | | | . . . . . . M | . . . . . . . . . | | --- | | . . . . . . . . . | . . . . . . . | | --- --- | | . . | . . . . . . . . . . . . . | . | | --- | | . . . . . . | . . . . . . | . . 1 2 | ---------------------------------------------------------------- 2. List: 2->1 Robots: 254 255 189 95 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: b * Undoing move * ---------------------------------------------------------------- | . . . . . | . . . . . . | . . . . . | | | | . . . | . . . . . . | . . . . . . . | | --- --- --- | | . . . . . . . . . . . . . . . | . | | | | . | . . . . . . . . . . . . . . . | | --- --- | | . . . . . . | . . . . . | . . . . . | |--- --- --- | | . . . . . . . . . . . . . . . 4 | | --- --- | | . . . . . . | . . . . . . | . . . . | | --- --- | | . . . . | . . . | . . | . . . . . . . | | --- | | . . . . . . . | . . | . . . . . . . | | --- --- --- --- | | . . | . . . | . . . . . . . . . . . | | --- | | . . . . . . . . . | . . . . . . . | |--- --- --- | | . . . . . . . . . . . . . | 3 . . | | | | . . . . . . M | . . . . . . . . . | | --- | | . . . . . . . . . | . . . . . . . | | --- --- | | . . | . . . . . . . . . . . . 1 | . | | --- | | . . . . . . | . . . . . . | . . . 2 | ---------------------------------------------------------------- 1. List: 1 Robots: 238 255 189 95 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: b *** You cannot undo past the beginning of the game. Please retry. *** ---------------------------------------------------------------- | . . . . . | . . . . . . | . . . . . | | | | . . . | . . . . . . | . . . . . . . | | --- --- --- | | . . . . . . . . . . . . . . . | . | | | | . | . . . . . . . . . . . . . . . | | --- --- | | . . . . . . | . . . . . | . . . . . | |--- --- --- | | . . . . . . . . . . . . . . . 4 | | --- --- | | . . . . . . | . . . . . . | . . . . | | --- --- | | . . . . | . . . | . . | . . . . . . . | | --- | | . . . . . . . | . . | . . . . . . . | | --- --- --- --- | | . . | . . . | . . . . . . . . . . . | | --- | | . . . . . . . . . | . . . . . . . | |--- --- --- | | . . . . . . . . . . . . . | 3 . . | | | | . . . . . . M | . . . . . . . . . | | --- | | . . . . . . . . . | . . . . . . . | | --- --- | | . . | . . . . . . . . . . . . 1 | . | | --- | | . . . . . . | . . . . . . | . . . 2 | ---------------------------------------------------------------- 1. List: 1 Robots: 238 255 189 95 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 2l Move robot 2 to square M ---------------------------------------------------------------- | . . . . . | . . . . . . | . . . . . | | | | . . . | . . . . . . | . . . . . . . | | --- --- --- | | . . . . . . . . . . . . . . . | . | | | | . | . . . . . . . . . . . . . . . | | --- --- | | . . . . . . | . . . . . | . . . . . | |--- --- --- | | . . . . . . . . . . . . . . . 4 | | --- --- | | . . . . . . | . . . . . . | . . . . | | --- --- | | . . . . | . . . | . . | . . . . . . . | | --- | | . . . . . . . | . . | . . . . . . . | | --- --- --- --- | | . . | . . . | . . . . . . . . . . . | | --- | | . . . . . . . . . | . . . . . . . | |--- --- --- | | . . . . . . . . . . . . . | 3 . . | | | | . . . . . . M | . . . . . . . . . | | --- | | . . . . . . . . . | . . . . . . . | | --- --- | | . . | . . . . . . . . . . . . 1 | . | | --- | | . . . . . . | . . . . . . | 2 . . . | ---------------------------------------------------------------- 2. List: 2->1 Robots: 238 252 189 95 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup:
Again running your program, but this time without boards displayed should look like what is shown below. It is helpful to have a copy of the original board to one side to visualize what the various moves do. Presenting program output in this manner, without the boards displayed, make it easier to compare your output to what it is supposed to be:
Enter T for true or F for false to indicate if boards and instructions will be displayed: f Enter 'r' for random robot and goal, 'd' for default or 's' to select: d 1. List: 1 Robots: 238 255 189 95 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 4d 2. List: 2->1 Robots: 238 255 189 159 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 4l 3. List: 3->2->1 Robots: 238 255 189 149 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 4d 4. List: 4->3->2->1 Robots: 238 255 189 245 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: b * Undoing move * 3. List: 3->2->1 Robots: 238 255 189 149 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: b * Undoing move * 2. List: 2->1 Robots: 238 255 189 159 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: b * Undoing move * 1. List: 1 Robots: 238 255 189 95 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: b *** You cannot undo past the beginning of the game. Please retry. *** 1. List: 1 Robots: 238 255 189 95 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 4d 2. List: 2->1 Robots: 238 255 189 159 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 4l 3. List: 3->2->1 Robots: 238 255 189 149 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 4d 4. List: 4->3->2->1 Robots: 238 255 189 245 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 4l 5. List: 5->4->3->2->1 Robots: 238 255 189 240 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 4u 6. List: 6->5->4->3->2->1 Robots: 238 255 189 176 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 2l 7. List: 7->6->5->4->3->2->1 Robots: 238 252 189 176 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 2u 8. List: 8->7->6->5->4->3->2->1 Robots: 238 108 189 176 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 2r 9. List: 9->8->7->6->5->4->3->2->1 Robots: 238 111 189 176 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 2d 10. List: 10->9->8->7->6->5->4->3->2->1 Robots: 238 159 189 176 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 2l 11. List: 11->10->9->8->7->6->5->4->3->2->1 Robots: 238 149 189 176 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 2d 12. List: 12->11->10->9->8->7->6->5->4->3->2->1 Robots: 238 245 189 176 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 2l 13. List: 13->12->11->10->9->8->7->6->5->4->3->2->1 Robots: 238 240 189 176 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 2u 14. List: 14->13->12->11->10->9->8->7->6->5->4->3->2->1 Robots: 238 192 189 176 Enter robot to move and direction (e.g. 2 r), x to exit or b to backup: 2r Great Job! You did it in only 15 moves. Exiting program...
We will use Zylabs to have you submit and test this last program to compute your grade. Within Zybooks scroll down to section 16.1, titled "Program 6: Robots Undo". You will need to upload (or drag-and-drop) your code into the interface shown, and the name of that code needs to be main.cpp. Once you select the "Submit for Grading" button the system will run six tests on your code and show you your results. (You do have to push the "Submit for Grading" button!)
The sample input for five of the six tests is shown. The sample input for the sixth test is hidden. Failing this last test will result in careful manual scrutiny of your code (to verify that you are in fact not hard-coding to the test!). You are allowed to submit 10 times, and the best submission is the one that will be graded. If your submission doesn't run due to an environment difference (e.g. uninitialized variable, or missing library such as cstdlib), I suggest you compile using the Zylabs programming window in section 1.13 to iron out those details before submitting again. Only code and test results uploaded into Zylabs and completed before the deadline will be considered.
Following are recommendations of steps to take to help you implement the code to undo moves.
Declare the Node, as a struct or as a class. If done as a class, be sure to declare the data members as public. You don't have to declare the data members as private with get and set member functions.
If you are using a Board class, such as in my sample code posted in Piazza, then all you need for your Node is a Board, the moveNumber, and a pointer to the next node.
If you are not using a Board class, such as in my non-class posted solution in Piazza, then your Node will need to store the board, the robots, the destination letter, destination letter position, and which robot is the goal robot.
Within main, declare a head pointer (e.g. Node *pHead=NULL;). After the initial board configuration is created, add the first board and move number to the first list node. This first node should always remain on the list as the starting position for the game. It should never be removed from the list. Consider implementing this code in a function such as addNodeToList(...). You will call it once from this initial part of the program, and again after making each move as described below. Don't forget to make the list pointer a reference parameter!
Within the main loop of your program find the part where you actually make the move. Immediately after making the move (and updating the board and the move number) again call addNodeToList(...).
Create the function to traverse the list and display the current move number as well as the move number stored with each node on the list, as illustrated above in the sample run of the program. You might call this displayList(...). Within displayList(...) also print out the index positions of the four robots. Call displayList(...) before prompting for each new move.
Lastly implement the undo feature. Check the first user input value, which should be the same place in your code where you check if 'X' to exit was chosen. Check to see if 'B' to backup was given as the user input. If so call a function to delete the front node from the list, e.g. deleteNodeFromList(...), then call continue to go back up to get user input again. After deleting the old head of the list don't forget to use the board and move number values on the new head of the list to reset the board and move number to that previous configuration. Again remember to make those values reference parameters so changes to them get reflected back in the calling part of your program.
At the beginning of deleteNodeFromList(...) you must check if you are attempting to undo a move when there is only this single first node on the list. If so display an error message and allow retrying the move.