Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 thisgame,of a 4x4 board Following that, a 4x4 board will be displayedwhere each space is marked by a letter (from A P). Theprogram will then prompt the user to select a space andaccordingly reveal whether a rabbit was hiding in that spaceor 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 name and age.

- Game: This is the top level class which represents the entire game. It contains a player and a board.

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();

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);

char userInput;

while (!myGame.getIsGameOver())

System.out.println("Current Play No: " + (myGame.getPlayCount()+1) + " ");

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];

System.out.println(" FINAL BOARD:");

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!");

* 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;

* 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[])

* 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()

/**

* @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)

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)

return false;

* this method reveals the space at i

* @param i the index of space which should be revealed

public void revealSpace(int i)

* TODO; students fill this

* @return the array of spaces that this board has

public Space[] getSpaces() {

return null;

* 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() {

* Method returns whether game is over or not (when all rabbits are revealed)

*

* @return true if game ended, false otherwise

public boolean getIsGameOver()

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)

* 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()

return 0;

* @return the Board that this game has

public Board getBoard() {

return null;

* @return the Player that this game has

public Player getPlayer() {

return null;

* 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)

* Method to get name

* @return the name of the player as string

public String getName()

return "";

* Method to get age

* @return the age of the player as an int

public int getAge()

return 0;

* 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)

*

* 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 "";

* This method returns whether the space has rabbit or not.

* @return true if this space has rabbit, false otherwise

public boolean getIsRabbit()

return false;

* This method returns whether the space is revealed or not

* @return true if this space is revealed, false otherwise

public boolean getIsRevealed()

return false;

* This method makes this space reveal! (show itself/unhide)

public void reveal()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions