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...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has...
Using C language Problem!! <3 x 3 tic tac toe> 2 players are doing tic tac...
Using C language Problem!! <3 x 3 tic tac toe> 2 players are doing tic tac toe alternatively. Anyone could win when they first make a bingo for any line(horizontal, vertical, diagonal) [Constraints] 1. Use 2dimensional Array(3 X 3) 2. Each elements are 1 or 2 so that can show their player1, player2's mark. 3. Inputs are 1 through 9 as you can see 1 2 3 4 5 6 7 8 9 4. No inputs are duplicated 5. if...
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
make a 4x4 tic tac toe in javascript (X is the user) (O is the computer)
make a 4x4 tic tac toe in javascript (X is the user) (O is the computer)
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 a game of Tic Tac Toe, two players take turns making an available cell in...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
In a game of Tic Tac Toe, two players take turns making an available cell in...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT