Need to complete a program in java please. There's 3 exercises. The program is a game with a character class, enemy clas

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Need to complete a program in java please. There's 3 exercises. The program is a game with a character class, enemy clas

Post by answerhappygod »

Need to complete a program in java please. There's 3 exercises. The program is a game with a character class, enemy class and game class. I completed the character and Enemy class and cannot get the game class to work. I'll provide the instructions along with the other 2 classes I coded. Need ASAP. Will give a thumbs up if done properly. Be sure to use the sample code in the images and follow the steps given in the pictures. Thank you. Last programmer did it incorrectly.
PlayableCharacter class:
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 1
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 1 (198 KiB) Viewed 19 times
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 2
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 2 (298.82 KiB) Viewed 19 times
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 3
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 3 (119.55 KiB) Viewed 19 times
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 4
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 4 (72.54 KiB) Viewed 19 times
Enemy class:
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 5
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 5 (242.37 KiB) Viewed 19 times
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 6
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 6 (332.2 KiB) Viewed 19 times
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 7
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 7 (332.62 KiB) Viewed 19 times
Instructions for Creating the Game class:
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 8
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 8 (179.29 KiB) Viewed 19 times
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 9
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 9 (153.3 KiB) Viewed 19 times
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 10
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 10 (134.26 KiB) Viewed 19 times
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 11
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 11 (144.47 KiB) Viewed 19 times
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 12
Need To Complete A Program In Java Please There S 3 Exercises The Program Is A Game With A Character Class Enemy Clas 12 (92.85 KiB) Viewed 19 times
Follow steps ABC and A thru H, doing as each step says. Please read the instructions and do it correctly, using any sample code that is provided. Thank you.
1 package lab;| 2 3 public class PlayableCharacter { 4 5 6 7 8 9 10 11- 12 13 140 15 16 17 189 19 20 21 22 23 24 25e 26 27 289 29 30 mm mm PENE 310 32 33 m & 34 //class fields private String name; private int currentHitPoints = 10; private int weaponDamage = 5; private int locationX = 0; private int locationY = 0; @return the name /** ** public String getName() { } public PlayableCharacter (String name) { this.name = name; //Alternatively, you could have used setName to set the name } return name; * @param name the name to set *1 public void setName(String name) { this.name = name; } /** * @return the currentHitPoints */ public int getCurrentHitPoints() { return currentHitPoints; } /** the urrentHitPoints to set
33 34- 35 36 378 38 39 40- 41 42 43- 44 45 46 47 48 49- 50 51 528 53 5 L 54 558 56 57 589 59 60 61- 62 63 555 } * @param currentHitPoints the currentHitPoints to set public void setCurrentHitPoints (int currentHitPoints) { this.currentHitPoints = currentHitPoints; } **** * @return the weaponDamage public int getWeaponDamage () { return weaponDamage; } /** c @param weaponDamage the weaponDamage to set public void setWeaponDamage (int weaponDamage) { this.weaponDamage = weaponDamage; } /** 53 @return the locationX */ public int getLocationX() { return locationX; } @param locationX the locationX to set */ public void setLocationX(int locationx) { this.locationX = locationX; * }
64- 65 66 676 68 69 70- 71 72 738 74 75 76 77 789 79 80 81 82 830 84 85 869 87 88 89 90 } /** @return the locationY public int getLocationY() { return locationY; } /** @param locationY the locationY to set */ public void setLocationY(int locationY) { this.locationY= locationY; } //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());
90 91- 92 93 94 95 96 97 98 99 100 101 102 103 public boolean equals (Object other) { //Cast the Object parameter to a PlayableCharacter 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; (this.name.equals(tempCast.getName())) { 104 105 } 106 } if } else { } return true; return false; Line: S
1 package lab; 8 9 10 public class Enemy [ //class fields private String name; private int currentHitPoints = 4; private int weaponDamage = 2; private int locationX = 8; 12 15 17 18 20 21- 22 23 245 25 26 278 28 29 306 31 32 33- 34 35 private int locationY = 0; /** * @return the name */ public String getName() { return name; } ** @param name the name to set *1 public void setName(String name) { this.name = name; rjava √** * @return the currentHitPoints *1/ public int getCurrentHitPoints() { return currentHitPoints; ** R } Enemy.java @param currentHitPoints the currentHitPoints to set public void setCurrentHitPoints (int currentHitPoints) { this.currentHitPoints = currentHitPoints; Game.java *Score.java
36- 37 379 38 39- st 41 42- 43 44 45- 46 47 48€ 49 50 519 52 53 540 55 56 570 58 59 60 61 62 630 64 65 ESE * @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; }
66- 67 68 694 70 71 74 750 76 *778 78 80 81 829 483 00 00 00 00 00 00 00 0 84 85 86 or 88 89 90- 91 92 93 94 95 96- 6666 6789 97 98 ** * @param location the locationY to set public void setLocationY(int locationY) { this locationY= locationY; } 99 } public void receiveDamage (int damageTaken) { } public void attackPlayable Character (PlayableCharacter player) { } public String toString() { return String.format("Name: %s, Current hit points: %d, Weapon Damage: %d, Position: (%d, %d)" name, currentHitPoints, weaponDamage, locationX, locationY); } @Override public boolean equals (Object other) { if(this == other) return true; } } 3 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);
Exercise 3: Creating the Game class 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); 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 *1 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
C. Check if the player's current hitpoints is above 0 11 //GAME LOOP START 11 while (!(pc.getLocationX() == 6 && pc.getLocationY() 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 () > e) 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: == //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": break; 6) && pc.getCurrentHitPoints() > 0) { pc.setLocationY (pc.getLocationY() + 1); case "r": break; pc.setLocationX(pc.getLocationX() + 1); 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."); } de to handle down and left.
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.getLocationY() == 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()); } 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:
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); case 1: } break; } zombie.setLocationY(zombie.getLocationY() - 1); case 2: break; if case 3: zombie.setLocationX (zombie.getLocationX() - 1); } break; zombie.setLocationx (zombie.getLocationX () + 1); break; } == == (pc.getLocationX() zombie.getLocationX () && pc.getLocationY() 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()); zombie.getLocationY() && pc.getCurrentHitPoints () > 0) { into the other one, they should attack and remove
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()); + zombie.getCurrentHitPoints()); System.out.println("Zombie hitpoints: 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.)
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply