Question
In this assignment, your task is to complete a Find The Rabbits game, where 3 rabbits will randomly hide in one of the spaces of
In this assignment, your task is to complete a Find The Rabbits game, where 3 rabbits will randomly hide in one of the spaces of a 4x4 board. The game will first ask the user to enter their name and age. 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). In the screenshot below shows the areas where no rabbit was found by the user, and where one rabbit was spotted
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 name and age. - Game: This is the top level class which represents the entire game. It contains a player and a board. The downloaded package will contain all the classes necessary to finish the game. Note that the Runner.java class should NOT be modified to make the game work. Your job is to complete the missing required instance variables for each class, and populate the missing code inside the constructors and methods of the classes. Here, you should only modify the body of the methods and not the methods signatures.
import java.util.Random;
import java.util.Scanner;
import cmps251.homework2.Board;
import cmps251.homework2.Game;
import cmps251.homework2.Player;
/**
* DO NOT MODIFY THIS CODE
*
* This code is only to help you run the code
* You can verify the functionality by logging in to
* oryx.qu.edu.qa (ssh). A perfect solution is one that
* matches the output on the ssh server
*
*/
public class Runner {
public static void main(String[] args) {
Runner r = new Runner();
r.studentRunner();
}
private void studentRunner()
{
Scanner s = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = s.nextLine();
System.out.print("Enter your age: ");
int age = s.nextInt();
s.nextLine(); //clear buffer
Player player = new Player(name, age);
Game myGame = new Game(getRandomPositions(), player);
myGame.printStartingGreeting();
char userInput;
while (!myGame.getIsGameOver())
{
System.out.println("Current Play No: " + (myGame.getPlayCount()+1) + " ");
myGame.getBoard().displayBoard();
System.out.print("Please select a position ('A'-'P'): ");
userInput = s.nextLine().toCharArray()[0]; //a trick to find the first character of the entered string
while (userInput < 'A' || userInput > 'P') {
System.out.print("Not a correct input, please try again: ");
userInput = s.nextLine().toCharArray()[0];
}
myGame.nextPlay(userInput);
System.out.println(" ");
}
System.out.println(" FINAL BOARD:");
myGame.getBoard().displayBoard();
System.out.println("You found all rabbits!");
System.out.println("It took you: " + myGame.getPlayCount() + " turns. Not bad for a " + myGame.getPlayer().getAge() + " year old!");
s.close();
}
/**
* Method to return three random positions in an array of 3 integers.
* Everytime you call this method, you'll get three different random values!
* @return an integer array of size 3 with three different random positions within
*/
private int[] getRandomPositions() {
Random rand = new Random(System.currentTimeMillis());
int[] rabbitPositions = { rand.nextInt(Board.NUM_SPACES), rand.nextInt(Board.NUM_SPACES),
rand.nextInt(Board.NUM_SPACES) };
return rabbitPositions;
}
}
package cmps251.homework2;
/**
* This class should have some 'spaces' (i.e. 16)
*
*/
public class Board {
public static final int NUM_SPACES = 16;
//TODO: add whatever instance variables you think are necessary
/**
* This constructor should initiate the state of the board with 16 different spaces
* Some of these spaces have rabbits. To determine which spaces have rabbits, you should
* see the elements of the @param rabbitsPositions. So if rabbitPositions = {3, 5, 7},
* then space[3], space[5] and space[7] have rabbits in them!
*
* @param rabbitPositions an array of 3 integers with positions of the rabbits as indexes
*/
public Board(int rabbitPositions[])
{
//FIXME
}
/**
* This method prints out the board with letters A-P initially
* in a 4x4 matrix.
* Once users select a letter, that particular letter disappears
* or is replaced with X depending on whether there was a rabbit
* or not at that position.
*
*/
public void displayBoard()
{
//FIXME
}
/**
* @param i index of space to check whether it has a rabbit or not
* @return returns true if a rabbit exists, false otherwise
*/
public boolean getIsRabbit(int i)
{
//FIXME
return false;
}
/**
* This method returns true if the space at i is revealed
*
* @param i index of space to check
* @return true if revealed, false otherwise
*
*/
public boolean checkRevealed(int i)
{
//FIXME
return false;
}
/**
* this method reveals the space at i
*
* @param i the index of space which should be revealed
*/
public void revealSpace(int i)
{
//FIXME
}
/**
* TODO; students fill this
*
* @return the array of spaces that this board has
*/
public Space[] getSpaces() {
//FIXME
return null;
}
}
package cmps251.homework2;
/**
* Top level class. This class has a board and a player
*
*/
public class Game {
private static final int MAX_RABBITS = 3;
//TODO: add as many instance variables as you think is necessary
/**
*
* This constructor sets initial state of the game it has (board game)
* and correctly places the player in it.
*
* You should initialize all your instance variables here
*
* @param rabbitPositions an array of 3 integers that have the locations of the rabbits
* @param player player to add
*/
public Game(int[] rabbitPositions, Player player)
{
}
/**
* This method prints "Hello ! Your game is starting... "
*/
public void printStartingGreeting() {
//FIXME
}
/**
* Method returns whether game is over or not (when all rabbits are revealed)
*
* @return true if game ended, false otherwise
*/
public boolean getIsGameOver()
{
//FIXME
return false;
}
/**
* This method advances to the next stage (turn) of the game
* It takes a character which the user selected from the board
* and updates the status of the board spaces so that this place
* on the board is revealed.
* In addition, if the space selected has a rabbit, it keeps a count
* of how many rabbits are found so far. Once all rabbits are found,
* it sets the game ended boolean to true because all rabbits are
* found! Don't forget, we have three rabbits per game
*
* Hint: you can use static method converCharToIndex(char a) below
*
* @param a the character the user selected on the board 'A'-'P'
*/
public void nextPlay(char a)
{
//FIXME
}
/**
* This method takes a character and returns the corresponding index
* for example, if takes 'A' and returns 0 (first element in the array)
*
* @param in character to convert
* @return integer position of that character
*/
private static int convertCharToIndex(char in)
{
return (int)(in-'A');
}
/**
* This method takes an integer position of the space on the board
* and returns the character representation. For example, if you pass 0
* it returns 'A'
* @param index the integer position value you want to convert
* @return the character representation of that position
*/
public static char convertIndexToChar(int index)
{
return (char)((int)('A')+index);
}
/**
* Get how many turns are played so far?
*
* @return the number of times the user selected a character so far.
*/
public int getPlayCount()
{
//FIXME
return 0;
}
/**
* @return the Board that this game has
*/
public Board getBoard() {
//FIXME
return null;
}
/**
* @return the Player that this game has
*/
public Player getPlayer() {
//FIXME
return null;
}
}
package cmps251.homework2;
/**
* Player information class.
* A player has name and age which gets set in the constructor
*/
public class Player {
//TODO: add instance variables as needed
/**
* Constructor sets the name and age
* @param name
* @param age
*/
public Player(String name, int age)
{
//FIXME
}
/**
* Method to get name
* @return the name of the player as string
*/
public String getName()
{
//FIXME
return "";
}
/**
* Method to get age
* @return the age of the player as an int
*/
public int getAge()
{
//FIXME
return 0;
}
}
package cmps251.homework2;
/**
* This class represents the spaces in the board
* Things the class should know about itself:
* Does it have rabbit? Is it revealed? What is the
* position of the space in the board overall?
*
*/
public class Space {
//TODO: add as many instance variables as you think necessary
/**
* This constructor should initialize the space
* At the beginning, it should be not revealed, and based
* on the parameters, it should set whether it has a rabbit
* and what position in the board it currently is
*
* @param isRabbit whether the space has a rabbit or not
* @param index the index (position) of this space in the board (0-16)
*/
public Space(boolean isRabbit, int index)
{
//FIXME
}
/**
*
* This method returns the index representation as a string.
* For example, if the index of this space is 0,
* we should get 'A', if it is '1', we should get 'B' and so on.
*
* If this space is revealed, we display empty space if no rabbit
* exists. Or "X" if rabbit exists.
*
* You have three cases you need to take care of:
* 1. If not revealed, then print out A-P depending on the position of the space
* 2. If revealed, then " " if no rabbit is here
* 3. If revealed, then "X" if rabbit is here
*
* Hint: use Game static method "convertIndexToChar()" to cover some
* of the cases
*
* @return "A"-"P" or " " or "X" depending on the space
*/
public String getAsString()
{
return "";
//FIXME
}
/**
* This method returns whether the space has rabbit or not.
*
* @return true if this space has rabbit, false otherwise
*/
public boolean getIsRabbit()
{
//FIXME
return false;
}
/**
* This method returns whether the space is revealed or not
*
* @return true if this space is revealed, false otherwise
*/
public boolean getIsRevealed()
{
//FIXME
return false;
}
/**
* This method makes this space reveal! (show itself/unhide)
*/
public void reveal()
{
//FIXME
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started