1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Viefleur [7K]
4 years ago
6

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).
Computers and Technology
1 answer:
Solnce55 [7]4 years ago
6 0

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:

You might be interested in
Implement a sublinear running time complexity recursive function in Java public static long exponentiation (long x, int n) to ca
Archy [21]

Answer:

Following are the code block in the Java Programming Language.

//define recursive function

public static long exponentiation(long x, int n) {

//check the integer variable is equal to the 0.

if (x == 0) {

//then, return 1

return 1;

}

//Otherwise, set else

else {

//set long data type variable

long q = exponentiation(x, n/2);

q *= q;

//check if the remainder is 1

if (n % 2 == 1) {

q *= x;

}

//return the variable

return q;

}

}

Explanation:

<u>Following are the description of the code block</u>.

  • Firstly, we define the long data type recursive function.
  • Then, set the if conditional statement and return the value 1.
  • Otherwise, set the long data type variable 'q' that sore the output of the recursive function.
  • Set the if conditional statement and check that the remainder is 1 and return the variable 'q'.
8 0
4 years ago
A major retailer wants to enhance their customer experience and reduce losses
Mrac [35]

Answer:

Predict Demand

Explanation:

The type of Artificial Intelligence (Al) solution that would be suitable for solving this problem is "Predict Deman"

By predicting the demand of the potential consumer through the Artificial intelligence solution the retailer would be able to eliminate leftover out-of-stock scenarios because the adequate quantity of the products or demands will be provided thereby enhancing customers' experience arising from out-of-stock scenarios and reducing losses arising from the leftover.

5 0
3 years ago
How can artificial intelligence be used in learning science?
tiny-mole [99]

Artificial intelligence can be used to help science students research and verify their research. It is also a very interesting topic to study in general.

Hope that helped!!! k

5 0
4 years ago
The term that refers to the standard computer language for creating web pages is called:
Leni [432]

CSS or HTML5

fdjsafhjdshfjdkslahfkdsajf

8 0
3 years ago
State 4 &amp; circumstances under which warm<br>booting of a computer may be necessary​
Brums [2.3K]

Answer:

to close an open application

3 0
3 years ago
Other questions:
  • Array A is not a heap. Clearly explain why does above tree not a heap? b) Using build heap procedure discussed in the class, con
    15·1 answer
  • The second row of letters on the keyboard is called the______ row
    14·2 answers
  • You can include up to _____ logical conditions in the and function.
    12·1 answer
  • Which of the following is one of the tools in REPL.it that helps prevent syntax errors?
    10·1 answer
  • What is 36 Denary in Binary?? PLEASE ANSWER I WILL GIVE U BRAINLY!!
    9·2 answers
  • Use the table on the right to convert from binary to decimal.
    11·2 answers
  • Hi, I am from India. Our brainly platform isn't so good, many of 'em keep spamming questions that I ask. US platform seems much
    9·1 answer
  • How do people decide their ethical behavior
    13·2 answers
  • In the software development process, which review studies the software design before it is released for coding?
    7·1 answer
  • What is the digital revolution and how did it change society?
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!