Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

More Books

Students also viewed these Databases questions

Question

What are major aspects of intrusion prevention?

Answered: 1 week ago

Question

Define the future value of an annuity due.

Answered: 1 week ago