Question

In: Computer Science

Tony Gaddis C++ Tic-Tac-Toe Write a program that allows two players (player X and player O)...

Tony Gaddis C++

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.

Solutions

Expert Solution

Code

#include<iostream>
using namespace std;

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

void displayBoard(char board[][COLS]);
//void playerTurn(char board[][COLS], char player);
bool checkForWinner(char board[][COLS], char player);
bool isBoardFull(char board[][COLS]);
void fillBoard(char [][3]);
bool gameOver(char board[][3]);
void getChoice(char board[][3],bool);
void showBoard(char board[][3]);
int main()
{
   char board[ROWS][COLS];
   bool playerToggle=false;
   fillBoard(board);
   showBoard(board);
   while(!gameOver(board))
   {
       getChoice(board,playerToggle);
       showBoard(board);
       playerToggle=!playerToggle;
   }
   return 1;
}

void fillBoard(char board[][3])
{
   for(int i=0;i<ROWS;i++)
       for(int j=0;j<COLS;j++)
           board[i][j]='*';
}

void showBoard(char board[][3])
{
   cout<<" 1 2 3"<<endl;
   for(int i=0;i<ROWS;i++)
   {
       cout<<(i+1)<<" ";
       for(int j=0;j<COLS;j++)
       {
           cout<<board[i][j]<<" ";
       }
       cout<<endl;
   }
}
bool gameOver(char board[][3])
{
   bool win=false;
   for(int i=0;i<3;i++)
   {
       if(board[i][0]=='X' &&board[i][1]=='X' && board[i][2]=='X')
       {
           cout<<"\nPlayer X Wins!"<<endl;
           win=true;
           return win;
       }
       if(board[0][i]=='X' &&board[1][i]=='X' && board[2][i]=='X')
       {
           cout<<"\nPlayer X Wins!"<<endl;
           win=true;
           return win;
       }
   }
   if(board[0][0]=='X' && board[1][1]=='X' && board[2][2]=='X')
   {
       cout<<"\nPlayer X Wins!"<<endl;
       win=true;
       return win;
   }
   if(board[0][2]=='X' && board[1][1]=='X' && board[2][0]=='X')
   {
       cout<<"\nPlayer X Wins!"<<endl;
       win=true;
       return win;
   }

   for(int i=0;i<3;i++)
   {
       if(board[i][0]=='O' &&board[i][1]=='O' && board[i][2]=='O')
       {
           cout<<"Player O Wins!"<<endl;
           win=true;
           return win;
       }
       if(board[0][i]=='O' &&board[1][i]=='O' && board[2][i]=='O')
       {
           cout<<"Player O Wins!"<<endl;
           win=true;
           return win;
       }
   }
   if(board[0][0]=='O' && board[1][1]=='O' && board[2][2]=='O')
   {
       cout<<"Player O Wins!"<<endl;
       win=true;
       return win;
   }
   if(board[0][2]=='O' && board[1][1]=='O' && board[2][0]=='O')
   {
       cout<<"Player O Wins!"<<endl;
       win=true;
       return win;
   }

  
   for(int i=0;i<ROWS;i++)
   {
       for(int j=0;j<COLS;j++)
       {
           if(board[i][j]=='*')
               return win;
       }
   }
   cout<<"Tie"<<endl;
   return true;
}
void getChoice(char board[][3],bool playerToggle)
{
   char symbol;
   int player;
   int row,col;
   if(playerToggle)
   {
       symbol='O';
       player=2;
   }
   else
   {
       symbol='X';
       player=1;
   }
   while(true)
   {
       while(true)
       {
           cout<<"Player "<<symbol<<", Enter Row: ";
           cin>>row;
           if(row>=1 && row<=3)
               break;
       }
       while(true)
       {
           cout<<"Player "<<symbol<<", Enter Column: ";
           cin>>col;
           if(col>=1 && col<=3)
               break;
       }
       if(board[row-1][col-1]=='*')
       {
           board[row-1][col-1]=symbol;
           break;
       }
       else
           cout<<"That postion is already taken! Try Again."<<endl;
   }
  
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional...
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 does the following: 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...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human players who alternate entering their moves on the same computer. Create a 3-by-3 two-dimensional array. Each player indicates their moves by entering a pair of numbers representing the row and column indices of the square in which they want to place their mark, either an 'X' or an 'O'. When the first player moves, place an 'X' in the specified square. When the second...
explain a pseudocode for tic tac toe in c++ between a computer and a player in...
explain a pseudocode for tic tac toe in c++ between a computer and a player in words
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start by asking the player one for their name and then player two for their name. Then should print the board and ask the user what column and then what row they would like to put their x (or o, depending on the player) . Use the clear function to make it look as though the board is never repeating.. Thanks!
USING RSTUDIO: Write a script that allows two people to play tic-tac-toe. The script should allow...
USING RSTUDIO: Write a script that allows two people to play tic-tac-toe. The script should allow each player to take sequential turns and to interactively enter their choice of position at the command line on each turn. For each turn, the script should output the set-up of the board. Note, you don’t have to use “x” and “o” as the symbol of each player. Note, the main body of this code (not including functions) should be able to be written...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed. I never said what type of code I needed in a previous question. I apologize and I can't go back and change it so here is the same question with more information Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players...
How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
I am having an issue with the Java program with a tic tac toe. it isn't...
I am having an issue with the Java program with a tic tac toe. it isn't a game. user puts in the array and it prints out this. 1. Write a method print that will take a two dimensional character array as input, and print the content of the array. This two dimensional character array represents a Tic Tac Toe game. 2. Write a main method that creates several arrays and calls the print method. Below is an example of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT