Objective : Write the backend classes for the game PickTheOneGame . There are 20
ID: 3678122 • Letter: O
Question
Objective:
Write the backend classes for the game PickTheOneGame. There are 20 spaces and you only have 5 chances to pick the right one! Download the Lab15Driver and create the following classes with these specifications.
Specification:
Create a class Space with the following instance variables:
selected - a boolean varaible that represents whether the space has been selected or not
isTheOne - a boolean variable for the one space that's the one!
With the following constructors:
Default constructor - Sets selected and isTheOne initially to false.
Getters & setters for all instance variables
Create a class PickTheOneGameBackend With instance variables:
spaces - An array of the newly created type Space which corresponds to the spaces of the front end
currTurns - corresponds to the number of turns the player has taken
gameOver - A boolean value that indicates whether or not the game is over
win - A boolean variable that indicates if the player has won.
Constant class variables (in other words, these should be defined with public static final preceding them):
SPACE_AMT - corresponds to the number of spaces in the game, which is 20.
MAX_TURNS - corresponds to the number of turns the player gets to try to find the one, which is 5
Constructors
Default Constructor - sets all of the instance variabels to default values, and initializes the array of spaces.
HINT: you may also want to call resetSpaces here
Getters for all instance variables
Additional methods:
resetSpaces - a method that returns nothing and has no parameters. Used to reset the game. First, all the spaces in the array need to be constructed. Then, a random space is selected to be the one. Finally, the other instance variables currTurns, gameOver, and win need to be set to initial values.
selectSpace - a method that takes an integer corresponding to the index of the space that's selected, and returns nothing. If the selected space has already been selected, do nothing. Otherwise:
set that space's selected value to true, and check if the space is the one. If so, then the game is over and the player has won.
Increment the number of turns, and check to see if the maximum number of turns has been exceeded. If it has, the game is over.
Example Dialog:
Lab 15 Driver:
Explanation / Answer
Answer: code the class requeried
public class PickTheOneGame {
private boolean selected;
private boolean isTheOne;
public PickTheOneGame(){
selected = false;
isTheOne = false;
}
public void setSelected(boolean b){
selected = b;
}
public boolean getSelected(){
return selected;
}
public void setisTheOne(boolean b){
isTheOne = b;
}
public boolean getisTheOne(){
return isTheOne;
}
class PickTheOneGameBackend{
private int spaces[];
private int currTurns;
private boolean gameOver;
private boolean win;
public static final int SPACE_AMT = 20;
public static final int MAX_TURNS = 5;
public PickTheOneGameBackend(){
resetSpaces();
}
public void resetSpaces(){
currTurns = 0;
gameOver = false;
win = false;
spaces = new int[SPACE_AMT];
for (int i=0; i<SPACE_AMT; i++)
spaces[i] = 0;
int x_rand = (int)Math.random()*SPACE_AMT;
spaces[x_rand]= 1;
}
public void selectSpace(int pos){
if (spaces[pos] == 0)
spaces[pos] = -1;
else
if (spaces[pos] == 1){
gameOver = true;
win = true;
}
++currTurns;
if (currTurns >= MAX_TURNS)
gameOver = true;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.