Extend the feature of your Tic-Tac-Toe program to have the computer play against
ID: 3571906 • Letter: E
Question
Extend the feature of your Tic-Tac-Toe program to have the computer play against the user. The computer will always start as the first move. The computer should never lose the game, no matter what the user enters. So the computer can win or draw the game. C++ WITH COMMENTS THAT WOULD BE GREAT PLS
#include <iostream>
using namespace std;
// Prints the grid
// Takes the 2D array with r row size and c column size
void printGrid(char grid[][3], int r, int c){
for(int i=0; i < r; i++){
for(int j=0; j< c; j++){
cout<<grid[i][j]<<' ';
}
cout<<endl;
}
}
// Check if it's a winner row col and returns true
bool checkIfWinner(char grid[3][3], int row, int col){
if (grid[row][0] == grid[row][1]
&& grid[row][0] == grid[row][2]) //Check row
return true;
if (grid[0][col] == grid[1][col]
&& grid[0][col] == grid[2][col]) //Check col
return true;
if (row == col && grid[0][0] == grid[1][1]
&& grid[0][0] == grid[2][2]) //top-left to bottom-right diagonal
return true;
if (row + col == 2 && grid[0][2] == grid[1][1]
&& grid[0][2] == grid[2][0]) //top-right to bottom-left diagonal
return true;
return false;
}
int main()
{
char grid[3][3];
int freeSlots, row, col;
char player, response = 'y';
bool end, hasWinner;
while (toupper(response) == 'Y')
{
//initializing the grid with dots
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
grid[r][c] = '.';
freeSlots = 9;
player = 'X';
hasWinner = end = false;
printGrid(grid, 3, 3);
while (!end) //while game is not over
{
row = col = -1; //get valid move
while (col > 2 || col < 0 || row > 2 || row < 0
|| grid[row][col] != '.')
{
cout << "Player "
<< player << ","
<<"row ";
cin >> row;
row--;
cout << "Player "
<< player << ", "
<<"column ";
cin >> col;
col--;
}
grid[row][col] = player; //place move
freeSlots--; //decrement free slots
printGrid(grid, 3, 3);
//check game status
if (freeSlots == 0) end = true;
hasWinner = end = checkIfWinner(grid, row, col);
//change player
if (!end) player = player == 'X' ? 'O' : 'X';
}
//display game result
if (hasWinner) cout <<"Player " << player << " wins ";
else cout << "The cat wins ";
//Ask for another game
cout << " Do you want to play a new game?(y/n) ";
cin >> response;
cout << endl;
}
return 0;
}
Explanation / Answer
#include<iostream>
#include<stdlib.h>
#include<vector>
using namespace std;
char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
char symbol[3];
string playername[3];
const bool CLEAR_SCREEN = true;
class TTTBoard {
public:
TTTBoard(){}
void clearScreen();
void drawBoard(string playername[]) const;
void move(string playername[]);
int state() const;
};
void TTTBoard :: clearScreen()
{
if (CLEAR_SCREEN) {
cout << "c";
}
cout<<" **** Initial state ********* ";
square[1] = '1'; square[2] = '2'; square[3] = '3';
square[4] = '4'; square[5] = '5'; square[6] = '6';
square[7] = '7'; square[8] = '8'; square[9] = '9';
move(playername);
}
void TTTBoard :: move(string playername[]){
int player = 1,i, position;
char mark;
do
{
drawBoard(playername);
player=(player%2)?1:2;
if(playername[player] == "Computer")
cout<<" Computer entered :"<<(position = (rand()% 9 + 1));
else{
cout <<" "<<playername[player] << ", enter a number: ";
cin >> position;
}
//mark=(player == 1) ? 'X' : 'O';
if(symbol[player] == 'X')
mark = 'X';
if(symbol[player] == 'O')
mark = 'O';
if ( position == 1 && square[1] == '1')
square[1] = mark;
else if (position == 2 && square[2] == '2')
square[2] = mark;
else if (position == 3 && square[3] == '3')
square[3] = mark;
else if (position == 4 && square[4] == '4')
square[4] = mark;
else if (position == 5 && square[5] == '5')
square[5] = mark;
else if (position == 6 && square[6] == '6')
square[6] = mark;
else if (position == 7 && square[7] == '7')
square[7] = mark;
else if (position == 8 && square[8] == '8')
square[8] = mark;
else if (position == 9 && square[9] == '9')
square[9] = mark;
else
{
cout<<" Invalid move ";
player--;
cin.ignore();
cin.get();
}
i = state();
player++;
}while(i==-1);
drawBoard(playername);
if(i==1)
cout<<" Player "<<playername[--player]<<" win ";
else
cout<<" Game Draw";
cin.ignore();
cin.get();
}
/*********************************************
*
* FUNCTION TO RETURN GAME STATUS
* 1 FOR GAME IS OVER WITH RESULT
* -1 FOR GAME IS IN PROGRESS
* O GAME IS OVER AND NO RESULT
***********************************************/
int TTTBoard :: state() const
{
enum status {PLAYING, X_WINS=1, Y_WINS=1, TIE=-1, UNDEF};
if (square[1] == square[2] && square[2] == square[3])
return X_WINS;
else if (square[4] == square[5] && square[5] == square[6])
return X_WINS;
else if (square[7] == square[8] && square[8] == square[9])
return X_WINS;
else if (square[1] == square[4] && square[4] == square[7])
return X_WINS;
else if (square[2] == square[5] && square[5] == square[8])
return Y_WINS;
else if (square[3] == square[6] && square[6] == square[9])
return Y_WINS;
else if (square[1] == square[5] && square[5] == square[9])
return Y_WINS;
else if (square[3] == square[5] && square[5] == square[7])
return Y_WINS;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
&& square[4] != '4' && square[5] != '5' && square[6] != '6'
&& square[7] != '7' && square[8] != '8' && square[9] != '9')
return PLAYING;
else
return TIE;
}
/*******************************************************************
* FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
*********************************************************************/
void TTTBoard :: drawBoard(string playername[]) const
{
cout << " Tic Tac Toe ";
cout << " | | " << endl;
cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;
cout << " _____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;
cout << " _____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;
cout << " | | " << endl << endl;
cout<<" "<<playername[1]<<" ("<<symbol[1]<<"), "<<playername[2]<<" ("<<symbol[2]<<") ";
cout << endl;
}
int main()
{
TTTBoard T;
cout<<" *** WelCome *** Enter player names 1. "; cin>>playername[1];
playername[2] = "Computer";
cout<<" 2. "<<playername[2];
cout<<" "<<playername[1]<<", Chose your Symbol : X or O ";
cin>>symbol[1];
if(symbol[1] == 'X')
symbol[2] = 'O';
if(symbol[1] == 'O')
symbol[2] = 'X';
cout<<" "<<playername[2]<<", Your's Symbol : "<<symbol[2]<<" ";
T.move(playername);
char ch;
do{
cout<<"Do you want play more (Y/y)? : ";
cin>>ch;
if(ch!= 'Y' && ch!= 'y')
return 0;
T.clearScreen();
}while(ch == 'Y' || ch == 'y');
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.