Question: Im trying to write a c++ program for tic tac toe, and it keep looping and ending the program everytime i try to run it.

Im trying to write a c++ program for tic tac toe, and it keep looping and ending the program everytime i try to run it. I want the players to be able to make each move, Here's the code that I have so far.

#include #include using namespace std; void Player1(); void Player2(); int Player1_Rows = 0; int Player1_Cols = 0; int Player2_Rows = 0; int Player2_Cols = 0; int TTT[3][3] = { 0 };

int main() { int counter = 0; bool isGameOver = true; while(isGameOver) { counter++; Player1(); TTT[Player1_Rows][Player1_Cols] = 1; if (TTT[0][0] == 1 && TTT[0][1] == 1 && TTT[0][2] == 1) isGameOver = false; else if (TTT[1][0] == 1 && TTT[1][1] == 1 && TTT[1][2] == 1) isGameOver = false; else if (TTT[2][0] == 1 && TTT[2][1] == 1 && TTT[2][2] == 1) isGameOver = false; //Across else if (TTT[0][0] == 1 && TTT[1][1] == 1 && TTT[2][2] == 1) isGameOver = false; else if (TTT[0][2] == 1 && TTT[1][1] == 1 && TTT[2][0] == 1) isGameOver = false;

if (!isGameOver) { cout << " Player 1 Won!!!!!" << endl; break; } Player2(); TTT[Player2_Rows][Player2_Cols] = 2;

if (TTT[0][0] == 2 && TTT[0][1] == 2 && TTT[0][2] == 2) isGameOver = false; else if (TTT[1][0] == 2 && TTT[1][1] == 2 && TTT[1][2] == 2) isGameOver = false; else if (TTT[2][0] == 2 && TTT[2][1] == 2 && TTT[2][2] == 2) isGameOver = false; else if (TTT[0][0] == 2 && TTT[1][1] == 2 && TTT[2][2] == 2) isGameOver = false; else if (TTT[0][2] == 2 && TTT[1][1] == 2 && TTT[2][0] == 2) isGameOver = false;

if (!isGameOver) { cout << " Player 2 Won!!!!!" << endl; break; } cout << "..............."; } cout << " Game Over in " << counter << " Moves" << endl; return 0; }

void Player1() { srand(time(NULL)); Player1_Rows = rand() % 3; Player1_Cols = rand() % 3; }

void Player2() { srand(time(NULL)); Player2_Rows = rand() % 3; Player2_Cols = rand() % 3; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!