简体   繁体   中英

Tic Tac Toe game won't work

So I learnt programming about a week ago. I decided to make a tic tac toe game as a hobby project, I got a rudimentary game input method up and running but one aspect doesn't work. I made a very simplified piece of code to verify if somebody has won once 5 moves have been played. Once somebody wins, it's supposed to display "Game Over!" but it doesn't for some reason. Can anyone help? _ __ _ __ _ __ _ ___ Note: The method to verify if somebody has won yet isn't complete in the code. It is only for rows because when I tested it, it didn't work.

#include <iostream>
#include <cstdlib>

using namespace std;
int main()
{
    char grid[3][3];
    int p;
    cout << "Enter column number. \n";
    cin >> p;
    int o;
    cout << "Enter row number. \n";
    cin >> o;
    for (int y = 0; y < 3; y++)
    {
        for (int x = 0; x < 3; x++)
        {
            grid[x][y] = '-';
            grid[o][p] = 'X';
            cout << grid[x][y] << " ";
        }
        cout << endl;
    }
    int q;
    cout << "Enter column number. \n";
    cin >> q;
    int r;
    cout << "Enter row number. \n";
    cin >> r;
    for (int y = 0; y < 3; y++)
    {
        for (int x = 0; x < 3; x++)
        {
            grid[x][y] = '-';
            grid[o][p] = 'X';
            grid[r][q] = 'O';
            cout << grid[x][y] << " ";
        }
        cout << endl;
    }
    int s;
    cout << "Enter column number. \n";
    cin >> s;
    int t;
    cout << "Enter row number. \n";
    cin >> t;
    for (int y = 0; y < 3; y++)
    {
        for (int x = 0; x < 3; x++)
        {
            grid[x][y] = '-';
            grid[o][p] = 'X';
            grid[r][q] = 'O';
            grid[t][s] = 'X';
            cout << grid[x][y] << " ";
        }
        cout << endl;
    }
    int v;
    cout << "Enter column number. \n";
    cin >> v;
    int b;
    cout << "Enter row number. \n";
    cin >> b;
    for (int y = 0; y < 3; y++)
    {
        for (int x = 0; x < 3; x++)
        {
            grid[x][y] = '-';
            grid[o][p] = 'X';
            grid[r][q] = 'O';
            grid[t][s] = 'X';
            grid[b][v] = 'O';
            cout << grid[x][y] << " ";
        }
        cout << endl;
    }
    int f;
    cout << "Enter column number. \n";
    cin >> f;
    int g;
    cout << "Enter row number. \n";
    cin >> g;
    for (int y = 0; y < 3; y++)
    {
        for (int x = 0; x < 3; x++)
        {
            grid[x][y] = '-';
            grid[o][p] = 'X';
            grid[r][q] = 'O';
            grid[t][s] = 'X';
            grid[b][v] = 'O';
            grid[g][f] = 'X';
            cout << grid[x][y] << " ";
        }
        cout << endl;
    }
    if (grid[0][0] == grid[0][1] == grid[0][2] || grid[1][0] == grid[1][1] == grid[1][2] || grid[2][0] == grid[2][1] == grid[2][2])
    {
        cout << "Game Over! \n\n";
    }
    int i;
    cout << "Enter column number. \n";
    cin >> i;
    int u;
    cout << "Enter row number. \n";
    cin >> u;
    for (int y = 0; y < 3; y++)
    {
        for (int x = 0; x < 3; x++)
        {
            grid[x][y] = '-';
            grid[o][p] = 'X';
            grid[r][q] = 'O';
            grid[t][s] = 'X';
            grid[b][v] = 'O';
            grid[g][f] = 'X';
            grid[u][i] = 'O';
            cout << grid[x][y] << " ";
        }
        cout << endl;
    }
    if (grid[0][0] == grid[0][1] == grid[0][2] || grid[1][0] == grid[1][1] == grid[1][2] || grid[2][0] == grid[2][1] == grid[2][2])
    {
        cout << "Game Over! \n\n";
    }
    int a1;
    cout << "Enter column number. \n";
    cin >> a1;
    int e1;
    cout << "Enter row number. \n";
    cin >> e1;
    for (int y = 0; y < 3; y++)
    {
        for (int x = 0; x < 3; x++)
        {
            grid[x][y] = '-';
            grid[o][p] = 'X';
            grid[r][q] = 'O';
            grid[t][s] = 'X';
            grid[b][v] = 'O';
            grid[g][f] = 'X';
            grid[u][i] = 'O';
            grid[e1][a1] = 'X';
            cout << grid[x][y] << " ";
        }
        cout << endl;
    }
    if (grid[0][0] == grid[0][1] == grid[0][2] || grid[1][0] == grid[1][1] == grid[1][2] || grid[2][0] == grid[2][1] == grid[2][2])
    {
        cout << "Game Over! \n\n";
    }
    int a8;
    cout << "Enter column number. \n";
    cin >> a8;
    int b8;
    cout << "Enter row number. \n";
    cin >> b8;
    for (int y = 0; y < 3; y++)
    {
        for (int x = 0; x < 3; x++)
        {
            grid[x][y] = '-';
            grid[o][p] = 'X';
            grid[r][q] = 'O';
            grid[t][s] = 'X';
            grid[b][v] = 'O';
            grid[g][f] = 'X';
            grid[u][i] = 'O';
            grid[e1][a1] = 'X';
            grid[b8][a8] = 'O';
            cout << grid[x][y] << " ";
        }
        cout << endl;
    }
    if (grid[0][0] == grid[0][1] == grid[0][2] || grid[1][0] == grid[1][1] == grid[1][2] || grid[2][0] == grid[2][1] == grid[2][2])
    {
        cout << "Game Over! \n\n";
    }
    int a9;
    cout << "Enter column number. \n";
    cin >> a9;
    int b9;
    cout << "Enter row number. \n";
    cin >> b9;
    for (int y = 0; y < 3; y++)
    {
        for (int x = 0; x < 3; x++)
        {
            grid[x][y] = '-';
            grid[o][p] = 'X';
            grid[r][q] = 'O';
            grid[t][s] = 'X';
            grid[b][v] = 'O';
            grid[g][f] = 'X';
            grid[u][i] = 'O';
            grid[e1][a1] = 'X';
            grid[b8][a8] = 'O';
            grid[b9][a9] = 'X';
            cout << grid[x][y] << " ";
        }
        cout << endl;
    }
    if (grid[0][0] == grid[0][1] == grid[0][2] || grid[1][0] == grid[1][1] == grid[1][2] || grid[2][0] == grid[2][1] == grid[2][2])
    {
        cout << "Game Over! \n\n";
    }
}

