Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How do I write a tic tac toe game for c++? Please help... I've seen the following code before but this code did not work.

How do I write a tic tac toe game for c++? Please help... I've seen the following code before but this code did not work. The instructions are in the pictures below. #include  #include  #include  using namespace std; const char S[3] = { '*', 'X', 'O' }; int winner (int b [3][3]); void draw(int b[3][3]); bool isMoveValid(int b[3][3]); bool isGameOver(int b[3][3]); // return 1 if player 1 has won // return 2 if player 2 has won // return 0 if neither player has won int winner(int b[3][3]) { int win = 0; for (int i = 0; i if (b[i][0] == b[i][1] && b[i][1] == b[i][2]) win = b[i][0]; for (int i = 0; i if (b[0][i] == b[1][i] && b[1][i] == b[2][i]) win = b[0][i]; if ((b[0][0] == b[1][1] && b[1][1] == b[2][2]) || (b[0][2] == b[1][1] && b[1][1] == b[2][0])) win = b[1][1]; return win; } void Draw(int b[3][3]) { cout for (int i = 0; i for (int j = 0; j switch (b[i][j]) { case 0:{ cout break; case 1:{ cout break; case 2:{ cout break; default: break; } } cout bool isMoveValid(int b[3][3], int row, int column) { if (row >= 0 && row = 0) { if (b[row][column] == 0) return true; else return false; } return false; } bool isGameOver(int b[3][3]) { if (winner(b) == 1 || winner(b) == 2) return true; for (int r = 0; r for (int c = 0; c if (b[r][c] == 0) return false; return true; } int main() { srand((unsigned)time(0)); int b[3][3] = { { 0 } }; int player = 1; int row, column, result; bool ValidMove; while(true) { coutint choice; cin>>choice; switch(choice) { case 1: coutwhile (!isGameOver(b)){ cout > row >> column; ValidMove = isMoveValid(b, row, column); while (!ValidMove){ cout > row >> column; ValidMove = isMoveValid(b, row, column); } b[row][column] = player; Draw(b); player = 3 - player; } result = winner(b); if (result == 0){ cout else { cout break; case 2: player = 1; Draw(b); while (!isGameOver(b)){ if(player==1){ cout > row >> column; ValidMove = isMoveValid(b, row, column); while (!ValidMove){ cout > row >> column; ValidMove = isMoveValid(b, row, column); } b[row][column] = player; Draw(b); } else { row=1; column=1; cout while (!ValidMove){ row=rand()%2; column=rand()%2; ValidMove = isMoveValid(b, row, column); } coutif (result == 0){ cout else { cout break; case 3: coutdefault: coutreturn 0; } image text in transcribedimage text in transcribed
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 in the array should be initialized with an asterisk (). The program should run a loop that: .Displays the contents of the board array .Allows player 1 to select a location on the board for an X. The program shoud ask the user to enter the row and column number .Allows player 2 to select a location on the board for an O. The program should ask the user to enter the row and column number Determines whether 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. At game end, show the menu to Play the Game, Display the Game Stats, or Quit. . Player 1 wins when there are three Xs in a row on the game board. The Xs can appear in a row, in a column, or diagonally across the board. Player 2 wins with the same conditions. A tie occurs when all of the locations on the board are full, but there is no winner The user is allowed to play the game multiple times. Use a menu to allow the player to choose to Play the Game, Display the Game Stats, or Quit. The screen clears each time the user plays a new game. Track game stats as follows total number of games played, how many times Player 1 wins, how many times Player 2 wins, and how many tie games. . . Use these functions as guidelines. Other designs are acceptable as long as the program is complete, logical and accurate good design. and utilizes a 2D array, structs, functions, parameters and .Outside of main define a struct Player with attributes name, symbol (X or O), wins, o Inside main TTT is a 2 player game. Either create 2 Player variables or an losses, ties array of Players with 2 elements. Pass the structs as parameters when needed. o Prompt the user to input two names for the players. Randomly decide who goes first and assign that name to Player 1. Randomly assign X or O to the players. main Show a menu with 3 options - Play Game, Show Stats, and Quit. Play game Loop to display the board, allow player 1 to take a turn, check for a win or tie, allow player 2 to take a turn (if not a win or tie), display the board, if not a win or te then loop again. Once a win or tie has been determined, display the game board one last time and display a message about the winner. Show the menu o

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions