Lab 12: Abstract Classes and Polymorphismdue Monday, February 24 at 9:20am on Canvas Pair Programming Required (or No Credit)
Pac-Cookie!
Game Specifications:
Welcome to Pac-Cookie! Your goal is to eat all remaining cookies (at least 20) - before you escape! But, beware the not-so-friendly ghosts... They will eat the cookies... and YOU! Ready to play? Let's go! Score: 0 Remaining Cookies: 53 H H H H H H H H H H H P * * * * * * * H H * * * * * * * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * * * * * * G * H H H H H H H H H H How do you want to move? (u/d/l/r): u Ouch! Score: 0 Remaining Cookies: 53 H H H H H H H H H H H P * * * * * * * H H * * * * * * * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * * * * * * G * H H H H H H H H H H How do you want to move? (u/d/l/r):
How do you want to move? (u/d/l/r): r Score: 3 Remaining Cookies: 47 H H H H H H H H H H H * * * * * * * H H * * * * * * * H H P H * * H * * H H * * H * * H * * H H * * H * * H * G H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Ouch! Score: 3 Remaining Cookies: 47 H H H H H H H H H H H * * * * * * * H H * * * * * * * H H P H * * H * * H H * * H * * H * * H H * * H * * H * G H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Ouch! Score: 3 Remaining Cookies: 47 H H H H H H H H H H H * * * * * * * H H * * * * * * * H H P H * * H * * H H * * H * * H * * H H * * H * * H * G H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r):
Score: 31 Remaining Cookies: 9 H H H H H H H H H H H H H H H H * * H H H H * P H H H H * H H H G * H H H H * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u
How do you want to move? (u/d/l/r): dd Invalid move! Score: 3 Remaining Cookies: 47 H H H H H H H H H H H * * * * * * * H H * * * * * * * H H P H * * H * * H H * * H * * H * * H H * * H * * H * G H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r):
Character.java Code:
/** * Character.java * @author * @author * CIS 36B, Lab 12 */ 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
/** * Player.java * @author FILL IN HERE * @author FILL IN HERE * CIS 36B, Lab 12 */ public final class Player extends Character{ private int score; /** * Player default constructor * Gives the player a starting * position of [1,1] 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 by 1 */ public void updateScore() { return; } } Ghost.java:
/** * Ghost.java * @author FILL IN HERE * @author FILL IN HERE * CIS 36B, Lab 12 */ 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 right * corner of the board [max-2, max-2] * 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 [1,1] * 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:
/** * Game.java * @author FILL IN HERE * @author FILL IN HERE * CIS 36B, Lab 12 */ 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. * Note that there are 53 cookies on * the board at the start of the game */ public Game() { } /** * Initializes the board to *s for cookies * and Hs for walls * Called by the constructor */ private void initializeBoard() { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { if( i == 0 || j == 0) { board[i][j] = "H"; } else if (j == board.length - 1 || i == board.length - 1) { board[i][j] = "H"; } else if (j == board.length / 3 && i > 2 && i < 8 ) { board[i][j] = "H"; } else if (j == (2 * board.length) / 3 && i > 2 && i < 8 ) { board[i][j] = "H"; } else { board[i][j] = "*"; } } } } /** * 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 blank spaces * unless the ghost is walking through a wall * in which case, only the player's space * is cleared * Hint: See initializeBoard method for * placement of interior walls */ public void clearSpace() { return; } /** * Prints out board as shown in sample output * including displaying the current score and * the number of remaining cookies */ 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 at least 20 points, * eating all possible cookies, and * escaping from the hole in the leftside * wall * @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!\n"); System.out.println("Your goal is to eat all remaining cookies (at least 20) - before you escape!"); System.out.println("But, beware the not-so-friendly ghosts..." + "\nThey will eat the cookies... and YOU!"); System.out.println("\nReady to play? Let's go!\n"); Game game = new Game(); //Add your code here! input.close(); } } Base Game (Before Spooky Added) Sample Output: Example 1: Winning the Game: Your goal is to eat all remaining cookies (at least 20) - before you escape! But, beware the not-so-friendly ghosts... They will eat the cookies... and YOU! Ready to play? Let's go! Score: 0 Remaining Cookies: 53 H H H H H H H H H H H P * * * * * * * H H * * * * * * * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * * * * * * G * H H H H H H H H H H How do you want to move? (u/d/l/r): u Ouch! Score: 0 Remaining Cookies: 53 H H H H H H H H H H H P * * * * * * * H H * * * * * * * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * * * * * * G * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 1 Remaining Cookies: 51 H H H H H H H H H H H * * * * * * * H H P * * * * * * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * G H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 2 Remaining Cookies: 49 H H H H H H H H H H H * * * * * * * H H * * * * * * * H H P * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * G H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 3 Remaining Cookies: 47 H H H H H H H H H H H * * * * * * * H H * * * * * * * H H * H * * H * * H H P * H * * H * * H H * * H * * H * G H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 4 Remaining Cookies: 45 H H H H H H H H H H H * * * * * * * H H * * * * * * * H H * H * * H * * H H * H * * H * G H H P * H * * H * H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 5 Remaining Cookies: 43 H H H H H H H H H H H * * * * * * * H H * * * * * * * H H * H * * H * G H H * H * * H * H H * H * * H * H H P * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 6 Remaining Cookies: 41 H H H H H H H H H H H * * * * * * * H H * * * * * * G H H * H * * H * H H * H * * H * H H * H * * H * H H * H * * H * H H P * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 7 Remaining Cookies: 39 H H H H H H H H H H H * * * * * * G H H * * * * * * H H * H * * H * H H * H * * H * H H * H * * H * H H * H * * H * H H * H * * H * H H P * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 8 Remaining Cookies: 37 H H H H H H H H H H H * * * * * G H H * * * * * * H H * H * * H * H H * H * * H * H H * H * * H * H H * H * * H * H H * H * * H * H H P * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 9 Remaining Cookies: 35 H H H H H H H H H H H * * * * G H H * * * * * * H H * H * * H * H H * H * * H * H H * H * * H * H H * H * * H * H H P H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 10 Remaining Cookies: 34 H H H H H H H H H H H * * * * G H H * * * * * * H H * H * * H * H H * H * * H * H H * H * * H * H H P H * * H * H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 11 Remaining Cookies: 33 H H H H H H H H H H H * * * * G H H * * * * * * H H * H * * H * H H * H * * H * H H P H * * H * H H H * * H * H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 12 Remaining Cookies: 32 H H H H H H H H H H H * * * * G H H * * * * * * H H * H * * H * H H P H * * H * H H H * * H * H H H * * H * H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 13 Remaining Cookies: 30 H H H H H H H H H H H * * * G H H * * * * * * H H P H * * H * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 14 Remaining Cookies: 28 H H H H H H H H H H H * * G H H P * * * * * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 15 Remaining Cookies: 26 H H H H H H H H H H H * G H H P * * * * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 16 Remaining Cookies: 24 H H H H H H H H H H H G H H P * * * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 17 Remaining Cookies: 23 H H H H H H H H H H H G H H P * * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 18 Remaining Cookies: 22 H H H H H H H H H H H H H P * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H * * * * * G * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 19 Remaining Cookies: 21 H H H H H H H H H H H H H P H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H H * * H * G H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 20 Remaining Cookies: 20 H H H H H H H H H H H H H H H H * * H P H H H * * H * H H H * * H * H H H * * H * H H H * * H * H H * * * * * G * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 21 Remaining Cookies: 19 H H H H H H H H H H H H H H H H * * H H H H * * H P H H H * * H * H H H * * H * H H H * * H * G H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 22 Remaining Cookies: 18 H H H H H H H H H H H H H H H H * * H H H H * * H H H H * * H P H H H * * H * G H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 23 Remaining Cookies: 17 H H H H H H H H H H H H H H H H * * H H H H * * H H H H * * H G H H H * * H P H H H * * H * H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 24 Remaining Cookies: 16 H H H H H H H H H H H H H H H H * * H H H H * * H G H H H * * H H H H * * H H H H * * H P H H * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 25 Remaining Cookies: 15 H H H H H H H H H H H H H H H H * * H G H H H * * H H H H * * H H H H * * H H H H * * H H H * * * * P * H H H H H H H H H H How do you want to move? (u/d/l/r): l Score: 26 Remaining Cookies: 14 H H H H H H H H H H H H H G H H H * * H H H H * * H H H H * * H H H H * * H H H H * * H H H * * * P * H H H H H H H H H H How do you want to move? (u/d/l/r): l Score: 27 Remaining Cookies: 13 H H H H H H H H H H H G H H H H H * * H H H H * * H H H H * * H H H H * * H H H H * * H H H * * P * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 28 Remaining Cookies: 12 H H H H H H H H H H H G H H H H H * * H H H H * * H H H H * * H H H H * * H H H H * P H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 29 Remaining Cookies: 11 H H H H H H H H H H H G H H H H H * * H H H H * * H H H H * * H H H H * P H H H H * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 30 Remaining Cookies: 10 H H H H H H H H H H H H H H H H * * H H H H * * H H H H * P H H H H * H H H G * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 31 Remaining Cookies: 9 H H H H H H H H H H H H H H H H * * H H H H * P H H H H * H H H G * H H H H * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 32 Remaining Cookies: 8 H H H H H H H H H H H H H H H H * P H H H H * H H H G * H H H H * H H H H * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Ouch! Score: 32 Remaining Cookies: 8 H H H H H H H H H H H H H H H H * P H H H H * H H H G * H H H H * H H H H * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): l Score: 33 Remaining Cookies: 7 H H H H H H H H H H H H H H H H P H H H G * H H H H * H H H H * H H H H * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 34 Remaining Cookies: 6 H H H H H H H H H H H H H H H G H H H H P H H H H * H H H H * H H H H * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 35 Remaining Cookies: 5 H H H H H H H H H H H H H G H H H H H H H H H H H P H H H H * H H H H * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 36 Remaining Cookies: 4 H H H H H H H H H H H G H H H H H H H H H H H H H H H H H P H H H H * H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 37 Remaining Cookies: 3 H H H H H H H H H H H G H H H H H H H H H H H H H H H H H H H H H P H H H * * * H H H H H H H H H H How do you want to move? (u/d/l/r): d Score: 38 Remaining Cookies: 2 H H H H H H H H H H H G H H H H H H H H H H H H H H H H H H H H H H H H * P * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 38 Remaining Cookies: 2 H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H * P G * H H H H H H H H H H How do you want to move? (u/d/l/r): l Score: 38 Remaining Cookies: 2 H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H G H H H * P * H H H H H H H H H H How do you want to move? (u/d/l/r): l Score: 39 Remaining Cookies: 1 H H H H H H H H H H H H H H H H H H H H H H H H H H H G H H H H H H H P * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 39 Remaining Cookies: 1 H H H H H H H H H H H H H H H H H H H H H H H G H H H H H H H H H H H P * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 39 Remaining Cookies: 1 H H H H H H H H H H H H H H H H H H H G H H H H H H H H H H H H H H H P * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 39 Remaining Cookies: 1 H H H H H H H H H H H H H H H G H H H H H H H H H H H H H H H H H H H P * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 39 Remaining Cookies: 1 H H H H H H H H H H H H H G H H H H H H H H H H H H H H H H H H H H H H P * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 39 Remaining Cookies: 1 H H H H H H H H H H H G H H H H H H H H H H H H H H H H H H H H H H H H P * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 40 Remaining Cookies: 0 H H H H H H H H H H H G H H H H H H H H H H H H H H H H H H H H H H H H P H H H H H H H H H H Congratulations! You win! Final score: 40 Example 2: Losing the Game: Your goal is to eat all remaining cookies (at least 20) - before you escape! But, beware the not-so-friendly ghosts... They will eat the cookies... and YOU! Ready to play? Let's go! Score: 0 Remaining Cookies: 53 H H H H H H H H H H H P * * * * * * * H H * * * * * * * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * * * * * * G * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 1 Remaining Cookies: 51 H H H H H H H H H H H P * * * * * * H H * * * * * * * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * G H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 2 Remaining Cookies: 49 H H H H H H H H H H H P * * * * * H H * * * * * * * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * G H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 3 Remaining Cookies: 47 H H H H H H H H H H H P * * * * H H * * * * * * * * H H * * H * * H * * H H * * H * * H * * H H * * H * * H * G H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 4 Remaining Cookies: 45 H H H H H H H H H H H P * * * H H * * * * * * * * H H * * H * * H * * H H * * H * * H * G H H * * H * * H * H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 5 Remaining Cookies: 43 H H H H H H H H H H H P * * H H * * * * * * * * H H * * H * * H * G H H * * H * * H * H H * * H * * H * H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 6 Remaining Cookies: 41 H H H H H H H H H H H P * H H * * * * * * * G H H * * H * * H * H H * * H * * H * H H * * H * * H * H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H How do you want to move? (u/d/l/r): r Score: 7 Remaining Cookies: 39 H H H H H H H H H H H G H H * * * * * * * H H * * H * * H * H H * * H * * H * H H * * H * * H * H H * * H * * H * H H * * H * * H * H H * * * * * * * * H H H H H H H H H H You lose! Example 3: Losing the Game: How do you want to move? (u/d/l/r): u Score: 13 Remaining Cookies: 8 H H H H H H H H H H H H H * H H * H H H H G H H H H H * H H H P * H * H H H H * H H H * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 13 Remaining Cookies: 7 H H H H H H H H H H H H H * H H G H H H H H H H H P H * H H H * H * H H H H * H H H * * H H H H H H H H H H How do you want to move? (u/d/l/r): u Score: 13 Remaining Cookies: 6 H H H H H H H H H H H H H G H H H H H H P H H H H H * H H H * H * H H H H * H H H * * H H H H H H H H H H You lose! What to Submit:
How You Will Be Graded:
|