Thread: Clueless about whats wrong(tic tac toe)

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    106

    Clueless about whats wrong(tic tac toe)

    This code is supposed to accept the input from the user and which ever spot the player chooses it makes it equal to one(for x), but for some reason its not working the way I would like... any help appreciated.. Thanks!

    Code:
    #include <iostream>
    
    typedef struct{
        int tttPositions[9];
    }gameBoard;
    
    //function prototypes
    void displayBoard(gameBoard);
    void getPlayerChoice(gameBoard);
    
    int main(){
        gameBoard board;
        board.tttPositions[0] = 0;
        board.tttPositions[1] = 0;
        board.tttPositions[2] = 0;
        board.tttPositions[3] = 0;
        board.tttPositions[4] = 0;
        board.tttPositions[5] = 0;
        board.tttPositions[6] = 0;
        board.tttPositions[7] = 0;
        board.tttPositions[8] = 0;
    
        bool playing = true;
        while (playing == true){
            displayBoard(board);
            getPlayerChoice(board);
        }
    
    }
    
    void displayBoard(gameBoard board){
        for(int loopNumber = 0; loopNumber <= 8; loopNumber++){
            switch(board.tttPositions[loopNumber]){
                case 0:
                    std::cout<< " - ";
                    break;
                case 1:
                    std::cout<< " X ";
                    break;
                case 2:
                    std::cout<< " O ";
                    break;
            }
            if (loopNumber == 2 || loopNumber == 5 || loopNumber == 8){
                    std::cout<< std::endl;
            }
    
        }
    }
    
    void getPlayerChoice(gameBoard board){
        int temp;
        std::cout<< "Where would you like to place your piece?" << std::endl;
        std::cin>> temp;
        std::cin.ignore();
        temp -= 1;
        board.tttPositions[temp] = 1;
    }

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    You need to pass the gameBoard object as a pointer, or by reference. Currently your code is creating a copy of the gameBoard object becuase you're passing by value when you call the getPlayerChoice function, so you are not making any changes to the original object.
    Last edited by Ushakal; 07-07-2010 at 05:26 AM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    106
    Okay so I see what he problem is.. I know what pointers and references are, but I'm not that sure how to pass them in a function to get the results needed(I've tried several different attempts)

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by jamort View Post
    Okay so I see what he problem is.. I know what pointers and references are, but I'm not that sure how to pass them in a function to get the results needed(I've tried several different attempts)
    Code:
    void displayBoard(gameBoard& board);
    void getPlayerChoice(gameBoard& board);
    //...
    void displayBoard(gameBoard& board){
        for(int loopNumber = 0; loopNumber <= 8; loopNumber++){
            switch(board.tttPositions[loopNumber]){
                case 0:
                    std::cout<< " - ";
                    break;
                case 1:
                    std::cout<< " X ";
                    break;
                case 2:
                    std::cout<< " O ";
                    break;
            }
            if (loopNumber == 2 || loopNumber == 5 || loopNumber == 8){
                    std::cout<< std::endl;
            }
    
        }
    }
    
    void getPlayerChoice(gameBoard& board) {
        int temp;
        std::cout<< "Where would you like to place your piece?" << std::endl;
        std::cin>> temp;
        std::cin.ignore();
        temp -= 1;
        board.tttPositions[temp] = 1;
    }
    Last edited by Programmer_P; 07-10-2010 at 08:52 AM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    106
    okay.. i see now i was placing the & in the wrong part of the program... thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tic tac toe check winner
    By dhardin in forum C++ Programming
    Replies: 15
    Last Post: 12-20-2009, 07:57 PM
  2. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  3. Tic Tac Toe AI help please...
    By Rune Hunter in forum Game Programming
    Replies: 12
    Last Post: 11-05-2004, 04:24 PM
  4. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  5. tic tac toe game
    By Leeman_s in forum Game Programming
    Replies: 9
    Last Post: 04-24-2002, 03:24 AM