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
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!
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...
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...
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes....
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 (such as #include <stdio.h> etc). Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players (you and a friend) take turns placing X’s and O’s onto the board. After each turn, the current board configuration should be displayed, and once a player connects...
Assume that the human player makes the first move against the computer in a game of Tic-Tac-Toe,
Assume that the human player makes the first move against the computer in a game of Tic-Tac-Toe, which has a 3 x 3 grid. Write a MATLAB function that lets the computer respond to that move. The function’s input argument should be the cell location of the human player’s move. The function’s output should be the cell location of the computer’s rst move. Label the cells as 1, 2, 3 across the top row; 4, 5, 6 across the middle...
C# (Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play...
C# (Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array of integers. The constructor should initialize the empty board to all 0s. Allow two human players. Wherever the first player moves, place a 1 in the specified square, and place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT