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).

Respuesta :

Answer:

#include <iostream>

using namespace std;

//function prototypes

void showBoard(char[][3]);

bool checkWinner(char[][3], char);

void playerMove(char[][3], char);

int main()

{

   //declare variables needed

   //declare 2D array for the board

   //and initialize with all *

   char board[3][3] = {{'*', '*', '*'},

                       {'*', '*', '*'},

                       {'*', '*', '*'}};

   int moves = 0;  //variable to keep track

                   //of number of moves

                   //to determine tie

   cout << "TIC - TAC - TOE\n\n";

   //while loop to repeat until 9 moves are done

   while(true){

       //display board

       showBoard(board);

       cout << "Player 1 moves\n";

       //get player X move

       playerMove(board, 'X');

       //increment moves counter

       moves++;

       //if this is a winning move

       //store winner and terminate

       if(checkWinner(board, 'X')){

           showBoard(board);

           cout << "\nPlayer 1 (X) wins!\n";

           return 0;

       }

       //if 9 moves are done

       //break from loop

       if(moves == 9)

           break;

       //display board again

       showBoard(board);

       cout << "Player 2 moves\n";

       //do the same thing for player O

       playerMove(board, 'O');

       moves++;

       if(checkWinner(board, 'O')){

           showBoard(board);

           cout << "\nPlayer 2 (O) wins!\n";

           return 0;

       }

   }

   //if we have gone this far and program

   //still has not terminated (no winner)

   //it means this is a tie

   showBoard(board);

   cout << "This game is a tie!\n";

   //return 0 to mark successful completion of program

   return 0;

}

//this function is helpful because we need to show

//the board repetitively during the program

void showBoard(char board[][3]){

   cout << endl;

   //loop on the rows

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

       //loop on the columns

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

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

       //display newline after each row

       cout << endl << endl;

   }

   cout << endl;

}

//this function checks if second argument

//is a winning player

bool checkWinner(char board[][3], char player){

   //boolean variable to check

   //for winner later

   bool flag;

   //CHECK FOR WINNER IN ROWS

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

       //initialize flag to true

       flag = true;

       //loop within a row

       for(int col = 0; col < 3; col++){

           //Notice that the right part of the

           //assignment operator, is an expression

           //with a relational operator (==)

           //this expression will yield either

           //true (1) or false (0)

           //while flag is already true (1)

           //if multiplied by true (1) will result

           //in true(0), or multiplied by false (0)

           //will result in false (0)

           flag *= (board[row][col] == player);

       }

       //after checking within row, if the flag

       //is still true at this point, it means we have

       //three chars of the same kind within the row,

       //thus we have a winner

       if(flag)

           return true;

       else

           continue;

   }

   //CHECK FOR WINNER IN COLUMNS

   //using a similar logic

   for(int col = 0; col < 3; col++){

       flag = true;

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

           flag *= (board[row][col] == player);

       }

       if(flag)

           return true;

       else

           continue;

   }

   //CHECK FIRST DIAGONAL (row = col)

   //reset flag to true

   flag = true;

   //check diagonal

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

       flag *= (board[i][i] == player);

   }

   //check if there is winner

   if(flag)

       return true;

   //CHECK OTHER DIAGONAL (row = 2 - col)

   //reset flag to true

   flag = true;

   //check diagonal

   for(int col = 0; col < 3; col++){

       flag *= (board[2-col][col] == player);

   }

   //check if there is winner

   if(flag)

       return true;

   //if all of these have been checked

   //and function still has not returned,

   //it means there is no winner

   return false;

}

//this function gets a move from the player,

//checks if it is valid, and if yes it puts

//it on the board

void playerMove(char board[][3], char player){

   //variables to store user move

   int row, col;

   //get user move

   cout << "Row: ";

   cin >> row;

   cout << "Col: ";

   cin >> col;

   //check if this is valid move

   //you have to check if that tile has

   //already been marked, or if tile

   //of choice is out of bounds of board

   while(board[row-1][col-1] != '*' ||

         row > 3 || row < 0 ||

         col > 3 || row < 0)

           {

       cout << "Invalid move! Try again\n";

       cout << "Row: ";

       cin >> row;

       cout << "Col: ";

       cin >> col;

   }

   //after validation, mark new move

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

}

Explanation:

Answer:

the third graph

Explanation:

C

ACCESS MORE