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

This uses Java to code: Thanks!! Create a class called WhackAMole . It contains

ID: 3840500 • Letter: T

Question

This uses Java to code: Thanks!!

Create a class called WhackAMole.

It contains three integer instance variables called score, molesLeft, and attempts. Make one more instance variable called moleGrid which is a 2-dimensional array of chars.


We will also have you create the following methods in this class.


WhackAMole(int numAttempts, int gridDimension) - Constructor for the game, specifies total number of whacks allowed, and the grid dimension.


boolean place(int x, int y) – Given a location, place a mole at that location. Return true if you can. (Also update number of moles left.)            


void whack(int x, int y) - Given a location, take a whack at that location. If that location contains a mole, the score and molesLeft is updated. If that location does not contain a mole, the number of attempts goes up.


void printGridToUser() – Print the grid without showing where the moles are. For every spot that has recorded a “whacked mole,” print out a W, or * otherwise.


void printGrid() - Print the grid completely. This is effectively dumping the 2d array on the screen. The places where the moles are should be printed as an ‘M’. The places where the moles have been whacked should be printed as a ‘W’. The places that don’t have a mole should be printed as *.


Putting it all together - main method
In order to play this game you need to create a main method.
Begin by creating a 10 by 10 grid where you randomly place the moles. Place a total of 10 moles.
Now allow the user (remember how to use Scanner?) to enter the x and y coordinates of where they would like to take a whack. Tell them they have a maximum of 50 attempts to get all the moles.
At any point in the game, they can input coordinates of -1, -1 in order to indicate that they are giving up. If the user gives up they get to see the entire grid.
The game ends if the user is able to uncover all the moles or if the user runs out of attempts.

Explanation / Answer

Create the WhackAMole class with the following code:

public class WhackAMole {
  
   public int score;
   public int molesLeft;
   public int attempts;
   public int moleGrid;
   public char[][] array;
   Scanner scan=new Scanner(System.in);
   WhackAMole(int numAttempts,int gridDimension ){
       this.attempts=numAttempts;
       this.moleGrid=gridDimension;
       array=new char[moleGrid][moleGrid];
       createGrid();
       waitingForCoordinates();
       molesLeft=10;
       //printGrid();
   }
  
   public void createGrid(){//for creating the moles randomly in the grid
      
       int fillMoles=10; //Custom defined number of moles
      
       System.out.println("Specify the Postion of 10 moles as xpos ypos");
      
      
       while(fillMoles > 0){ //Creating the required moles randomly
          
           int x=scan.nextInt();
           int y=scan.nextInt();
           if(place(x,y)){
               fillMoles--;
           }
           else{
               System.out.println("Invalid location spceified, Speciy the location again");
               continue;
           }
          
              
       }
      
      
      
       for(int i=0;i<moleGrid;i++){ //Filling the rest with *
           for(int j=0;j<moleGrid;j++ ){
               if(array[i][j]!='M'){
                   array[i][j]='*';
               }
           }
       }
      
      
      
      
   }
   public boolean place(int x,int y){
       if(array[x][y]!='M' && (x>=0&&x<moleGrid) && (y>=0&&y<moleGrid)){
           array[x][y]='M';
           return true;
       }
       else
           return false;
   }
  
   public void printGrid(){
       System.out.println("Print Grid:");
       for(int x=0;x<moleGrid;x++){
           for(int y=0;y<moleGrid;y++ ){
               System.out.print(array[x][y]+" ");
           }
           System.out.println();
   }
  
}
  
public void waitingForCoordinates(){
   System.out.println("Specify the Postion for whack as xpos ypos");
  
   // Scanner scan=new Scanner(System.in);
   while(attempts > 0){
       System.out.println("attempts:"+attempts);
       int x=scan.nextInt();
       int y=scan.nextInt();
      
       if(x==-1&&y==-1){ //Cutoff Condition (-1,-1)
           printGrid();
           return;
       }
       else
       whack(x,y);
   }
  
   printGrid();
}

public void whack(int x,int y){
   if((x>=0&&x<moleGrid) && (y>=0&&y<moleGrid)){
       if(array[x][y]=='M' ){
           array[x][y]='W';
           //attempts--;
           molesLeft--;
       }
       else{
           attempts--;
       }
   }
   else
       System.out.println("Invalid location specified, Specify the location again");
  
}

public void printGridToUser(){
   System.out.println("Print GridToUser:");
   for(int x=0;x<moleGrid;x++){ //Showing the status of W without showing the moles *
           for(int y=0;y<moleGrid;y++ ){
               if(array[x][y]=='W'){
                   System.out.print(array[x][y]+" ");
               }
               else
                   System.out.print("*");
           }
           System.out.println();
   }
}

}

Create the Main class to test & launch the above WhackAMole functions:

public class WhacKMain {
  
   public static void main(String[] args){
       WhackAMole w=new WhackAMole(10,10);
      
   }

}

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