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

Part 1. Tic-Tac-Toe Write a program that allows two players (player X and player

ID: 3841847 • Letter: P

Question

Part 1. Tic-Tac-Toe Write a program that allows two players (player X and player O) 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 players take turns making moves and the program keeps track of whose turn it is. Player X moves first. The program should run a loop that: Displays the contents of the board array (see prompts and output , below). Prompts and allows the player whose turn it is to select a location on the board for an X in the case of player X or an O in the case of player O. The program should ask the player to enter the row and column number. Valid row or column numbers are 1, 2, or 3. The loop terminates when 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 say so and end. Player X (O) wins when there are three Xs (three Os) in a row on the game board. The Xs (Os) can appear in a row, in a column, or diagonally across the board. A tie occurs when all of the locations on the board are full, but there is no winner. Input Validation: The program should check the validity of the row and column numbers entered by the players. To be valid, the row and column number must refer to an unoccupied position in the game board. When an invalid entry is made, the program simply redisplays its most recent prompt and attempts to reread the value . Prompts And Output Labels: The board is displayed as a sequence of three rows, each with three characters . The characters are "X" or "O" or "." depending on whether player X or player O has chosen that position or whether the position is still unselected. Thus the following: . . X O . X . . . represents the board when it's O's turn and when X has moved twice (to row1/column3 and row2/column3) and O has moved once (to row2/column1). Players are prompted as "Player X, row and column:" or "Player O, row and column:". The game's end is announced either by "Player X wins", or "Player O wins" or "The cat wins" (in the case of no winner).

Im using C++. This is the code I have and it keeps giving me this message.

     The contents of your standard output is incorrect.
          There is an error in your prompts.

My current code

#include <iostream>

using namespace std;

// Constants - rows and columns

const int ROWS = 3;

const int COLS = 3;

// Function prototypes

void print(char [][COLS]);

void input(char [][COLS], char);

bool gameOver(char [][COLS]);

bool win(char [][COLS], char);

bool playerCanWin(char [][COLS], char);

void displayWinner(char [][COLS]);

int main()

{

char ch;

do{

// Game board array

char gameBoard[ROWS][COLS] = { '*', '*', '*',

'*', '*', '*',

'*', '*', '*'

};

do //do-while loop

{

// Display game board.

print(gameBoard);

// It is player 1's turn.

input(gameBoard, 'X');

// Display game board again.

print(gameBoard);

// If game not over, it

// is player 2's turn.

if (!gameOver(gameBoard))

input(gameBoard, 'O');

} while (!gameOver(gameBoard));

print(gameBoard);

// Display winner.

displayWinner(gameBoard);

cout<<"Do you want to play again (y/n): ";

cin>>ch;

}while(ch=='y' || ch=='Y');

return 0; //return a 0 value

}

// DisplayBoard function - displays the contents of

// the board.

void print(char board[][COLS])

{

// Display column headings.

cout << " Columns ";

cout << " 1 2 3 ";

// Display rows.

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

{

// Row label.

cout << "Row " << (row + 1)<< ": ";

// Row contents.

for (int col = 0; col < COLS; col++)

{

cout << board[row][col] << " ";

}

cout << endl;

}

}

// PlayerTurn function - allows a player to

// take a turn (X or O).

void input(char board[][COLS], char symbol)

{

// isAvailable flag - set to true when

// player selects an available location on the board.

bool isAvailable = false;

int row; // Row available for symbol

int col; // column availabale for symbol

// Player prompt to enter location.

cout << endl;

cout << "Enter player " << symbol << ", row and column: ";

// Get and validate the location.

while (!isAvailable)

{

// Get the row.

cin >> row >> col;

// Validate the row and column.

while (row < 1 || row > ROWS || col < 1 || col > COLS)

{

cout << "Invalid Row or Column!. Input again ";

cin >> row >> col;

}

// Determine whether the selected

// cell is available.

if (board[row - 1][col - 1] == '*')

isAvailable = true;

else

{

cout << "That location is not available. "<< "Select another location. ";

}

}

// Place the player's symbol on the board

// at the selected location.

board[row - 1][col - 1] = symbol;

}

// gameOver function - returns true if game

// is over. This is the case when either player has

// already won, or there is a tie.

bool gameOver(char board[][COLS])

{

// Once a player wins, game over.

if (win(board, 'X') || win(board, 'O'))

return true;

// if one player can still win then game

// not over.

else if (playerCanWin(board, 'X') || playerCanWin(board, 'O'))

{

return false;

}

// a tie equals game over.

else

return true;

}

// win function - accepts game board and

// player symbols of X or O as arguments. It will return

// true if the player has won.

bool win(char board[][COLS], char symbol)

{

// Check first horizontal row.

if (board[0][0] == symbol && board[0][1] == symbol && board[0][2] == symbol)

return true;

// Check second horizontal row.

if (board[1][0] == symbol && board[1][1] == symbol && board[1][2] == symbol)

return true;

// Check third horizontal row.

if (board[2][0] == symbol && board[2][1] == symbol && board[2][2] == symbol)

return true;

// Check first column.

if (board[0][0] == symbol && board[1][0] == symbol && board[2][0] == symbol)

return true;

// Check second column.

if (board[0][1] == symbol && board[1][1] == symbol && board[2][1] == symbol)

return true;

// Check third column.

if (board[0][2] == symbol && board[1][2] == symbol && board[2][2] == symbol)

return true;

// Check the diagonal

if (board[0][0] == symbol && board[1][1] == symbol && board[2][2] == symbol)

return true;

if (board[0][2] == symbol && board[2][0] == symbol && board[1][1] == symbol)

return true;

return false;

}

// playerCanWin function - returns true if a

// player can still win.

bool playerCanWin(char board[][COLS], char symbol)

{

// Check first horizontal row for a possibility.

if ((board[0][0] == symbol || board[0][0] == '*') &&(board[0][1] == symbol || board[0][1] == '*') &&(board[0][2] == symbol || board[0][2] == '*'))

return true;

// Check second horizontal row for a possibility.

if ((board[1][0] == symbol || board[1][0] == '*') &&(board[1][1] == symbol || board[1][1] == '*') &&(board[1][2] == symbol || board[1][2] == '*'))

return true;

// Check third horizontal row for a possibility.

if ((board[2][0] == symbol || board[2][0] == '*') && (board[2][1] == symbol || board[2][1] == '*') && (board[2][2] == symbol || board[2][2] == '*'))

return true;

// Check first column for a possibility.

if ((board[0][0] == symbol || board[0][0] == '*') && (board[1][0] == symbol || board[1][0] == '*') &&(board[2][0] == symbol || board[2][0] == '*'))

return true;

// Check second column for a possibility.

if ((board[0][1] == symbol || board[0][1] == '*') &&(board[1][1] == symbol || board[1][1] == '*') &&(board[2][1] == symbol || board[2][1] == '*'))

return true;

// Check third column for a possibility.

if ((board[0][2] == symbol || board[0][2] == '*') &&(board[1][2] == symbol || board[1][2] == '*') &&(board[2][2] == symbol || board[2][2] == '*'))

return true;

// Check the diagonal for a possibility.

if ((board[0][0] == symbol || board[0][0] == '*') &&(board[1][1] == symbol || board[1][1] == '*') &&(board[2][2] == symbol || board[2][2] == '*'))

return true;

// Check the diagonal for a possibility.

if ((board[0][2] == symbol || board[0][2] == '*') &&(board[1][1] == symbol || board[1][1] == '*') &&(board[2][0] == symbol || board[2][0] == '*'))

return true;

return false;

}

// displayWinner function - displays the winner.

void displayWinner(char board[][COLS])

{

if (win(board, 'X'))

cout << "Player (X) WINS!!!!! ";

else if (win(board, 'O'))

cout << "Player (O) WINS!!!!! ";

else

cout << "The cat wins ";

//system ("pause");

}

Explanation / Answer

Hi there,

How are you? Looks like you are going through the c++ tictactoe assignment phase huh? I had that too, I made this program and kept improving it overtime yet made beginner friendly! When I wrote this I was a novice, so the code'll be easy to read and has some pretty good practices like not -> using namespace std; , which I do a lot now :p, hehe

#include <iostream>
#include <string>


int board[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
int turns = 0;  
int r = 0, c = 0;
int p = 1;
std::string name1, name2;


bool isWinner(int p);
bool winCheck(int p, int x1,int y1, int x2,int y2, int x3,int y3);
bool isFull();
bool isCat();
bool isValid(int r, int c);
int numTurns();
int playerAt(int r, int c);
bool isTaken(int r, int c);
std::string XO(int status);
void print_board();
void playMove(int p, int r, int c);

int main()
{
   std::cout << "Player1 Enter your name: ";
   std::cin >> name1;
   std::cout << "Player2 Enter your name: ";
   std::cin >> name2;

   // While the board's still got game to play. This is the main loop of the
   // game that keeps the game running until it's won or is filled up!
   while (!(isWinner(1) || isWinner(2) || isFull()))
   {
       print_board();
       if (p == 1)
           std::cout << name1 << " enter an 'X': ";
       if (p == 2)
           std::cout << name2 << " enter an 'O': ";
       std::cin >> r >> c;

       // Check user input, lock him in loop if not valid co-ordinates.
       while (!isValid(r, c) || isTaken(r, c))
       {
           if (!isValid(r, c))
           {
               std::cout << "Not a valid location. Try again."
                   << std::endl;
           }
           else if (isTaken(r, c))
           {
               std::cout << "Location already full. Try again."
                   << std::endl;
           }
           print_board();
           if (p == 1)
               std::cout << name1 << " enter an 'X': ";
           if (p == 2)
               std::cout << name2 << " enter an 'O': ";
           std::cin >> r >> c;  
       }
       playMove(p, r, c);

       if (p == 1)
           p = 2;
       else
           p = 1;
   }
   print_board();

   // Print stuff incase there is a winner or the board is full.
   if (isWinner(1))
       std::cout << name1 << " is the winner!"
           << std::endl;
   else if (isWinner(2))
       std::cout << name2 << " is the winner!"      
           << std::endl;
   else if (isCat())
       std::cout << "Game is a tie.";
    return 0;
}

/**
* Takes the player number and then returns true if
* the player has his number over any of the below
* combinations.
*/
bool isWinner(int p)
{
   // top row
   if (winCheck(p, 0,0, 0,1, 0,2)) return true;
   // middle row
   if (winCheck(p, 1,0, 1,1, 1,2)) return true;
   // bottom row
   if (winCheck(p, 2,0, 2,1, 2,2)) return true;
   // left column
   if (winCheck(p, 0,0, 1,0, 2,0)) return true;
   // middle column
   if (winCheck(p, 0,1, 1,1, 2,1)) return true;
   // right column
   if (winCheck(p, 0,2, 1,2, 2,2)) return true;
   // diagonal top-left to bottom-right
   if (winCheck(p, 0,0, 1,1, 2,2)) return true;
   // diagonal top-right to bottom-left
   if (winCheck(p, 0,0, 0,1, 0,2)) return true;

   return false;
}

/**
* isWinner function calls this function to check
* if a boards value is equal to the players number
* if it is it returns true.
*/
bool winCheck(int p, int x1,int y1, int x2,int y2, int x3,int y3)
{
   return (
           board[x1][y1] == p
       && board[x2][y2] == p
       && board[x3][y3] == p
   );
}

/**
* This function returns true if a board is full.
*/
bool isFull()
{
   return turns == 9;
}

/**
* Returns true if board game is a tie.
*/
bool isCat()
{
   return isFull() && !isWinner(1) && !isWinner(2);
}

/**
* Checks to see if the user entered data is under limits.
*/
bool isValid(int r, int c)
{
   if (0 <= r && r <= 2 && 0 <= c && c <= 2)
       return true;
   return false;
}

/**
* Returns the number of turns made.
*/
int numTurns()
{
   return turns;
}

/**
* Get's you the player at given point.
*/
int playerAt(int r, int c)
{
   if (isValid(r, c))
       return board[r][c];
   return 9;
}

/**
* Returns true if the cell is not empty.
*/
bool isTaken(int r, int c)
{
   int p = playerAt(r, c);
   if (p == 1 || p == 2)
       return true;
   return false;
}

/**
* This function is used by print board. It returns X or O respective to player
* status.
*/
std::string XO(int status)
{
   if (status == 0) return " ";
   else if (status == 1) return "X";
   else if (status == 2) return "O";
}

/**
* Prints the whole board.
*/
void print_board()
{
   std::string out = "";
   out += " 0 " + XO(board[0][0]) +"|"+ XO(board[0][1]) +"|"+ XO(board[0][2]) +" ";
   out += "    --+-+--" + std::string(" ");
   out += " 1 " + XO(board[1][0]) +"|"+ XO(board[1][1]) +"|"+ XO(board[1][2]) +" ";
   out += "    --+-+--" + std::string(" ");
   out += " 2 " + XO(board[2][0]) +"|"+ XO(board[2][1]) +"|"+ XO(board[2][2]) +" ";
   out += "    --+-+--" + std::string(" ");
   std::cout << out << std::endl;
}

/**
* Plays a move.
*/
void playMove(int p, int r, int c)
{
   board[r][c] = p;
   turns++;
}

Beaware that this is code from my repository from github, so mix those functions up a bit so that you'll not be marked plagarized from my multiple online scripts or repos(I got names like primehero and rj.. so, you know, change it up and it'll be fun fixing broken stuff while changing). FYI I'll also tell you what's wrong with your code soon, so you get to know about the errors too.

Cheers,

Rj