Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the program below written in c++, make changes to correctly display which player (1 or 2) has won when the tic-tac-toe game is done.

Using the program below written in c++, make changes to correctly display which player (1 or 2) has won when the tic-tac-toe game is done. The issue with this program is that when you compile the code and play through one game, it always displays that player 2 has won. Please do not make any significant changes to the program and try to fix this issue. Thank you in advance!

// Header files #include #include #include

using namespace std;

// Declare a char array to represent the TTT board tiles char boardTile[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

// Display the board and show which player has 'X' or 'O'. void drawBoard() { cout << " \tTic Tac Toe "; cout << "Player 1 has 'X' - Player 2 has 'O'" << endl cout << " | | " << endl; cout << " " << boardTile[1] << " | " << boardTile[2] << " | " << boardTile[3] << " " << endl; cout << " _______|_______|_______ " << endl; cout << " | | " << endl; cout << " " << boardTile[4] << " | " << boardTile[5] << " | " << boardTile[6] << " " << endl; cout << " _______|_______|_______ " << endl; cout << " | | " << endl; cout << " " << boardTile[7] << " | " << boardTile[8] << " | " << boardTile[9] << " " << endl; cout << " | | " << endl; }

// Function to determine if either player 1 or player 2 has won bool win() { // Check for a win by row if (boardTile[1] == boardTile[2] && boardTile[2] == boardTile[3]) return true; else if (boardTile[4] == boardTile[5] && boardTile[5] == boardTile[6]) return true; else if (boardTile[7] == boardTile[8] && boardTile[8] == boardTile[9]) return true;

// Check for a win by column else if (boardTile[1] == boardTile[4] && boardTile[4] == boardTile[7]) return true; else if (boardTile[2] == boardTile[5] && boardTile[5] == boardTile[8]) return true; else if (boardTile[3] == boardTile[6] && boardTile[6] == boardTile[9]) return true;

// Check for a win by diagonal else if (boardTile[1] == boardTile[5] && boardTile[5] == boardTile[9]) return true; else if (boardTile[3] == boardTile[5] && boardTile[5] == boardTile[7]) return true;

// If none of the above conditions are met, then there is no win else return false; } // Function to determine if the choice is valid and within the range (1-9) bool validChoice(int choice) { if (choice >= 1 && choice <= 9) return true; else return false; } // Function to reset the board after a game has been completed void resetBoard() { for (int i = 1; i < 10; i++) boardTile[i] = char(i + '0'); }

int main() // Main function of the program { int player = 1; // Player 1 starts first. In this game of tic-tac-toe, we are player 1. If player 2 were to win, it would display a loss int choice; // Stores the player's choice char mark; // Stores the player's mark ('X' or 'O') bool gameOver = false; // Flag to determine if the game is over

int wins = 0; // Stores the number of wins, losses, and ties int losses = 0; int ties = 0;

do { drawBoard(); // Draws the board by invoking the function

player = (player % 2) ? 1 : 2; // Alternates between player 1 and 2

cout << " Player " << player << ", enter a number: "; cin >> choice;

mark = (player == 1) ? 'X' : 'O'; // Assigns a mark (X or O) to the player

while (!validChoice(choice)) { // Checks if the choice is invalid. cout << "Invalid move! Please enter a valid number (1-9): "; cin >> choice; // Prompts the player to enter another number within the range 1-9 }

if (validChoice(choice)) { // Checks if the choice is valid

boardTile[choice] = mark; // Assigns the mark to the board tile

gameOver = win(); // Checks if someone has won

player++; // Increments to switch players

} if (gameOver) { // If someone has won, display a message and increment wins/losses/ties accordingly

drawBoard(); // Redraws the board with updated tiles

if (player == 1) { cout << "Player 1 wins!" << endl; wins++; } else if (player == 2) { cout << "Player 2 wins!" << endl; losses++; } else { cout << "It's a tie!" << endl; ties++; }

cout << "Wins: " << wins << endl; cout << "Losses: " << losses << endl; cout << "Ties: " << ties << endl;

char playAgain; // Stores player input for playing again or not cout << " Do you want to play again? (y/n): "; // Asks the player if they would like to play again. cin >> playAgain;

if (playAgain == 'y' || playAgain == 'Y') { // If the player says Y or y, the game will start over. gameOver = false; resetBoard(); player = 1; }

} } while (!gameOver); // Keep looping until someone has won or all tiles have been filled.

return 0; }

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

Recommended Textbook for

Databases Illuminated

Authors: Catherine M. Ricardo, Susan D. Urban, Karen C. Davis

4th Edition

1284231585, 978-1284231588

More Books

Students also viewed these Databases questions

Question

find all matrices A (a) A = 13 (b) A + A = 213

Answered: 1 week ago