Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Extend the feature of your Tic-Tac-Toe program to have the computer play against

ID: 3842083 • 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.

can someone please help me with this ? this is my program

#include <iostream>

using namespace std;

//printing out 2D array

void printGrid(char grid[][3], int row, int column){    //size row & column

    for(int i=0; i < row; i++){

        for(int j=0; j< column; j++){

            cout<<grid[i][j]<<' ';

        }

        cout<<endl;

    }

}

//check if we have a winner, return true if we do

//checking for all directions possible

//

bool IsItWinner(char grid[3][3], int row, int column){

    if (grid[row][0] == grid[row][1]

    //check row

            && grid[row][0] == grid[row][2])

      return true;

    //check columns

    if (grid[0][column] == grid[1][column]

            && grid[0][column] == grid[2][column])

        return true;

    if (row == column && grid[0][0] == grid[1][1]

    //checking for top left to bottom right diag

            && grid[0][0] == grid[2][2])

        return true;

    if (row + column == 2 && grid[0][2] == grid[1][1]

    //checking for top right to botton left

            && grid[0][2] == grid[2][0])

        return true;

    return false;

}

int main()

{

    char grid[3][3];

    int freeSlots, row, column;

    char participant;

    bool end, hasWinner;

    {

        for (int row = 0; row < 3; row++)

            for (int column = 0; column < 3; column++)

                grid[row][column] = '*';

        freeSlots = 9;

        participant = 'X';

        hasWinner = end = false;

        printGrid(grid, 3, 3);

        while (!end)

        {

            row = column = -1;

            while (column > 2 || column < 0 || row > 2 || row < 0

                   //im assuming what you meant with initializing each element of the array with asterisk

                   //means the XO table is suppose to print out in asterisk ??

                    || grid[row][column] != '*') //printing out rows and columns in asterisk

            {

                //printing out whose turn is it to choose which row

                cout << "Player " << participant << "," <<"row and column ";

                cin >> row;

                cin >>column;

                row--;   

                column--;

            }

            grid[row][column] = participant;

            freeSlots--;

            printGrid(grid, 3, 3);

            if (freeSlots == 0) end = true;

            hasWinner = end = IsItWinner(grid, row, column);

            //switch between participant/player X and participant/player O

            if (!end) participant = participant == 'X' ? 'O' : 'X';

        }

        //display game results if we have a winner

        if (hasWinner) cout <<"Player " << participant << " wins ";

        //display game results if a tie has occurred

        else cout << "The cat wins ";

    }

    return 0;

}

Explanation / Answer

