Thread: Tic Tac Toe Start

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    80

    Tic Tac Toe Start

    I am starting an assignment that involves tic tac toe. Its just a start. can someone look it over and see where I can go from here. The requirements for each function is in the code. Any help is appreciated.

    Code:
    #include<iostream>
    using namespace std;
    
    class Board
        {
        
        public:
        /* (Constructor) This initializes the board, which means it sets all the positions to empty
            and does all else necessary to make the board ready for use  
        */
     
        Board();
        //~Board();
        
        //void Place(char, int, int);
        /* Place: A procedure that take Player P and sets the column and Row of the board to be thr Player
           P if that position is empty. If the position is ocupied, the player loses a turn.
        
        void Place();  // right now testing to make sure things work in it
        
        /* WIN:  A function that returns true if Player P has won the game and false otherwise
        */
        bool Win(char);
    
        /* A function that returns true if the board represents a game in stalemate, and false otherwise */
        bool Stalemate();
    
        int ReturnColumn();
        int ReturnRow();
    
        private:
        int Column;
        int Row;    
        };
    
    int Board::ReturnColumn()
        {
        return Column;
        }
        
    int Board::ReturnRow() 
        {
        return Row;
        }
    
    
    /* 
    bool Board::Win(char P)
        {
    cout << P << "Wins";
        }
    */
         Board::Board()
            {
            //displays the board and what spaces are what
             
            cout << "    1   |   2   |  3 " << endl;
            cout << "    4   |   5   |  6 " << endl;
            cout << "    7   |   8   |  9 " << endl;
            }
    
         
       void Board::Place()
             {   
                int selection=-1;
                int matrix[3][3]={1,2,3,4,5,6,7,8,9};
           
                cin >> Column;
                cin >> Row;
                
                if (Column == 0 && Row == 0)
                    {
                    selection =1;
                    }
                else if (Column ==0 && Row == 1) 
                    {
                    selection =2;
                    }
                else if (Column ==0 && Row== 2)
                    {
                    selection =3;
                    }
                else if (Column ==1 && Row ==0)
                    {
                    selection =4;
                    }
                else if (Column == 1 && Row ==1)
                    {
                    selection =5;
                    }
                else if (Column == 1 && Row ==2)
                    {
                    selection =6;
                    }
                else if (Column == 2 && Row ==0)
                    {
                    selection =7;
                    }
                else if (Column ==2 && Row ==1)
                    {
                    selection =8;
                    }
                else
                    {
                    selection =9;
                    }
                  
                cout << selection;
    
                 switch (selection)
                {
                   case 1:
                   matrix[0][0];
                   cout <<"hello";
                   cout << matrix[0][0];
                   break;
    
                  case 2:
                  matrix[0][1];
                  cout << matrix[0][1];
                  break;
    
                  case 3:
                  matrix[0][3];
                  break;
    
                  case 4:
                  matrix[1][0];
                  break;
    
                  case 5:
                  matrix[1][1];
                  break;
    
                  case 6:
                  matrix[1][2];
                  break;
    
                  case 7:
                  matrix[2][0];
                  break;
    
                  case 8:
                  matrix[2][1];
                  break;
    
                  case 9:
                  matrix[2][2];
                  break;
            } //end 
               
                 
             }//end while
     /*
        winning conditions are 1,2,3   matrix[0][0],matrix[0][1],matrix[0][2]
                                         4,5,6   matrix[1][0],matrix[1][1],matrix[1][2]
                                         7,8,9   matrix[2][0],matrix[2][1],matrix[2][2]
                                         1,4,7   matrix[0][0],matrix[1][0],matrix[2][0]
                                         2,5,8   matrix[0][1],matrix[1][1],matrix[2][1]
                                         3,6,9   matrix[0][2],matrix[1][2],matrix[2][2]
                                         1,5,9   matrix[0][0].matrix[1][1],matrix[2][2]
                                         3,5,7   matrix[0][2],matrix[1][1],matrix[2][0]
     */
    
    
    int main()
        {
        Board check;
        check.Place();
        enum Player {X,O,empty};
    
        system("pause");
        return 0;
        }

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    Code:
    void Board::Place()
    {
        int selection=-1;
       int matrix[3][3]={1,2,3,4,5,6,7,8,9};
    
        cin >> Column;
        cin >> Row;
    You should probably tell the user which one they are inputting. As in:
    Code:
    void Board::Place()
    {
        int selection=-1;
        int matrix[3][3] =
        {
            {1,2,3},
            {4,5,6},
            {7,8,9}
        };
    
        cout << "Column: ";
        cin >> Column;
        cout << "Row: ";
        cin >> Row;
    If your case statements run more than one line you should get into the habit of putting braces around them. Also put a default case in even if all it does is break out of the switch statement.
    Last edited by eaane74; 09-16-2008 at 04:29 PM.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    All of this:
    Code:
    if (Column == 0 && Row == 0)
                    {
                    selection =1;
                    }
                else if (Column ==0 && Row == 1) 
                    {
                    selection =2;
                    }
                else if (Column ==0 && Row== 2)
                    {
                    selection =3;
                    }
                else if (Column ==1 && Row ==0)
                    {
                    selection =4;
                    }
                else if (Column == 1 && Row ==1)
                    {
                    selection =5;
                    }
                else if (Column == 1 && Row ==2)
                    {
                    selection =6;
                    }
                else if (Column == 2 && Row ==0)
                    {
                    selection =7;
                    }
                else if (Column ==2 && Row ==1)
                    {
                    selection =8;
                    }
                else
                    {
                    selection =9;
                    }
    Can be condensed to:
    Code:
    selection = matrix[Column][Row];

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    It is better for a player to be a string rather than a char. And you can prompt for players name when the board is created.

    Here is an idea how to make the program generally.
    1) Have a function display. Have a matrix that will be the board (like board[][]) that will be initialized with spaces. The function display will have as a parameter the matrix. The function will print the values of the matrix in a "nice" way as you already do when you initialize the board. But instead of 1,2, ..., 9 you will print the value of board[][].
    2) Instead of selection you just do board[Row][Column] = "X" if first player or ="0" if second inside function Place. Then place will call display. So when someone places a piece the whole board will be displayed on the screen. You then check if there is a winner, checking the right left, up down squares from the last played piece.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    80

    a little further

    I got a litle further on this tic tac toe

    here is the code i currently have.The rules that i have to follow are posted next to some of the function names its only a really rough draft.
    Also is there any way to post the code without it taking up the whole page, sort of like using a scrolling?

    Code:
    #include<iostream>
    using namespace std;
    
    /* OK, the first problem I see with this is that your board is not properly initialized. 
     You should initialize the board in the construstor.  your matrix would do better as a 
     class variable, instead of only visible in place.  It might help to keep count of the 
     empty squares, too.  Try to incluse the enum given in the problem.  That's the type your
      matrix should be.  Use a loop to cycle through the columns and rows to set all the spaces 
      to "empty".  You don't need column and for as class variables, be cause they are constantly 
      changing.  Let's get the constructor correct, then we can worry about Place.
      
      */
      
    
    class Board
        {
        
        public:
        /* (Constructor) This initializes the board, which means it sets all the positions to empty
            and does all else necessary to make the board ready for use  
        */
     
        Board();
        //~Board();
        
        //void Place(char, int, int);
        /* Place: A procedure that take Player P and sets the column and Row of the board to be thr Player
           P if that position is empty. If the position is ocupied, the player loses a turn.
        */
        
        void Place(int, int);  // right now testing to make sure things work in it
        
        /* WIN:  A function that returns true if Player P has won the game and false otherwise
        */
        bool Win(char);
    
        /* A function that returns true if the board represents a game in stalemate, and false otherwise */
        bool Stalemate();
    
        void DisplayBoard(char DisplayStatus[3][3]);
    
        char ReturnBoard();
        
        char TheCurrentBoard[3][3];
    
        };
    
    char Board::ReturnBoard()
        {
        return TheCurrentBoard[3][3];
        }
    
       void Board::DisplayBoard(char TheBoard[3][3])
        {   
            cout << "The current board status is ";
            
            cout<<"\n\t" << TheBoard[0][0] << " | " << TheBoard[0][1] << " | " << TheBoard[0][2];
            cout<<"\n\t" <<  TheBoard[1][0] << " | " << TheBoard[1][1] << " | " << TheBoard[1][2];
            cout<<"\n\t" <<  TheBoard[2][0] << " | " << TheBoard[2][1] << " | " << TheBoard[2][2];
            cout <<"\n\n\n\n\n\n\n\n";
    
        }
    
    
    /* 
    bool Board::Win(char P)
        {
    cout << P << "Wins";
        }
    */
         Board::Board()
            {
            //displays the board and what spaces are what
            
             int matrix[3][3]={1,2,3,4,5,6,7,8,9};
          
             cout << " Here is the board layout";
             cout << "\n";
             cout << "            Column   Column  Column  " << endl;
             cout << "         Row__0,0__|__0,1__|___0,2__    " << endl;
             cout << "         Row__1,0__|__1,1__|___1,2__     " << endl;
             cout << "         Row__2,0__|__2,1__|___2,2__    " << endl;     
            }
    
         
       void Board::Place(int Column,int Row)    
             {   
              
                
                int matrix[3][3]=
                    {
                      {1,2,3},
                      {4,5,6},
                      {7,8,9}
                    };
                bool player=1;
                char pieceX='X';
                char pieceO='O';
    
                char location[3][3]=
                    {
                        {'A','B','C'},
                        {'D','E','F'},
                        {'G','H','I'}
                    };
    
                cout << " THe board currently";
                
            cout<<"\n\t" << location[0][0] << " | " << location[0][1] << " | " << location[0][2];
            cout<<"\n\t" <<  location[1][0] << " | " << location[1][1] << " | " << location[1][2];
            cout<<"\n\t" <<  location[2][0] << " | " << location[2][1] << " | " << location[2][2];
            cout <<"\n\n\n\n\n\n\n\n";
                  
               
                         
          switch (Column)
          {
             case 0:
                switch (Row)
                {
                   case 0:
                      matrix[0][0];
                                        
                      // also add in if a certain player is valid, matrix [][] = X or 0
                      // like if (P) 
                         
                          if (player)
                          {
                           location[0][0] = 'X';                           
                          }                        
                          else
                              {
                              location[0][0] ='Y';
                              }
                              
                
                    
                      break;
    
    
                   case 1:
                      matrix[0][1];
                      //cout << matrix[0][1];
    
                     
                                        
                      // also add in if a certain player is valid, matrix [][] = X or 0
                      // like if (P) 
                         
                          if (player)
                          {
                           location[0][1] = 'X';                           
                          }                        
                          else
                              {
                              location[0][1] ='Y';
                              }
                      break;
    
    
                   case 2:
                      matrix[0][2];
    
                      if (player)
                          {
                           location[0][2] = 'X';                           
                          }                        
                          else
                              {
                              location[0][2] ='Y';
                              }
                      break;
    
    
                   default:
                      cout<<"Opps!"<<endl;
                      break;
           }             
                break;
    
    
             case 1:
                switch (Row)
                {
                   case 0:
                      matrix[1][0];
    
                      if (player)
                          {
                           location[1][0] = 'X';                           
                          }                        
                          else
                              {
                              location[1][0] ='Y';
                              }
                      break;
    
    
                   case 1:
                      matrix[1][1];
    
                      if (player)
                          {
                           location[1][1] = 'X';                           
                          }                        
                          else
                              {
                              location[1][1] ='Y';
                              }
                      break;
    
    
                   case 2:
                      matrix[1][2];
    
                      if (player)
                          {
                           location[1][2] = 'X';                           
                          }                        
                          else
                              {
                              location[1][2] ='Y';
                              }
                      break;
    
    
                   default:
                      cout<<"Opps!"<<endl;
                      break;
                }              
                break;
    
    
             case 2:
                switch (Row)
                {
                   case 0:
                      matrix[2][0];
    
                      if (player)
                          {
                           location[2][0] = 'X';                           
                          }                        
                          else
                              {
                              location[2][0] ='Y';
                              }
                      break;
    
    
                   case 1:
                      matrix[2][1];
    
                      if (player)
                          {
                           location[2][1] = 'X';                           
                          }                        
                          else
                              {
                              location[2][1] ='Y';
                              }
                      break;
    
    
                   case 2:
                      matrix[2][2];
    
                      if (player)
                          {
                           location[2][2] = 'X';                           
                          }                        
                          else
                              {
                              location[2][2] ='Y';
                              }
                      break;
    
    
                   default:
                      cout<<"Opps!"<<endl;
                      break;
                }              
                break;
    
    
             default:
                cout<<"Opps!"<<endl;
                break;
          } //end 
    
                 cout << " THe board currently";
                
            cout<<"\n\t" << location[0][0] << " | " << location[0][1] << " | " << location[0][2];
            cout<<"\n\t" <<  location[1][0] << " | " << location[1][1] << " | " << location[1][2];
            cout<<"\n\t" <<  location[2][0] << " | " << location[2][1] << " | " << location[2][2];
            cout <<"\n\n";
    
            //change player turn
            //player=!player;
    
       }//end while
         
     /*
        winning conditions are 1,2,3   matrix[0][0],matrix[0][1],matrix[0][2]
                                         4,5,6   matrix[1][0],matrix[1][1],matrix[1][2]
                                         7,8,9   matrix[2][0],matrix[2][1],matrix[2][2]
                                         1,4,7   matrix[0][0],matrix[1][0],matrix[2][0]
                                         2,5,8   matrix[0][1],matrix[1][1],matrix[2][1]
                                         3,6,9   matrix[0][2],matrix[1][2],matrix[2][2]
                                         1,5,9   matrix[0][0].matrix[1][1],matrix[2][2]
                                         3,5,7   matrix[0][2],matrix[1][1],matrix[2][0]
     */
    
    
    int main()
        {
        Board check;
        char playerchoice= 'y'; 
        int ThisColumn;
        int ThisRow;
               
         // should be while a player has not won the game or all the blocks are full repeat the game
        while (playerchoice=='y')    // only a repeat to test the board at the moment
            {
    
                cout << " Enter your selection as shown {Column, Row) ";
                cout << "Please choose the column :" ;
                cin >> ThisColumn; 
               
                cout << "Please choose the Row :";           
                cin >> ThisRow;
    
        check.Place(ThisColumn, ThisRow);
        check.DisplayBoard(check.ReturnBoard())
        
        //check.Stalemate() check to see if there was a stalemate
        //check.Win(P) check to see if a player won 
        cout << "Play again? y or n";
        cin >> playerchoice;
            }
    
        system("pause");
        return 0;
        }

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    80
    I got a little further on this assignment. My question is, how do i use the enum Player? And how can I create player to take turns?

    Code:
    #include<iostream>
    using namespace std;
    
    
    class Board
        {
        
        public:    
        Board();
        
        ~Board();
        
        void Place(char , int, int);  // right now testing to make sure things work in it
        
        bool Win(char);
    
        bool Stalemate(char);
        
        void DisplayBoard();
    
        enum Player{X,O,Empty}; 
        
        char TheCurrentBoard[3][3];  
        };
    
        Board::Board()  //Constructor
            {
             cout << " Here is the board layout";
             cout << "\n";
             cout << "            Column   Column  Column  " << endl;
             cout << "         Row__0,0__|__0,1__|___0,2__    " << endl;
             cout << "         Row__1,0__|__1,1__|___1,2__     " << endl;
             cout << "         Row__2,0__|__2,1__|___2,2__    " << endl;    
            
    
            // u can make a for loop here
           // Initialize the board to set the positions with no values
                TheCurrentBoard[0][0]='-';// or empty
                TheCurrentBoard[0][1]='-';
                TheCurrentBoard[0][2]='-';
                TheCurrentBoard[1][0]='-';
                TheCurrentBoard[1][1]='-';
                TheCurrentBoard[1][2]='-';
                TheCurrentBoard[2][0]='-';
                TheCurrentBoard[2][1]='-';
                TheCurrentBoard[2][2]='-'; 
            }
       
       Board::~Board() {}; //Destructor
    
       void Board::DisplayBoard()
        {            
            cout << " \n The board is currently";            
                
            cout<<"\n\t" << TheCurrentBoard[0][0] << " | " << TheCurrentBoard[0][1] << " | " << TheCurrentBoard[0][2];
            cout<<"\n\t" <<  TheCurrentBoard[1][0] << " | " << TheCurrentBoard[1][1] << " | " << TheCurrentBoard[1][2];
            cout<<"\n\t" <<  TheCurrentBoard[2][0] << " | " << TheCurrentBoard[2][1] << " | " << TheCurrentBoard[2][2];
            cout <<"\n\n";
        }
    
    
    bool Board::Win(char P)    // need to go back and set the diagonals
        
        {            // these set the straights
        for(int i = 0; i < 3; i++)
        {
        if ((TheCurrentBoard[0][i]==P ) && (TheCurrentBoard[1][i]== P) && (TheCurrentBoard[2][i]== P))
              {
                 cout << " You win ";
              }
        else if ((TheCurrentBoard[i][0]== P) && (TheCurrentBoard[i][1]== P) && (TheCurrentBoard[i][2]=='X'))
              {
                 cout << " You win ";
              }
         }
        return true;  // just place holding atm for the program to complile
        }
        
        bool Board::Stalemate(char P)
        {
        return true; // just place holding atm for the program to complile
        }
    
         
    
         
       void Board::Place(char P ,int Column,int Row)    
             {
             
             if (TheCurrentBoard[Row][Column] == 2)
                 {
                 TheCurrentBoard[Row][Column] = 0;  // 0 just place holding a the moment to complile
                 }
    
            
           }
    
    
    
    int main()
        {
        Board check;
        int ThisColumn;
        int ThisRow;
       
        
                                             
                cout << "The current player is "; 
                      
                cout << "\n\n X will go first:" << endl;
                
                cout << "\n\n Enter your selection as shown {Column, Row) ";
                cout << "Please choose the column :" ;
                cin >> ThisColumn; 
               
                cout << "Please choose the Row :";           
                cin >> ThisRow;
                           
                //check.Place(A ,ThisColumn, ThisRow);
                
                //check.DisplayBoard();
       
                         
           
        //check.Stalemate() check to see if there was a stalemate
        //check.Win(P) check to see if a player won 
        system("pause");
        system ("cls");
       
    
        system("pause");
        return 0;
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making Tic Tac Toe Smarter
    By abh!shek in forum C Programming
    Replies: 15
    Last Post: 06-05-2008, 10:43 AM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. Tic Tac Toe... so close...
    By SlayerBlade in forum C Programming
    Replies: 14
    Last Post: 10-10-2005, 08:58 PM
  4. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 09:59 AM
  5. tic tac toe (arggg, I really need help!)
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 10-11-2001, 12:10 PM