The files must be named: Board.hpp, Board.cpp, TicTacToe.hpp and TicTacToe.cpp W
ID: 3810485 • Letter: T
Question
The files must be named: Board.hpp, Board.cpp, TicTacToe.hpp and TicTacToe.cpp Write a class called Board that represents a tic-tac-toe board. It should have a 3x3 array as a data member, which will store the locations of the players' moves. It should have a default constructor that initializes the 3x3 array to being empty. It should have a method called makeMove that takes the x and y coordinates of the move (see the example below) and which player's turn it is as parameters. If that location is unoccupied, makeMove should record the move and return true. If that location is already occupied, makeMove should just return false. There should be a method called gameState that takes no parameters and returns one of the four following values: X_WON, O_WON, DRAW, or UNFINISHED - use an enum for this, not strings (the enum definition should go in Board.hpp). There should also be a method called print, which just prints out the current board to the screen. Write a class called TicTacToe that allows two people to play a game. This class will have a field for a Board object and a field to keep track of which player's turn it is. It should have a constructor that takes a char parameter that specifies whether 'x' or 'o' should have the first move. It should have a method called play that starts the game. The play method should keep looping, asking the correct player for their move and sending it to the board (with makeMove) until someone has won or it's a draw (as indicated by gameState), and then declare what the outcome was. Write a main method (in TicTacToe.cpp) that asks the user which player should go first, creates a new TicTacToe object and starts the game. For this assignment only, you will not comment out your main method. Input validation: If someone tries to take an occupied square, tell them that square is already occupied and ask for a different move. Here's an example portion of a game (already in progress): 0 1 2 0 x . . 1 . . . 2 . . . Player O: please enter your move. 1 2 0 1 2 0 x . . 1 . . o 2 . . . Player X: please enter your move. 1 2 That square is already taken. 0 1 2 0 x . . 1 . . o 2 . . . Player X: please enter your move.
Explanation / Answer
#include <iostream>
#include <math.h>
#include <string>
#include <istream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#include "Board.hpp"
#include "TicTacToe.hpp"
TicTacToe::TicTacToe()
{
}
void TicTacToe::play()
{
string userinput;
bool gameFinished = 0;
int playerOrder = 0;
int tempPlayer = 0;
int xLoc;
int yLoc;
cout << "Welcome to TicTacToe! ";
bool validInput = 0;
while(validInput == 0)
{
cout << "Will player 1 or player 2 go first? (Type '1' or '2'.) ";
cin >> playerOrder;
if(playerOrder == 1)
{
validInput = 1;
}
else if(playerOrder == 2)
{
validInput = 1;
}
else
{
cout << "Sorry, but that is not a valid option. Please select again. ";
}
}
currentPlayer = playerOrder - 1;
while(gameFinished == 0)
{
myBoard.print();
if(myBoard.gameState() == 3)
{
if(currentPlayer == 0)
{
cout << "Player X: please enter your move. ";
cin >> xLoc >> yLoc;
if(myBoard.makeMove(xLoc,yLoc,currentPlayer))
{
tempPlayer = 1;
}
else
{
cout << "That square is already taken. ";
}
}
else
{
cout << "Player O: please enter your move. ";
cin >> xLoc >> yLoc;
if(myBoard.makeMove(xLoc,yLoc,currentPlayer))
{
tempPlayer = 0;
}
else
{
cout << "That square is already taken. ";
}
}
currentPlayer = tempPlayer;
}
else
{
cout << "The game is over. ";
if(myBoard.gameState() == 0)
{
cout << "Congratulations Player 1! ";
gameFinished = 1;
}
if(myBoard.gameState() == 1)
{
cout << "Congratulations Player 2! ";
gameFinished = 1;
}
if(myBoard.gameState() == 2)
{
cout << "The game was a draw. You both lose. ";
gameFinished = 1;
}
}
}
cout << "Thank you for playing! ";
}
int main()
{
TicTacToe myTicTacToeGame;
myTicTacToeGame.play();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.