#include <iostream>
using namespace std;
void printGrid(char Grid[3][3], char whoVal)
{
int r, c;
cout<<whoVal<<"'s turn."<<endl;
//allows the user for selection
cout<<"WHERE DO YOU WANT YOUR "<<whoVal<<" PLACED?"<<endl;
//Enters the rows and columns in numbers.
cout<<"PLEASE ENTER ROW NUMBER AND COLUMN NUMBER SEPARATED BY A SPACE."<<endl;
cin>>r>>c;
cout<<"YOU HAVE ENTERED ROW #"<<r<<endl;
cout<<" AND COLUMN #"<<c<<endl;
if(r >= 0 && r < 3 && c >= 0 && c < 3)
{
if(Grid[r][c] != '*')
{
cout<<"THAT CELL IS ALREADY TAKEN."<<endl;
cout<<"PLEASE MAKE ANOTHER SELECTIOn."<<endl;
printGrid(Grid, whoVal);
}
else
{
cout<<"THANK YOU FOR YOUR SELECTION."<<endl;
Grid[r][c] = whoVal;
}
}
else
{
cout<<"INVALID ENTRY: TRY AGAIN."<<endl;
cout<<"ROW & COLUMN NUMBERS MUST BE EITHER 0, 1, 2."<<endl;
printGrid(Grid, whoVal);
}
}
void displayBoardS(char Grid[3][3])
{
int i, j;
cout<<"---+---+---+---+"<<endl;
cout<<"| 0 | 1 | 2 | 3 |"<<endl;
cout<<"---+---+---+---+"<<endl;
for(i = 0; i < 3; i++)
{
cout<<"| "<<i+1<<" | ";
for(j = 0; j < 3; j++)
if(Grid[i][j] == '*')
cout<<" | ";
else
cout<<Grid[i][j]<<" | ";
cout<<endl<<"---+---+---+---+"<<endl;
}
}
bool gameComplete(char Grid[3][3])
{
bool won = false;
int i = 1;
if(Grid[i][i] == Grid[i][i+1] && Grid[i][i] == Grid[i][i-1] && Grid[i][i] != '*')
won = true;
if(Grid[i][i] == Grid[i+1][i] && Grid[i][i] == Grid[i-1][i] && Grid[i][i] != '*')
won = true;
if(Grid[i][i] == Grid[i-1][i-1] && Grid[i][i] == Grid[i+1][i+1] && Grid[i][i] != '*')
won = true;
if(Grid[i][i] == Grid[i-1][i+1] && Grid[i][i] == Grid[i+1][i-1] && Grid[i][i] != '*')
won = true;
if(Grid[i-1][i-1] == Grid[i-1][i] && Grid[i-1][i] == Grid[i-1][i+1] && Grid[i-1][i] != '*')
won = true;
if(Grid[i-1][i-1] == Grid[i][i-1] && Grid[i][i-1] == Grid[i+1][i-1] && Grid[i][i-1] != '*')
won = true;   
if(Grid[i+1][i-1] == Grid[i+1][i] && Grid[i+1][i] == Grid[i+1][i+1] && Grid[i+1][i] != '*')
won = true;
if(Grid[i-1][i+1] == Grid[i][i+1] && Grid[i][i+1] == Grid[i+1][i+1] && Grid[i][i+1] != '*')
won = true;
return won;
}
//A TIE OCCURS WHEN ALL OF THE LOCATIONS ON THE BOARD ARE FULL, BUT THERE IS NO WINNER.
bool boardFilled(char Grid[3][3])
{
bool space = false;
int i, j;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
if(Grid[i][j] == '*')
space = true;
return !space;   
}
int main()
{
//USE A TWODIMENSIONAL CHAR ARRAY WITH THREE ROWS AND THREE COLUMNS AS THE GAME BOARD.
char Grid[3][3], turn, again;
int i, j;
while(1)
{
//EACH ELEMENT OF THE ARRAY SHOULD BE INITIALIZED WITH AN ASTERISK (*).
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
Grid[i][j] = '*';
cout<<"NEW GAME: X GOES FIRST."<<endl;
turn = 'X';
//THE PROGRAM SHOULD RUN A LOOP THAT
while(!gameComplete(Grid) && !boardFilled(Grid))
{
//DISPLAYS THE CONTENTS OF THE BOARD ARRAY
displayBoardS(Grid);
printGrid(Grid, turn);
//ALLOWS PLAYER 2 TO SELECT A LOCATION ON THE BOARD FOR AN O.
if(turn == 'X')
turn = 'O';
else
turn = 'X';
}
//DETERMINES WHETHER A PLAYER HAS WON, OR A TIE HAS OCCURRED.
if(boardFilled(Grid))
{
//IF A TIE HAS OCCURRED, THE PROGRAM SHOULD SAY SO AND END.
cout<<"DRAW! NOBODY WINS!"<<endl;
}
else
{
//IF A PLAYER HAS WON, THE PROGRAM SHOULD DECLARE THAT PLAYER THE WINNER AND END.
//PLAYER 1 WINS WHEN THERE ARE THREE XS IN A ROW ON THE GAME BOARD.
if(turn == 'X')
cout<<"O IS THE WINNER!!!"<<endl;
else
cout<<"X IS THE WINNER!!!"<<endl;
}
displayBoardS(Grid);   
cout<<"ANOTHER GAME? ENTER Y OR Y FOR YES."<<endl;
cin>>again;
if(again != 'Y' && again != 'y')
{
cout<<"THANK YOU FOR PLAYING!"<<endl;
exit(0);
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote