-2

I'm a beginner in C++ and I am trying to make a tic tac toe game. I am almost complete, but the win checker says one has won with two consecutive symbols instead of three. Tips to make the code smaller are also appreciated. Keep in mind I am a beginner who has learned only a few months.

the checker:

//wincheck(messy(put it in a true if statement to make it smaller))

if (true) {

    if ((_board[0][0] == 'X') && (_board[1][1] == 'X') && (_board[2][2] = 
'X')) {
        printBoard();
        cout << name1 << " has won!" << endl;
        return true;
    }
    else if ((_board[2][0] == 'X') && (_board[1][1] == 'X') && (_board[0]
[2] = 'X')) {
        printBoard();
        cout << name1 << " has won!" << endl;
        return true;
    }
    else
        //left to right
        if ((_board[0][0] == 'X') && (_board[0][1] == 'X') && (_board[0]
[2] = 'X')) {
            printBoard();
            cout << name1 << " has won!" << endl;
            return true;
        }
        else if ((_board[1][0] == 'X') && (_board[1][1] == 'X') && 
(_board[1][2] = 'X')) {
            printBoard();
            cout << name1 << " has won!" << endl;
            return true;
        }
        else if ((_board[2][0] == 'X') && (_board[2][1] == 'X') && 
(_board[2][2] = 'X')) {
            printBoard();
            cout << name1 << " has won!" << endl;
            return true;
        }
        else
            // up to down
            if ((_board[0][0] == 'X') && (_board[1][0] == 'X') && 
(_board[2][0] = 'X')) {
                printBoard();
                cout << name1 << " has won!" << endl;
                return true;
            }
            else if ((_board[0][1] == 'X') && (_board[1][1] == 'X') && 
(_board[2][1] = 'X')) {
                printBoard();
                cout << name1 << " has won!" << endl;
                return true;
            }
            else if ((_board[0][2] == 'X') && (_board[1][2] == 'X') && 
(_board[2][2] = 'X')) {
                printBoard();
                cout << name1 << " has won!" << endl;
                return true;
            }


            // check if O won


            //diagonals
            if ((_board[0][0] == 'O') && (_board[1][1] == 'O') && 
(_board[2][2] = 'O')) {
                printBoard();
                cout << name1 << " has won!" << endl;
                return true;
            }
            else if ((_board[2][0] == 'O') && (_board[1][1] == 'O') && 
(_board[0][2] = 'O')) {
                printBoard();
                cout << name1 << " has won!" << endl;
                return true;
            }
            else
                //left to right
                if ((_board[0][0] == 'O') && (_board[0][1] == 'O') && 
(_board[0][2] = 'O')) {
                    printBoard();
                    cout << name1 << " has won!" << endl;
                    return true;
                }
                else if ((_board[1][0] == 'O') && (_board[1][1] == 'O') 
&& (_board[1][2] = 'O')) {
                    printBoard();
                    cout << name1 << " has won!" << endl;
                    return true;
                }
                else if ((_board[2][0] == 'O') && (_board[2][1] == 'O') 
&& (_board[2][2] = 'O')) {
                    printBoard();
                    cout << name1 << " has won!" << endl;
                    return true;
                }
                else
                    // up to down
                    if ((_board[0][0] == 'O') && (_board[1][0] == 'O') && 
(_board[2][0] = 'O')) {
                        printBoard();
                        cout << name1 << " has won!" << endl;
                        return true;
                    }
                    else if ((_board[0][1] == 'O') && (_board[1][1] == 
'O') && (_board[2][1] = 'O')) {
                        printBoard();
                        cout << name1 << " has won!" << endl;
                        return true;
                    }
                    else if ((_board[0][2] == 'O') && (_board[1][2] == 
'O') && (_board[2][2] = 'O')) {
                        printBoard();
                        cout << name1 << " has won!" << endl;
                        return true;
                    }
}



}
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
RioT1133
  • 11
  • 3
  • Welcome to Stack Overflow! Please review our [SO Question Checklist](https://stackoverflow.com/help/how-to-ask) to help you to ask a good question, and thus get a good answer. "... doesn't work properly." is not a good question. Please clarify what is the problem, what you expect and what you get instead. And boil down your code to be minimal, complete, and verifiable (means strip off all code not related to your problem). – Heri Nov 12 '17 at 18:08
  • The next sentence explains what's wrong with it – RioT1133 Nov 12 '17 at 18:11
  • Your code is too long and messy to find the error in. I recommend rewriting the checking algorithm from scratch with a better technique - I bet the error will vanish if you find a more effective way of checking rows/columns. – Josh Karpel Nov 12 '17 at 18:14
  • Debugger. Use a debugger. A debugger will allow you to single step through your code, *watching* values of variables. Using a debugger is often faster than posting correctly to StackOverflow and *waiting* for somebody to inspect your code or debug your program for you. Please edit your post with the text results of your debugging session. – Thomas Matthews Nov 12 '17 at 18:17
  • @RioT1133 Don't change the question title, accept the answer that helped. – Blastfurnace Nov 12 '17 at 18:27
  • When you feel a need to reach for copy-and-paste, that's usually because there's a much better way of doing it and you should stop and think instead. Have you learned about functions and loops yet? – molbdnilo Nov 12 '17 at 19:26
  • yes i have, i don't know how to use them in this situation @molbdnilo – RioT1133 Nov 12 '17 at 20:24

1 Answers1

1
if ((_board[0][0] == 'X') && (_board[1][1] == 'X') && (_board[2][2] = 'X'))

Your third check is actually an assignment, so this condition is true if 2 symbols are placed consecutively (because a = b is always true), same for the following checks.

(An improvement to your code could be a bitboard algorithm using bitshifting https://stackoverflow.com/a/7053051/6945068)

mholle
  • 577
  • 1
  • 10
  • 19
  • My stupid mistake. It worked now. The bitboard algorithm is a bit too advanced for me, I'll stick with the if statements for now – RioT1133 Nov 12 '17 at 18:23