I made a Tic Tac Toe game in C ++. I did not know how to display the correct board, so I created a grid with an array labeled 1-9 to allow the user to break the number to tell the computer where to put it. The second player is a computer. I am very happy, since this is my first working program, but there is a problem with the code, sometimes it does not always display "Invalid" before exiting, I used the word to tell the user when he enters the wrong number, Maybe someone please help me understand why this happens and the logic in my code, because after some time even what I wrote confuses me. Thank you very much.
#include <iostream>
#include <string>
#include <time.h>
#include <cstdlib>
using namespace std;
int main() {
int tic[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int entered = 0;
int myrandom;
int cswitcher, uswitcher;
int gameover = 0;
srand(time(0));
while(gameover != 1) {
cswitcher = 0;
uswitcher = 0;
cout << tic[0][0] << tic[0][1] << tic[0][2] << endl;
cout << tic[1][0] << tic[1][1] << tic[1][2] << endl;
cout << tic[2][0] << tic[2][1] << tic[2][2] << endl;
while (uswitcher != 1) {
cout << "Pick your turn" << endl;
cin >> entered;
if (entered >= 1 || entered <= 9) {
switch (entered) {
case 1: if (tic[0][0] != 0) {tic[0][0] = 0; uswitcher = 1;} else {cout << "Invalid\n"; uswitcher = 0; continue;} break;
case 2: if (tic[0][1] != 0) {tic[0][1] = 0; uswitcher = 1;} else {cout << "Invalid\n"; uswitcher = 0; continue;} break;
case 3: if (tic[0][2] != 0) {tic[0][2] = 0; uswitcher = 1;} else {cout << "Invalid\n"; uswitcher = 0; continue;} break;
case 4: if (tic[1][0] != 0) {tic[1][0] = 0; uswitcher = 1;} else {cout << "Invalid\n"; uswitcher = 0; continue;} break;
case 5: if (tic[1][1] != 0) {tic[1][1] = 0; uswitcher = 1;} else {cout << "Invalid\n"; uswitcher = 0; continue;} break;
case 6: if (tic[1][2] != 0) {tic[1][2] = 0; uswitcher = 1;} else {cout << "Invalid\n"; uswitcher = 0; continue;} break;
case 7: if (tic[2][0] != 0) {tic[2][0] = 0; uswitcher = 1;} else {cout << "Invalid\n"; uswitcher = 0; continue;} break;
case 8: if (tic[2][1] != 0) {tic[2][1] = 0; uswitcher = 1;} else {cout << "Invalid\n"; uswitcher = 0; continue;} break;
case 9: if (tic[2][2] != 0) {tic[2][2] = 0; uswitcher = 1;} else {cout << "Invalid\n"; uswitcher = 0; continue;} break;
}
} else {continue;}
}
while (cswitcher != 1) {
myrandom = rand()%9+1;
switch (myrandom) {
case 1: if (tic[0][0] != 0) {tic[0][0] = 0; cswitcher = 1;} else {cswitcher = 0; continue;} break;
case 2: if (tic[0][1] != 0) {tic[0][1] = 0; cswitcher = 1;} else {cswitcher = 0; continue;} break;
case 3: if (tic[0][2] != 0) {tic[0][2] = 0; cswitcher = 1;} else {cswitcher = 0; continue;} break;
case 4: if (tic[1][0] != 0) {tic[1][0] = 0; cswitcher = 1;} else {cswitcher = 0; continue;} break;
case 5: if (tic[1][1] != 0) {tic[1][1] = 0; cswitcher = 1;} else {cswitcher = 0; continue;} break;
case 6: if (tic[1][2] != 0) {tic[1][2] = 0; cswitcher = 1;} else {cswitcher = 0; continue;} break;
case 7: if (tic[2][0] != 0) {tic[2][0] = 0; cswitcher = 1;} else {cswitcher = 0; continue;} break;
case 8: if (tic[2][1] != 0) {tic[2][1] = 0; cswitcher = 1;} else {cswitcher = 0; continue;} break;
case 9: if (tic[2][2] != 0) {tic[2][2] = 0; cswitcher = 1;} else {cswitcher = 0; continue;} break;
}
}
if (tic[0][0] + tic[0][1] + tic[0][2] == 0 || tic[1][0] + tic[1][1] + tic[1][2] == 0 ||
tic[2][0] + tic[2][1] + tic[2][2] == 0 || tic[0][0] + tic[1][0] + tic[2][0] == 0 ||
tic[0][1] + tic[1][1] + tic[2][1] == 0 || tic[0][2] + tic[1][2] + tic[2][2] == 0 ||
tic[0][0] + tic[1][1] + tic[2][2] == 0 || tic[2][0] + tic[1][1] + tic[0][2] == 0) {gameover = 1; break;}
}
return 0;
}
source
share