Thread: 3D Tic Tac Toe

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

    3D Tic Tac Toe

    I've written a program so that a user may play against the computer in 3D tic tac toe. However, the computer never takes its turn, and the user can just continue to place marks indefinitely. I have no idea how to fix this, the code is really long (i am fairly new a C++) and may be more work that necessary but this is still how its coded. The code runs and the boards display correctly but only the user makes moves. Any help is appreciated!
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <time.h>
    
    using namespace std;
    
    class boardGame //board class
    {
    public:
        int board [3][3];    //initializes array
        boardGame()            //constructor that initializes array
        {
            for(int x = 0; x < 3; x++)
            {
                for (int y = 0; y < 3; y++)
                {
                    board [x][y] = 0;
                }
            }
        }
        ~boardGame() {}                    //frees memory
        void setX(int x, int y);        //set square to X
        void setO(int x, int y);        //set square to Y
        bool vacancy(int x, int y);        //check if space is full
        void draw();                    //draws board
        string checkMarks(int x);        //inserts X's & O's
        bool checkWinner();                //check for winner
        void userMove();                //lets user make moves
        void compMove();
    };
    
    class tdBoard: public boardGame
    {
    public:
        int boardTwo [3][3][3];
        int userScore;
        int compScore;
        tdBoard()
        {
            for(int x = 0; x < 3; x++)
            {
                for(int y = 0; y < 3; y++)
                {
                    for(int z = 0; z < 3; z++)
                    {
                        boardTwo [x][y][z] = 0;
                    }
                }
            }
            userScore = 0;
            compScore = 0;
        }
        void draw();
        ~tdBoard() {}
        void setX(int z, int x, int y);
        void setO(int z, int x, int y);
        bool vacancy(int z, int x, int y);
        string checkMarks(int x);
        int checkWinner();
        void userMove();
        void compMove();
        bool checkFull();
    };
    //set space to X
    void boardGame::setX(int x, int y)
    {
        if(vacancy(x,y))
        {
            board[x][y] = 8;
        }
        else
        {
            cout<<"This space is taken!";
        }
    }
    //set space to O
    void boardGame::setO(int x, int y)
    {
        if(vacancy(x,y))
        {
            board [x] [y] = 9;
        }
        else
        {
            cout<<"This space is taken!";
        }
    }
    
    bool boardGame::vacancy(int x, int y)
    {
        if(board[x][y] != 0)
        {
            return 0;
        }
        else
        {
            return 1;
        }
    }
    
    bool boardGame::checkWinner()
    {
        int result = 50;
        //check rows
        for(int x = 0; x < 3; x++)
        {
            if(((board [x][0] == 8)&&(board [x][1] == 8)&&(board[x][2] == 8))||((board [x][0] == 9)&&(board[x][1] == 9)&&(board [x][2] == 9)))
            {
                result = board[x][0];
            }
        }
        //check columns
        for(int y = 0; y < 3; y++)
        {
            if(((board[0][y] == 8)&&(board[1][y] == 8)&&(board[2][y] == 8))||((board[0][y] == 9)&&(board[1][y] == 9)&&(board[2][y])))
            {
                result = board[0][y];
            }
        }
        //check diagnols
        if((((board [0][0] == 8)&&(board [1][1] == 8)&&(board [2][2] == 8))||((board [0][2] == 8)&&(board [1][1] == 8)&&(board [2][0] == 8))||
            ((board [0][0] == 9)&&(board [1][1] == 9)&&(board [2][2] == 9))||((board [0][2] == 9)&&(board [1][1] == 9)&&(board [2][0] == 9))))
            {
                result = board[1][1];
            }
        //check filled spaces
        int fullCount = 0;
        for(int x = 0; x < 3; x++)
        {
            for(int y = 0; y < 3; y++)
            {
                if(board [x][y] != 0)
                {
                    fullCount++;
                }
            }
        //if allspaces filled
        if(fullCount ==9)
        {
            result = 0;
        }
        
        if(result == 8)
        {
            cout<<"\nYou Win!\n";
            return 0;
        }
        else if(result == 9)
        {
            cout<<"\nYou Lost!\n";
            return 0;
        }
        else if(result == 0)
        {
            cout<<"\nIts a Tie!\n";
            return 0;
        }
        return 1;
        }
    }
    //print board 
    void boardGame::draw()
    {
        cout<<"\n";
        cout<<" 0 1 2 \n";
        for(int x = 0; x < 3; x++)
        {
            cout<<x;
            for(int y = 0; y < 3; y++)                            //draw coordinates
            {
                cout<<"|"<<checkMarks(board [x][y])<<"|";        //draw board
            }
            cout<<endl;
        }
    }
    //print marks
    string boardGame::checkMarks(int x)
    {
        if(x == 0)
        {
            return " ";        //space empty
        }
        else if(x == 8)
        {
            return "X";        //space marked by user
        }
        return "O";            //space marked by computer
    }
    //user move
    void boardGame::userMove()
    {
        int xCoord;
        int yCoord;
        cout<<"Please enter the row you would like to place your mark: \n";
        cin>>xCoord;
        cout<<"Please enter the column you would like to place your mark: \n";
        cin>>yCoord;
        //checks vacancy
        if(vacancy(xCoord,yCoord))
        {
            setX(xCoord, yCoord);
        }
        else
        {
            cout<<"Not allowed, please choose a different location!\n";
            userMove();
        }
    }
    //computer move
    void boardGame::compMove()
    {
        int exit = 0;
        int compXCoord;
        int compYCoord;
        do
        {
            //rand(time(NULL));
            //srand ((unsigned)time(NULL));
            compXCoord = rand() % 3;
            compYCoord = rand() % 3;
            
            if(vacancy(compXCoord, compYCoord))
            {
                setO(compXCoord, compYCoord);
                exit = 1;
            }
        }while(exit == 0);
    }
    
    void tdBoard::setX(int z, int x, int y)
    {
        boardTwo [x][y][z] = 8;
    }
    
    void tdBoard::setO(int z, int x, int y)
    {
        boardTwo [x][y][z] = 9;
    }
    
    bool tdBoard::vacancy(int x, int y, int z)
    {
        if(boardTwo [x][y][z] != 0)
        {
            return 0;
        }
        else
        {
            return 1;
        }
    }
    
    int tdBoard::checkWinner()
    {
        for(int z = 0; z < 3; z++)
        {
            for(int x = 0; x< 3; x++)
            {
            if(((boardTwo [x][0][z] == 8)&&(boardTwo [x][1][z] == 8)&&(boardTwo [x][2][z] == 8))||
               ((boardTwo [x][0][z] == 9)&&(boardTwo [x][1][z] == 9)&&(boardTwo [x][2][z] == 9)))
               {
                   if((boardTwo [x][0][z] == 8)&&(boardTwo [x][1][z] == 8)&&(boardTwo [x][2][z] ==8))
                   {
                       userScore++;
                   }
                   else
                   {
                       compScore++;
                   }
               }
            
        }
        
        for(int y = 0; y < 3; y++)
        {
            if(((boardTwo [0][y][z] == 8)&&(boardTwo [1][y][z] == 8)&&(boardTwo [2][y][z] == 8))||
               ((boardTwo [0][y][z] == 9)&&(boardTwo [1][y][z] == 9)&&(boardTwo [2][y][z] == 9)))
               {
                   if((boardTwo [0][y][z] == 8&&(boardTwo [1][y][z] == 8)&&(boardTwo [2][y][z] == 8)))
                   {
                       userScore++;
                   }
                   else
                   {
                       compScore++;
                   }
               }
        }
    
        if((((boardTwo [0][0][z] == 8)&&(boardTwo [1][1][z] == 8)&&(boardTwo [2][2][z] == 8))||
            ((boardTwo [0][2][z] == 8)&&(boardTwo [1][1][z] == 8)&&(boardTwo [2][0][z] == 8)))||
            ((boardTwo [0][0][z] == 9)&&(boardTwo [1][1][z] == 9)&&(boardTwo [2][2][z] == 9))||
            ((boardTwo [0][2][z] == 9)&&(boardTwo [1][1][z] == 9)&&(boardTwo [2][0][z] == 9)))
           {
               if(((boardTwo [0][0][z] == 8)&&(boardTwo [1][1][z] == 8)&&(boardTwo [2][2][z] == 8))||
                  ((boardTwo [0][2][z] == 8)&&(boardTwo [1][1][z] == 8)&&(boardTwo [2][0][z] == 8)))
                  {
                      userScore++;
                  }
                  else
                  {
                      compScore++;
                  }
           }
    }
        int z = 0;
        //top left
        if(boardTwo [0][0][z]+boardTwo [0][0][z+1]+boardTwo [0][0][z+2] == 24)
        {
            userScore++;
        }
        //top middle
        if(boardTwo [1][0][z]+boardTwo [1][0][z+1]+boardTwo [1][0][z+2] == 24)
        {
            userScore++;
        }
        //top right
        if(boardTwo [2][0][z]+boardTwo [2][0][z+1]+boardTwo [2][0][z+2] == 24)
        {
            userScore++;
        }
        //middle left
        if(boardTwo [0][1][z]+boardTwo [0][1][z+1]+boardTwo [0][1][z+2] == 24)
        {
            userScore++;
        }
        //middle middle
        if(boardTwo[1][1][z]+boardTwo[1][1][z+1]+boardTwo [1][1][z+2] == 24)
        {
            userScore++;
        }
        //middle right
        if(boardTwo[2][1][z]+boardTwo[2][1][z+1]+boardTwo [2][1][z+2] == 24)
        {
            userScore++;
        }
        //bottom left
        if(boardTwo[0][2][z]+boardTwo[0][2][z+1]+boardTwo [0][2][z+2] == 24)
        {
            userScore++;
        }
        //bottom middle
        if(boardTwo[1][2][z]+boardTwo[1][2][z+1]+boardTwo [1][2][z+2] == 24)
        {
            userScore++;
        }
        //bottom right
        if(boardTwo[2][2][z]+boardTwo[2][2][z+1]+boardTwo [2][2][z+2] == 24)
        {
            userScore++;
        }
        //Diagonals
        //top baord top left to bottom board bottom right
        if(boardTwo[0][0][z]+boardTwo[1][1][z+1]+boardTwo [2][2][z+2] == 24)
        {
            userScore++;
        }
        //top board top right to bottom board bottom left
        if(boardTwo[2][0][z]+boardTwo[1][1][z+1]+boardTwo [0][2][z+2] == 24)
        {
            userScore++;
        }
        //top board bottom left to bottom board top right
        if(boardTwo[0][2][z]+boardTwo[1][1][z+1]+boardTwo [2][0][z+2] == 24)
        {
            userScore++;
        }
        //top board top right to bottom board top left
        if(boardTwo[2][2][z]+boardTwo[1][1][z+1]+boardTwo [0][0][z+2] == 24)
        {
            userScore++;
        }
        //top board top middle to bottom board bottom middle
        if(boardTwo[1][0][z]+boardTwo[1][1][z+1]+boardTwo [1][2][z+2] == 24)
        {
            userScore++;
        }
        //bottom board top middle to top board bottom middle
        if(boardTwo[1][2][z]+boardTwo[1][1][z+1]+boardTwo [1][0][z+2] == 24)
        {
            userScore++;
        }
        //top board left middle to bottom board right middle
        if(boardTwo[0][1][z]+boardTwo[1][1][z+1]+boardTwo [2][1][z+2] == 24)
        {
            userScore++;
        }
        //bottom board left middle to top board right middle
        if(boardTwo[2][1][z]+boardTwo[1][1][z+1]+boardTwo [0][1][z+2] == 24)
        { 
            userScore++;
        }
        //side diaganols
        //top board bottom right to bottom board bottom left
        if(boardTwo[2][2][z]+boardTwo[1][2][z+1]+boardTwo [0][2][z+2] == 24)
        {
            userScore++;
        }
        //top board bottom left to bottom board bottom right
        if(boardTwo[0][2][z]+boardTwo[1][2][z+1]+boardTwo [2][2][z+2] == 24)
        {
            userScore++;
        }
        //top board bottom left to bottom board top left
        if(boardTwo[0][2][z]+boardTwo[0][1][z+1]+boardTwo [0][0][z+2] == 24)
        {
            userScore++;
        }
        //top board top left to bottom board bottom left
        if(boardTwo[0][0][z]+boardTwo[0][1][z+1]+boardTwo [0][2][z+2] == 24)
        {
            userScore++;
        }
        //top board top left to bottom board top right
        if(boardTwo[0][0][z]+boardTwo[1][0][z+1]+boardTwo [2][0][z+2] == 24)
        {
            userScore++;
        }
        //top board top right to bottom board top left
        if(boardTwo[2][0][z]+boardTwo[1][0][z+1]+boardTwo [0][0][z+2] == 24)
        {
            userScore++;
        }
        //top board top right to bottom board bottom right
        if(boardTwo[2][0][z]+boardTwo[2][1][z+1]+boardTwo[2][2][z+2] == 24)
        {
            userScore++;
        }
        //top board bottom right to bottom board top right
        if(boardTwo[2][2][z]+boardTwo[2][1][z+1]+boardTwo [2][0][z+2] == 24)
        {
            userScore++;
        }
        //computer conditions
        //top left
        if(boardTwo[0][0][z]+boardTwo[0][0][z+1]+boardTwo [0][0][z+2] == 27)
        {
            compScore++;
        }
        //top middle
        if(boardTwo[1][0][z]+boardTwo[1][0][z+1]+boardTwo [1][0][z+2] == 27)
        {
            compScore++;
        }
        //top right
        if(boardTwo[2][0][z]+boardTwo[2][0][z+1]+boardTwo [2][0][z+2] == 27)
        {
            compScore++;
        }
        //middle left
        if(boardTwo[0][1][z]+boardTwo[0][1][z+1]+boardTwo [0][1][z+2] == 27)
        {
            compScore++;
        }
        //middle middle
        if(boardTwo[1][1][z]+boardTwo[1][1][z+1]+boardTwo [1][1][z+2] == 27)
        {
            compScore++;
        }
        //middle right
        if(boardTwo[2][1][z]+boardTwo[2][1][z+1]+boardTwo [2][1][z+2] == 27)
        {
            compScore++;
        }
        //bottom left
        if(boardTwo[0][2][z]+boardTwo[0][2][z+1]+boardTwo [0][2][z+2] == 27)
        {
            compScore++;
        }
        //bottom middle
        if(boardTwo[1][2][z]+boardTwo[1][2][z+1]+boardTwo [1][2][z+2] == 27)
        {
            compScore++;
        }
        //bottom right
        if(boardTwo[2][2][z]+boardTwo[2][2][z+1]+boardTwo [2][2][z+2] == 27)
        {
            compScore++;
        }
        //Diagonals
        //top baord top left to bottom board bottom right
        if(boardTwo[0][0][z]+boardTwo[1][1][z+1]+boardTwo [2][2][z+2] == 27)
        {
            compScore++;
        }
        //top board top right to bottom board bottom left
        if(boardTwo[2][0][z]+boardTwo[1][1][z+1]+boardTwo [0][2][z+2] == 27)
        {
            compScore++;
        }
        //top board bottom left to bottom board top right
        if(boardTwo[0][2][z]+boardTwo[1][1][z+1]+boardTwo [2][0][z+2] == 27)
        {
            compScore++;
        }
        //top board top right to bottom board top left
        if(boardTwo[2][2][z]+boardTwo[1][1][z+1]+boardTwo [0][0][z+2] == 27)
        {
            compScore++;
        }
        //top board top middle to bottom board bottom middle
        if(boardTwo[1][0][z]+boardTwo[1][1][z+1]+boardTwo [1][2][z+2] == 27)
        {
            compScore++;
        }
        //bottom board top middle to top board bottom middle
        if(boardTwo[1][2][z]+boardTwo[1][1][z+1]+boardTwo [1][0][z+2] == 27)
        {
            compScore++;
        }
        //top board left middle to bottom board right middle
        if(boardTwo[0][1][z]+boardTwo[1][1][z+1]+boardTwo [2][1][z+2] == 27)
        {
            compScore++;
        }
        //bottom board left middle to top board right middle
        if(boardTwo[2][1][z]+boardTwo[1][1][z+1]+boardTwo [0][1][z+2] == 27)
        {
            compScore++;
        }
        //side diaganols
        //top board bottom right to bottom board bottom left
        if(boardTwo[2][2][z]+boardTwo[1][2][z+1]+boardTwo[0][2][z+2] == 27)
        {
            compScore++;
        }
        //top board bottom left to bottom board bottom right
        if(boardTwo[0][2][z]+boardTwo[1][2][z+1]+boardTwo[2][2][z+2] == 27)
        {
            compScore++;
        }
        //top board bottom left to bottom board top left
        if(boardTwo[0][2][z]+boardTwo[0][1][z+1]+boardTwo[0][0][z+2] == 27)
        {
            compScore++;
        }
        //top board top left to bottom board bottom left
        if(boardTwo[0][0][z]+boardTwo[0][1][z+1]+boardTwo[0][2][z+2] == 27)
        {
            compScore++;
        }
        //top board top left to bottom board top right
        if(boardTwo[0][0][z]+boardTwo[1][0][z+1]+boardTwo[2][0][z+2] == 27)
        {
            compScore++;
        }
        //top board top right to bottom board top left
        if(boardTwo[2][0][z]+boardTwo[1][0][z+1]+boardTwo[0][0][z+2] == 27)
        {
            compScore++;
        }
        //top board top right to bottom board bottom right
        if(boardTwo[2][0][z]+boardTwo[2][1][z+1]+boardTwo[2][2][z+2] == 27)
        {
            compScore++;
        }
        //top board bottom right to bottom board top right
        if(boardTwo[2][2][z]+boardTwo[2][1][z+1]+boardTwo[2][0][z+2] == 27)
        {
            compScore++;
        }
        
        //results
        if(userScore<compScore)
        {
            return 0; //user lost
        }
        else if(userScore > compScore)
        {
            return 1; //user won
        }
        else
        {
            return 1; //tie game
        }
    }
    
    //draw baord
    void tdBoard::draw()
    {
        cout<<"\n";
        
        for(int z = 0; z < 3; z++)
        {
            cout<<"Board: "<<z<<endl;
            cout<<"  0  1  2 \n";
            for(int y = 0; y < 3; y++)
            {
                cout<<y;
                for(int x = 0; x < 3; x++)
                {
                    cout<<"|"<<checkMarks(boardTwo[x][y][z])<<"|";
                }
                cout<<endl;
            }
            cout<<endl;
        }
        cout<<"\n";
    }
    
    //display marks
    string tdBoard::checkMarks(int x)
    {
        if(x == 0)
        {
            return " ";
        }
        else if(x == 8)
        {
            return "X";
        }
            return "O";
    }
    
    //user move method
    void tdBoard::userMove()
    {
        int xCoord;
        int yCoord;
        int zCoord;
        
        cout<<"Enter the board you wish to play on: ";
        cin>>zCoord;
        cout<<"Enter the column you would like to place your mark on: ";
        cin>>xCoord;
        cout<<"Enter the row you would like to place your mark on: ";
        cin>>yCoord;
        
        //check vacancy
        if(vacancy(xCoord,yCoord,zCoord))
        {
            setX(zCoord, xCoord, yCoord);
        }
        else
        {
            cout<<"Error: Please try again!\n";
            userMove();
        }
    }
    
    //computer move method
    void tdBoard::compMove()
    {
        srand(time(NULL));
        //srand ((unsigned)time(NULL));
        int exit = 0;
        int compXCoord;
        int compYCoord;
        int compZCoord;
        do
        {
            compXCoord = rand()%3;
            compYCoord = rand()%3;
            compZCoord = rand()%3;
            
            if(vacancy(1,1,1))
            {
                setO(1,1,1);
                return;
            }
            
            if(vacancy(compXCoord,compYCoord,compZCoord))
            {
                setO(compZCoord,compXCoord,compYCoord);
                exit = 1;
            }
        }while(exit == 0);
    }
    
    //check for board being full
    bool tdBoard::checkFull()
    {
        int totalCount = 0;
        for(int z = 0; z < 3; z++)
        {
            for(int y = 0; y < 3; y++)
            {
                for(int x = 0; x < 3; x++)
                {
                    if(boardTwo[x][y][z] != 0)
                    {
                        totalCount++;
                    }
                }
            }
        }
        if(totalCount == 27)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    
    //main function
    int main()
    {
        tdBoard game;
        int userTurn;
        //rand(time(NULL));
        //srand((unsigned)time(NULL));
        userTurn = rand()%2;
        bool contGame = 1;
        
        do
        {
            game.draw();
            if(userTurn = 1)
            {
                game.userMove();
                userTurn = 0;
            }
            else
            {
                game.compMove();
                userTurn = 1;
                cout<<"Computer starts: "<<endl;
            }
            
            if(game.checkFull())
            {
                contGame = 0;
            }
        }
        while(contGame == 1);
        game.draw();
        
        if(game.checkWinner() == 1)
        {
            cout<<"\nUser Score: "<<game.userScore;
            cout<<"\nComputer Score: "<<game.compScore<<endl;
            cout<<"You Won!";
        }
        else if(game.checkWinner() == 0)
        {
            cout<<"\nUser Score: "<<game.userScore;
            cout<<"\nComputer Score: "<<game.compScore<<endl;
            cout<<"You Lost!";        
        }
        else if(game.checkWinner() == 2)
        {
            cout<<"\nUser Score: "<<game.userScore;
            cout<<"\nComputer Score: "<<game.compScore<<endl;
            cout<<"Its a tie!";        
        }
        cout<<"\nGame Over!";
        return 0;
    }

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Turn on your compiler warnings. I haven't compiled your code, but line 710 should have == and not = so that's one jump out at me mistake that any modern compiler should warn about

    change
    Code:
    if(userTurn = 1)
    to

    Code:
    if(userTurn == 1)

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    20
    Yeah that was all that was stopping it from working. Found it after looking over the code for about an hour. Compiler never gave that warning, but it does give errors.

Popular pages Recent additions subscribe to a feed

Tags for this Thread