Question

In: Computer Science

FileWrite a program that will allow two users to play a tic-tac-toe game. You should write...


FileWrite a program that will allow two users to play a tic-tac-toe game. You should write the program such that two people can play the game without any special instructions. (assume they know how to play the game). You should code the program to do the five items below only. You do not have to write the entire program, just the five items below.
• Use a 2D array(3 rows, 3 columns) of datatype char. Initialize the 2D
array with a “ “(space) before the game starts.
• Prompt first player(X) to enter their first move.
(Enter row and column where they want to place their move)
• Then draw the game board showing the move. (see sample to the right)
• Prompt the second player(O) to enter their first move.
• Then draw the game board showing both moves. (see sample to the right)
For the purposes of this assignment, everything can be done in main….The assignment requirements stop here!
If you would like to program the entire game, you will need to:
1) Use a 2D array(3 rows, 3 columns) of datatype char. Initialize the 2D array with a “ “(space) before the game starts.
2) Test each move to see if that space is still available (a data validation loop should check the content of the array at the row and column entered by the user to see if an ‘X’ or ‘O’ is stored there….ask the user to keep entering row and column values until they enter numbers that do not have an ‘X’ or ‘O’.)
3) After each move, check to see if someone has won the game yet, if so, the game should end.
4) After 9 moves, if there is not winner declare a draw.
You can use a user-defined function named scanMove. scanMove should do the following:
• Will NOT return a value to main using a return statement. Should have parameters that include the 2D array, the turn number (so you know whether the player is ‘X’ or ‘O’. Player 1 should place an ‘X’, Player 2 will place a ‘Y’)
• Read in the row and column numbers of the box that the player would like to place their ‘X’ or ‘O’.
• Test each move to see if it’s a valid move (has the element already been used for an X or O?)
• Once you have a valid move, place the correct letter in the requested box.
You can use a user-defined function named drawBoard. drawBoard should draw the tic-tac-toe game board with ‘X’ and ‘O’ showing in the correct places. The board should look something like this:

Solutions

Expert Solution

C++ program for Tic-Tac-Toe Game -:

#include <iostream>

using namespace std;

void displayBoard(char b[][3], int size);
void grabInput(char player);
void updateBoard(char b[3][3], int /*size*/, char player, char currentMove);
void checkWin();
void checkDraw();
bool isPlaying = true; //Not necessary but I'll keep it for future changes
bool hasWon = false;
bool hasDrawn = false;

char player1 = 'X';
char player2 = 'O';
char activePlayer = player1;
char currentMove;

char b[3][3] =
   {
       {'1','2','3'},
       {'4','5','6'},
       {'7','8','9'}
   };

int main()
{
   displayBoard(b,3);

   while (isPlaying && !hasWon && !hasDrawn)
   {
       grabInput(activePlayer);
       char tile = b[0][currentMove - '1'];

       if (currentMove < '1' || currentMove >   '9')
           cout<<"No such square.\n";

       else
       {
           if (tile == 'X' || tile == 'O')
           {
               cout<<"Invalid Move\n";
           }
           else
           {
               updateBoard(b,3,activePlayer,currentMove);
               checkWin();
               checkDraw();
           }

       }

       if (hasWon)
           return 0;
       else
           displayBoard(b,3);

       if (activePlayer == player1) activePlayer = player2;
       else activePlayer = player1;
   }

}

void displayBoard(char b[3][3], int size)
{

   cout<<"::Tic Tac Toe::\n\n";

   for (int x = 0; x < size; x++)
   {
       for (int y = 0; y < size; y++)
       {
           cout<<b[x][y];
           cout<<" ";
       }
       cout<<endl;
   }
   cout<<endl;
}

void grabInput(char player)
{
   cout<<"Enter your move "<<player<<": ";
   cin>>currentMove;
}

void updateBoard(char b[3][3], int /*size*/, char player, char currentMove)
{
   if (currentMove > '0' && currentMove <= '9')
       b[0][currentMove - '1'] = player;
}

