Very similar to textbook C++: How to Program 7th Edition ch. 9, prob 15 but a di
ID: 3640319 • Letter: V
Question
Very similar to textbook C++: How to Program 7th Edition ch. 9, prob 15 but a different output coding.Implement a class TicTacToe to play the game of tic-tac-toe with two players of private data members 3 by 3 array of integers. The constructor should initialize the empty board to zeros. When the first player moves, place a X in the specied square; place an O when the second player moves. Each move must be an empty square. Show a win or draw.
Sample Implementation:
0 0 0
0 0 0
0 0 0
First Player (row, col): 1 2
0 X 0
0 0 0
0 0 0
Second Player (row, col): 1 1
O X 0
0 0 0
0 0 0
First Player (row, col): 1 1
1 1 is invalid move. Choose another: 2 2
O X 0
0 X 0
0 0 0
...and so on until there is a win or draw ("First player Wins!")
Explanation / Answer
if you want the board to display 0 at unoccupied slots instead of . then change the function in ostream& operator<<
// * unoccupied slot display character: '.' (dot)
//Main.cpp
#include <iostream>
#include "TicTacToe.h"
using namespace std;
int main()
{
//LOCAL DECLARATIONS
TicTacToe board;
int player = 1;
int row, col;
//PROCEDURES
cout << board << endl;
while (!board.full() && !board.over())
{
cout << (player == 1 ? "First" : "Second") << " Player (row, col): ";
cin >> row >> col;
while (!board.setMove(row, col, player))
{
cout << row << " " << col << " is invalid move. Choose another: ";
cin >> row >> col;
}
player = (player == 1 ? 2 : 1);
cout << board << endl;
}
//Display game result
if (board.full())
cout << "Game Draws" << endl;
else
cout << (player == 2 ? "First" : "Second") //if first win then player == 2
<< " player Wins!" << endl;
cin.sync();
cin.get();
cout << endl;
return 0;
}
//TicTacToe.h
#include <iostream>
using namespace std;
#ifndef TICTACTOE_H
#define TICTACTOE_H
class TicTacToe
{
public:
friend ostream& operator<<(ostream&, const TicTacToe&);
TicTacToe();
TicTacToe(const TicTacToe&);
const TicTacToe& operator=(const TicTacToe&);
~TicTacToe();
bool setMove(int row, int col, int player);
bool over() const;
bool full() const;
private:
bool checkRow(int row, int player) const;
bool checkCol(int col, int player) const;
bool checkDiag1(int player) const;
bool checkDiag2(int player) const;
private:
int board[3][3];
int lastMoveRow;
int lastMoveCol;
};
#endif
//TicTacToe.cpp
#include "TicTacToe.h"
//------------------------------------------
// PUBLIC METHODS
//------------------------------------------
/** Default constructor
*
*/
TicTacToe::TicTacToe()
{
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
board[row][col] = 0;
lastMoveRow = -1;
lastMoveCol = -1;
}
//------------------------------------------
/** Copy constructor
*
*/
TicTacToe::TicTacToe(const TicTacToe& rhs)
{
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
board[row][col] = rhs.board[row][col];
lastMoveRow = rhs.lastMoveRow;
lastMoveCol = rhs.lastMoveCol;
}
//------------------------------------------
/** Overloaded operator =
*
*/
const TicTacToe& TicTacToe::operator=(const TicTacToe& rhs)
{
if (this != &rhs)
{
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
board[row][col] = rhs.board[row][col];
lastMoveRow = rhs.lastMoveRow;
lastMoveCol = rhs.lastMoveCol;
}
return *this;
}
//------------------------------------------
TicTacToe::~TicTacToe()
{
}
//------------------------------------------
/** setMove
* @params
* row: integer (valid value 1/2/3)
* col: integer (valid value 1/2/3)
* player: integer (valid value 1/2) indicates first or second player move
* accept the given player move to the board
* @return
* true: indicates successully set move
* false: indicates the current slot is occupied
*/
bool TicTacToe::setMove(int row, int col, int player)
{
if (row > 0 && row < 4 && col > 0 && col < 4 &&
player > 0 && player < 3)
{
if (!board[row - 1][col - 1])
{
board[row - 1][col - 1] = player;
lastMoveRow = row - 1;
lastMoveCol = col - 1;
return true;
}
}
return false;
}
//------------------------------------------
/** over
* check whether the board has a winner
* depend on last move
*/
bool TicTacToe::over() const
{
if (lastMoveRow < 0 && lastMoveCol < 0)
return false;
int player = board[lastMoveRow][lastMoveCol];
if (checkRow(lastMoveRow, player))
return true;
if (checkCol(lastMoveCol, player))
return true;
if (lastMoveRow == 1)
{
if (lastMoveCol == 1)
return checkDiag1(player) && checkDiag2(player);
}
else
{
if (lastMoveRow == lastMoveCol)
return checkDiag1(player);
else if (lastMoveCol != 1)
return checkDiag2(player);
}
return false;
}
//------------------------------------------
/** Friend operator <<
* display the board to the given std::ostream
* unoccupied slot display character: '.' (dot)
* first player display character: 'X' (capital x)
* second player display character: 'O' (capital o)
*/
ostream& operator<<(ostream& strm, const TicTacToe& rhs)
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
if (!rhs.board[row][col])
strm << '.'; //'0'
else if (rhs.board[row][col] == 1)
strm << 'X';
else
strm << 'O';
strm << (col == 2 ? " " : " ");
}
}
return strm;
}
//------------------------------------------
/** full
* check whether the board is full
*/
bool TicTacToe::full() const
{
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
if (!board[row][col])
return false;
return true;
}
//------------------------------------------
// PRIVATE METHODS
//------------------------------------------
bool TicTacToe::checkRow(int row, int player) const
{
return (board[row][0] == player &&
board[row][1] == player &&
board[row][2] == player);
}
//------------------------------------------
bool TicTacToe::checkCol(int col, int player) const
{
return (board[0][col] == player &&
board[1][col] == player &&
board[2][col] == player);
}
//------------------------------------------
bool TicTacToe::checkDiag1(int player) const
{
return (board[0][0] == player &&
board[1][1] == player &&
board[2][2] == player);
}
//------------------------------------------
bool TicTacToe::checkDiag2(int player) const
{
return (board[0][2] == player &&
board[1][1] == player &&
board[2][0] == player);
}
//------------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.