Using your solution or my solution to program 5, use a linked list to store all moves made in the game, allowing infinite undo of moves back to the beginning of the game. Find this in Zybooks section 12.12
Note no late submissions will be accepted for program 6! Grading will be done on only the automated tests, with no grading for programming style. We will still run the MOSS program comparison program, to compare the parts of code that you do write.
Running your program could look like the following:
Welcome to Pentago, where you try to get 5 of your pieces in a line. At any point enter 'x' to exit, 'i' for instructions, or 'u' to undo. 1 2 3 4 5 6 1-----------------------2 A | . . . | . . . | A | | | B | . . . | . . . | B | | | C | . . . | . . . | C |-----------+-----------| D | . . . | . . . | D | | | E | . . . | . . . | E | | | F | . . . | . . . | F 3-----------------------4 1 2 3 4 5 6 List of moveNumber:playerToMove is 1:X 1. Enter row, column, quadrant, direction for X: u *** You cannot undo past the beginning of the game. Please retry. *** 1 2 3 4 5 6 1-----------------------2 A | . . . | . . . | A | | | B | . . . | . . . | B | | | C | . . . | . . . | C |-----------+-----------| D | . . . | . . . | D | | | E | . . . | . . . | E | | | F | . . . | . . . | F 3-----------------------4 1 2 3 4 5 6 List of moveNumber:playerToMove is 1:X 1. Enter row, column, quadrant, direction for X: a12r 1 2 3 4 5 6 1-----------------------2 A | X . . | . . . | A | | | B | . . . | . . . | B | | | C | . . . | . . . | C |-----------+-----------| D | . . . | . . . | D | | | E | . . . | . . . | E | | | F | . . . | . . . | F 3-----------------------4 1 2 3 4 5 6 List of moveNumber:playerToMove is 2:O->1:X 2. Enter row, column, quadrant, direction for O: a21r 1 2 3 4 5 6 1-----------------------2 A | . . X | . . . | A | | | B | . . O | . . . | B | | | C | . . . | . . . | C |-----------+-----------| D | . . . | . . . | D | | | E | . . . | . . . | E | | | F | . . . | . . . | F 3-----------------------4 1 2 3 4 5 6 List of moveNumber:playerToMove is 3:X->2:O->1:X 3. Enter row, column, quadrant, direction for X: c31l 1 2 3 4 5 6 1-----------------------2 A | X O X | . . . | A | | | B | . . . | . . . | B | | | C | . . . | . . . | C |-----------+-----------| D | . . . | . . . | D | | | E | . . . | . . . | E | | | F | . . . | . . . | F 3-----------------------4 1 2 3 4 5 6 List of moveNumber:playerToMove is 4:O->3:X->2:O->1:X 4. Enter row, column, quadrant, direction for O: u * Undoing move * 1 2 3 4 5 6 1-----------------------2 A | . . X | . . . | A | | | B | . . O | . . . | B | | | C | . . . | . . . | C |-----------+-----------| D | . . . | . . . | D | | | E | . . . | . . . | E | | | F | . . . | . . . | F 3-----------------------4 1 2 3 4 5 6 List of moveNumber:playerToMove is 3:X->2:O->1:X 3. Enter row, column, quadrant, direction for X: u * Undoing move * 1 2 3 4 5 6 1-----------------------2 A | X . . | . . . | A | | | B | . . . | . . . | B | | | C | . . . | . . . | C |-----------+-----------| D | . . . | . . . | D | | | E | . . . | . . . | E | | | F | . . . | . . . | F 3-----------------------4 1 2 3 4 5 6 List of moveNumber:playerToMove is 2:O->1:X 2. Enter row, column, quadrant, direction for O: u * Undoing move * 1 2 3 4 5 6 1-----------------------2 A | . . . | . . . | A | | | B | . . . | . . . | B | | | C | . . . | . . . | C |-----------+-----------| D | . . . | . . . | D | | | E | . . . | . . . | E | | | F | . . . | . . . | F 3-----------------------4 1 2 3 4 5 6 List of moveNumber:playerToMove is 1:X 1. Enter row, column, quadrant, direction for X: u *** You cannot undo past the beginning of the game. Please retry. *** 1 2 3 4 5 6 1-----------------------2 A | . . . | . . . | A | | | B | . . . | . . . | B | | | C | . . . | . . . | C |-----------+-----------| D | . . . | . . . | D | | | E | . . . | . . . | E | | | F | . . . | . . . | F 3-----------------------4 1 2 3 4 5 6 List of moveNumber:playerToMove is 1:X 1. Enter row, column, quadrant, direction for X: b31r 1 2 3 4 5 6 1-----------------------2 A | . . . | . . . | A | | | B | . . . | . . . | B | | | C | . X . | . . . | C |-----------+-----------| D | . . . | . . . | D | | | E | . . . | . . . | E | | | F | . . . | . . . | F 3-----------------------4 1 2 3 4 5 6 List of moveNumber:playerToMove is 2:O->1:X 2. Enter row, column, quadrant, direction for O: x Exiting program...
The code you start with should do everything you need to play the game, except for implementing undo using a linked list. You will need to do the following:
Create the data structures and operations used for the linked list:
//------------------------------------------------------------------------------------- // Node declaration to implement a linked list to store moves, used to implement // undo within a level. You should store the old board, the old playerToMove, // the move number, and a pointer to the next Node.class Node { // ... }; //-------------------------------------------------------------------------------- // Display the move numbers on the linked listvoid displayList( Node *pTemp) { // ... } // end displayList()//-------------------------------------------------------------------------------- // Delete the front node on the list and restore current game values from the // next node that reflects the previous move. // Parameters should be: // pointer to the head of the list, which could change, so should be a Node * reference. // the Board, which should also be a reference parameter, and is the game board to be // restored from the list's next node // the playerToMove, which should be restored from the list's next node // the moveNumber, which should be restored from the list's next node
void deleteNodeFromList( ) { // ... } //end deleteNodeFromList()//-------------------------------------------------------------------------------- // Create a new node and prepend it to the beginning of the list. // Parameters should be: // pointer to the head of the list, which could change, so should be a Node * reference. // the Board, which should also be a reference parameter, and is the game board to be // restored from the list // the playerToMove, which should be added to the list // the moveNumber, which should be added to the listvoid addNodeToList() { // Create a new node and store current values into it // ... // Prepend it onto the front of the list // ... }
Declare pHead to be a Node pointer, and initialize it to NULL. pHead will be used to keep track of the head of the list. Also declare pTemp to be a Node pointer. pTemp will be used in allocating new memory for new Nodes.
Immediately after creating the initial board within main(), store it onto the linked list. This first node should always be on the list. You should create a copy constructor in your Board class. When checking to see if the user attempts to undo a move at the beginning of the game, you will need to check to see if there is only this single node on the list.