Question: Implement a Tic Tac Toe game with a command line interface. Players should be able to take turns and undo past turns. Past turns (
Implement a Tic Tac Toe game with a command line interface. Players should be able to take turns and undo past turns. Past turns called Moves will be stored in the MoveStack class, and the the tic tac toe game board will be managed by the GameState class.
MoveStack
A regular stack that stores instances of the Move structure. A Move records the coordinates of the x or o token placed on the game board. All past moves are stored here. The stack is required in order to be able to undo moves. In the template, you're provided with public functions. Add any private variablesfunctions as needed, but leave public function prototypes unchanged. You may use a linked list or an array to store the Moves. You will need to implement the member functions in the MoveStack.cpp file not provided A test driver is provided in "downloadable files".
MoveStack Initializes the stack.
~MoveStack Cleans up the dynamically allocated memory if any Must be defined, even if empty.
int getSize Returns the size of the stack number of Moves currently on the stack
Move top Returns a move on top of the stack but does not remove it
void pushMove move Adds the move to the top of the stack.
void pop Removes a move from the top of the stack but does not return it
Notes:
You must submit a program for grading by : Friday Apr worth pts It must pass of the first four automatic test cases.
Do not use a Standard Template Library class to implement MoveStack. Use an array, a dynamically allocated array, or a linked list.
GameState
The GameState class represents the current state of the game. It stores the current board in the "boardState" variable and past moves in the "moveStack" variable. The checkLastPlayerWin function returns true if the last player who moved has won the game this function is provided in the template file A test driver is provided in "downloadable files".
GameState Initializes the board to all underscore characters
int getCurrentPlayer Return the player that has to make the next turn. Hint: use the size of the stack to determine who's move it is Extra hint: oddeven
int addMoveMove move Updates the state of the board and stack of the past moves given a move of the current player. If the turn is invalid the target position is not empty it returns If all positions are filled, it returns If the move was successfully done and there are moves available, it returns Hint: use the size of the stack to determine if all positions are filled.
bool undoLast Undoes the last turn by changing the board state to the previous one and removing the last move from the stack. Returns true if the move was removed, false if there are no moves to undo.
void displayBoardStatestd::ostream& out Prints the board state to the "out" stream.
Note: checkLastPlayerWin uses this syntax: c x : y If you are curious, it is called the "conditional operator". Now you can look it up
main.cpp
Contains the main game loop. Players should be able to enter their move in a format "move x y where x and y are the coordinates of the target cell. Also, players should be able to undo the previous move of their opponent by entering "undo". The player board should be displayed at the beginning of the game and after every valid move. After every valid move the win condition has to be checked for the last player who made the move checkLastPlayerWin If the win condition is reached a winner should be declared and the program should be terminated. The same goes for if the game ends in a draw.
Sample execution.
Player make a turn.
move
x
Player make a turn.
move
xo
Player make a turn.
undo
x
Player make a turn.
undo
Player make a turn.
undo
No moves to undo.
Player make a turn.
move
x
Player make a turn.
move
xo
Player make a turn.
move
xo
x
Player make a turn.
move
Incorrect move. Please try again.
Player make a turn.
move
Incorrect move. Please try again.
Player make a turn.
move
o
xo
x
Player make a turn.
undo
xo
x
Player make a turn.
move
o
xo
x
Player make a turn.
move
o
xo
xx
Player make a turn.
move
oo
xo
xx
Player make a turn.
move
oo
xo
xxx
Player won!
Here is the given gamestate.h file
#include
#include "MoveStack.h
class GameState
char boardState;
MoveStack moveStack;
public:
GameState;
int getCurrentPlayer;
int addMoveMove move;
bool undoLast;
void displayBoardStatestd::ostream& out;
bool checkLastPlayerWin;
;
and the given movestack.h file
struct Move
int x;
int y;
Move x; y;
Moveint a int b xa yb;
;
class MoveStack
private:
public:
int start;
MoveStack;
~MoveStack;
int getSize;
Move top;
void pushMove move;
void pop;
;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
