0

Couple questions for my Tic Tac Toe game C++

Hey guys I have a couple questions I'm having trouble with.. First is I can't get out of my For loop for my game.. I want the game to end when the board is full and I will cout "tie" or something more exciting but even when I have it set to (j=0; j<9ji++) it is not exiting. Also with my array I can't figure out how to get bars across my rows (for looks purposes) I can do the columns but can not figure out the rows. I would like to keep it as much to the same format as possible. int i, j; int nums[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1 }; cout << "Welcome to Tic Tac Toe!" << endl; display(nums); for (j = 0; j < 9; j++) { //enter x do { cout << "Please enter an X in an unplayed position." << endl; cin >> i; //1-9 if (nums[i - 1] != -1) cout << "Invalid Space! Space already taken." << endl; else break; } while (true); nums[i - 1] = 1; display(nums); //display if (gameoverx(nums)) //check to see if game is over break; cout << "The computer's move is..." << endl; system("pause"); ai(nums); //enter O display(nums); if (gameovero(nums)) //check to see if game is over break; } cout << "Thank You for playing!!!" << endl; system("pause"); } void display(int n[]) { int count = 0; int r, c; for (r = 0; r < 3; r++) { for (c = 0; c < 3; c++) { if (n[count] == -1) cout << setw(2) << count + 1 << " |"; else if (n[count] == 1) cout << setw(2) << 'X'; else if (n[count] == 0) cout << setw(2) << 'O'; count++; } cout << endl; } }

30th Oct 2019, 2:43 AM
Drew Shelton
Drew Shelton - avatar
4 Antworten
+ 1
Hey I'm not that good at C++ but I could probably figure it out if you post the code. To post the code press the circle with the + and go to insert code and at the drop down arrow in the corner go to my codes and choose the tic tac toe one Should show up like this https://code.sololearn.com/c39ejXQWGE0Z/?ref=app
30th Oct 2019, 3:36 AM
Odyel
Odyel - avatar
0
Thank You! I hope this works for you! https://code.sololearn.com/c6k2qJzxNtS1/#cpp
30th Oct 2019, 3:54 AM
Drew Shelton
Drew Shelton - avatar
0
It works fine for me, Also at the if(j = 5) on line 52 change that to == that should stop the "Its a tie!" from always appearing https://www.sololearn.com/post/166473/?ref=app (Sorry if it's hard to see)
30th Oct 2019, 4:14 AM
Odyel
Odyel - avatar
0
If you're testing this out on SoloLearn and dunno much about how much inputs are taken in on here look at this https://www.sololearn.com/post/161825/?ref=app The way I tested your code is that I just slammed random numbers from 1-9 multiple times like 2 5 3 5 8 1 5 7 9 5 6 etc.
30th Oct 2019, 4:18 AM
Odyel
Odyel - avatar