Question: I need help for my java lab. Thank you.. After the initial introduction to the game, the player should be asked to enter one of

I need help for my java lab. Thank you..

After the initial introduction to the game, the player should be asked to enter one of four possible options to move the character along the game board:

u (up) d (down) l (left) r (right)

After the player has enter a selection, the Ghost(s) should move and then the updated gameboard should be displayed.

Do not display the game board before the Ghost has moved, as well.

If the Player lands on at a row and column in the board that contains a *, the Player should be given one point and the * should be replaced with a blank space for the remainder of the game.

Also, any cookies eaten by the Ghost(s) should replace *s with blank spaces on the board.

Note that you must perform some error checking:

First, the Player character cannot go out of bounds of the 2D array.

Otherwise, the game prints a message "Ouch!"

How do you want to move? (u/d/l/r): u

Ouch!

Score: 0

P * * * * * * * * *

* * * * * * * * * *

* * * * * * * * * *

* * * * * * * * * *

* * * * * * * * * *

* * * * * * * * * *

* * * * * * * * * *

* * * * * * * * * *

* * * * * * * * * *

* * * * * * * * * G

How do you want to move? (u/d/l/r): l

Ouch!

Score: 0

P * * * * * * * * *

* * * * * * * * * *

* * * * * * * * * *

* * * * * * * * * *

* * * * * * * * * *

* * * * * * * * * *

* * * * * * * * * *

* * * * * * * * * *

* * * * * * * * * *

* * * * * * * * * G

Second, if the user enters incorrect input, the program must print "Invalid move!" and the game should not advance until the user types in correct input:

How do you want to move? (u/d/l/r): r Score: 8

P *

* * * * * * * * * G

* * * * * * * * *

* * * * * * * * *

* * * * * * * * *

* * * * * * * * *

* * * * * * * * *

* * * * * * * * *

* * * * * * * * *

* * * * * * * * *

How do you want to move? (u/d/l/r): rr Invalid move! Score: 8

P *

* * * * * * * * * G

* * * * * * * * *

* * * * * * * * *

* * * * * * * * *

* * * * * * * * *

* * * * * * * * *

* * * * * * * * *

* * * * * * * * *

* * * * * * * * *

The game should continue until one of the following events occurs:

The Player gets eaten by a Ghost (both end up at the same row and col within the 2D array)

The Player scores 20 points

The are not enough cookies left on the game board for the Player to win.

Required: You must implement the classes whose starter code is provided below.

Additionally, you must extend the Ghost class to write two more Ghost sub-classes (Spectre.java and Spooky.java), each representing a different Ghost characters

All 3 Ghost characters should behave differently in the game (polymorphism) but should all be represented with a G on the game board

You must implement the Ghost.java class provided, and then it is up to your own powers of invention to create two additional Ghost classes and determine how the Ghosts behave in the game.

The names of these two Ghost classes should be Spectre.java and Spooky.java and both should extend Ghost.java

Below is the starter code for the required classes.

Class character

public abstract class Character { private int xPos; private int yPos; /** * Sets the x (column) location of the character * @param x the x position of the character in the x-y plane */ public void setXPos(int x) { xPos = x; } /** * Sets the y (row) location of the character * @param y the y position of the character in the x-y plane */ public void setYPos(int y) { yPos = y; } /** * Returns the x position of the character * @return the x position of the character in the x-y plane */ public int getXPos() { return xPos; } /** * Returns the y position of the character * @return the y position of the character in the x-y plane */ public int getYPos() { return yPos; } /** * Moves the character one space to the left * by subtracting one from its x position * Or, prevents the player from going outside * the bounds of the board by leaving the * character in the same position. */ public void moveLeft() { xPos-=1; } /** * Moves the character one space to the right * by adding one to its x position * Or, prevents the player from going outside * the bounds of the board by leaving the * character in the same position. */ public void moveRight() { xPos+=1; } /** * Moves the character one space down * by adding one to its y position * Or, prevents the player from going outside * the bounds of the board by leaving the * character in the same position. */ public void moveDown() { yPos+=1; } /** * Moves the character one space up * by subtracting one from its y position * Or, prevents the player from going outside * the bounds of the board by leaving the * character in the same position. */ public void moveUp() { yPos-=1; } }

Player.java

At a minimum, you must implement the below methods, but may also add any additional methods you would like.

For example: you may consider adding bounds checking methods within this class so that the Player cannot go out of bounds in the game.

Alternately, you can implement this code within Game.java

public final class Player extends Character{ private int score; /** * Player default constructor

* Gives the player a starting

* position of [0,0] on the board

* and sets score to 0

*/ public Player() { } /** * Returns the player's current score * @return the score */ public int getScore() { return -1; } /** * Updates the player's score */ public void updateScore() { return; } }

Ghost.java:

At a minimum, you must implement the below methods, but may also add any additional methods you would like.

You must then inherit from this class to create Spectre.java and Spooky.java classes.

import java.util.Random;

public class Ghost extends Character{ private int numMoves; int max; /** * One argument constructor * Calls setXPos() and setYPos() * to give the ghost its initial * placement at the bottom left * of the board * Also initializes max (the max * x and y dimension on the board) * and numMove to 0 * @param max the */ public Ghost(int max) { } /** * Moves the Ghost according to an * algorithim: * If the number of moves it has made * is divisible by 10, it generates a * random move. Otherwise, it moves up * until it cannot go farther. Then, * it moves left until it can go no * farther. * Once it reaches the 0,0 * corner it jumps back to its starting * position. Updates numMoves. */ public void move() { } /** * Places the ghost at a random * board position */ public void generateRandomMove() { } }

Game.java:

At a minimum, you must implement the below methods, but may also add any additional methods you would like to represent the game play as shown in the sample output below.

Also you should declare two new private variables of type Spectre and Spooky

import java.util.Scanner;

public class Game { private int upperX; private int upperY; private int totalCookies; private Ghost g; private Player p; private String board[][];

/**

* Constructor for the Game class

* Initializes private variables

* Calls initialize board to intialize

* the board. Places characters at their

* starting positions

*/

public Game() { } /** * Initializes the board to all *s * Called by the constructor */ private void initializeBoard() {

return;

} /** * Places the player and ghost at a * new spot on the board */ public void updateBoard() {

return;

} /** * Replaces the current location of the * player and ghost with two * */ public void clearBoard() {

return;

} /** * Prints out board as shown in sample output * including displaying the current score */ public void printBoard() {

return;

} /** * Determines whether the player has lost by either * being eaten by the ghost or because there * are not enough cookies left on the board to win * @return whether the player has lost */ public boolean gameOverLose() {

return false;

} /** * Determines whether the player has won * the game by scoring 20 points * @return whether the player has won */ public boolean gameOverWin() {

return false;

} public static void main(String[] args) { Scanner input = new Scanner(System.in); String choice = ""; System.out.println("Welcome to Pac-Cookie! "); System.out.println("Your goal is to eat 20 cookies!"); System.out.println("But, beware the not-so-friendly ghost..." + " He will eat the cookies... and YOU!"); System.out.println(" Ready to play? Let's go! "); Game game = new Game();

//Add your code here!

input.close(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!