Question
Fix the c++ progam involving tic-tac-toe below so it correctly displays which player has won. When compiling the code, player 2 would always display when
Fix the c++ progam involving tic-tac-toe below so it correctly displays which player has won. When compiling the code, player 2 would always display when there is a winner. Fix it so this doesn't happen. Thank you in advance!
// Header files #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(char player) { // Check if player 1 has won
// Check rows if (boardTile[1] == player && boardTile[2] == player && boardTile[3] == player) return true; else if (boardTile[4] == player && boardTile[5] == player && boardTile[6] == player) return true; else if (boardTile[7] == player && boardTile[8] == player && boardTile[9] == player) return true; // Check columns else if (boardTile[1] == player && boardTile[4] == player && boardTile[7] == player) return true; else if (boardTile[2] == player && boardTile[5] == player && boardTile[8] == player) return true; else if (boardTile[3] == player && boardTile[6] == player && boardTile[9] == player) return true; // Check Diagonals else if (boardTile[1] == player && boardTile[5] == player && boardTile[9] == player) return true; else if (boardTile[3] == player && boardTile[5] == player && boardTile[7] == player) return true;
// If no one has won, return false 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(player); // 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started