Thread: Tic Tac Toe

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    3

    Tic Tac Toe

    Hey All,

    So I'm running thru Jumping into C++ and I have just finished my Tic Tac Toe game and just wanted to get some input on the final Product. Its not perfect but it plays a game, it can check for a Valid move thats Played between 0 and 8 but goes into an endless loop if a Char is entered.

    So anyway. any hints or Tips would be great.

    Code:
    //Tic Tac Toe Program. 2 Players Play until the computer determines if 1 player has won#include <iostream>
    
    
    
    
    using namespace std;
    
    
    /*Used to draw the Game board on the Screen*/
    void cDrawBoard(char cBoard[])
    {
        cout << endl << endl << endl;
        cout << " " << cBoard[0] << " | " << cBoard[1] << " | " << cBoard[2] << endl;
        cout << "------------" << endl;
        cout << " " << cBoard[3] << " | " << cBoard[4] << " | " << cBoard[5] << endl;
        cout << "------------" << endl;
        cout << " " << cBoard[6] << " | " << cBoard[7] << " | " << cBoard[8] << endl;
        cout << endl << endl << endl;
    }
    
    
    int nCheckifXWins(char cBoard[], int& nPlayerXWins)
    {
        nPlayerXWins = 0;
        if ((cBoard[0] == 'X' && cBoard[1] == 'X' && cBoard[2] == 'X') ||
                (cBoard[0] == 'X' && cBoard[3] == 'X' && cBoard[6] == 'X') ||
                (cBoard[0] == 'X' && cBoard[4] == 'X' && cBoard[8] == 'X') ||
                (cBoard[1] == 'X' && cBoard[4] == 'X' && cBoard[7] == 'X') ||
                (cBoard[2] == 'X' && cBoard[5] == 'X' && cBoard[8] == 'X') ||
                (cBoard[2] == 'X' && cBoard[4] == 'X' && cBoard[6] == 'X') ||
                (cBoard[3] == 'X' && cBoard[4] == 'X' && cBoard[5] == 'X') ||
                (cBoard[6] == 'X' && cBoard[7] == 'X' && cBoard[8] == 'X'))
        {
            //cout << "X Wins";
            nPlayerXWins = 1;
            return nPlayerXWins;
        }
        else
        {
            return nPlayerXWins;
        }
    }
    
    
    int nCheckifOWins(char cBoard[], int& nPlayerOWins)
    {
        nPlayerOWins = 0;
        if ((cBoard[0] == 'O' && cBoard[1] == 'O' && cBoard[2] == 'O') ||
                (cBoard[0] == 'O' && cBoard[3] == 'O' && cBoard[6] == 'O') ||
                (cBoard[0] == 'O' && cBoard[4] == 'O' && cBoard[8] == 'O') ||
                (cBoard[1] == 'O' && cBoard[4] == 'O' && cBoard[7] == 'O') ||
                (cBoard[2] == 'O' && cBoard[5] == 'O' && cBoard[8] == 'O') ||
                (cBoard[2] == 'O' && cBoard[4] == 'O' && cBoard[6] == 'O') ||
                (cBoard[3] == 'O' && cBoard[4] == 'O' && cBoard[5] == 'O') ||
                (cBoard[6] == 'O' && cBoard[7] == 'O' && cBoard[8] == 'O'))
        {
            //cout << "O Wins";
            nPlayerOWins = 1;
            return nPlayerOWins;
        }
        else
        {
            return nPlayerOWins;
        }
    
    
    }
    
    
    int main()
    {
        int nPlayerXWins = 0;
        int nPlayerOWins = 0;
        bool nPlayer = true; //true = X
        int nMove;
        char cBoard[9] = {'0','1','2','3','4','5','6','7','8'}; // Sets the Board to have 0 - 8 in each Square.
        //cDrawBoard(cBoard); //Draws the Initial Board on the screen
    
    
        for (int nGame = 0; nGame < 9; nGame++) // Sets the game to run for 9 moves
        {
            if (nPlayer == true) //when nplayer == true - X's turn
                {
                for (int nValidMove = 0; nValidMove != 1;) //Variable if a valid move or asks for a new move.
                {
                    cDrawBoard(cBoard); //Draws the Board
                    cout << "Player X - Please Enter your move: ";
                    cin >> nMove;
                    if ((cBoard[nMove] != 'X' || cBoard[nMove] != 'O') &&
                        (nMove >= 0 && nMove <= 8)) //Checks the move is Valid?
                    {
                        cBoard[nMove] = 'X'; //Places an X at the selected Square
                        nValidMove = 1;
                        nCheckifXWins(cBoard, nPlayerXWins); //Checks to see if wins and returns a value of 1 to end the game
                        nPlayer = false; // Switches the player
                        if (nPlayerXWins > 0)   //ends the game
                        {
                            cout << endl << "Player X has Won!";
                            nGame = 10;
                            break;
                        }
                    }
                    else
                    {
                        cout << "Please enter a Valid Move.";
                    }
                }
                }
                else if (nPlayer == false)
                {
                    for (int nValidMove = 0; nValidMove != 1;)
                    {
                        cDrawBoard(cBoard);
                        cout << "Player O - Please Enter your move: ";
                        cin >> nMove;
                        if ((cBoard[nMove] != 'X' || cBoard[nMove] != 'O') &&
                            (nMove >= 0 && nMove <= 8))
                        {
                            cBoard[nMove] = 'O';
                            nValidMove = 1;
                            nCheckifOWins(cBoard, nPlayerOWins);
                            nPlayer = true;
                            if (nPlayerOWins > 0)
                            {
                                cout << endl <<"Player O has won!";
                                nGame = 10;
                                break;
                            }
                        }
                        else
                        {
                            cout << "Please enter a Valid Move.";
                        }
                    }
    
    
    
    
                }
        }
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    To check whether an input error has happened, you can do:
    Code:
    if (!(cin >> nMode))
        // Error, clean the input stream, clear the fail bit and try again
    or:
    Code:
    cin >> nMode;
    if (cin.fail())
        // Same as the above
    EDIT: Remember to clear the state before trying to receive any new input, by "cin.clear()"
    Last edited by GReaper; 05-12-2012 at 08:57 PM.
    Devoted my life to programming...

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If I were to declare an array like so:
    Code:
    static const int[][3] = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{1,5,9},{2,4,6}};
    Can you see how a loop with a much shorter if-statement can be used to check for all possible wins?
    It's called a lookup table, and they really rock for this kind of thing
    You should also be able to combine the methods that check for O and X wins, by passing in an 'O' or an 'X' as a parameter.

    You don't need to both return a value and use an out-parameter. Just use the return value and make sure you use the result where you call the function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed