Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this program you will modify the code you created for program 1 - Tic Tac Toe. Playing one game of Tic-Tac-Toe is nice, but

In this program you will modify the code you created for program 1 - Tic Tac Toe. Playing one game of Tic-Tac-Toe is nice, but we usually want to play several games to account for mistakes, errors, or simply bad play. For this reason, you will enclose your existing code in a user-controlled loop that will continue to play the game until the user decides to terminate the loop. While the game is running, you will keep track of : Number of individual games won Number of games won by X Number of games won by O In addition, if the user makes an invalid menu selection or chooses to play a 1 player game you should present them with an appropriate message and return them to the menu rather than exiting the program. After the user terminates the game, the program should return to the menu. If the user decides to end the game, you are required to report : The total number of games played during this "session" The total number of games won by X this "session" The total number of games won by O this "session" The total number of games resulting in a tie this "session" In addition, you will report the same information with a "historical" context by reading the statistics from a file you will create called "TTTStats.txt". The file will be read in, used and then you will upda

//Describe program

// Adam Horton

#include #include #include #include

using namespace std;

void displayBoard (char* board); bool isWinning (char* board, char player); void getMove (char* board, char player); bool isEven (int digit); char* setupGame (); int play1PGame (); int play2PGame (); void swap (char& current, char& old);

int main () { int winner, // Which player won? numPlayers; //number of players (1 or 2) numPlayers=0; winner=0; int x = 0; int O =0; int tie=0; ifstream inFile("tttstats.txt"); ostream outFile("tttstats.txt");

while(numPlayers < 1 or numPlayers > 3 or cin.fail()) {cin.clear(); cout << "Welcome to Doc Brown's Tic-Tac-Toe" << endl; cout << "Enter your selection from the options below..." << endl;

cout << endl << " 1. 1 Player Game" << endl; cout << " 2. 2 Player Game" << endl; cout << " 3. Exit" << endl; cout << "Enter your choice : ";

cin >> numPlayers;

switch (numPlayers) { case 1 : cout << "Sorry, but that is not an option ... yet" << endl; break; case 2 : winner = play2PGame (); break; case 3 : cout << "Have a great day" << endl; return 0; outfile("tttstats.txt"); default : cout << "That is an invalid option. Please try again" << endl; break; }

if (winner == 1); { x win= x win +1; cout << "Congrats X, you win!" << endl; } else if (winner == 2) { O win= O win +1; cout << "Congrats O, you win!" << endl; } else { tie=tie+1; cout << "Oh no! It was a tie!" << endl; } numPlayers=0; winner= 0; } inFile.open("tttstats.txt"); outFile.open("tttstats.txt"); cout<< "This session player1 won : "<

void displayBoard (char* board) { system ("CLS"); cout << " | | " << endl; cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl; cout << " | | " << endl; cout << "-----------" << endl; cout << " | | " << endl; cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl; cout << " | | " << endl; cout << "-----------" << endl; cout << " | | " << endl; cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl; cout << " | | " << endl; }

bool isEven (int digit) { if (digit % 2 == 0) return true; return false; }

bool isWinning (char* board, char player) { if ( (board[0] == player && board[1] == player && board[2] == player) || (board[3] == player && board[4] == player && board[5] == player) || (board[6] == player && board[7] == player && board[8] == player) || (board[0] == player && board[3] == player && board[6] == player) || (board[1] == player && board[4] == player && board[7] == player) || (board[2] == player && board[5] == player && board[8] == player) || (board[0] == player && board[4] == player && board[8] == player) || (board[2] == player && board[4] == player && board[6] == player) ) return true; else return false; }

void getMove (char* board, char player) { int userChoice;

do { cout << "It is " << player <<"'s turn - pick a space : "; cin >> userChoice; } while (userChoice <= 0 || userChoice >= 10 || board[userChoice - 1] == 'O' || board[userChoice -1] == 'X'); board[userChoice - 1] = player; }

char* setupGame () { char* board = new char[10]; for (int i = 0; i < 9; i++) board[i] = '0' + (i + 1); return board; }

void swap (char& current, char& old) { char rearrange; rearrange = current; current = old; old = rearrange; } int evaluate (char* board, int player) {

} int play1PGame () { int moves = 1; bool winner = false; char* board = setupGame (); int player = 1;

}

int play2PGame () { int moves = 1; bool winner = false; char* board = setupGame (); char player = 'X'; char lastPlayer = 'O';

displayBoard (board); do {

getMove (board, player); moves++; winner = isWinning (board, player); swap (player, lastPlayer); displayBoard (board); } while (moves <= 9 && !(winner)); if (!winner) return 3; else if (isEven(moves)) return 1; else if (isEven (moves - 1)) return 2;

te the values stored in the file when the user exits the program Heres my code so far:

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions

Question

2. Identify the purpose of your speech

Answered: 1 week ago