Here is my code I have:
It's supposed to be a game with characters and enemies. I followed the instructions but it doesn't work. Here's the instructions:
Hoping someone can read through all the steps in the pictures I sent and correct my code accordingly.
If you need my other 2 classes of the lab package and assignment here they are as well:
PlayableCharacter class:
Enemy class:
Whenever I run the program I receive an error, please read the instructions included in the images and correct my Game class (and the other 2 if needed) to get this program Lab4.java to run properly. I need ASAP and I'll give a thumbs up
Here is what I keep getting:
Good luck!
PLEASE READ STEPS IN IMAGES AND PROPERLY FOLLOW THEM TO CORRECT MY GAME CLASS IN THE FIRST IMAGES ABOVE!!!
Product.java 1 package lab; 2 3 import java.util.Scanner; 4 5 public class Game 6 THE 600 Basket.java 79 public static void main(String[] args) { Scanner input = new Scanner(System.in); 10 11 12 13 45671 15 /* PlayableCharacter.java 16 9 System.out.print("Welcome to the Dungeon! What is your character's name? (Input name then press enter): "); String playerName = input.nextLine(); //Create instance of PlayableCharacter with inputed name PlayableCharacter pc = new PlayableCharacter (playerName); System.out.println("Hello, + pc.getName()); 11 * Enemy class constructor has 3 arguments. Argument 1: String name 18 * Argument 2: int locationX * Argument 3: int location\ Enemy java 19 20 */ 21 Enemy zombie = new Enemy ("Zombie", 2, 3); 22 23 11 24 //GAME LOOP START 25 // *Game.java *Score.java Student.java Course Java 26 27 while (!(pc.getLocationX() == 6 && pc.getLocationY() == 6) && pc.getCurrentHitPoints () > 0) { System.out.print(pc); 28 29 if (zombie.getCurrentHitPoints() > 0) System.out.print(zombie); 38 } 31
31. 32 System.out.println("Game Over!"); 34 35 36 44 45 46 47 48 49 50 } 37 38 //Player's turn 39 System.out.println("Where to move? u for up, d for down, 1 for left, r for right. Any other key will skip the turn."); 40 String in = input.nextLine(); 41 switch (in) { case "u": 42 43 95 in 55 51 53 54 4500 System.out.println("Character: System.out.println("Remaining + pc.getCurrentHitPoints()); System.out.println("Zombie hitpoints: + zombie.getCurrentHitPoints()); 55 56 input.close(); 57 58} break; case "p": 41 pc.setLocationY(pc.getLocationY() + 1); } + pc.getName()); break; default: 52 if (pc.getLocationX() == zombie.getLocationX() && pc.getLocationY() == zombie.getLocationY() && zombie.getCurrentHitPoints() > 0) { System.out.printf("%s attacked %s\n", pc.getName(), zombie.getName()); hitpoints: pc.setLocationx(pc.getLocationX() + 1); System.out.println("Turn Skipped. u for up, d for down, 1 for left, r for right. Any other key will skip the turn."); pc.attackEnemy (zombie); if (zombie.getCurrentHitPoints() <= 0) { System.out.printf("%s defeated %s\n", pc.getName(), zombie.getName());
58 59 60 if (zombie.getCurrentHitPoints() 61 Scanner rand = null; 162 int direction 63 64 65 66 67 68 69 AATTE 70 71 72 73 4 75 6 7 78 7 88 81 82 74 76 77 79 80 m 00 00 00 switch (direction) { case 0: case 1: 83 84} 85 86 H break; } zombie.setLocationY(zombie.getLocationY() + 1); case 2: if break; zombie.setLocationY(zombie.getLocationY() case 3: } = break; > 0) { rand.nextInt (4); zombie.setLocationX (zombie.getLocationX() - 1); break; 1); zombie.setLocationX(zombie.getLocationX() + 1); (pc.getLocationX() == zombie.getLocationX() && pc.getLocation Y() == zombie.getLocationY() && pc.getCurrentHitPoints () > 0) { System.out.printf("%s attacked %s\n", zombie.getName(), pc.getName()); zombie.attackPlayableCharacter(pc); if (pc.getCurrentHitPoints() <= 0) { System.out.printf("%s defeated %s\n", zombie.getName(), pc.getName()); }
We will now be creating the Game class. This class will contain the game logic that uses the Playable Character and Enemy classes to create a simple game. A. We want the main method to use a Scanner object to ask for the player's name. This Scanner object will also be used later for playing the game. Let's add the code for creating a Scanner object and asking for the player's name: Scanner input = new Scanner(System.in); System.out.print("Welcome to the Dungeon! What is your character's name? (Input name then press enter): "); String playerName = input.nextLine(); //Create instance of PlayableCharacter with inputed name PlayableCharacter pc = new PlayableCharacter (playerName); 11 System.out.println("Hello, + pc.getName()); B. We will now create a simple Enemy object called zombie. We will use the constructor with "Zombie" as the name, 2 as the locationX, and 3 as the location Y. * Enemy class constructor has 3 arguments. * Argument 1: String name Argument 2: int locationX * Argument 3: int locationY Enemy zombie = new Enemy ("Zombie", 2, 3);
C. Now, we will create the "game loop". This loop will handle the turns of the player and enemy while the player's hitpoints are not below 0 and while the player's location is not where the "stairs" are located (at 6,6). The condition of this loop will do the following three things: A. Check if the player's X location is 6 B. Check if the player's Y location is 6 C. Check if the player's current hitpoints is above 0 1/ //GAME LOOP START 11 while (!(pc.getLocationX() == 6 && pc.getLocationY() == 6) && pc.getCurrentHitPoints() > 0) { A. Let us print out both the player and the zombie (only if the zombie's current hitpoints are above 0: System.out.print(pc); if (zombie.getCurrentHitPoints () > 0) System.out.print (zombie);
B. We now want to go through the player's turn. The player should be prompted with instructions on how to move the character in this grid-based game. After getting the input from the player, a good option for moving the player based on their entry is to use a switch statement. (Note: The String in the switch expression is compared with the expressions associated with each case label as if the String.equals method were being used). Here is the incomplete code for this: 1/Player's turn System.out.println("Where to move? u for up, d for down, 1 for left, r for right. Any other key will skip the turn."); String in input.nextLine(); switch (in) { case "u": pc.setLocationY(pc.getLocationY() + 1); break; case "r": pc.setLocationX (pc.getLocationX() + 1); break; default: System.out.println("Turn Skipped. u for up, d for down, 1 for left, r for right. Any other key will skip the turn."); C. Finish this code to handle down and left. D. Next, we want the player to attack the zombie if the player moves to the same location as the zombie. We will check if the position of player is the same as the zombie and if the zombie's health is above 0. We will call pc.attackEnemy(zombie) to actually "attack" the enemy as the player. If the player's attack causes the zombie's health to go below 0, we will print a message saying the player defeated the zombie. if (pc.getLocationX() == zombie.getLocationX() && pc.getLocation Y() == zombie.getLocationY() && zombie.getCurrentHitPoints () > 0) { System.out.printf("%s attacked %s\n", pc.getName(), zombie.getName()); pc.attackEnemy (zombie); if (zombie.getCurrentHitPoints() <= 0) { System.out.printf("%s defeated %s\n", pc.getName(), zombie.getName()); } diraction
E. Now, we want to do the same thing as the previous two steps, but for the zombie. In this case, we will use a Random object so we can randomly generate the next direction the zombie should move in. We want to create a Random object so we can use it later. Add this to your code inside of your main method: Random rand = new Random(); F. Now, lets add the code for the zombie's turn: if (zombie.getCurrentHitPoints () > 0) { int direction = rand.nextInt (4); switch (direction) { case 0: zombie.setLocationY(zombie.getLocationY() + 1); break; case 1: zombie.setLocationY (zombie.getLocationY() - 1); break; case 2: zombie.setLocationx (zombie.getLocationX() 1); break; case 3: zombie.setLocationX (zombie.getLocationX() + 1); break; } if (pc.getLocationX() zombie.getLocationX() && pc.getLocationY() == zombie.getLocationY() && pc.getCurrentHitPoints () > 0) { System.out.printf("%s attacked %s\n", zombie.getName(), pc.getName()); zombie.attackPlayableCharacter (pc); if (pc.getCurrentHitPoints() <= 0) { System.out.printf("%s defeated %s\n", zombie.getName(), pc.getName()); } HE
G. And that's it! This logic should handle moving the player and enemy throughout the grid. If the player or enemy moves into the other one, they should attack and remove health, and if the player moves to 6,6 (where our "stairs" are), then the game ends. To make it clear to the player that the game is over (whether they were defeated or if they reached the goal), we should print a few things about the player and zombie to show their status. After the closing curly brace of the while loop, add this code: System.out.println("Game Over!"); System.out.println("Character: + pc.getName()); System.out.println("Remaining hitpoints: + pc.getCurrentHitPoints()); System.out.println("Zombie hitpoints: zombie.getCurrentHitPoints()); 11 TI input.close(); H. For the final step, add another enemy (in addition to the zombie) and write the code to handle the logic for it. It should follow the same logic as the zombie (moving in a random direction and attacking the player). (All you will have to do is create a new Enemy object, not make a new class.)
1 package lab; 3 public class PlayableCharacter { NAFn600 HIN 2 7 8 9 10 11- 12 13 14- 15 16 17 18- 19 20 219 22 23 //class fields private String name; private int currentHitPoints = 10; private int weaponDamage = 5; private int locationX 0; private int locationY = 0; ** @return the name NN 306 MARTIN to 31 32 EX 35 *1 public String getName() { return name; } 24 25 26 27- public int getCurrentHitPoints() { return currentHitPoints; 28 29 36- * @param name the name to set public void setName(String name) { this.name = name; /** * @param currentHitPoints the currentHitPoints to set *1 33- public void setCurrentHitPoints(int currentHitPoints) { this.currentHitPoints = currentHitPoints; 34 } 1 ** @return the currentHitPoints } /***
36 37 39- 40 41 42- 43 44 45- 46 47 48- 49 50 51- 52 53 54- 55 15.09 65 66- 67 10 10 /** 68 *k @return the weaponDamage 69- public int getWeaponDamage() { return weaponDamage; } ** @param weaponDamage the weaponDamage to set */ public void setWeaponDamage (int weaponDamage) { this.weaponDamage weaponDamage; * /** 56 57- 58 59 60- /** 61 * @return the locationY 62 */ 63- public int getLocationY() { return locationY; 64 public int getLocationX() { return locationX; @return the locationX } /** * @param locationX the locationX to set */ public void setLocationX(int locationX) { this.locationX = locationX; } = } ** * @param locationY the locationy to set */ public void setlocation(int location)
68 69- 70 71 72 73 74- 75 76 77 78 798 80 81 829 83 84 85 86 879 88 89 90 91 92 93 94 95 96 */ public void setLocationY(int locationY) { this.locationY= locationY; 97 } //Lower our current hitpoints by the damageTaken argument public void receiveDamage (int damageTaken) { this.currentHitPoints = damageTaken; } //Object interaction. The parameter is an Enemy object //We call the enemy's receiveDamage method within this attackEnemy method public void attackEnemy (Enemy enemy) { enemy.receiveDamage (this.weaponDamage); } public String toString() { //%s means String //%d means decimal number (integer) } return String.format("%s location: %d, %d. HitPoints: %d\n", this.getName(), this.getLocationX(), this.getLocationY(), this.getCurrentHitPoints()); public boolean equals (Object other) { //Cast the Object parameter to a Playable Character so we can access its name //If the name of the current PlayableCharacter equals the other PlayableCharacter being passed as a parameter, return true. Else, return false. PlayableCharacter tempCast = (PlayableCharacter) other; if (this.name.equals(tempCast.getName())) { } to set return true; } else { } return false;
85 86 -87- 88 89 90 91 92 93 94 95 96 97 98 % means decimal number (integer) return String.format("%s location: %d, %d. HitPoints: %d\n", this.getName(), this.getLocationX(), this.getLocationY(), this.getCurrentHitPoints()); public boolean equals (Object other) { //Cast the Object parameter to a PlayableCharacter so we can access its name //1f the name of the current PlayableCharacter equals the other PlayableCharacter being passed as a parameter, return true. Else, return false. PlayableCharacter tempCast = (PlayableCharacter) other; if (this.name.equals(tempCast.getName())) { } return true; } else { return false; //Constructor which will take name as parameter and will set to //this class name field public PlayableCharacter (String name) { this.name = name; 99 100 101- 102 103 } 104 } //End PlayableCharacter
2 Product.java 1 package lab; 2 Min 3 public class Enemy { 4 5 6 7 8 F550 600 9 10 119 12 13 18- 19 20 21- 22 23 24 25 22 14- public String getName() { 15 return name; 16 26 27- 28 30- 31 w w w w 32 336 3456 34 35 Basket java //class fields private String name; private int currentHitPoints = 4; private int weaponDamage = 2; private int locationx = 0; private int locationY = 0; /** * @return the name 36- } PlayableCharacter.java * @param name the name to set * public void setName (String name) { this.name = name; @return the currentHitPoints *1 public int getCurrentHitPoints() { return currentHitPoints; X Enemy. Java "Game.java @param currentHitPoints the currentHitPoints to set public void setCurrentHitPoints(int currentHitPoints) { this.currentHitPoints = currentHitPoints;
37 39- 40 41 42- 43 44 45- 46 47 489 49 50 51- 52 53 54- 55 56 579 58 59 606 61 62 630 64 65 66- 67 68 69- 70 71 70 * @return the weaponDamage public int getWeaponDamage() { return weaponDamage; } /** * @param weaponDamage the weaponDamage to set } public void setWeaponDamage (int weaponDamage) { this.weaponDamage = weaponDamage; /** * @return the locationX public int getLocationX() { return locationX; } /** * @param locationx the locationX to set public void setLocationX(int locationx) { this locationX = locationX; } @return the locationy */ public int getLocationY() { return locationY; Le Java * } /** * emy.java @param locationY the location to set *1 public void setLocationY(int locationY) { this locationY= locationY; } Gamejava *Score.java Student.java Coursejava
71 72 730 74 750 76 -77 0 78 79 80 81 820 00 00 00 00 00 00 0 83 84 85 86 61 87 88 89 908 91 92 93 94 95 οοοο οι } 966 97 98 99 } public void receiveDamage (int damageTaken) { } public void attackPlayableCharacter (PlayableCharacter player) { } public String toString() { } } ny; @Override public boolean equals (Object other) { if(this == other) return true; } return String.format("Name: %s, Current hit points: %d, Weapon Damage: %d, Position: (%d, %d)" name, currentHitPoints, weaponDamage, locationX, location Y); public Enemy (String name, int locationX, int locationY) { this.name = name; this.locationX = locationX; this.locationY= locationY; } if(!(other instanceof Enemy)) return false; Enemy enemy = (Enemy) other; return name != null && name.equals (enemy.name); public Enemy (String name) { this (name, 2, 3);
> Lab 4 (in 14-console-game-jbwill88) [14-console-gam 53 54 > STC > Y >Enemy.java >Gamejava N > lab 00 > PlayableCharacter.java >> PlayableCharacter JRE System Library [jre] README.md JRE System Library [JavaSE-17] src > example In on 55 5.6 57 58 } 59 60 if (zombie.getCurrentHitPoints() > 0) { 61 Scanner rand = null; 362 int direction = rand.nextInt (4); switch (direction) { case 0: 63 64 65 66 67 68 69 70 71 72 73 74 75 75 7 76 System.out.printf("%s attacked %s\n", pc.getName(), zombie.getName()); pc.attackEnemy (zombie); if (zombie.getCurrentHitPoints() <= 0) { System.out.printf("%s defeated %s\n", pc.getName(), zombie.getName()); } 77 zombie.setLocationY(zombie.getLocationY() + 1); break; case 1: zombie.setLocationY(zombie.getLocationY() break; case 2: zombie.setLocationX (zombie.getLocationX() - 1); break; case 3: 1); zombie.setLocationX (zombie.getLocationX() + 1); break; } if (pc.getLocationX() == zombie.getLocationX() && pc.getLocationY() == zombi Console Problems D Debug Shell Coverage Git Repositories Game.OddEvenSum Java Application] C:\Users\jbwgr\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win Error: Could not find or load main class lab. Game$OddEvenSum Caused by: java.lang.ClassNotFoundException: lab. Game$OddEvenSum