Menu

(Solved) : Game 4×4 Board Game First Ask User Enter Name Age Following 4×4 Board Displayed Space Mark Q27740522 . . .

In this game, of a 4×4 board. The game will first ask the userto enter their name and age. Following that, a 4×4 board will bedisplayed where each space is marked by a letter (from ‘A’ – ‘P’).The program will then prompt the user to select a space andaccordingly reveal whether a rabbit was hiding in that space ornot. If it is, that space becomes marked with ‘X’. Otherwise, thespace becomes empty (no letter is displayed in the space). In thescreenshot below shows the areas where no rabbit was found by theuser, 1) import java.util.Random; import java.util.Scanner; importcmps251.homework2.Board; import cmps251.homework2.Game; importcmps251.homework2.Player; /** * DO NOT MODIFY THIS CODE * * Thiscode is only to help you run the code * You can verify thefunctionality by logging in to * oryx.qu.edu.qa (ssh). A perfectsolution 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 voidstudentRunner() { 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) + “n”);myGame.getBoard().displayBoard(); System.out.print(“Please select aposition (‘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(“Nota correct input, please try again: “); userInput =s.nextLine().toCharArray()[0]; } myGame.nextPlay(userInput);System.out.println(“nn”); } System.out.println(“nnFINALBOARD:”); myGame.getBoard().displayBoard(); System.out.println(“Youfound 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 randomvalues! * @return an integer array of size 3 with three differentrandom 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; } } 2) package cmps251.homework2; /** *This class should have some ‘spaces’ (i.e. 16) * */ public classBoard { public static final int NUM_SPACES = 16; //TODO: addwhatever instance variables you think are necessary /** * Thisconstructor should initiate the state of the board with 16different spaces * Some of these spaces have rabbits. To determinewhich spaces have rabbits, you should * see the elements of the@param rabbitsPositions. So if rabbitPositions = {3, 5, 7}, * thenspace[3], space[5] and space[7] have rabbits in them! * * @paramrabbitPositions an array of 3 integers with positions of therabbits as indexes */ public Board(int rabbitPositions[]) { //FIXME} /** * This method prints out the board with letters A-P initially* in a 4×4 matrix. * Once users select a letter, that particularletter disappears * or is replaced with X depending on whetherthere was a rabbit * or not at that position. * */ public voiddisplayBoard() { //FIXME } /** * @param i index of space to checkwhether it has a rabbit or not * @return returns true if a rabbitexists, false otherwise */ public boolean getIsRabbit(int i) {//FIXME return false; } /** * This method returns true if the spaceat i is revealed * * @param i index of space to check * @returntrue if revealed, false otherwise * */ public booleancheckRevealed(int i) { //FIXME return false; } /** * this methodreveals the space at i * * @param i the index of space which shouldbe revealed */ public void revealSpace(int i) { //FIXME } /** *TODO; students fill this * * @return the array of spaces that thisboard has */ public Space[] getSpaces() { //FIXME return null; } }3) package cmps251.homework2; /** * Top level class. This class hasa board and a player * */ public class Game { private static finalint MAX_RABBITS = 3; //TODO: add as many instance variables as youthink is necessary /** * * This constructor sets initial state ofthe game it has (board game) * and correctly places the player init. * * You should initialize all your instance variables here * *@param rabbitPositions an array of 3 integers that have thelocations of the rabbits * @param player player to add */ publicGame(int[] rabbitPositions, Player player) { } /** * This methodprints “Hello !nnYour game is starting…nn” */ public voidprintStartingGreeting() { //FIXME } /** * Method returns whethergame is over or not (when all rabbits are revealed) * * @returntrue if game ended, false otherwise */ public booleangetIsGameOver() { //FIXME return false; } /** * This methodadvances to the next stage (turn) of the game * It takes acharacter which the user selected from the board * and updates thestatus of the board spaces so that this place * on the board isrevealed. * In addition, if the space selected has a rabbit, itkeeps a count * of how many rabbits are found so far. Once allrabbits are found, * it sets the game ended boolean to true becauseall rabbits are * found! Don’t forget, we have three rabbits pergame * * 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 } /** * Thismethod takes a character and returns the corresponding index * forexample, if takes ‘A’ and returns 0 (first element in the array) ** @param in character to convert * @return integer position of thatcharacter */ private static int convertCharToIndex(char in) {return (int)(in-‘A’); } /** * This method takes an integer positionof the space on the board * and returns the characterrepresentation. 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 */ publicstatic char convertIndexToChar(int index) { return(char)((int)(‘A’)+index); } /** * Get how many turns are played sofar? * * @return the number of times the user selected a characterso 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; } } 4) packagecmps251.homework2; /** * Player information class. * A player hasname and age which gets set in the constructor */ public classPlayer { //TODO: add instance variables as needed /** * Constructorsets the name and age * @param name * @param age */ publicPlayer(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 ofthe player as an int */ public int getAge() { //FIXME return 0; } }5) package cmps251.homework2; /** * This class represents thespaces in the board * Things the class should know about itself: *Does it have rabbit? Is it revealed? What is the * position of thespace in the board overall? * */ public class Space { //TODO: addas many instance variables as you think necessary /** * Thisconstructor should initialize the space * At the beginning, itshould be not revealed, and based * on the parameters, it shouldset whether it has a rabbit * and what position in the board itcurrently is * * @param isRabbit whether the space has a rabbit ornot * @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. * Forexample, if the index of this space is 0, * we should get ‘A’, ifit is ‘1’, we should get ‘B’ and so on. * * If this space isrevealed, we display empty space if no rabbit * exists. Or “X” ifrabbit exists. * * You have three cases you need to take care of: *1. If not revealed, then print out A-P depending on the position ofthe space * 2. If revealed, then ” ” if no rabbit is here * 3. Ifrevealed, then “X” if rabbit is here * * Hint: use Game staticmethod “convertIndexToChar()” to cover some * of the cases * *@return “A”-“P” or ” ” or “X” depending on the space */ publicString getAsString() { return “”; //FIXME } /** * This methodreturns whether the space has rabbit or not. * * @return true ifthis space has rabbit, false otherwise */ public booleangetIsRabbit() { //FIXME return false; } /** * This method returnswhether the space is revealed or not * * @return true if this spaceis revealed, false otherwise */ public boolean getIsRevealed() {//FIXME return false; } /** * This method makes this space reveal!(show itself/unhide) */ public void reveal() { //FIXME }

Expert Answer


Answer to Game 4×4 Board Game First Ask User Enter Name Age Following 4×4 Board Displayed Space Mark Q27740522 . . .

OR