Search code examples
c++tic-tac-toe

Checking for Win in Tic-Tac-Toe


Hello I'm 17 and trying to learn how to code. I am having trouble debugging this section of code and would appreciate some help.

bool checkifwin(char grid[3][3], char player)
{
    if (checkverticle(char grid[3][3], char player) 
        || checkhorizontal(char grid[3][3], char player)
        || checkdiagonal( grid[3][3], player) 
       return true;
    else
       return false;
};

It says expected primary-expression before char.


Solution

  • checkverticle() is a call to a function not a declaration so you don't need the "char"s.

    bool checkifwin(char grid[3][3], char player)
    {
        if ( checkverticle(grid, player) || 
             checkhorizontal(grid, player) || 
             checkdiagonal( grid, player) return true;
        else return false;
    };
    

    Just some coding advice. In my opinion:

    bool func()
    {
        if (expr)
            return true;
        else
            return false;
    }
    

    Is not great style. I'd suggest refactoring it to:

    bool func()
    {
        bool result = false; // This just makes it clear the expected default.
        // You could even go as far as
        // bool result = expr  or event return expr; - although I suggest sticking with a
        // variable in case you need to debug.
        result = expr; // e.g. result = checkVert() || checkHoriz() || checkDiag();
        return result;
    }