1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

c++ help with TicTacToe

Discussion in 'Meet and Greet' started by sharan93, Apr 11, 2012.

  1. sharan93

    sharan93 New Member

    Joined:
    Apr 11, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hey I want the game board for the first code in my second code, help please, could some tell me in details how do I add this board. and My second displays 1 and 2 instead of x and o , how do I correct that.
    first code: human vs human
    HTML:
    #include<iostream>#include<string>#include"110ct.h"using namespace std;class TicTacToe { private: 	ColourController ct;    ColourController cls;     CursorController crs;    char Board[3][3];     int play();     void playermove();     int turn(int);     int check (int, int);     void boardProgress(char x[][3]);     int Winner(char x[][3]);     int player1, player2, draw, player,winner,done;     int row, col;  public:     int y, z, t, s;     void intBoard();     int Statistics(int);      char playAgain(char); }playBall;  int main() {     playBall.y = 0,playBall.z = 0,playBall.t = 0,playBall.s = 0;     int Winner = 0; 	ColourController ct;    ColourController cls;     cls.setForeground(Aqua);        cout << " welcome to TIC TAC TOE" <<endl;      char done = false;      while (!done)         {         ColourController cls;         cls.setForeground(Aqua);        cout << " Good Luck!"<< endl;                                  playBall.intBoard();          done = playBall.playAgain(done); }  cout << "Thank you for my playing my game"<< endl; system ("pause"); return 0; } void TicTacToe::intBoard() {     done = false;     winner = 0;     player = 2;     player1 = 1;     player2 = 2;     draw = 3;     int i,j;     for (j=0;j<3;j++)     {         for (i=0;i<3;i++)         {             Board[j][i]=0;         }     }     play(); }  int TicTacToe::play() {     int done = false;     boardProgress(Board);     while(!done)     {         playermove();         boardProgress(Board);         done = Winner(Board);     }     Statistics(winner);     return 0; } void TicTacToe::playermove() {     int answer = false;     while (answer == false)     {         cout <<"Row: ";         cin >> row;         row--;         cout <<"Column: ";         cin >> col;         col--;         answer = check(row,col);     }     player = turn(player);     if (player == 1)         Board[row][col] = 'X';     else if (player == 2)         Board[row][col] = 'O';     else         cout<< "Failed."; } int TicTacToe::check(int row, int col) {     if(Board[row][col] == 0)     {         return true;     }     else if (Board[row][col] != 0)     {         cout <<"You can't go there"<< endl;         return false;     }     else     {         cout<< "Going through Check";         Winner(Board);     }     return false; }  int TicTacToe::turn(int player) {     switch(player)     {     case 1: player = 2;         {         cout<<"It's Player 1's turn"<< endl;         break;         }     case 2: player = 1;         {         cout<<"It's Player 2's turn"<< endl;         break;         }     }     return player; }  int TicTacToe::Winner(char x[][3]) {     winner = 0;     int count = 0;     int a = row;     int b = col;     for (a=0;a<3;a++)     {         for (b=0;b<3;b++)         {             if (Board[a][b]==0)             {                 count++;             }         }     }     if (count > 0)     {         int row, col, r, c, d, r1, c1, d1;         for (row=0; row<3; row++)         {             r=0;             r1=0;              for (col=0; col<3; col++)             { 			                if(x[row][col]=='X')                     r++;                 if(x[row][col]=='O')                     r1++;                 if (r1==3)                 {                      winner=2;                 }                 if (r==3)                 {                        winner=1;                 }             }         }         for (col=0; col<3; col++)         {             r=0;             r1=0;             for (row=0; row<3; row++)             {                     if(x[row][col]=='X')                     r++;                 if(x[row][col]=='O')                     r1++;                 if (r1==3)                 {                        winner=2;                 }                 if (r==3)                 {                     winner=1;                     }             }         }         if (x[0][0]=='X' && x[1][1]=='X' && x[2][2]=='X')         {             winner=1;         }         else if (x[0][0]=='O' && x[1][1]=='O' && x[2][2]=='O')         {             winner=2;         }         else if (x[2][0]=='X' && x[1][1]=='X' && x[0][2]=='X')         {             winner=1;         }         else if (x[2][0]=='O' && x[1][1]=='O' && x[0][2]=='O')         {             winner=2;         }     }     else if (count == 9)     {         cout << "Its a draw" << endl;         winner = 3;     }        else     {         cout<< "next player's turn"<< endl;     }     if (winner > 0)     {         done = true;     } return done; }  int TicTacToe::Statistics(int winner)     {     playBall.y++;     switch(winner)     {     case 1: z++;         {         break;         }     case 2: t++;         {         break;         }     case 3: s++;         {         break;         }     }     cout<<" Number of games you have played: " << y;     cout<<" Number of games Player 1 has won: " << z << " games.";      cout<<" Number of games Player 2 has won: " << t << " games.";     cout<<" Number have draws you had: " << s << " draws.";     return 0; }  char TicTacToe::playAgain(char done) {     cout <<"Would you like to play again"<< endl;     cout <<"Y/N: " << endl;     cin >> done;     if(done == 'Y' || done == 'y')     {          done = false;     }     else     {         done = true;     }     return done; } void TicTacToe::boardProgress(char x[][3]) {	cls.setForeground(Purple);    cout << "      |     |     \n";     cout << "   " << x[0][0] << "  |  " << x[0][1] << "  |  " << x[0][2] <<"  \n";     cout << " _____|_____|_____\n";     cout << "      |     |     \n";     cout << "   " << x[1][0] << "  |  " << x[1][1] << "  |  " << x[1][2] <<"  \n";     cout << " _____|_____|_____\n";     cout << "      |     |     \n";     cout << "   " << x[2][0] << "  |  " << x[2][1] << "  | " << x[2][2] <<"  \n";     cout << "      |     |     \n"; } 
    second code: human vs computer:
    HTML:
    #include <string>#include <iostream>#include"110ct.h"//Use standard namespace to eliminate typing of std::using namespace std;/* !-------- Declare Classes Here --------! */class TicTacToe{   private:      int Grid[9];      int Player;      int TurnCounter;      int Players;      int PlayerType[20];   public:      TicTacToe();      void PrintTurn();      int PlayerHuman();      void HumanMove();      void ComputerMove();      void DrawBoard();      int  Winner();      int  FullBoard();      void NextTurn();}; //End of the Class Declaration/* !-------- Develop Methods Here --------! */TicTacToe::TicTacToe(){    srand(time(0)); //Get a Random Number    Player = 1; //Determines who starts first    TurnCounter = 0; //Initializes the turn counter        //Set up New Players when game begins    Players = 2;    PlayerType[1] = 1; //PlayerType[Player] is the Human User(1)    PlayerType[2] = 0; //PlayerType[Player] is the Computer (0)    for (int i = 0; i < 9; i++)    {       Grid[i] = 0;    }}void TicTacToe::DrawBoard(){	ColourController cls;	cls.setForeground(Red);   cout << endl        << Grid[0] << " | " << Grid[1] << " | " << Grid[2]        << "\n---------\n"        << Grid[3] << " | " << Grid[4] << " | " << Grid[5]        << "\n---------\n"        << Grid[6] << " | " << Grid[7] << " | " << Grid[8]        << endl;} //End Drawing the TicTacToe Boardvoid TicTacToe::PrintTurn(){   cout << "\nPlayer " << Player << "'s turn.\n";} //End Printing Turnvoid TicTacToe::NextTurn(){   TurnCounter++;   if (++Player > Players)   {      Player = 1;   }} //End Next Turnint TicTacToe::PlayerHuman(){   return PlayerType[Player];} //End Determination of A Human Playervoid TicTacToe::HumanMove(){	ColourController cls;	cls.setBackground(Blue);	cls.setForeground(Aqua);   cout << " Enter your box number please: ";   int Move;   do   {      cin >> Move;      Move--; // For user to enter 1-9 instead of 0-8   }   while (Move < 0 || Move > 8 || Grid[Move] != 0);   Grid[Move] = Player;} //End the Human Movevoid TicTacToe::ComputerMove(){   int Move;   do   {      Move  = rand() % 9; //Not really a Smart AI, Just picks a random move   }   while (Move < 0 || Move > 8 || Grid[Move] != 0);   Grid[Move] = Player;} //End the Computer Moveint TicTacToe::Winner(){   int i = 0;   int Board[8][3] = {	                	   {0,1,2},       {3,4,5},       {6,7,8},       {0,3,6},       {1,4,7},       {2,5,8},       {0,4,8},       {2,4,6}   }; //List of all possible winning outcomes   for ( i = 0; i < 8; i++) //Loop for all possible winning outcomes   {      if ((Grid[Board[i][0]] == Grid[Board[i][1]]) && (Grid[Board[i][1]] == Grid[Board[i][2]]) && Grid[Board[i][0]] != 0 )      {         cout << "Player " << Grid[Board[i][0]]              <<  " IS THE WINNER!" <<endl;         system("pause");         return 1;  //Returns a true value (1 with the bool data type).      }   }   return 0; //Else a winner has not been found yet, and return a false (0) value} //End Checking the Winnerint TicTacToe::FullBoard(){   if (TurnCounter == 9)   {      cout << "DRAW" <<endl;      return 1;   }   else   {      return 0;   }} //End Checking for a Full Boardint main(void){   TicTacToe game;   ColourController cls;   cls.setForeground(Green);   cout <<"Welcome! Below is the board to play this game, you must press the number on your keyboard to chose your box!" << endl;       cout    << " 1 | 2 | 3 " << endl;    cout    << "-----------" << endl;    cout    << " 4 | 5 | 6 " << endl;    cout    << "-----------" << endl;    cout    << " 7 | 8 | 9 " << endl;           game.DrawBoard();      do   {      game.PrintTurn();      if (game.PlayerHuman()) //If it is the Human turn, allow the human to move      {         game.HumanMove();      }      else //Else if it is not the Human turn, it is the computer's turn      {         game.ComputerMove();      }            game.DrawBoard();      game.NextTurn();    }    while (!game.Winner() && !game.FullBoard()); //Execute the code in the do loop while there is no winner                                                     //And the game board is not full    return 0;}
     
  2. sharan93

    sharan93 New Member

    Joined:
    Apr 11, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Sorry As I am new to this din't realise how to post a code here they are :
    first one
    Code:
    #include<iostream>
    #include<string>
    #include"110ct.h"
    using namespace std;
    
    class TicTacToe 
    { 
    private: 
    	ColourController ct;
        ColourController cls; 
        CursorController crs;
        char Board[3][3]; 
        int play(); 
        void playermove(); 
        int turn(int); 
        int check (int, int); 
        void boardProgress(char x[][3]); 
        int Winner(char x[][3]); 
        int player1, player2, draw, player,winner,done; 
        int row, col; 
     
    public: 
        int y, z, t, s; 
        void intBoard(); 
        int Statistics(int); 
     
        char playAgain(char); 
    }playBall; 
     
    int main() 
    { 
        playBall.y = 0,playBall.z = 0,playBall.t = 0,playBall.s = 0; 
        int Winner = 0; 
    	ColourController ct;
        ColourController cls; 
        cls.setForeground(Aqua);
            cout << " welcome to TIC TAC TOE" <<endl; 
     
        char done = false; 
     
        while (!done) 
            { 
            ColourController cls; 
            cls.setForeground(Aqua);
            cout << " Good Luck!"<< endl;
                     
     
            
            playBall.intBoard(); 
     
        
        done = playBall.playAgain(done); 
    } 
     
    
    cout << "Thank you for my playing my game"<< endl; 
    system ("pause"); 
    return 0; 
    } 
    void TicTacToe::intBoard() 
    { 
        done = false; 
        winner = 0; 
        player = 2; 
        player1 = 1; 
        player2 = 2; 
        draw = 3; 
        int i,j; 
        for (j=0;j<3;j++) 
        { 
            for (i=0;i<3;i++) 
            { 
                Board[j][i]=0; 
            } 
        } 
        play(); 
    } 
     
    int TicTacToe::play() 
    { 
        int done = false; 
        boardProgress(Board); 
        while(!done) 
        { 
            playermove(); 
            boardProgress(Board); 
            done = Winner(Board); 
        } 
        Statistics(winner); 
        return 0; 
    } 
    void TicTacToe::playermove() 
    { 
        int answer = false; 
        while (answer == false) 
        { 
            cout <<"Row: "; 
            cin >> row; 
            row--; 
            cout <<"Column: "; 
            cin >> col; 
            col--; 
            answer = check(row,col); 
        } 
        player = turn(player); 
        if (player == 1) 
            Board[row][col] = 'X'; 
        else if (player == 2) 
            Board[row][col] = 'O'; 
        else 
            cout<< "Failed."; 
    } 
    int TicTacToe::check(int row, int col) 
    { 
        if(Board[row][col] == 0) 
        { 
            return true; 
        } 
        else if (Board[row][col] != 0) 
        { 
            cout <<"You can't go there"<< endl; 
            return false; 
        } 
        else 
        { 
            cout<< "Going through Check"; 
            Winner(Board); 
        } 
        return false; 
    } 
     
    int TicTacToe::turn(int player) 
    { 
        switch(player) 
        { 
        case 1: player = 2; 
            { 
            cout<<"It's Player 1's turn"<< endl; 
            break; 
            } 
        case 2: player = 1; 
            { 
            cout<<"It's Player 2's turn"<< endl; 
            break; 
            } 
        } 
        return player; 
    } 
     
    int TicTacToe::Winner(char x[][3]) 
    { 
        winner = 0; 
        int count = 0; 
        int a = row; 
        int b = col; 
        for (a=0;a<3;a++) 
        { 
            for (b=0;b<3;b++) 
            { 
                if (Board[a][b]==0) 
                { 
                    count++; 
                } 
            } 
        } 
        if (count > 0) 
        { 
            int row, col, r, c, d, r1, c1, d1; 
            for (row=0; row<3; row++) 
            { 
                r=0; 
                r1=0; 
     
                for (col=0; col<3; col++) 
                { 
    			
                    if(x[row][col]=='X') 
                        r++; 
                    if(x[row][col]=='O') 
                        r1++; 
                    if (r1==3) 
                    {  
                        winner=2; 
                    } 
                    if (r==3) 
                    {    
                        winner=1; 
                    } 
                } 
            } 
            for (col=0; col<3; col++) 
            { 
                r=0; 
                r1=0; 
                for (row=0; row<3; row++) 
                { 
                        if(x[row][col]=='X') 
                        r++; 
                    if(x[row][col]=='O') 
                        r1++; 
                    if (r1==3) 
                    {    
                        winner=2; 
                    } 
                    if (r==3) 
                    { 
                        winner=1; 
                        } 
                } 
            } 
            if (x[0][0]=='X' && x[1][1]=='X' && x[2][2]=='X') 
            { 
                winner=1; 
            } 
            else if (x[0][0]=='O' && x[1][1]=='O' && x[2][2]=='O') 
            { 
                winner=2; 
            } 
            else if (x[2][0]=='X' && x[1][1]=='X' && x[0][2]=='X') 
            { 
                winner=1; 
            } 
            else if (x[2][0]=='O' && x[1][1]=='O' && x[0][2]=='O') 
            { 
                winner=2; 
            } 
        } 
        else if (count == 9) 
        { 
            cout << "Its a draw" << endl; 
            winner = 3; 
        }    
        else 
        { 
            cout<< "next player's turn"<< endl; 
        } 
        if (winner > 0) 
        { 
            done = true; 
        } 
    return done; 
    } 
     
    int TicTacToe::Statistics(int winner)     
    { 
        playBall.y++; 
        switch(winner) 
        { 
        case 1: z++; 
            { 
            break; 
            } 
        case 2: t++; 
            { 
            break; 
            } 
        case 3: s++; 
            { 
            break; 
            } 
        } 
        cout<<" Number of games you have played: " << y; 
        cout<<" Number of games Player 1 has won: " << z << " games.";  
        cout<<" Number of games Player 2 has won: " << t << " games."; 
        cout<<" Number have draws you had: " << s << " draws."; 
        return 0; 
    } 
     
    char TicTacToe::playAgain(char done) 
    { 
        cout <<"Would you like to play again"<< endl; 
        cout <<"Y/N: " << endl; 
        cin >> done; 
        if(done == 'Y' || done == 'y') 
        { 
             done = false; 
        } 
        else 
        { 
            done = true; 
        } 
        return done; 
    } 
    void TicTacToe::boardProgress(char x[][3]) 
    
    {
    	cls.setForeground(Purple);
    
        cout << "      |     |     \n"; 
        cout << "   " << x[0][0] << "  |  " << x[0][1] << "  |  " << x[0][2] <<"  \n"; 
        cout << " _____|_____|_____\n"; 
        cout << "      |     |     \n"; 
        cout << "   " << x[1][0] << "  |  " << x[1][1] << "  |  " << x[1][2] <<"  \n"; 
        cout << " _____|_____|_____\n"; 
        cout << "      |     |     \n"; 
        cout << "   " << x[2][0] << "  |  " << x[2][1] << "  | " << x[2][2] <<"  \n"; 
        cout << "      |     |     \n"; 
    } 
    
    
    second one
    Code:
    #include <string>
    #include <iostream>
    #include"110ct.h"
    //Use standard namespace to eliminate typing of std::
    using namespace std;
    
    /* !-------- Declare Classes Here --------! */
    
    class TicTacToe
    {
       private:
          int Grid[9];
          int Player;
          int TurnCounter;
          int Players;
          int PlayerType[20];
       public:
          TicTacToe();
          void PrintTurn();
          int PlayerHuman();
          void HumanMove();
          void ComputerMove();
          void DrawBoard();
          int  Winner();
          int  FullBoard();
          void NextTurn();
    }; //End of the Class Declaration
    
    /* !-------- Develop Methods Here --------! */
    
    TicTacToe::TicTacToe()
    {
        srand(time(0)); //Get a Random Number
        Player = 1; //Determines who starts first
        TurnCounter = 0; //Initializes the turn counter
            
    //Set up New Players when game begins
        Players = 2;
        PlayerType[1] = 1; //PlayerType[Player] is the Human User(1)
        PlayerType[2] = 0; //PlayerType[Player] is the Computer (0)
    
        for (int i = 0; i < 9; i++)
        {
           Grid[i] = 0;
        }
    }
    
    void TicTacToe::DrawBoard()
    {
    	ColourController cls;
    	cls.setForeground(Red);
       cout << endl
            << Grid[0] << " | " << Grid[1] << " | " << Grid[2]
            << "\n---------\n"
            << Grid[3] << " | " << Grid[4] << " | " << Grid[5]
            << "\n---------\n"
            << Grid[6] << " | " << Grid[7] << " | " << Grid[8]
            << endl;
    } //End Drawing the TicTacToe Board
    
    void TicTacToe::PrintTurn()
    {
       cout << "\nPlayer " << Player << "'s turn.\n";
    } //End Printing Turn
    
    void TicTacToe::NextTurn()
    {
       TurnCounter++;
    
       if (++Player > Players)
       {
          Player = 1;
       }
    } //End Next Turn
    
    int TicTacToe::PlayerHuman()
    {
       return PlayerType[Player];
    } //End Determination of A Human Player
    
    void TicTacToe::HumanMove()
    {
    	ColourController cls;
    	cls.setBackground(Blue);
    	cls.setForeground(Aqua);
       cout << " Enter your box number please: ";
       int Move;
    
       do
       {
          cin >> Move;
          Move--; // For user to enter 1-9 instead of 0-8
       }
       while (Move < 0 || Move > 8 || Grid[Move] != 0);
       Grid[Move] = Player;
    } //End the Human Move
    
    void TicTacToe::ComputerMove()
    {
       int Move;
    
       do
       {
          Move  = rand() % 9; //Not really a Smart AI, Just picks a random move
       }
       while (Move < 0 || Move > 8 || Grid[Move] != 0);
       Grid[Move] = Player;
    } //End the Computer Move
    
    int TicTacToe::Winner()
    {
       int i = 0;
       int Board[8][3] = {
    	                
    	   {0,1,2},
           {3,4,5},
           {6,7,8},
           {0,3,6},
           {1,4,7},
           {2,5,8},
           {0,4,8},
           {2,4,6}
       }; //List of all possible winning outcomes
    
       for ( i = 0; i < 8; i++) //Loop for all possible winning outcomes
       {
          if ((Grid[Board[i][0]] == Grid[Board[i][1]]) && (Grid[Board[i][1]] == Grid[Board[i][2]]) && Grid[Board[i][0]] != 0 )
          {
             cout << "Player " << Grid[Board[i][0]]
                  <<  " IS THE WINNER!" <<endl;
    
             system("pause");
             return 1;  //Returns a true value (1 with the bool data type).
          }
       }
       return 0; //Else a winner has not been found yet, and return a false (0) value
    } //End Checking the Winner
    
    int TicTacToe::FullBoard()
    {
       if (TurnCounter == 9)
       {
          cout << "DRAW" <<endl;
          return 1;
       }
       else
       {
          return 0;
       }
    } //End Checking for a Full Board
    
    int main(void)
    {
    
       TicTacToe game;
       ColourController cls;
       cls.setForeground(Green);
       cout <<"Welcome! Below is the board to play this game, you must press the number on your keyboard to chose your box!" << endl;
       
        cout    << " 1 | 2 | 3 " << endl;
        cout    << "-----------" << endl;
        cout    << " 4 | 5 | 6 " << endl;
        cout    << "-----------" << endl;
        cout    << " 7 | 8 | 9 " << endl;
            
       game.DrawBoard();
       
       do
       {
          game.PrintTurn();
          if (game.PlayerHuman()) //If it is the Human turn, allow the human to move
          {
             game.HumanMove();
          }
          else //Else if it is not the Human turn, it is the computer's turn
          {
             game.ComputerMove();
          }
          
          game.DrawBoard();
          game.NextTurn();
        }
        while (!game.Winner() && !game.FullBoard()); //Execute the code in the do loop while there is no winner
                                                         //And the game board is not full
        return 0;
    }
    
    
    
     

Share This Page