Question

I made a Tic Tac Toe game in C++. I did not know how to output a proper board, so I made a grid with an array labelled 1-9 to let the user punch in the number to tell the computer where to put his shot. The second player is the computer. I'm very happy since it's my first working program, but there's a problem with the code, it sometimes, not always, outputs an 'Invalid' before exiting, I have used the word to tell the user when he is entering an invalid number. Could someone please help me understand why this is happening and the logic in my code, because after some time, even I'm confused about what I wrote. Very grateful, thanks.

/*
 * Tic Tac Toe Game
 *
 *
*/

#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;
}
Was it helpful?

Solution

Your error is due to this line:

if (entered >= 1 || entered <= 9)

You need to use && instead of ||.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top