void checkWin()
{
   if (b[0][0] == 'X' && b[0][1] == 'X' && b[0][2] == 'X')
   {
           cout<<"\nPlayer 1 Wins!\n\n";
           hasWon = true;
   }
   if (b[1][0] == 'X' && b[1][1] == 'X' && b[1][2] == 'X')
   {
           cout<<"\nPlayer 1 Wins!\n\n";
           hasWon = true;
   }
   if (b[2][0] == 'X' && b[2][1] == 'X' && b[2][2] == 'X')
   {
           cout<<"\nPlayer 1 Wins!\n\n";
           hasWon = true;
   }

   if (b[0][0] == 'X' && b[1][0] == 'X' && b[2][0] == 'X')
   {
           cout<<"\nPlayer 1 Wins!\n\n";
           hasWon = true;
   }
   if (b[0][1] == 'X' && b[1][1] == 'X' && b[2][1] == 'X')
   {
           cout<<"\nPlayer 1 Wins!\n\n";
           hasWon = true;
   }
   if (b[0][2] == 'X' && b[1][2] == 'X' && b[2][2] == 'X')
   {
           cout<<"\nPlayer 1 Wins!\n\n";
           hasWon = true;
   }

   if (b[0][0] == 'X' && b[1][1] == 'X' && b[2][2] == 'X')
   {
           cout<<"\nPlayer 1 Wins!\n\n";
           hasWon = true;
   }
   if (b[0][2] == 'X' && b[1][1] == 'X' && b[2][0] == 'X')
   {
           cout<<"\nPlayer 1 Wins!\n\n";
           hasWon = true;
   }


   if (b[0][0] == 'O' && b[0][1] == 'O' && b[0][2] == 'O')
   {
       cout<<"\nPlayer 2 Wins!\n\n";
       hasWon = true;
   }
   if (b[1][0] == 'O' && b[1][1] == 'O' && b[1][2] == 'O')
   {
       cout<<"\nPlayer 2 Wins!\n\n";
       hasWon = true;
   }
   if (b[2][0] == 'O' && b[2][1] == 'O' && b[2][2] == 'O')
   {
       cout<<"\nPlayer 2 Wins!\n\n";
       hasWon = true;
   }

   if (b[0][0] == 'O' && b[1][0] == 'O' && b[2][0] == 'O')
   {
       cout<<"\nPlayer 2 Wins!\n\n";
       hasWon = true;
   }
   if (b[0][1] == 'O' && b[1][1] == 'O' && b[2][1] == 'O')
   {
       cout<<"\nPlayer 2 Wins!\n\n";
       hasWon = true;
   }
   if (b[0][2] == 'O' && b[1][2] == 'O' && b[2][2] == 'O')
   {
       cout<<"\nPlayer 2 Wins!\n\n";
       hasWon = true;
   }

   if (b[0][0] == 'O' && b[1][1] == 'O' && b[2][2] == 'O')
   {
       cout<<"\nPlayer 2 Wins!\n\n";
       hasWon = true;
   }
   if (b[0][2] == 'O' && b[1][1] == 'O' && b[2][0] == 'O')
   {
       cout<<"\nPlayer 2 Wins!\n\n";
       hasWon = true;
   }

}

void checkDraw()
{
   if (b[0][0] != '1' && b[0][1] != '2' && b[0][2] != '3' && b[1][0] != '4' && b[1][1] != '5' && b[1][2] != '6' &&
       b[2][0] != '7' && b[2][1] != '8' && b[2][2] != '9' && !hasWon)
   hasDrawn = true;
   if (hasDrawn)
   {
       cout<<"\nThe game is a draw!\n\n";

   }
}

OUTPUT -:


Related Solutions

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...
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...
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 LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your...
Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your program must use min-max search and should be invoked by the function call: > (Tic-Tac-Toe) The game is single player, human vs the computer AI.
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...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...
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...
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...
If you were to write a game of tic-tac-toe, you may store the representation of the...
If you were to write a game of tic-tac-toe, you may store the representation of the game board as a two dimensional list such as   [['X', '', 'X'], ['O', 'X', ''], ['', 'O', 'X']] where each sublist is a row in the board.   Empty strings ('') denote a space that does not yet have a value. Assuming this representation, write functions (contained in the same file) that do the following: a) Create a new empty tic-tac-toe board. This function should...
Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out...
Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out the state of the board, asks the user where they would like to place their mark, and implements this decision. The program then places its own mark on a randomly chosen available position. Once one of the player won, the program declares the result and asks if the user would like to continue. The first player is selected at random.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT