Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to play the game isolation. Game Instructions There are two play

ID: 3809056 • Letter: W

Question

Write a program to play the game isolation. Game Instructions There are two players "x" and "0". Either player can go first Each player can move horizontal, vertical, or diagonal, 1 or more spaces The space the player leaves is now blocked, and neither player can move to that space anymore A player cannot jump over a blocked space, or the other player The game is over when a player has no more legal moves. The player that runs out of moves to make loses. The goal is to block in your opponent, so they have nowhere left to move. Guidelines The start state is the image above Your program can be "X" or "O" You program or the human should be able to go first Yout program should be written in C++. Your program should take no more than 30 seconds to move Game continues to play until someone wins Turn in a Word doc that explains the algorithm (s) that was/were used to find the solution

Explanation / Answer

#include<iostream>
using namespace std;
class Game
{
   char a[4][4];
   public:
   Game()
   {
       for(int i=0;i<4;i++)
       {
           for(int j=0;j<4;j++)
           {
               a[i][j]='n';
          
           }
       }
           a[0][0]='X';
           a[3][3]='O'; //setting up the intial positions of the players
   }
   void info()
   {
       cout<<endl<<"there are two players one player being the computer and other being the user";
       cout<<endl<<"the first player is represented by X";
       cout<<endl<<"the user is represented by O";
       cout<<endl<<"the blocked spaces are represented by character 'b' ";
       cout<<endl<<"the unblocked spaces are represented by n";
       cout<<endl<<"after each computer move and user move the pattern will be displayed";
   }
   void display()
   {
       for(int i=0;i<4;i++)
       {
           cout<<endl;
           for(int j=0;j<4;j++)
           {
               cout<<" "<<a[i][j]<<" ";
           }
       }
   }
   int usermove(char p)
   {
       int opt;
       cout<<endl<<"choose uah direction of move";
       cout<<endl<<"press 1 to move in horizontal direction ";
       cout<<endl<<"press 2 to move in vertical direction ";
       cout<<endl<<"press 3 to move in diagonal direction ";
       cin>>opt;
       switch(opt)
       {
           int spaces;           //no. of spaces user wants to move
           int row,col;       //current row of 'O' and current column of 'X'
           cout<<endl<<"enter the number of spaces you want to move ";
           cin>>spaces;
           for(int i=0;i<4;i++)
           {
               for(int j=0;j<4;j++)
               {
                   if(a[i][j]==p)
                   {
                       row=i;
                       col=j;  
                   }
               }
           }
      
           case 1:
                   char ch;
                  cout<<"press R to move right and press L to move left";
                   cin>>ch;
                   if(ch=='R')
                   {
                       int i;                                                   /*if the player choses to move right*/
                       for( i=row;spaces>0 && i<4 ;i++)
                       {
                           spaces--;
                           if(a[i][col]!='n')
                           {                                               /*checks if the player had chosen a correct move or not*/
                               return 0;
                           }  
                               else
                           {
                           a[i][col]='b';                                   /*if the cosen move is correct, the player is allowed to move to the desired position
                                                                           by blocking the path which he leaves*/
                       }
                       }
                       a[i][col]=p;
                       return 1;
                   }
                       if(ch=='L')
                   {
                       int i;
                       for( i=row;spaces>0;i--)
                       {
                           spaces--;
                           if(a[i][col]!='n')
                           {
                               return 0;
                           }  
                       }
                       a[i][col]=p;
                       return 1;
                   }
               break;
       case 2:
           char ch1;
           cout<<"press U to move up and D to move down ";
           cin>>ch;
           if(ch1=='U')
           {
               int j;
               for( j=col;spaces>0 && j<4;j++)
               {
                   spaces--;
                   if(a[row][j]!='n')
                   {
                       return 0;
                   }
                   else
                   {
                       a[row][j]='b';
                   }
               }
               a[row][j]=p;
               return 1;
               }  
               if(ch1=='D')
           {
               int j;
               for(j=col;spaces>0 && j<4;j--)
               {
                   spaces--;
                   if(a[row][j]!='n')
                   {
                       return 0;
                   }
                   else
                   {
                       a[row][j]='b';
                   }
               }
               a[row][j]=p;
               return 1;
               }
               break;  
          
           case 3:
               char ch2;
               cout<<endl<<"enter U to move diagonally UP and D to move diagonally down";
               cin>>ch;
               if(row+spaces<4 &&col+spaces<4)
               {
                   int i,j;
                   if(ch2=='U')
                   {
          
                   for( i=row,j=col;spaces>0;)
                   {
                       spaces--;
                       if(a[i+1][j+1]!= 'n')
                       {
                           return 0;
                       }
                       else
                       {
                           a[i][j]='b';
                           i++;
                           j++;
                          
                       }
                   }
                   a[i][j]=p;
                   return 1;
               }
                   if(ch2=='D')
                   {
          
                   for( i=row,j=col;spaces>0;)
                   {
                       spaces--;
                       if(a[i-1][j-1]!= 'n')
                       {
                           return 0;
                       }
                       else
                       {
                           a[i][j]='b';
                           i--;
                           j--;
                          
                       }
                   }
                   a[i][j]=p;
               return 1;
               }
       }
          
           break;
       }
   }
   int notEnd(char p)
   {
       int row,col;                                   // checks if the player has a chnace of any move and returns 1 if he has a chnace else 0(end of the game)
       for(int i=0;i<4;i++)
           {
               for(int j=0;j<4;j++)
               {
                   if(a[i][j]==p)
                   {
                       row=i;
                       col=j;  
                   }
               }
           }
           if(a[row+1][col+1] == 'n' ||a[row+1][col]== 'n' ||a[row][col+1]=='n')
           {
               return 1;
           }
           if(a[row-1][col]== 'n' ||a[row-1][col-1]=='n'||a[col-1][row]=='n')
           {
               return 1;
           }
           else
           {
               return 0;
           }
      
       }
  
};
main()
{
   Game g;
   g.display();
   g.info();
   while(true)
   {
   if(g.notEnd(X)== 0)
   {
  
       cout<<endl<<"O is the winner";
           exit(0);
   }
   else
   {
  
   if(g.usermove(X)==0)
   {
       cout<<"please enter a valid move ";
      
   }
   }
   if(g.notEnd(O)==0)
   {
       cout<<endl<<"X is the winner";
       exit(0);
   }
   else
   {
  
   if(g.usermove(O)==0)
   {
       cout<<"please enter a valid move";
   }
   }
   }
  
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote