(Tic-Tac-Toe) Write a program that allows two players to play the tic-tac-toe ga
ID: 3604315 • Letter: #
Question
(Tic-Tac-Toe) Write a program that allows two players to play the tic-tac-toe game. Your program must contain the class ticTacToe to implement a ticTacToe object. Include a -by-two-dimensional array, as a private member variable, to create the board. If needed, include additional member variables. Some of the operations on a ticTacToe object are printing the current board, getting a move, checking if a move is valid, and determining the winner after each move. Add additional operations as needed.
Explanation / Answer
Given below is the completed code . In case of any issues, post a comment. If it helped, please rate it. Thank you.
ticTacToe.h
#ifndef ticTacToe_h
#define ticTacToe_h
class ticTacToe
{
private:
char grid[3][3];
static const char INITIAL = '.';
int moves ;
char winner;
void checkWinner(char player);
public:
ticTacToe(char firstPlayer);
bool makeMove(char player, int r, int c);
char getWinner(); //returns 'x' , 'o' , 't' or ' '
void print();
bool gameOver();
};
#endif /* ticTacToe_h */
ticTacToe.cpp
#include "ticTacToe.h"
#include <iostream>
#include <cctype>
using namespace std;
ticTacToe::ticTacToe(char firstPlayer)
{
for(int i = 0 ;i < 3; i++)
for(int j = 0; j < 3; j++)
grid[i][j] =INITIAL;
moves = 0;
}
bool ticTacToe::makeMove(char player, int r, int c)
{
if(grid[r][c] == INITIAL)
{
grid[r][c] = tolower(player);
moves++;
checkWinner(player);
return true;
}
else
return false;
}
//return x or o or t (for tie)
char ticTacToe::getWinner()
{
return winner;
}
void ticTacToe::print()
{
cout << endl;
//print the column numbers in 1st line
cout << " 0 1 2" << endl;
for(int i = 0; i < 3; i++)
{
//print the row number
cout << i << " ";
for(int j = 0; j < 3; j++)
cout << grid[i][j] << " ";
cout << endl;
}
cout << endl;
}
bool ticTacToe::gameOver()
{
return moves == 9 || winner == 'x' || winner == 'o';
}
void ticTacToe::checkWinner(char player)
{
//top horizontal line
if(grid [0][0] == player && grid[0][0] == grid[0][1] && grid[0][1] == grid[0][2])
winner = grid[0][0];
//middle horizontal line
else if(grid [1][0] == player && grid[1][0] == grid[1][1] && grid[1][1] == grid[1][2])
winner = grid[1][0];
//bottom horizontal line
else if(grid [2][0] == player && grid[2][0] == grid[2][1] && grid[2][1] == grid[2][2])
winner = grid[2][0];
//left vertical line
else if(grid [0][0] == player && grid[0][0] == grid[1][0] && grid[1][0] == grid[2][0])
winner = grid[0][0];
//middle vertical line
else if(grid [0][1] == player && grid[0][1] == grid[1][1] && grid[1][1] == grid[2][1])
winner = grid[0][1];
//right verticle line
else if(grid [0][2] == player && grid[0][2] == grid[1][2] && grid[1][2] == grid[2][2])
winner = grid[0][2];
//diagonal starting at left top
else if(grid [0][0] == player && grid[0][0] == grid[1][1] && grid[1][1] == grid[2][2])
winner = grid[0][0];
//diagonal starting at right top
else if(grid [0][2] == player && grid[0][2] == grid[1][1] && grid[1][1] == grid[2][0])
winner = grid[0][2];
else
{
if(moves == 9)
winner = 't'; //tie
else
winner = ' ';
}
}
main.cpp
#include "ticTacToe.h"
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
char player;
//get the player
do
{
cout << "Who will play first x or o ? ";
cin >> player;
player = tolower(player);
}while(player != 'x' && player != 'o');
ticTacToe tic(player);
int row, col;
while(!tic.gameOver())
{
tic.print();
cout << "player " << player << ": please enter your move (enter row col) > ";
cin >> row >> col;
if(!tic.makeMove(player, row, col))
{
cout << "that position is already taken.";
continue;
}
if(player == 'x')
player = 'o';
else
player = 'x';
}
tic.print();
if(tic.getWinner() == 'x')
cout << "x Won!" << endl;
else if(tic.getWinner() == 'o')
cout << "o Won!" << endl;
else
cout << "It's a draw!" << endl;
return 0;
}
output
Who will play first x or o ? x
0 1 2
0 . . .
1 . . .
2 . . .
player x: please enter your move (enter row col) > 1 1
0 1 2
0 . . .
1 . x .
2 . . .
player o: please enter your move (enter row col) > 1 0
0 1 2
0 . . .
1 o x .
2 . . .
player x: please enter your move (enter row col) > 1 1
that position is already taken.
0 1 2
0 . . .
1 o x .
2 . . .
player x: please enter your move (enter row col) > 2 2
0 1 2
0 . . .
1 o x .
2 . . x
player o: please enter your move (enter row col) > 0 2
0 1 2
0 . . o
1 o x .
2 . . x
player x: please enter your move (enter row col) > 0 0
0 1 2
0 x . o
1 o x .
2 . . x
x Won!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.