The task is to complete find the rabbits game, where 3 rabbits randomly hide in
ID: 3734784 • Letter: T
Question
The task is to complete find the rabbits game, where 3 rabbits randomly hide in one of the spaces of 4x4 board .the game will first ask the user to enter their name and age In this game, of a 4x4 board Following that, a 4x4 board will be displayed where each space is marked by a letter (from 'A' - 'P'). The program will then prompt the user to select a space and accordingly reveal whether a rabbit was hiding in that space or not. If it is, that space becomes marked with 'X Otherwise, the space becomes empty (no letter is displayed in the space Each time the game starts, the computer will randomly select 3 locations (from the available 16) to put the hiding rabbits in. After that, the player begins by trying each space individually. Once all three rabbits are found, the game terminates and informs the user how many attempts it took him/her to find the three hidden rabbits. The game contains the following 4 Java Classes: Space: This class represents the spaces in the board. The class should have attributes that tell us whether it contains a rabbit, whether that space has already been visited (revealed), and the index (position) of this space in the board (i.e. from index 0 to 15) Board: This class has spaces (16 spaces) Player: This class represents a human player, and contains a namne and age Game: This is the top level class which represents the entire game. It contains a player and a board.Explanation / Answer
import java.util.Random;
import java.util.Scanner;
class Space {
boolean rabbitFlag;
boolean visitedFlag;
int indexInBoard;
}
class Board {
Space[] spaceArray= new Space[16];
public Board (){
for ( int g=0; g<this.spaceArray.length; g++) {
this.spaceArray[g]=new Space();
}
Random rand = new Random();
int c=50;
int i=0;
int a= rand.nextInt(16);
int b=a;
for (;a==b;) {
b=rand.nextInt(16);// Generate random integers in range 0 to 15
}
c=a;
for (;c==a || c==b;) {
c=rand.nextInt(16);
}
for (i=0;i<16;i++) {
this.spaceArray[i].indexInBoard=i;
if (i==a) {
spaceArray[i].rabbitFlag=true;
}
if (i==b) {
spaceArray[i].rabbitFlag=true;
}
if (i==c) {
spaceArray[i].rabbitFlag=true;
}
}
}
}
class Player {
String name;
int age;
public Player () {
System.out.println("Please enter your name");
Scanner name = new Scanner(System.in);
this.name= name.next();
System.out.println("Please enter your age");
Scanner age = new Scanner(System.in);
this.age= age.nextInt();
}
}
public class Game {
public static void main(String args[]) {
Player player = new Player();
Board board = new Board();
char choice;
int r=0;
int n=0;
for (;r<3;) {
System.out.println("Please enter a letter between A-P");
n++;
Scanner choicein = new Scanner(System.in);
choice= choicein.next(".").charAt(0);
int m = (((int) choice) - 65);
board.spaceArray[m].visitedFlag=true;
if (board.spaceArray[m].rabbitFlag==true) {
System.out.println("You found the rabbit");
r++;
continue;
}
System.out.println("Sorry, try again");
}
System.out.println("You found all three rabbits. Number of Attempts="+n);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.