Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you please fix my code which isnt working properly ? *** I think the issue is in the for loop for goPlay() as seen

Can you please fix my code which isnt working properly ?

*** I think the issue is in the for loop for goPlay() as seen in the picture where the newlife variable that holds the players health does get updated but does not carry the new updated value for the next monster as the for loop reads through the monsters in the dungeon that the player must fight. ***

//requirements//

1. A number of monsters are created, each of which has a name and a strength value 2. A player is created, with a fixed starting health value 3. A monster is chosen at random, and added to the dungeon 4. The player is shown how many monsters are in the dungeon, and asked if they think theyll be able to handle another one 5. Steps 3 & 4 are repeated until the player decides thats enough 6. The player proceeds through the dungeon, with the players health reduced by the strength value of each monster encountered 7. If the players health drops to 0 or below, they die and lose the game 8. If the player makes it through all the monsters without dying, they win, and their score for the game is the total strength of the monsters defeated

Scanner technique to ask the user repeatedly if they are ready to enter the dungeon. You will also need to us a while loop for this task. I would recommend using an ArrayList to keep track of all the monsters that have been added to the dungeon for the player to fight. You will need to reduce the players health as they face monsters, and increase the variable tracking the players score in case they win. When the user does enter the dungeon, your program should produce output in the following format for each of the monsters the player faces in the dungeon. Monster 1 is a Goblin. It deals 5 damage to you, leaving you with 35 health

Code//

package labTask1;

import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Scanner;

public class lab2 {

public static void main(String[] args) { // TODO Auto-generated method stub List monsters = new ArrayList(); ArrayList dungeons = new ArrayList(); monsters.add(new Monsters("Dragon", 10)); monsters.add(new Monsters("Goblin", 4)); monsters.add(new Monsters("Goblin", 6)); monsters.add(new Monsters("Goblin", 2)); monsters.add(new Monsters("Skeletons", 4)); monsters.add(new Monsters("Skeletons", 3)); monsters.add(new Monsters("Skeletons", 7)); monsters.add(new Monsters("Skeletons", 5)); monsters.add(new Monsters("Witches", 2)); monsters.add(new Monsters("Witches", 6)); Scanner input = new Scanner(System.in); System.out.println("Are you ready to enter the dungeon?"); String ready= input.nextLine(); start(monsters, dungeons,ready); } public static void start(List monsters, ArrayList dungeons,String ready) { if(ready.equals("Yes")) { Collections.shuffle(monsters); Monsters randomMonster = monsters.get(0); dungeons.add(randomMonster); System.out.println("Random monster appear: " + randomMonster.getName()+ " Strength: "+randomMonster.getStrength()); goPlay(monsters, dungeons,randomMonster,Player.getpHealth()); } else { while (ready.equals("No")) { Collections.shuffle(monsters); Monsters randomMonster = monsters.get(0); dungeons.add(randomMonster); //int conversion = dungeons.size(); //System.out.println(conversion); System.out.println("Random monster appear: " + randomMonster.getName()+ " Strength: "+randomMonster.getStrength()); Scanner input = new Scanner(System.in); System.out.println("Are you ready to enter the dungeon?"); ready= input.nextLine(); if(ready.equals("Yes")) { goPlay(monsters, dungeons, randomMonster,Player.getpHealth()); } } } } public static void goPlay( List monsters, ArrayList dungeons, Monsters randomMonster,int pHealth ){ int newLife; System.out.println("Random monster appear: "); int conversion = dungeons.size(); System.out.println(conversion); int player = Player.getpHealth(); for(int i = 0; i

My other classes//

package labTask1;

public class Monsters { String mName; int mStrength;

public Monsters(String mName, int mStrength) { //super(); this.mName=mName; this.mStrength=mStrength; } public String getName(){return mName;} public int getStrength(){return mStrength;}

package labTask1;

public class Player { int overall; static int pHealth=8;

public Player (int pHealth ) { //super(); //this.overall=overall; this.pHealth=pHealth; }

public static int getpHealth () { return pHealth; }

} image text in transcribed

Are you ready to enter the dungeon? No Random monster appear: Skeletons Strength: 4 Are you ready to enter the dungeon? No Random monster appear: Skeletons Strength: 3 Are you ready to enter the dungeon? Yes Random monster appear: 2 Skeletons: 4 4: IM STRENGTH 8: IM HEALTH 41 AM LEFT LEFT OVER LIFE4 Skeletons: 3 3: IM STRENGTH 8 : IM HEALTH 51 AM LEFT LEFT OVER LIFE5 You Won

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

3rd Edition

978-1119907466

Students also viewed these Databases questions

Question

What was the first HR error to be made?

Answered: 1 week ago