Thread: 3x3x3 Tic Tac Toe

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

    Question 3x3x3 Tic Tac Toe

    This is an assignment in a programming class I am currently in. In a previous assignment we were required to program a 3x3x3 TTT board where you it was player vs computer for a single game. The objective, is to get as many 3-in-a-rows as possible before the entire board is full.
    Now I am having two computers play against each other, but they have to play 10 games in a row and whichever computer wins the most of the 10 games is the overall winner. The code runs and the two computers play all 10 games, however, I am having a hard time figuring out how to add up all the scores from the 10 games so that I can create an overall score for each player and ultimately a winner. Each time a new game is played the score is restarted to 0 and the process begins again until all games are played. The code for the program is below, any help is appreaciated. I am fairly new at C/C++ as this is my first semester learning it.
    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 comp2Move();                //lets user make moves
        void comp1Move();
    };
    
    class tdBoard: public boardGame
    {
    public:
        int boardTwo [3][3][3];
        int comp2Score;
        int comp1Score;
        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;
                    }
                }
            }
            comp2Score = 0;
            comp1Score = 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 comp2Move();
        void comp1Move();
        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++;
                }
            }
        }
    }
    //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::comp2Move()
    {
        int exit = 0;
        int xCoord;
        int yCoord;
        do
        {
            //rand(time(NULL));
            //srand ((unsigned)time(NULL));
            xCoord = rand() % 3;
            yCoord = rand() % 3;
            
            if(vacancy(xCoord, yCoord))
            {
                setX(xCoord, yCoord);
                exit = 1;
            }
        }while(exit == 0);
    }
    
    //computer move
    void boardGame::comp1Move()
    {
        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 < 6; z++)
        {
            for(int x = 0; x< 6; 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))
                   {
                       comp2Score++;
                   }
                   else
                   {
                       comp1Score++;
                   }
               }
            
        }
        
        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)))
                   {
                       comp2Score++;
                   }
                   else
                   {
                       comp1Score++;
                   }
               }
        }
    
        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)))
                  {
                      comp2Score++;
                  }
                  else
                  {
                      comp1Score++;
                  }
           }
    }
        int z = 0;
        //top left
        if(boardTwo [0][0][z]+boardTwo [0][0][z+1]+boardTwo [0][0][z+2] == 24)
        {
            comp2Score++;
        }
        //top middle
        if(boardTwo [1][0][z]+boardTwo [1][0][z+1]+boardTwo [1][0][z+2] == 24)
        {
            comp2Score++;
        }
        //top right
        if(boardTwo [2][0][z]+boardTwo [2][0][z+1]+boardTwo [2][0][z+2] == 24)
        {
            comp2Score++;
        }
        //middle left
        if(boardTwo [0][1][z]+boardTwo [0][1][z+1]+boardTwo [0][1][z+2] == 24)
        {
            comp2Score++;
        }
        //middle middle
        if(boardTwo[1][1][z]+boardTwo[1][1][z+1]+boardTwo [1][1][z+2] == 24)
        {
            comp2Score++;
        }
        //middle right
        if(boardTwo[2][1][z]+boardTwo[2][1][z+1]+boardTwo [2][1][z+2] == 24)
        {
            comp2Score++;
        }
        //bottom left
        if(boardTwo[0][2][z]+boardTwo[0][2][z+1]+boardTwo [0][2][z+2] == 24)
        {
            comp2Score++;
        }
        //bottom middle
        if(boardTwo[1][2][z]+boardTwo[1][2][z+1]+boardTwo [1][2][z+2] == 24)
        {
            comp2Score++;
        }
        //bottom right
        if(boardTwo[2][2][z]+boardTwo[2][2][z+1]+boardTwo [2][2][z+2] == 24)
        {
            comp2Score++;
        }
        //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)
        {
            comp2Score++;
        }
        //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)
        {
            comp2Score++;
        }
        //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)
        {
            comp2Score++;
        }
        //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)
        {
            comp2Score++;
        }
        //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)
        {
            comp2Score++;
        }
        //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)
        {
            comp2Score++;
        }
        //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)
        {
            comp2Score++;
        }
        //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)
        { 
            comp2Score++;
        }
        //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)
        {
            comp2Score++;
        }
        //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)
        {
            comp2Score++;
        }
        //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)
        {
            comp2Score++;
        }
        //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)
        {
            comp2Score++;
        }
        //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)
        {
            comp2Score++;
        }
        //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)
        {
            comp2Score++;
        }
        //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)
        {
            comp2Score++;
        }
        //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)
        {
            comp2Score++;
        }
        //computer conditions
        //top left
        if(boardTwo[0][0][z]+boardTwo[0][0][z+1]+boardTwo [0][0][z+2] == 27)
        {
            comp1Score++;
        }
        //top middle
        if(boardTwo[1][0][z]+boardTwo[1][0][z+1]+boardTwo [1][0][z+2] == 27)
        {
            comp1Score++;
        }
        //top right
        if(boardTwo[2][0][z]+boardTwo[2][0][z+1]+boardTwo [2][0][z+2] == 27)
        {
            comp1Score++;
        }
        //middle left
        if(boardTwo[0][1][z]+boardTwo[0][1][z+1]+boardTwo [0][1][z+2] == 27)
        {
            comp1Score++;
        }
        //middle middle
        if(boardTwo[1][1][z]+boardTwo[1][1][z+1]+boardTwo [1][1][z+2] == 27)
        {
            comp1Score++;
        }
        //middle right
        if(boardTwo[2][1][z]+boardTwo[2][1][z+1]+boardTwo [2][1][z+2] == 27)
        {
            comp1Score++;
        }
        //bottom left
        if(boardTwo[0][2][z]+boardTwo[0][2][z+1]+boardTwo [0][2][z+2] == 27)
        {
            comp1Score++;
        }
        //bottom middle
        if(boardTwo[1][2][z]+boardTwo[1][2][z+1]+boardTwo [1][2][z+2] == 27)
        {
            comp1Score++;
        }
        //bottom right
        if(boardTwo[2][2][z]+boardTwo[2][2][z+1]+boardTwo [2][2][z+2] == 27)
        {
            comp1Score++;
        }
        //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)
        {
            comp1Score++;
        }
        //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)
        {
            comp1Score++;
        }
        //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)
        {
            comp1Score++;
        }
        //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)
        {
            comp1Score++;
        }
        //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)
        {
            comp1Score++;
        }
        //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)
        {
            comp1Score++;
        }
        //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)
        {
            comp1Score++;
        }
        //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)
        {
            comp1Score++;
        }
        //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)
        {
            comp1Score++;
        }
        //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)
        {
            comp1Score++;
        }
        //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)
        {
            comp1Score++;
        }
        //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)
        {
            comp1Score++;
        }
        //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)
        {
            comp1Score++;
        }
        //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)
        {
            comp1Score++;
        }
        //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)
        {
            comp1Score++;
        }
        //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)
        {
            comp1Score++;
        }
        
        //results
        if(comp2Score<comp1Score)
        {
            return 0; //user lost
        }
        else if(comp2Score > comp1Score)
        {
            return 1; //user won
        }
        else
        {
            return 2; //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";
    }
    
    void tdBoard::comp2Move()
    {
        srand(time(NULL));
        srand ((unsigned)time(NULL));
        int exit = 0;
        int xCoord;
        int yCoord;
        int zCoord;
        do
        {
            xCoord = rand()%3;
            yCoord = rand()%3;
            zCoord = rand()%3;
            
            if(vacancy(xCoord,yCoord,zCoord))
            {
                setX(zCoord,xCoord,yCoord);
                return;
            }
            
            if(vacancy(xCoord,yCoord,zCoord))
            {
                setO(zCoord,xCoord,yCoord);
                exit = 1;
            }
        }while(exit == 0);
    }
    
    //computer move method
    void tdBoard::comp1Move()
    {
        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(compXCoord,compYCoord,compZCoord))
            {
                setO(compZCoord,compXCoord,compYCoord);
                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 playgame()
    {
        tdBoard game;
        int userTurn;
        srand((unsigned)time(NULL));
        userTurn = rand()%2;
        bool contGame = 1;
        
        do
        {
            game.draw();
            if(userTurn == 1)
            {
                game.comp2Move();
                userTurn = 0;
                cout<<"Player 2's (X) move: "<<endl; //replace with player name
    
            }
            else
            {
                game.comp1Move();
                userTurn = 1;
                cout<<"Player 1's (O) move: "<<endl; //replace with player name
            }
            
            if(game.checkFull())
            {
                contGame = 0;
            }
        }
        while(contGame == 1);
        game.draw();
        
        if(game.checkWinner() == 1)
        {
            cout<<"\nPlayer 1 (O) Score: "<<game.comp1Score;
            cout<<"\nPlayer 2 (X) Score: "<<game.comp2Score<<endl;
            cout<<"\nPlayer 2 (X) Won!";
        }
        else if(game.checkWinner() == 0)
        {
            cout<<"\nPlayer 1 (O) Score: "<<game.comp1Score;
            cout<<"\nPlayer 2 (X) Score: "<<game.comp2Score<<endl;
            cout<<"\nPlayer 1 (O) Won!";        
        }
        else if(game.checkWinner() == 2)
        {
            cout<<"\nPlayer 1 (O) Score: "<<game.comp1Score;
            cout<<"\nPlayer 2 (X) Score: "<<game.comp2Score<<endl;
            cout<<"\nIts a tie!";        
        }
        cout<<"\nGame Over!";
        return 0;
    }
    
    int main()
    {
        int i = 0;
        while(i < 10)
        {
            playgame();
            i++;
        }
    }

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,490
    Maybe pass pointers to the player scores to playgame(). E.g.

    Code:
    int playgame(int *player1Score, int *player2Score)
    And set increment them appropriately where you're checking for the winner. E.g.
    Code:
        if(game.checkWinner() == 1)
        {
            cout<<"\nPlayer 1 (O) Score: "<<game.comp1Score;
            cout<<"\nPlayer 2 (X) Score: "<<game.comp2Score<<endl;
            cout<<"\nPlayer 2 (X) Won!";
        }
        *player2Score = *player2Score + 1;
    
           /* etc */
    You'd have to have the variables in main (and init them to 0) of course. You could also use a struct.

    Why is playgame returning an int?

    Also, you should only be calling srand() once in your program, not lots of times like you're doing.

  3. #3
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    169
    I am having a hard time figuring out how to add up all the scores from the 10 games so that I can create an overall score for each player and ultimately a winner. Each time a new game is played the score is restarted to 0 and the process begins again until all games are played.
    Code:
    int main (void)
    {
          int i = 0;
          pair <int , int> scores [10];
          int SumOfScoresOfComp1 = 0;
          int SumOfScoresOfComp2 = 0;
          while(i < 10)
          {
                scores[i] = playgame (); // make playgame () return a pair of integers that are the scores of Comp1 and Comp2
                SumOfScoresOfComp1 += scores[i].first;
                SumOfScoresOfComp2 += scores[i].second;
                i++;
          }
    
          if (SumOfScoresOfComp1 > SumOfScoresOfComp2) cout << "YAY! ONE HAS WON"; // Whatever you want
    }
    Instead of returning 0 in playgame (), make it return the current game score of Comp1 and Comp2 as a pair.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,490
    Quote Originally Posted by Zeus_ View Post

    Instead of returning 0 in playgame (), make it return the current game score of Comp1 and Comp2 as a pair.
    Yeah, this is probably better. I for some reason didn't notice this was c++

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    694
    This code is pretty senseless, actually.
    Why is tdBoard derived from boardGame?
    It's not using any of it's functionality or data at all!
    You could delete 150+ lines and the program would be identical!

    And the srand call(s!) are misplaced.
    There should only be one call to srand and it should be in main before the loop.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  6. #6
    Registered User
    Join Date
    Oct 2019
    Posts
    8
    Quote Originally Posted by Zeus_ View Post
    Code:
    int main (void)
    {
          int i = 0;
          pair <int , int> scores [10];
          int SumOfScoresOfComp1 = 0;
          int SumOfScoresOfComp2 = 0;
          while(i < 10)
          {
                scores[i] = playgame (); // make playgame () return a pair of integers that are the scores of Comp1 and Comp2
                SumOfScoresOfComp1 += scores[i].first;
                SumOfScoresOfComp2 += scores[i].second;
                i++;
          }
    
          if (SumOfScoresOfComp1 > SumOfScoresOfComp2) cout << "YAY! ONE HAS WON"; // Whatever you want
    }
    Instead of returning 0 in playgame (), make it return the current game score of Comp1 and Comp2 as a pair.
    So I've tried adding your code into mine and added
    Code:
    return (game.comp1Score, game.comp2Score);
    to the end of playgame() but now I get [Error] expected primary-expression before 'int' Line 745. I've tried adding a few different things into playgame(here) but I still am unable to get it to work. I'm obviously missing something somewhere but I don't know where. I have never used pair before either.

  7. #7
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    169
    If you expect to return a pair where your return type is int, then obviously you'd get an error. Also, you're using wrong brackets in the return statement.

    Change this:

    Code:
    int playgame()
    to

    Code:
    pair <int , int> playgame()
    Also, comp1score and comp2score are private data members. You can't access them directly so add two getter functions to your class:
    Code:
    int GetComp1Score () { return comp1score; }
    int GetComp2Score () { return comp2score; }
    Change the return statement to:
    Code:
    return { game.GetComp1Score () , game.GetComp2Score () };
    
    // or
    
    return make_pair (game.GetComp1Score () , game.GetComp2Score ());
    EDIT: You must include <utility> to use pair. If you don't want to take the pain of including header files based on what you are using, remove all your includes and just include this: <bits/stdc++.h>. Also, I have a feeling your warnings are not set to their highest levels. Check all warning options in your compiler settings.

    > I have never used pair before either

    Pair in C++ Standard Template Library (STL) - GeeksforGeeks
    Last edited by Zeus_; 1 Day Ago at 02:09 AM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,234
    Quote Originally Posted by Zeus_
    If you don't want to take the pain of including header files based on what you are using, remove all your includes and just include this: <bits/stdc++.h>.
    ... and experience the pain of your code failing to compile because the header does not exist

    Or experience the pain of never having using namespace std; in your code, not even in local scopes, because the risk of unforeseen name collision is too high.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2019
    Posts
    8
    Quote Originally Posted by Zeus_ View Post
    If you expect to return a pair where your return type is int, then obviously you'd get an error. Also, you're using wrong brackets in the return statement.

    Change this:

    Code:
    int playgame()
    to

    Code:
    pair <int , int> playgame()
    Also, comp1score and comp2score are private data members. You can't access them directly so add two getter functions to your class:
    Code:
    int GetComp1Score () { return comp1score; }
    int GetComp2Score () { return comp2score; }
    Change the return statement to:
    Code:
    return { game.GetComp1Score () , game.GetComp2Score () };
    
    // or
    
    return make_pair (game.GetComp1Score () , game.GetComp2Score ());
    EDIT: You must include <utility> to use pair. If you don't want to take the pain of including header files based on what you are using, remove all your includes and just include this: <bits/stdc++.h>. Also, I have a feeling your warnings are not set to their highest levels. Check all warning options in your compiler settings.

    > I have never used pair before either

    Pair in C++ Standard Template Library (STL) - GeeksforGeeks

    Thank you very much for your assistance I got the program to work as intended. Here is an output example:
    3x3x3 Tic Tac Toe-annotation-2019-12-05-130226-png

  10. #10
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    169
    Awesome! Now, as John mentions, a lot of code is not needed. Look at post #8 and try fixing your code...
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  11. #11
    Registered User
    Join Date
    Dec 2017
    Posts
    694
    I'm wondering if he was supposed to use the "boardGame" class in making the new class. I don't think inheritance would be correct, but maybe something like:
    Code:
    class tdBoard {
        boardGame bg[3];
    public:
        // then use bg's functionality here as much as possible
    };
    Maybe not, though.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Tags for this Thread