Thread: Tic tac toe game help

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128

    Tic tac toe game help

    Hi everyone,

    I have a few issues with my new program, which is making the tic tac toe game from Jumping Into C++. There is another thread about this (we are obviously using the same book, at the same time!). This is causing slight issues in me not wanting to view the other thread in case it gives away the answers. So, I am working at this and my problems follow the code:

    Code:
    //  Tic tac toe
    //  Created by Sam on 21/04/2013.
    
    #include <iostream>
    
    //Top right (TR), Centre left (CL), Bottom Middle (BM) etc...
    void printGameBoard(char TL, char TM, char TR, char CL, char CM, char CR, char BL, char BM, char BR);
    void facilitateMove(int squareChoice);
    
    //initialize values for starting board
    char TL('1');
    char TM('2');
    char TR('3');
    char CL('4');
    char CM('5');
    char CR('6');
    char BL('7');
    char BM('8');
    char BR('9');
    
    //variable to hold the 'X' for player 1, and 'O' for player 2
    char playerMarker;
    
    int main()
    {
        enum SquareStates {SS_O, SS_X, SS_HAS_NUMBER}; //3 states - has a 'O', has a 'X', has a number '1', '2' etc...
        SquareStates currentValue; //current value of the square on the board, SS_O, SS_X or SS_HAS_NUMBER
        //the board squares already have a value 1, 2, 3, 4... so how to use SS_HAS_NUMBER?
        
        //variable to switch whos turn it is (starts with player 1 == true)
        int playerTurn = 1;
        
        //after game is finished, a choice for another go
        bool anotherGo = 1;
        
        //saves the number a user has inputted to compare with enums (??) for allowed/illegal move
        char squareChoice;
        
        do
        {
            //set markers for each player
            if (playerTurn == 1)
            {
                playerMarker = 'X';
            }
            else
            {
                playerMarker = 'O';
            }
            
            //prints game board with updated variables depending on player move
            printGameBoard(TL, TM, TR, CL, CM, CR, BL, BM, BR);
            
            std::cout << "Player " << playerTurn << ", your go: ";
            std::cin >> squareChoice;
            
            //CHECK MOVE IS LEGAL
            /* I planned to use like this, presuming player 1 is using an 'X':
             
             if(player1 == 1 && currentValue == SS_X)
             {
             illegal move
             }
             
             if(player2 == 1 && currentValue == SS_O)
             {
             illegal move
             }
            */
            
            //facilitate move
            facilitateMove(squareChoice);
            
            //switches between player 1 and player 2 turn after each move
            if (playerTurn == 1)
            {
                playerTurn = 2;
            }
            else
            {
                playerTurn = 1;
            }
            
        } while (anotherGo == 1);
    }
    
    void printGameBoard(char TL, char TM, char TR, char CL, char CM, char CR, char BL, char BM, char BR)
    {
        std::cout << "+" << "-" << "+" << "-" << "+" << "-" << "+" << std::endl;
        std::cout << "|" << TL << "|" << TM << "|" << TR << "|" << std::endl;
        std::cout << "+" << "-" << "+" << "-" << "+" << "-" << "+" << std::endl;
        std::cout << "|" << CL << "|" << CM << "|" << CR << "|" << std::endl;
        std::cout << "+" << "-" << "+" << "-" << "+" << "-" << "+" << std::endl;
        std::cout << "|" << BL << "|" << BM << "|" << BR << "|" << std::endl;
        std::cout << "+" << "-" << "+" << "-" << "+" << "-" << "+" << std::endl;
    }
    
    void facilitateMove(int squareChoice)
    {
        switch(squareChoice)
        {
            case '1':
                TL = playerMarker;
                break;
            case '2':
                TM = playerMarker;
                break;
            case '3':
                TR = playerMarker;
                break;
            case '4':
                CL = playerMarker;
                break;
            case '5':
                CM = playerMarker;
                break;
            case '6':
                CR = playerMarker;
                break;
            case '7':
                BL = playerMarker;
                break;
            case '8':
                BM = playerMarker;
                break;
            case '9':
                BR = playerMarker;
                break;
        }
    }
    What works:

    1) Switching players
    2) Using correct marker for each player (X, O etc..)

    Problems:

    1) I am using global variables, is there a way around this other than dumping it all in main, or is it suitable for this program?

    2) How do I get my enums involved? It hasn't yet clicked with me on how to actually use them. There are 3 states to each square on the board, a number, a 'X' or a 'O'.

    I have a few ideas which I have commented in but the lightbulb is most certainly out right now! Once I understand how to use the enums, I think it will be a lot easier getting the logic for:

    a) when a move is illegal
    c) when someone has won

    Just a bit stuck. Any help is very appreciated, thanks.
    Last edited by samwillc; 04-21-2013 at 05:25 AM.

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    62
    1) I am using global variables, is there a way around this other than dumping it all in main, or is it suitable for this program?
    you can use global variables if you want but if you do there is no need to pass them as arguments to your functions (that's what makes them global), so if you try:
    Code:
    #include <iostream>
    char test[]="xpto";
    void printtest(){
      std::cout<<test;
    }
    main(){
      printtest();
    }
    you will notice that it works.

    you could instead of declaring 9 separate variables just declare an array "board[3][3]", it would probably even facilitate the code when you implement a "check win condition" function.

    edit: and you seem to know this since you used it for your second function, however for the 1st know that the local scope superseeds the global variables, I mean, if you name a global variable "TL" and then make a function void f1(char TL) inside that function TL means "the argument that was passed to me" so if you then call that function with a different argument, f1(TM) for example, it will do its operations with TM.
    Worst of all, this is not a syntax error, so it will compile correctly, and it doesn't "look wrong" so it would be a great way for you to spend hours looking for a bug in a larger project simply because "TL is not TL inside that function"
    Last edited by killme; 04-21-2013 at 09:38 AM.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Ok, I converted the many variables at the top into an array and updated the appropriate conditions:

    Code:
    //  Tic tac toe
    //  Created by Sam on 21/04/2013.
    
    #include <iostream>
    
    char gameBoard [3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
    
    void printGameBoard();
    
    //variable to hold the 'X' for player 1, and 'O' for player 2
    char playerMarker;
    
    int main()
    {
        
        //variable to switch whos turn it is (starts with player 1 == true)
        int playerTurn = 1;
        
        //after game is finished, a choice for another go
        bool bAnotherGo = 1;
        
        do
        {
            //set markers for each player
            if (playerTurn == 1)
            {
                playerMarker = 'X';
            }
            else
            {
                playerMarker = 'O';
            }
            
            //prints game board with updated variables depending on player move
            printGameBoard();
            
            std::cout << "Player " << playerTurn << ", your go: ";
            //saves the number a user has inputted to compare with enums (??) for allowed/illegal move
            char squareChoice;
            std::cin >> squareChoice;
            bool bIsValid;
            
            
            do {
                
                bIsValid = true;
                
                
                // Check for a valid move
                
                if (squareChoice == '1' && gameBoard[0][0] == '1')
                {
                    
                    gameBoard[0][0] = playerMarker;
                    
                }
                else if (squareChoice == '2' && gameBoard[0][1] == '2')
                {
                    
                    gameBoard[0][1] = playerMarker;
                    
                }
                else if (squareChoice == '3' && gameBoard[0][2] == '3')
                {
                    
                    gameBoard[0][2] = playerMarker;
                    
                }
                else if (squareChoice == '4' && gameBoard[1][0] == '4')
                {
                    
                    gameBoard[1][0] = playerMarker;
                    
                }
                else if (squareChoice == '5' && gameBoard[1][1] == '5')
                {
                    
                    gameBoard[1][1] = playerMarker;
                    
                }
                else if (squareChoice == '6' && gameBoard[1][2] == '6')
                {
                    
                    gameBoard[1][2] = playerMarker;
                    
                }
                else if (squareChoice == '7' && gameBoard[2][0] == '7')
                {
                    
                    gameBoard[2][0] = playerMarker;
                    
                }
                else if (squareChoice == '8' && gameBoard[2][1] == '8')
                {
                    
                    gameBoard[2][1] = playerMarker;
                    
                }
                else if (squareChoice == '9' && gameBoard[2][2] == '9')
                {
                    
                    gameBoard[2][2] = playerMarker;
                    
                }
                else
                {
                    std::cout << "Invalid Move. Try again: ";
                    std::cin >> squareChoice;
                    bIsValid = false;
                    
                }
                
            } while (!bIsValid);
            
            //switches between player 1 and player 2 turn after each move
            if (playerTurn == 1)
            {
                playerTurn = 2;
            }
            else
            {
                playerTurn = 1;
            }
            
        } while (bAnotherGo == 1);
    }
    
    void printGameBoard()
    {
        std::cout << "+" << "-" << "+" << "-" << "+" << "-" << "+" << std::endl;
        std::cout << "|" << gameBoard[0][0] << "|" << gameBoard[0][1] << "|" << gameBoard[0][2] << "|" << std::endl;
        std::cout << "+" << "-" << "+" << "-" << "+" << "-" << "+" << std::endl;
        std::cout << "|" << gameBoard[1][0] << "|" << gameBoard[1][1] << "|" << gameBoard[1][2] << "|" << std::endl;
        std::cout << "+" << "-" << "+" << "-" << "+" << "-" << "+" << std::endl;
        std::cout << "|" << gameBoard[2][0] << "|" << gameBoard[2][1] << "|" << gameBoard[2][2] << "|" << std::endl;
        std::cout << "+" << "-" << "+" << "-" << "+" << "-" << "+" << std::endl;
    }
    'void printGameBoard' could be done in a loop by the looks of the pattern to save typing gameBoard[n][n] so many times.

    As it stands then, I find it pretty hard to read!
    Last edited by samwillc; 04-21-2013 at 10:22 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by killme View Post
    you can use global variables if you want but if you do there is no need to pass them as arguments to your functions (that's what makes them global), so if you try:
    Code:
    #include <iostream>
    char test[]="xpto";
    void printtest(){
      std::cout<<test;
    }
    main(){
      printtest();
    }
    you will notice that it works.

    you could instead of declaring 9 separate variables just declare an array "board[3][3]", it would probably even facilitate the code when you implement a "check win condition" function.

    edit: and you seem to know this since you used it for your second function, however for the 1st know that the local scope superseeds the global variables, I mean, if you name a global variable "TL" and then make a function void f1(char TL) inside that function TL means "the argument that was passed to me" so if you then call that function with a different argument, f1(TM) for example, it will do its operations with TM.
    Worst of all, this is not a syntax error, so it will compile correctly, and it doesn't "look wrong" so it would be a great way for you to spend hours looking for a bug in a larger project simply because "TL is not TL inside that function"
    In C++, remember to use std::string instead of char arrays and that main must return int. You cannot omit the return type of functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Ok, so I'm getting there after getting sidetracked with enums. I only have a few problems left now.

    Code:
    #include <iostream>
    
    char gameBoard [3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
    
    /*
     
     1,2,3
     4,5,6
     7,8,9
     
     [0][0], [0][1], [0][2]
     [1][0], [1][1], [1][2]
     [2][0], [2][1], [2][2]
     
     */
    
    void printGameboard(); //no need to pass global variables as arguments to this function - info thanks to killme!
    void resetGameboard();
    
    int main()
    {
        
        //variable to hold the 'X' for player 1, and 'O' for player 2
        char playerMarker;
        
        //the square a player chooses
        int squareChoice;
        
        //after game is finished, a choice for another go
        bool bAnotherGo = true;
        
        //main game loop - exits program if anotherGo is false
        do
        {
            
            bool bGameOver = false;
            
            //variable to switch whos turn it is (starts with player 1 == true)
            int playerTurn = 1;
            
            do //exits loop when the game is over i.e. true, then asks user if wants another go
            {
                //set markers for each player
                if (playerTurn == 1)
                {
                    playerMarker = 'X';
                }
                else
                {
                    playerMarker = 'O';
                }
            
                //prints game board with updated variables depending on player move
                printGameboard();
            
                //get player input
                std::cout << "Player " << playerTurn << ", your go: ";
                std::cin >> squareChoice;
    
                //check move is valid
                bool bIsValid;
            
                do
                {
                    bIsValid = true;
    
                    if (squareChoice == 1 && gameBoard[0][0] == '1')
                    {
                        gameBoard[0][0] = playerMarker;
                    }
                    else if (squareChoice == 2 && gameBoard[0][1] == '2')
                    {
                        gameBoard[0][1] = playerMarker;
                    }
                    else if (squareChoice == 3 && gameBoard[0][2] == '3')
                    {
                        gameBoard[0][2] = playerMarker;
                    }
                    else if (squareChoice == 4 && gameBoard[1][0] == '4')
                    {
                        gameBoard[1][0] = playerMarker;
                    }
                    else if (squareChoice == 5 && gameBoard[1][1] == '5')
                    {
                        gameBoard[1][1] = playerMarker;
                    }
                    else if (squareChoice == 6 && gameBoard[1][2] == '6')
                    {
                        gameBoard[1][2] = playerMarker;
                    }
                    else if (squareChoice == 7 && gameBoard[2][0] == '7')
                    {
                        gameBoard[2][0] = playerMarker;
                    }
                    else if (squareChoice == 8 && gameBoard[2][1] == '8')
                    {
                        gameBoard[2][1] = playerMarker;
                    }
                    else if (squareChoice == 9 && gameBoard[2][2] == '9')
                    {
                        gameBoard[2][2] = playerMarker;
                    }
                    else
                    {
                        std::cout << "Invalid Move. Try again: ";
                        std::cin >> squareChoice;
                        bIsValid = false;
                    }
                
                } while (!bIsValid);
    
            //check to see if the previous move is a winning one
            
            if (!(gameBoard[0][0] == '1')) //if square 1 is not a number...
            {
                if (gameBoard[0][0] == gameBoard[0][1] && gameBoard[0][0] == gameBoard[0][2]) //3 in a row 1,2,3
                {
                    bGameOver = true;
                }
                
                if (gameBoard[0][0] == gameBoard[1][0] && gameBoard[0][0] == gameBoard[2][0]) //3 in a row 1,4,7
                {
                    bGameOver = true;
                }
            }
                
            if (!(gameBoard[1][1] == '5')) //if square 5 is not a number...
            {
                if (gameBoard[1][1] == gameBoard[0][1] && gameBoard[1][1] == gameBoard[2][1]) //3 in a row 2,5,8
                {
                    bGameOver = true;
                }
                
                if (gameBoard[1][1] == gameBoard[1][0] && gameBoard[1][1] == gameBoard[1][2]) //3 in a row 4,5,6
                {
                    bGameOver = true;
                }
                
                if (gameBoard[1][1] == gameBoard[0][2] && gameBoard[1][1] == gameBoard[2][0]) //3 in a row 7,5,3
                {
                    bGameOver = true;
                }
                
                if (gameBoard[1][1] == gameBoard[0][0] && gameBoard[1][1] == gameBoard[2][2]) //3 in a row 1,4,9
                {
                    bGameOver = true;
                }
            }
                
            if (!(gameBoard[2][2] == '9')) //if square 9 is not a number...
            {
                if (gameBoard[2][2] == gameBoard[2][1] && gameBoard[2][2] == gameBoard[2][0]) //3 in a row 7,8,9
                {
                    bGameOver = true;
                }
                
                if (gameBoard[2][2] == gameBoard[1][2] && gameBoard[2][2] == gameBoard[0][2]) //3 in a row 3,6,9
                {
                    bGameOver = true;
                }
            }
                
            //check for a tie
            // ??????????? //
                
                
            //congratulate player when game is over
            if (bGameOver == true)
            {
                std::cout << "Well done! Player " << playerTurn << " wins!" << std::endl;
            }
                
            //move is valid, switch players
            if (playerTurn == 1)
            {
                playerTurn = 2;
            }
            else
            {
                playerTurn = 1;
            }
            
            } while (bGameOver == false);
            
            //ask if they want another go? using while (bAnotherGo == 1) to exit loop and end game;
            std::cout << "Do you want another go? 1 for yes, 0 for no: ";
            std::cin >> bAnotherGo;
            
            //if they do want another go, reset the board, somehow...
            if (bAnotherGo == true)
            {
                resetGameboard();
            }
             
        } while (bAnotherGo == true);
    
    std::cout << "I hope you enjoyed your game :)";
        
    }
    
    void printGameboard()
    {
        for (int i = 0; i < 3; i++)
        {
            std::cout << "|" << gameBoard[i][0] << "|" << gameBoard[i][1] << "|" << gameBoard[i][2] << "|" << std::endl;
            
            if (i < 2)
            {
                std::cout << "+" << "-" << "+" << "-" << "+" << "-" << "+" << std::endl;
            }
        }
    }
    
    void resetGameboard()
    {
        gameBoard [3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}}; //excess elements in scalar initializer??
    }
    My problems now are:

    1) How to check for a tie - still trying to figure out the logic and where in the program to check this.
    2) How to reset board, the way I tried failed miserably! Error posted in comments above.
    3) Finding a way to use enums in this code.

    Any hints are appreciated but maybe not on #1, I feel I should really be able to get that myself by now, after all, I know how to check for a win, halfway there! Thanks for any input.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, so first of, the way you go about it is a mess. And because it is a mess, you won't find how to use enums.
    Consider this: we have a board of 9 squares, which can each contain circle, X or be empty. What is a good way to represent this in a computer?
    How would you in reality go about checking if a move is valid, check for a winner or tie?

    Also,
    >>gameBoard [3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}}; //excess elements in scalar initializer??
    Well, first off, you are trying to assign everything to one element which is out of bounds. How you can you assign 9 elements to one element?
    If you removed the index subscript, used C++11 and std::array, you would get
    gameBoard = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}}; //excess elements in scalar initializer??
    which should work. But don't do that. This way of representing the board is just weird.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    In reality, if I physically have a board in front of me, the squares are blank. I couldn't put a cross where there is already a cross. I couldn't put a circle where there is already a circle. I can only place a cross or circle on a blank square. I could use:

    Code:
    enum TicTacToeSquare {Blank, Circle, Cross};
    
    TicTacToeSquare currentValue; //where currentValue holds 0,1 or 2
    That makes sense to me in theory, but how do I translate that into a program? My squares have values, 1,2,3 etc... they are never empty.

    It feels like I need to find a way that the printed numbers are just a representation of the board (so the user can pick a square) but the real values are simply 0,1 or 2 i.e. blank, cross or circle. I will keep working on this.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why do your squares have numbered values? To me, each square is either a circle, X or empty.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Quote Originally Posted by Elysia View Post
    Why do your squares have numbered values? To me, each square is either a circle, X or empty.
    You're right, in a real life scenario, but if I was playing this game online gor example, I would expect the squares to be numbered to show which one to choose. Rather that than choose a row 1-3, choose a column 1-3... that's just making things difficult for the player. I have seen both examples online and much prefer the former.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    62
    That makes sense to me in theory, but how do I translate that into a program? My squares have values, 1,2,3 etc... they are never empty.

    It feels like I need to find a way that the printed numbers are just a representation of the board (so the user can pick a square) but the real values are simply 0,1 or 2 i.e. blank, cross or circle. I will keep working on this.
    so basicly the only thing that stands in the way of using enums as you want to is your printGameboard() function, if you start by rewriting it "enum-friendly" every thing else falls into place much easier.

    1) How to check for a tie - still trying to figure out the logic and where in the program to check this.
    what do you mean "check for a tie", you are aiming to check if after some move in the middle of the game there is no longer anyway of one of the players winning? or would you be contempt with letting the board fill up completly? the questions themselfs answer the "where", as for the how.... well, the 2nd case is pretty straight forward, the 1st... what do YOU look at in a board to see if its a tie? (warning: implementing a function to do it this way will probably take you as long as the rest of the program combined)
    Last edited by killme; 04-23-2013 at 04:15 PM.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    62
    Quote Originally Posted by samwillc View Post
    You're right, in a real life scenario, but if I was playing this game online gor example, I would expect the squares to be numbered to show which one to choose. Rather that than choose a row 1-3, choose a column 1-3... that's just making things difficult for the player. I have seen both examples online and much prefer the former.
    yes but thats only an interface problem, you should try to separate in your head what data the program needs for running from what you want it to show on the screen. does it need 123456789 inside the data structure? can't you just "translate" the position?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by killme View Post
    yes but thats only an interface problem, you should try to separate in your head what data the program needs for running from what you want it to show on the screen. does it need 123456789 inside the data structure? can't you just "translate" the position?
    This.
    You need to figure out how, given some input, you find the appropriate place which keeps that state in your program and modify it.
    You are doing a rather poor job of that right now.

    That sounds very ambiguous, doesn't it? So I'll give you an example.
    Consider a weird calculator. It stores a set of 3 numbers, then allows you to sum, subtract, multiply or divide these numbers.
    So you basically get a menu like:

    - Enter a number
    -- Store as number 1
    -- Store as number 2
    -- Store as number 3
    -Add these numbers...
    - ...

    How would you store these numbers and find them later?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Quote Originally Posted by Elysia View Post
    This.
    You need to figure out how, given some input, you find the appropriate place which keeps that state in your program and modify it.
    You are doing a rather poor job of that right now.
    I'm still working on arrays but let's just say I have a bunch of integers for now. This is how I imagine it working if this helps to clarify my position:

    Board is blank, this is shown on screen:

    | | | |
    ------
    | | | |
    ------
    | | | |
    Enter 1-9 (have to figure out a way to present this i.e. data stored vs what's presented on screen. But for now, get the values stored correctly)

    Player 1 is a cross, player 2 is a circle.

    I have enums (0) blank, (1) cross, (2) circle.

    int boardPos1 = blank;
    int boardPos2 = blank;
    int boardPos3 = blank;
    int boardPos4 = blank;
    int boardPos5 = blank;
    int boardPos6 = blank;
    int boardPos7 = blank;
    int boardPos8 = blank;
    int boardPos9 = blank;

    If player 1 enters 6:

    int boardPos6 = cross

    If player 2 enters 9:

    int boardPos9 = circle;

    So I could use the new values to update the board:

    int boardPos1 = blank;
    int boardPos2 = blank;
    int boardPos3 = blank;
    int boardPos4 = blank;
    int boardPos5 = blank;
    int boardPos6 = cross
    int boardPos7 = blank;
    int boardPos8 = blank;
    int boardPos9 = circle;

    But wouldn't this just update the board like this:

    | | | |
    ------
    | | |1|
    ------
    | | |0|

    Once I store things in the proper place, I can see that the comparisons would all be pretty straightforward. However, I'm finding the bit in between step 1 and 3 rather more tricky. And perhaps this isn't the way to go about it anyway!
    Last edited by samwillc; 04-24-2013 at 01:21 AM.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's a pretty common subject, tic tac toe. I've written about it before, so I guess my advice is just to search harder.

    There is no reason to check for a cat game before you fill all the squares though.

  15. #15
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Quote Originally Posted by whiteflags View Post
    It's a pretty common subject, tic tac toe. I've written about it before, so I guess my advice is just to search harder.

    There is no reason to check for a cat game before you fill all the squares though.
    I will go away and research into this. I think otherwise this thread will go on forever.

    I am aware that a game can be finished before all the squares are filled up but I had no idea what a 'cats game' was until I looked it up. We call this game noughts and crosses here, 'tic tac toe'?!

    Will crack on later after work. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-18-2010, 09:42 PM
  2. Should I use a game engine in making a RPG game?
    By m3rk in forum Game Programming
    Replies: 6
    Last Post: 01-26-2009, 04:58 AM
  3. Guessing game: how to quit the game?
    By hzr in forum C Programming
    Replies: 5
    Last Post: 12-18-2008, 10:53 AM
  4. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 07:58 PM
  5. Game Designer vs Game Programmer
    By the dead tree in forum Game Programming
    Replies: 8
    Last Post: 04-28-2005, 09:17 PM