Thread: Tic Tac Toe! (Noob Coder)_Problem 3 Chapter 7 Jumping into C++

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    20

    Tic Tac Toe! (Noob Coder)_Problem 3 Chapter 7 Jumping into C++

    Hi guys,

    I'm a self learning student and have been working on the tic tac toe problem of C++ Book. Before I try to make it fancy, I was wondering if anyone could help me understand what I'm doing wrong.

    Basically the game seems to run well as long as the users puts valid input. If the users don't put valid input, it gets stuck on the BackGround_Work loop invalid output. I think it has something to do with the input buffer but don't know enough to fix it.

    Basically I use cin.clear and cin.ignore to clear the input buffer but I don't think that's working in this case. I also would really be interested in understanding cin.clear and cin.ignore more. I kind of just use it with a sense that it helps clear my input but don't know too much detail as to how and why. If anyone can enlighten me on this and other input related commands and concepts I would be very happy to learn!

    Listed below is my code:
    Code:
    // Write a two-player tic-tac-toe game, allowing two humans to play against each other; use enums when possible to represent the values of the board.
    
    
    #include <iostream>
    
    
    using namespace std;
    
    
    int x; // My clasic and favorite input variable
    
    
    // Declaring Prototype Functions
    void Show_Board ();
    void BackGround_Work_P1 ();
    void BackGround_Work_P2 ();
    void UserInput ();
    void ErrorMove ();
    int GameCheck ();
    
    
    //Board Visual_Spaces as Global Variables
    char Space_1 = ('1');
    char Space_2 = ('2');
    char Space_3 = ('3');
    char Space_4 = ('4');
    char Space_5 = ('5');
    char Space_6 = ('6');
    char Space_7 = ('7');
    char Space_8 = ('8');
    char Space_9 = ('9');
    
    
    // Board Space_Statuses Declared Globaly
    enum TicTacToe {Empty, X_Filled, O_Filled};
    
    
    int iSpace_1 = Empty;
    int iSpace_2 = Empty;
    int iSpace_3 = Empty;
    int iSpace_4 = Empty;
    int iSpace_5 = Empty;
    int iSpace_6 = Empty;
    int iSpace_7 = Empty;
    int iSpace_8 = Empty;
    int iSpace_9 = Empty;
    
    
    int main ()
    {
        
        // Intro and Intstructions...
        cout << "Awesome Student Presents C++ Tic Tac Toe!!\n";
        cout << "Intructions:\n";
        cout << "Get 3 in a row to win on a 3x3 grid!\n";
        cout << "Choose from squares 1 to 9 by inputing a number!\n\n";
        
        cout << "The grid can be seen as followed..\n\n";
        Show_Board();
        
        cout << "Player 1 --> X\n";
        cout << "Player 2 --> O\n\n";
        
        // Game
        bool game_done = false;
        while (!game_done)
        {
            cout << "Player 1's Turn!\n";
            UserInput();
            BackGround_Work_P1();
            GameCheck();
            Show_Board();
            if (GameCheck() != 0)
                break;
            
            cout << "Player 2's Turn!\n";
            UserInput();
            BackGround_Work_P2();
            GameCheck();
            Show_Board();
            if (GameCheck() != 0)
                game_done = true;
                
        }
        
        // Ending
        switch (GameCheck()) {
            case 1:
                cout << "Player 1 Wins!\n";
                break;
            case 2:
                cout << "Player 2 Wins!\n";
                break;
            case 3:
                cout << "It's a Draw!\n";
                break;
            default:
                break;
        }
    }
    
    
    
    
    void Show_Board () // Shows the Tic Tac Toe Board
    {
        cout << "\t"<<Space_1<<"\t|\t"<<Space_2<<"\t|\t"<<Space_3<<"\n";
        cout << "--------+-------+-------\n";
        cout << "\t"<<Space_4<<"\t|\t"<<Space_5<<"\t|\t"<<Space_6<<"\n";
        cout << "--------+-------+-------\n";
        cout << "\t"<<Space_7<<"\t|\t"<<Space_8<<"\t|\t"<<Space_9<<"\n\n";
    }
    
    
    void BackGround_Work_P1 ()
    {
        // Visual Change & Background Work
        bool done = false;
        while (!done) {
            switch (x) {
                case 1:
                {
                    if (iSpace_1 == Empty)
                        Space_1 = ('X'), iSpace_1 = X_Filled, done = true;
                    else if (iSpace_1 != Empty)
                        ErrorMove();
                    break;
                }
                case 2:
                {
                    if (iSpace_2 == Empty)
                        Space_2 = ('X'), iSpace_2 = X_Filled, done = true;
                    else if (iSpace_2 != Empty)
                        ErrorMove();
                    break;
                }
                case 3:
                {
                    if (iSpace_3 == Empty)
                        Space_3 = ('X'), iSpace_3 = X_Filled, done = true;
                    else if (iSpace_3 != Empty)
                        ErrorMove();
                    break;
                }
                case 4:
                {
                    if (iSpace_4 == Empty)
                        Space_4 = ('X'), iSpace_4 = X_Filled, done = true;
                    else if (iSpace_4 != Empty)
                        ErrorMove();
                    break;
                }
                case 5:
                {
                    if (iSpace_5 == Empty)
                        Space_5 = ('X'), iSpace_5 = X_Filled, done = true;
                    else if (iSpace_5 != Empty)
                        ErrorMove();
                    break;
                }
                case 6:
                {
                    if (iSpace_6 == Empty)
                        Space_6 = ('X'), iSpace_6 = X_Filled, done = true;
                    else if (iSpace_6 != Empty)
                        ErrorMove();
                    break;
                }
                case 7:
                {
                    if (iSpace_7 == Empty)
                        Space_7 = ('X'), iSpace_7 = X_Filled, done = true;
                    else if (iSpace_7 != Empty)
                        ErrorMove();
                    break;
                }
                case 8:
                {
                    if (iSpace_8 == Empty)
                        Space_8 = ('X'), iSpace_8 = X_Filled, done = true;
                    else if (iSpace_8 != Empty)
                        ErrorMove();
                    break;
                }
                case 9:
                {
                    if (iSpace_9 == Empty)
                        Space_9 = ('X'), iSpace_9 = X_Filled, done = true;
                    else if (iSpace_9 != Empty)
                        ErrorMove();
                    break;
                }
                default:
                    cin.clear(), cin.ignore(100, '\n'), cout << "Invalid Move!\n";
                    break;
            }
        }
    }
    
    
    void BackGround_Work_P2 ()
    {
        // Visual Change & Background Work
        bool done = false;
        while (!done) {
            switch (x) {
                case 1:
                {
                    if (iSpace_1 == Empty)
                        Space_1 = ('O'), iSpace_1 = O_Filled, done = true;
                    else if (iSpace_1 != Empty)
                        ErrorMove();
                    break;
                }
                case 2:
                {
                    if (iSpace_2 == Empty)
                        Space_2 = ('O'), iSpace_2 = O_Filled, done = true;
                    else if (iSpace_2 != Empty)
                        ErrorMove();
                    break;
                }
                case 3:
                {
                    if (iSpace_3 == Empty)
                        Space_3 = ('O'), iSpace_3 = O_Filled, done = true;
                    else if (iSpace_3 != Empty)
                        ErrorMove();
                    break;
                }
                case 4:
                {
                    if (iSpace_4 == Empty)
                        Space_4 = ('O'), iSpace_4 = O_Filled, done = true;
                    else if (iSpace_4 != Empty)
                        ErrorMove();
                    break;
                }
                case 5:
                {
                    if (iSpace_5 == Empty)
                        Space_5 = ('O'), iSpace_5 = O_Filled, done = true;
                    else if (iSpace_5 != Empty)
                        ErrorMove();
                    break;
                }
                case 6:
                {
                    if (iSpace_6 == Empty)
                        Space_6 = ('O'), iSpace_6 = O_Filled, done = true;
                    else if (iSpace_6 != Empty)
                        ErrorMove();
                    break;
                }
                case 7:
                {
                    if (iSpace_7 == Empty)
                        Space_7 = ('O'), iSpace_7 = O_Filled, done = true;
                    else if (iSpace_7 != Empty)
                        ErrorMove();
                    break;
                }
                case 8:
                {
                    if (iSpace_8 == Empty)
                        Space_8 = ('O'), iSpace_8 = O_Filled, done = true;
                    else if (iSpace_8 != Empty)
                        ErrorMove();
                    break;
                }
                case 9:
                {
                    if (iSpace_9 == Empty)
                        Space_9 = ('O'), iSpace_9 = O_Filled, done = true;
                    else if (iSpace_9 != Empty)
                        ErrorMove();
                    break;
                }
                default:
                    cin.clear(), cin.ignore(100, '\n'), cout << "Invalid Move!\n";
                    break;
            }
        }
    
    
    }
    
    
    
    
    void UserInput ()  // So that letter inputs don't mess up the loops!
    {
        while (!(cin >> x))
        {
            cout << "A number please..\n";
            cin.clear();
            cin.ignore(100, '\n');
        }
    }
    
    
    void ErrorMove () //Tells the user error move
    {
        cin.clear();
        cin.ignore(100, '\n');
        cout << "Sorry but that space is filled by something.\n";
        
    }
    
    
    int GameCheck ()
    {
        // Player 1 win conditions
        // -----------------------
        // Horizontal Wins
        if (iSpace_1 == X_Filled && iSpace_2 == X_Filled && iSpace_3 == X_Filled) // 1-2-3
            return 1;
        else if (iSpace_4 == X_Filled && iSpace_5 == X_Filled && iSpace_6 == X_Filled) // 4-5-6
            return 1;
        else if (iSpace_7 == X_Filled && iSpace_8 == X_Filled && iSpace_9 == X_Filled) // 7-8-9
            return 1;
        
        // Vertical Wins
        else if (iSpace_1 == X_Filled && iSpace_4 == X_Filled && iSpace_7 == X_Filled) // 1-4-7
            return 1;
        else if (iSpace_2 == X_Filled && iSpace_5 == X_Filled && iSpace_8 == X_Filled) // 2-5-8
            return 1;
        else if (iSpace_3 == X_Filled && iSpace_6 == X_Filled && iSpace_9 == X_Filled) // 3-6-9
            return 1;
        
        // Diagonal Wins
        else if (iSpace_1 == X_Filled && iSpace_5 == X_Filled && iSpace_9 == X_Filled) // 1-5-9
            return 1;
        else if (iSpace_3 == X_Filled && iSpace_5 == X_Filled && iSpace_7 == X_Filled) // 3-5-7
            return 1;
        
    
    
        // Player 2 win conditions
        // -----------------------
        // Horizontal Wins
        if (iSpace_1 == O_Filled && iSpace_2 == O_Filled && iSpace_3 == O_Filled) // 1-2-3
            return 2;
        else if (iSpace_3 == O_Filled && iSpace_5 == O_Filled && iSpace_6 == O_Filled) // 4-5-6
            return 2;
        else if (iSpace_7 == O_Filled && iSpace_8 == O_Filled && iSpace_9 == O_Filled) // 7-8-9
            return 2;
        
        // Vertical Wins
        else if (iSpace_1 == O_Filled && iSpace_4 == O_Filled && iSpace_7 == O_Filled) // 1-4-7
            return 2;
        else if (iSpace_2 == O_Filled && iSpace_5 == O_Filled && iSpace_8 == O_Filled) // 2-5-8
            return 2;
        else if (iSpace_3 == O_Filled && iSpace_6 == O_Filled && iSpace_9 == O_Filled) // 3-6-9
            return 2;
        
        // Diagonal Wins
        else if (iSpace_1 == O_Filled && iSpace_5 == O_Filled && iSpace_9 == O_Filled) // 1-5-9
            return 2;
        else if (iSpace_3 == O_Filled && iSpace_5 == O_Filled && iSpace_7 == O_Filled) // 3-5-7
            return 2;
        
        // Draw conditions
        // ----------------
        
        else if (iSpace_1 != Empty && iSpace_2 != Empty && iSpace_3 != Empty && iSpace_4 != Empty && iSpace_5 != Empty && iSpace_6 != Empty && iSpace_7 != Empty && iSpace_8 != Empty && iSpace_9 != Empty)
            return 3;
        
        // Continue conditions
        else
            return 0;
    }
    Any feedback will be greatly appreciated! : )

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Surely your book doesn't say to use global variables!
    By chapter 7, it must have covered passing arguments to functions.
    And it must have covered arrays, too.
    I suggest you employ those concepts.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    20

    Chapters are short and simple.

    Quote Originally Posted by oogabooga View Post
    Surely your book doesn't say to use global variables!
    By chapter 7, it must have covered passing arguments to functions.
    And it must have covered arrays, too.
    I suggest you employ those concepts.
    This book has a total of 32 short chapters that just focus on one thing at a time. Information is there but to me it doesn't feel like the book explains the concepts enough. : ( (But I since I have it might as well use it)

    Chapter 1: Finding an IDE
    Chapter 2: Basics (Comments, cout, include, int main)
    Chapter 3: Declaring variables and input
    Chapter 4: If Statements
    Chapter 5: Loops
    Chapter 6: Functions
    (Yes it did tell me not to use global variables when I don't need to but as you can see several functions depend on these variables. Also the only example it provided for passing arguments was..)
    Code:
    int add (int x, int y)
    {
     return x + y;
    }
    Chapter 7: Switch Case and Enums

    I do not believe that I have been introduced to arrays yet..

    Basically I think that some of it's practice problems in general are a bit hard after reading the chapters. I usually have to go online for help to solve them before being able to move on to the next chapter. : /

    Btw what type of functions with passed arguments would you use for this scenario? (I'm still new ><)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jumping Into C++ - Chapter 8 Problem[3]
    By Mohamed Adel in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2013, 09:14 AM
  2. Jumping To C++ - Chapter 8 Problem
    By Mohamed Adel in forum C++ Programming
    Replies: 4
    Last Post: 08-27-2013, 01:02 PM
  3. Jumping into C++ chapter 7 help
    By DarthOrmus in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2013, 01:48 AM
  4. Replies: 6
    Last Post: 08-20-2012, 07:09 AM
  5. Jumping into C++ Chapter 5 problem 6 - Critique please
    By Kranky in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2012, 05:44 PM

Tags for this Thread