Share:
Notifications
Clear all

[Solved] CS304 Assignment 2 Fall 2021


admin
(@admin)
Admin
Joined: 3 years ago
Posts: 6751
Topic starter  

CS304 Assignment 2 Fall 2021 solution idea:

 

Code:

# include <iostream> 
using namespace std; 
class TicTacToe

{ 

   private: 
           
            char TwoDimensionalArray[3][3]; 
            

   public: 

            TicTacToe(); 

            void makeMove(); 

            void printBoard(); 

            int validMove(); 

            int gameStatus(); 

}; 
 
 void TicTacToe::printBoard() 
 
            { 

                  cout<<"  0     1      2"<<endl<<endl;
				 
				  cout<<"0  "<<TwoDimensionalArray[0][0]<<" :  "<<TwoDimensionalArray[0][1]<<" :  "<<TwoDimensionalArray[0][2]<<endl;
				  cout<<" ________ "<<endl;
				  cout<<" : :     ";
				  cout<<endl<<"1  "<<TwoDimensionalArray[1][0]<<" :  "<<TwoDimensionalArray[1][1]<<" :  "<<TwoDimensionalArray[1][2]<<endl;
				  cout<<" ________ "<<endl;
				  cout<<" : :     "<<endl;
				  cout<<"2  "<<TwoDimensionalArray[2][0]<<" :  "<<TwoDimensionalArray[2][1]<<" :  "<<TwoDimensionalArray[2][2]<<endl;
				  cout<<endl;	 

}


      void TicTacToe::makeMove() 

{ 

              validMove();         

} 




         int TicTacToe::validMove() 

{ 

       int PlayerMoveXCoordinates , PlayerMoveYCoordinates , PlayerSelection = 1  , i; 

        char PlayerMovePlace = 'X'; 

  do 

{ 

        PlayerSelection = (PlayerSelection%2) ? 1 : 2; 

         printBoard(); 

         PlayerMovePlace  = (PlayerSelection == 1) ? 'X' : 'O'; 

        cout << "Player " << PlayerMovePlace  << " enter move: "; 

         cin >> PlayerMoveXCoordinates  >> PlayerMoveYCoordinates ;	 
         cout <<endl;
                    PlayerMovePlace  =(PlayerSelection == 1) ? 'X' : 'O'; 



               if 
			     (PlayerMoveXCoordinates == 0 && PlayerMoveYCoordinates == 0  && TwoDimensionalArray[0][0] == ' ') 

               {   

                 TwoDimensionalArray[0][0] = PlayerMovePlace ; 

               } 

 

             else if
			         (PlayerMoveXCoordinates == 0 &&  PlayerMoveYCoordinates == 1 && TwoDimensionalArray[0][1] == ' ') 

               { 

                 TwoDimensionalArray[0][1] = PlayerMovePlace ; 

               }   

            else if
			        (PlayerMoveXCoordinates == 0 &&  PlayerMoveYCoordinates == 2 && TwoDimensionalArray[0][2] == ' ') 

              {   

              TwoDimensionalArray[0][2] = PlayerMovePlace ; 

              } 

            else if     
                (PlayerMoveXCoordinates == 1 &&  PlayerMoveYCoordinates == 0 && TwoDimensionalArray[1][0] == ' ') 

             { 

              TwoDimensionalArray[1][0] = PlayerMovePlace ; 

             } 

              else if
			         (PlayerMoveXCoordinates == 1  && PlayerMoveYCoordinates == 1 && TwoDimensionalArray[1][1] == ' ') 

            { 

             TwoDimensionalArray[1][1] = PlayerMovePlace ; 

            } 

           else if 
		           (PlayerMoveXCoordinates == 1  &&  PlayerMoveYCoordinates == 2  && TwoDimensionalArray[1][2] == ' ') 

           {    

             TwoDimensionalArray[1][2] = PlayerMovePlace ; 

           } 

 

            else if
			      (PlayerMoveXCoordinates == 2  &&   PlayerMoveYCoordinates == 0  && TwoDimensionalArray[2][0] == ' ') 

          { 

          TwoDimensionalArray[2][0] = PlayerMovePlace ; 

          } 

        else if 
              (PlayerMoveXCoordinates == 2  && PlayerMoveYCoordinates == 1 && TwoDimensionalArray[2][1] == ' ') 

              { 

              TwoDimensionalArray[2][1] = PlayerMovePlace ; 

              } 

             else if 
			        (PlayerMoveXCoordinates == 2 &&  PlayerMoveYCoordinates == 2 && TwoDimensionalArray[2][2] == ' ') 

                  {  

                   TwoDimensionalArray[2][2] = PlayerMovePlace ; 

                  } 


                    i=gameStatus(); 

 

                       PlayerSelection++; 

                    }while(i==-1); 

                   printBoard(); 

                if(i==1){ 

    	      --PlayerSelection; 

        
             cout<<endl<<"Player  win"<<endl;
        
        

                    for(int Row = 0; Row < 3; Row++) 

        { 

          for(int Colum = 0; Colum < 3; Colum++) 

      {  

          TwoDimensionalArray[Row][Colum] = ' '; 

      } 

} 

} 

         

    else 

    {   
    
    cout<<endl<<"Game Draw"<<endl;

      

       for(int R = 0; R < 3; R++) 

      { 

        for(int C = 0; C < 3; C++) 

{ 

         TwoDimensionalArray[R][C] = ' '; 

} 

} 

} 

} 

 

        TicTacToe::TicTacToe() 

   { 

     for(int row = 0; row < 3; row++) 

    { 

     for(int Col = 0; Col < 3; Col++) 

 { 

      TwoDimensionalArray[row][Col] = ' '; 

} 

} 

} 



int TicTacToe::gameStatus() 

{ 

     if
     (TwoDimensionalArray[0][0] == TwoDimensionalArray[0][1] && TwoDimensionalArray[0][1] ==TwoDimensionalArray[0][2] && TwoDimensionalArray[0][0] != ' ') 

       {
       	return 1; 
	   }

        

     if 
	      (TwoDimensionalArray[1][0] == TwoDimensionalArray[1][1] && TwoDimensionalArray[1][1] == TwoDimensionalArray[1][2] && TwoDimensionalArray[1][0] != ' ') 

           {
       	     return 1; 
	       }

         

     if 
	      (TwoDimensionalArray[2][0] == TwoDimensionalArray[2][1] && TwoDimensionalArray[2][1] == TwoDimensionalArray[2][2] && TwoDimensionalArray[2][0] != ' ') 

 
             {
       	        return 1; 
	         }
        
      if 
	      (TwoDimensionalArray[0][0] == TwoDimensionalArray[1][0] && TwoDimensionalArray[1][0] == TwoDimensionalArray[2][0] && TwoDimensionalArray[0][0] != ' ') 

 
            {
       	     return 1; 
	        }
  
     if 
	      (TwoDimensionalArray[0][1] == TwoDimensionalArray[1][1] && TwoDimensionalArray[1][1] == TwoDimensionalArray[2][1] && TwoDimensionalArray[0][1] != ' ') 

 
             {
       	      return 1; 
	         }

     if
	      (TwoDimensionalArray[0][2] == TwoDimensionalArray[1][2] && TwoDimensionalArray[1][2] == TwoDimensionalArray[2][2] && TwoDimensionalArray[0][2] != ' ') 

             {
           	return 1; 
	         } 

    if 
	      (TwoDimensionalArray[0][0] == TwoDimensionalArray[1][1] && TwoDimensionalArray[1][1] == TwoDimensionalArray[2][2] && TwoDimensionalArray[0][0] != ' ') 

            {
       	      return 1; 
	        }

     if 
	       (TwoDimensionalArray[0][2] == TwoDimensionalArray[1][1] && TwoDimensionalArray[1][1] == TwoDimensionalArray[2][0] && TwoDimensionalArray[0][2] != ' ') 

             {
       	        return 1; 
	         } 



     if 
	        (TwoDimensionalArray[0][0] != ' ' && TwoDimensionalArray[0][1] != ' ' && TwoDimensionalArray[0][2] != ' '  

                    && TwoDimensionalArray[1][0] != ' ' && TwoDimensionalArray[1][1] != ' ' && TwoDimensionalArray[1][2] != ' '  

                  && TwoDimensionalArray[2][0] != ' ' && TwoDimensionalArray[2][1] != ' ' && TwoDimensionalArray[2][2] != ' ') 

                {
       	             return 1; 
	           }
                 
 
    else 

        {
       	    return -1; 
	     }
		
		

} 

 

        int main() 

              { 

 
                   TicTacToe TwoHumanPlayGame; 

                   TwoHumanPlayGame.makeMove();
 
                    return 0; 


                }

Students kindly share assignment files in relevant subject timely for discussion/solution.
or directly share with us " Click here"
QueryVU Telegram Groups subject wise Join Now
QueryVU WhatsApp groups subject wise Join Now


Quote
Share: