Write a class called Board that represents a tic-tac-toe board. It should have a
ID: 3673510 • Letter: W
Question
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.
The files must be named: Board.hpp, Board.cpp, TicTacToe.hpp andTicTacToe.cpp
Explanation / Answer
//board.hpp
#ifndef BOARD_H
#define BOARD_H
class Board
{
public:
Board ( char newboard[3][3]);
char getboard();
void printboard();
void printXmove(char newboard[3][3],int row, int column);
void printOmove(char newboard[3][3],int row, int column);
bool fullboard(char newboard[3][3]);
bool playerwins(char newboard[3][3]);
bool validmove(char newboard[3][3],int row, int column);
private:
char mboard[3][3];
};
#endif
//board.cpp
#include "board.h"
#include <iostream>
using namespace std;
Board::Board(char newboard[][3])
{
mboard[][3] = newboard[][3];
}
char Board::getboard()
{
return mboard[][3];
}
bool Board::validmove(char newboard[3][3],int row, int column)
{
if ( row>=3 && row<=1 && column>=3 && column<=1 && board[row-1][column-1] =! " ")
return false;
return true;
}
void Board::printXmove(char newboard , int row, int column )
{
board[row-1][column-1]= 'X';
}
void Board::printOmove(char newboard , int row, int column )
{
board[row-1][column-1]= 'O';
}
bool Board :: fullboard(char newboard[][3])
{ for (int row=0; row<3;row++ )
{for(int column =0; column<3;column++)
if(board[row][column]==' ')
}
return false;
return true;
}
bool Board :: playerwins(char newboard [][3])
{
if (board[0][0]== board[0][1]==board [0][2] || board [0][0]== board [1][1]== board [2][2] || board [0][2]== board [1][1]==board [2][0] )
return true;
return false;
}
void Board::printboard()
{
char board[3][3]={0};
cout<<" 1 2 3 ";
cout<<"1 "<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<" ";
cout<< " ---|---|--- ";
cout<<"2 "<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<" ";
cout<< " ---|---|--- ";
cout<<"3 "<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2];
}
#include "TicTacToe.hpp"
#include <iostream>
using namespace std;
int main()
{
char select;
cout << "This game will generate a tic tac toe board and allow the user ";
cout << " to enter coordinates for player1 and player2 . It will continue ";
cout << " with the game until someone wins or it is a draw.." << endl;
cout << " Please select who player1 will be, either 'x' or 'o': ";
cin >> select;
TicTacToe game(select);//object declared
game.play();//play function call
return 0;
}
//TicTacToe.cpp
#include "TicTacToe.cpp"
#include <iostream>
using namespace std;
void main()
{
char playagain = '';
while(playagain == 'Y'|| playagian =='y')
{ Board playboard (board[][3]);
playboard.printboard();
int row=0,column=0;
while(playboard.fullboard == false)
{
cout<<"Player X's turn, please enter column and row ";
while(true)
{
cout<<"row";
cin>>row;
cout<<"column";
cin>>column;
playboard.validmove(board[][3], row, column);
cout<<"invalid move";
}
else
break;
playboard.printXmove(board[][3],row,column);
if(playboard.playerwins(board[][3])== true)
cout<<"player X wins";
cout<<"would you like to play again?";
cin>>playagain;
else
cout<<"player O's turn, please enter row and column ";
while(true)
{
cout<<"row";
cin>>row;
cout<<"column";
cin>>column;
playboard.validmove(board[][3], row, column);
cout<<"invalid move";
}
else
break;
playboard.printOmove(board[][3],row,column);
if(playboard.playerwins(board[][3])== true)
cout<<"player O wins";
cout<<"would you like to play again?";
cin>>playagain;
}
cout<<"Tie game";
cout<<"would you like to play again (Y/N) ";
cin>>playagain;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.