So I wrote a small little tic tac toe game, it works without fail, now I'm just looking at ways to shorten this if/else conglomerate I've created, but so far haven't thought of a way to do so, I thought about using a switch but thought it would be about the same either way. Can any of the pro's give me some pointers ?

ps. I've been here before and know most of the rules, I'm not looking for code handouts, and this isn't for a college assignment(not even in college to be honest). :P

pps. what happened to the code box ie where you had to scroll to view code? now it only does syntax highlighting ?

    void tick::checkWin()
    {

                //Beginning of Horizontal Check
                if(tick::board[0] == X && tick::board[1] == X && tick::board[2] == X)
                {
                    cout <<"Player One Won, starting new game" << endl;
                    tick::resetBoard();
                    tick::resetLocks();
                    tick::currentPlayer = 1;
                }
                else if(tick::board[0] == O && tick::board[1] == O && tick::board[2] == O)
                {
                    cout <<"Player Two Won, starting new game" << endl;
                    tick::resetBoard();
                    tick::resetLocks();
                    tick::currentPlayer = 1;
                }
                else if(tick::board[3] == X && tick::board[4] == X && tick::board[5] == X)
                {
                    cout <<"Player One Won, starting new game" << endl;
                    tick::resetBoard();
                    tick::resetLocks();
                    tick::currentPlayer = 1;
                }
                else if(tick::board[3] == O && tick::board[4] == O && tick::board[5] == O)
                {
                    cout <<"Player Two Won, starting new game" << endl;
                    tick::resetBoard();
                    tick::resetLocks();
                    tick::currentPlayer = 1;
                }
                else if(tick::board[6] == X && tick::board[7] == X && tick::board[8] == X)
                {
                    cout <<"Player One Won, starting new game" << endl;
                    tick::resetBoard();
                    tick::resetLocks();
                    tick::currentPlayer = 1;
                }
                else if(tick::board[6] == O && tick::board[7] == O && tick::board[8] == O)
                {
                    cout <<"Player Two Won, starting new game" << endl;
                    tick::resetBoard();
                    tick::resetLocks();
                    tick::currentPlayer = 1;
                }
                //end of horiztonal check
                //start of diagonal check
                else if(tick::board[0] == X && tick::board[4] == X && tick::board[8] == X)
                {
                    cout <<"Player One Won, starting new game" << endl;
                    tick::resetBoard();
                    tick::resetLocks();
                    tick::currentPlayer = 1;
                }
                else if(tick::board[0] == O && tick::board[4] == O && tick::board[8] == O)
                {
                    cout <<"Player Two Won, starting new game" << endl;
                    tick::resetBoard();
                    tick::resetLocks();
                    tick::currentPlayer = 1;
                }
                else if(tick::board[2] == X && tick::board[4] == X && tick::board[6] == X)
                {
                    cout <<"Player One Won, starting new game" << endl;
                    tick::resetBoard();
                    tick::resetLocks();
                    tick::currentPlayer = 1;
                }
                else if(tick::board[2] == O && tick::board[4] == O && tick::board[6] == O)
                {
                    cout <<"Player Two Won, starting new game" << endl;
                    tick::resetBoard();
                    tick::resetLocks();
                    tick::currentPlayer = 1;
                }
                //end of diagonal check
                //beginning of vertical check
                else if(tick::board[0] == X && tick::board[3] == X && tick::board[6] == X)
                {
                    cout <<"Player One Won, starting new game" << endl;
                    tick::resetBoard();
                    tick::resetLocks();
                    tick::currentPlayer = 1;
                }
                else if(tick::board[0] == O && tick::board[3] == O && tick::board[6] == O)
                {
                    cout <<"Player Two Won, starting new game" << endl;
                    tick::resetBoard();
                    tick::resetLocks();
                    tick::currentPlayer = 1;
                }
                else if(tick::board[1] == X && tick::board[4] == X && tick::board[7] == X)
                {
                    cout <<"Player One Won, starting new game" << endl;
                    tick::resetBoard();
                    tick::resetLocks();
                    tick::currentPlayer = 1;
                }
                else if(tick::board[1] == O && tick::board[4] == O && tick::board[7] == O)
                {
                    cout <<"Player Two Won, starting new game" << endl;
                    tick::resetBoard();
                    tick::resetLocks();
                    tick::currentPlayer = 1;
                }
                else if(tick::board[2] == X && tick::board[5] == X && tick::board[8] == X)
                {
                    cout <<"Player One Won, starting new game" << endl;
                    tick::resetBoard();
                    tick::resetLocks();
                    tick::currentPlayer = 1;
                }
                else if(tick::board[2] == O && tick::board[5] == O && tick::board[8] == O)
                {
                    cout <<"Player Two Won, starting new game" << endl;
                    tick::resetBoard();
                    tick::resetLocks();
                    tick::currentPlayer = 1;
                }
     }

Recommended Answers

All 2 Replies

if player 1 wins you are calling same method,and similarly for player two.So you can do this in 2 if-else statement.Add all conditions in one with ||(OR).Your condition check will check all conditions for palyer one win in first if and similarly for second.

oh man why didn't I think of || XD thanks IIM!, I should have remembered that guess I was thinking too hard in the wrong direction.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.