In C++, the == sign doesn't work the way it does in algebra class. The expression a == b == c has a very different meaning in C++. In C++, it only works on pairs of items, and returns a true or false value. (See more below.)

To see if 3 things all equal each other, you need to say (a == b && b == c) . Your grid check would need to look like this:

if ( (grid[0][0] == grid[0][1] && grid[0][1] == grid[0][2]) || 
     (grid[1][0] == grid[1][1] && grid[1][1] == grid[1][2]) ||
     (grid[2][0] == grid[2][1] && grid[2][1] == grid[2][2]) )

You'll also notice I put extra parentheses in there. They are not strictly necessary, but some compilers warn when you mix && and || in the same expression. The parentheses make it clear which items you intend to group together and will quell those warnings.


So what was the C++ compiler doing with your code? You had written grid[0][0] == grid[0][1] == grid[0][2] . The compiler interprets that as (grid[0][0] == grid[0][1]) == grid[0][2] according to its operator precedence rules.

The compiler will first compare grid[0][0] to grid[0][1] . This will produce a true or false boolean value (C++ bool ) depending on whether they were equal. So far, so good.

After the comparison, it will compare that bool result against grid[0][2] . Since grid[0][2] is a char , it promotes the bool to a numeric value. true becomes 1 and false becomes 0. None of your grid values have the value 0 or 1—they are all either '-' , 'X' or 'O' —so the second comparison always fails.

The same situation repeats for all three rows.

For my understanding you cannot write:

grid[0][0] == grid[0][1] == grid[0][2]

You should have:

grid[0][0] == grid[0][1] && grid[0][1]== grid[0][2]

In C++ these are binary operators, and thus == can only be applied to two variables, not chained to three. Now it compares first result (true/false) to the third number.

My sources:

C++ Tic Tac Toe Game

Your program would be a lot smaller if you used one array and number the boxes 1 through 9:

+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
| 7 | 8 | 9 |
+---+---+---+

The array would like this:

const unsigned int BOARD_CAPACITY = 9;
unsigned int board[BOARD_CAPACITY + 1];

The board array is declared with one extra slot so you can safely use indices 1 through 9.

A single row check:

bool row_wins = false;
if ((board[1] == board[2]) && (board[2] == board[3]))
{
  row_wins = true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM