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

Write a program that allows two players to play a game of tic-tac-toe. Use a two

ID: 3728758 • Letter: W

Question

Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that:

Displays the contents of the board array

Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row number and then the column number.

Allows player 2 to select a location on the board for an O. The program should ask the user to enter the row number and then the column number.

Validates the user's input:

Do not let the user input a row outside the range [1,3]

Do not let the user input a column outside the range [1,3]

Do not let the user input into a cell that already has an 'X' or 'O'

If the user enters an invalid input, just ask them for that input again.

This is best done using nested do-while loops.

Determines whether a player has won, or a tie has occurred. If a player has won, the program should declare that player the winner and end. If a tie has occurred, the program should declare that and end.

These are the following end game outputs:

Player 1 wins!

Player 2 wins!

Tie!

Player 1 wins when there are three Xs in a row on the game board. The Xs can appear in a row, in a column, or diagonally across the board. The same logic applies to player 2, but with Os. A tie occurs when all of the locations on the board are full, but there is no winner.

This is my code so far. If you could please work with what I have that'd be great. Thanks so much.

#include

using namespace std;

const int ROWS = 3;
const int COLS = 3;

char printArray(char board[3][3], int rows, int cols) //function to print the board
{
    int i, j;

    cout << " 123" << endl;
    for(i = 0; i < ROWS; ++i)
    {
        cout << i + 1;
        for(j = 0; j < COLS; ++j)
        {
            cout << board[i][j];
        }
        cout << endl;
    }
}

/*
void playerInput(char board[][3], int turn) ///FIXME
{

}
*/

int main()
{
    char ticTacToe[ROWS][COLS] = { {'*', '*', '*'},
                                   {'*', '*', '*'},
                                   {'*', '*', '*'}};

    int p1InputRow, p1InputCol, p2InputRow, p2InputCol;

    printArray(ticTacToe, ROWS, COLS); //outputs the board

    do
    {
        do
        {
           cout << "Player 1, Row: ";
           cin >> p1InputRow;
        } while (p1InputRow > ROWS || p1InputRow <= 0); //loops if input is greater than rows or <= 0

        do
        {
            cout << "Player 1, Col: ";
            cin >> p1InputCol;
        }   while (p1InputCol > COLS || p1InputCol <= 0); //loops if input is greater than cols or <= 0
    } while (printArray(ticTacToe, ROWS, COLS) == 'X' || printArray(ticTacToe, ROWS, COLS) == 'O'); //keeps looping if there is already an X or O


    return 0;
}

Explanation / Answer

// c++ code


#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
const int ROWS = 3;
const int COLS = 3;

void printArray(char board[3][3], int rows, int cols) //function to print the board
{
int i, j;

for(i = 0; i < ROWS; ++i)
{
for(j = 0; j < COLS; ++j)
{
cout << board[i][j]<<" ";
}
cout << endl;
}
}
bool isEmpty(char board[3][3], int rows, int cols){
for (int i=0;i<ROWS;i++){
for (int j=0;j<COLS;j++){
if (board[i][j]=='*')
return true;
}
}
return false;
}

string hasWon(char board[3][3], int rows, int cols,char player){
string Winner="";
if (player=='X')
Winner = "Player 1 wins!";
else
Winner = "Player 2 wins!";
if (board[0][0] == player && board[0][1] == player && board[0][2] == player)
return Winner;
else if (board[1][0] == player && board[1][1] == player && board[1][2] == player)
return Winner;
else if (board[2][0] == player && board[2][1] == player && board[2][2] == player)
return Winner;
else if (board[0][0] == player && board[1][0] == player && board[2][0] == player)
return Winner;
else if (board[0][1] == player && board[1][1] == player && board[2][1] == player)
return Winner;
else if (board[0][2] == player && board[1][2] == player && board[2][2] == player)
return Winner;
else if (board[0][0] == player && board[1][1] == player && board[2][2] == player)
return Winner;
else if (board[2][0] == player && board[1][1] == player && board[0][2] == player)
return Winner;
else
return "Tie!";
}

int main()
{
char ticTacToe[ROWS][COLS] = { {'*', '*', '*'},
{'*', '*', '*'},
{'*', '*', '*'}};

int p1InputRow, p1InputCol, p2InputRow, p2InputCol;
int turn = 1;
string Winner = "Tie!";
do{
printArray(ticTacToe, ROWS, COLS); //outputs the board
if (turn == 1){
cout<<"Player 1, Row: ";
cin>>p1InputRow;
while (p1InputRow>=3 || p1InputRow<0){
cout<<"Player 1, Row: ";
cin>>p1InputRow;
}
cout<<"Player 1, Column: ";
cin>>p1InputCol;
while (p1InputCol>=3 || p1InputCol<0){
cout<<"Player 1, Column: ";
cin>>p1InputCol;
}
if (ticTacToe[p1InputRow][p1InputCol]!= '*'){
cout<<"Cell is already filled,Enter again! "<<endl;
continue;
}
else
ticTacToe[p1InputRow][p1InputCol] = 'X';
}
else{
cout<<"Player 2, Row: ";
cin>>p2InputRow;
while (p2InputRow>=3 || p2InputRow<0){
cout<<"Player 2, Row: ";
cin>>p2InputRow;
}
cout<<"Player 2, Column: ";
cin>>p2InputCol;
while (p2InputCol>=3 || p2InputCol<0){
cout<<"Player 2, Column: ";
cin>>p2InputCol;
}
if (ticTacToe[p2InputRow][p2InputCol]!= '*'){
cout<<"Cell is already filled,Enter again! "<<endl;
continue;
}
else
ticTacToe[p2InputRow][p2InputCol] = 'O';
}
Winner = hasWon(ticTacToe, ROWS, COLS,((turn == 1)?'X':'O')) ;
if (Winner != "Tie!")
break;
turn = (turn +1 )%2;
cout<<endl;
}while(isEmpty(ticTacToe, ROWS, COLS)==true);
cout<<endl;
cout<<Winner<<endl;

return 0;
}

// Sample Output:

* * *                                                                                                                            

* * *                                                                                                                            

* * *                                                                                                                            

Player 1, Row: 23                                                                                                                

Player 1, Row: 1                                                                                                                 

Player 1, Column: 12                                                                                                             

Player 1, Column: 1                                                                                                              

                                                                                                                                 

* * *                                                                                                                            

* X *                                                                                                                            

* * *                                                                                                                            

Player 2, Row: 1                                                                                                                 

Player 2, Column: 1                                                                                                              

Cell is already filled,Enter again!                                                                                              

                                                                                                                                 

* * *                                                                                                                            

* X *                                                                                                                            

* * *                                                                                                                            

Player 2, Row: 0                                                                                                                 

Player 2, Column: 0                                                                                                              

                                                                                                                                 

O * *                                                                                                                            

* X *                                                                                                                            

* * *                                                                                                                            

Player 1, Row: 0                                                                                                                 

Player 1, Column: 3                                                                                                              

Player 1, Column: 0                                                                                                              

Cell is already filled,Enter again!   

                                                                                                                                 

O * *                                                                                                                            

* X *                                                                                                                            

* * *                                                                                                                            

Player 1, Row: 1                                                                                                                 

Player 1, Column: 2                                                                                                              

                                                                                                                                 

O * *                                                                                                                            

* X X                                                                                                                            

* * *                                                                                                                            

Player 2, Row: 1                                                                                                                 

Player 2, Column: 0                                                                                                              

                                                                                                                                 

O * *                                                                                                                            

O X X                                                                                                                            

* * *                                                                                                                            

Player 1, Row: 2                                                                                                                 

Player 1, Column: 0                                                                                                              

                                                                                                                                 

O * *                                                                                                                            

O X X                                                                                                                            

X * *                                                                                                                            

Player 2, Row: 0                                                                                                                 

Player 2, Column: 1   

                                                                                                                                 

O O *                                                                                                                            

O X X                                                                                                                            

X * *                                                                                                                            

Player 1, Row: 0                                                                                                                 

Player 1, Column: 2   

                                                                                                           

Player 1 wins!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote