Question: Can someone help me fix this java code to make it fit the standards of this checklist and UML attached below? import java.awt.Point; import java.io.*;
Can someone help me fix this java code to make it fit the standards of this checklist and UML attached below?
import java.awt.Point; import java.io.*; import java.util.*;
public class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in);
Hero hero = null; Level map = new Level(); ItemGenerator itemGenerator = new ItemGenerator(); EnemyGenerator enemyGenerator = new EnemyGenerator(); File f = new File( "hero.dat" );
if( f.exists() ) { try { ObjectInputStream in = new ObjectInputStream(new FileInputStream( f )); hero = (Hero) in.readObject(); in.close(); } catch( IOException e ) { System.out.println("Error processing file."); } catch( ClassNotFoundException e ) { System.out.println("Could not find class."); }
if (hero.getLevel() == 4) { System.out.println("Welcome back! You have completed all 3 levels. So, you will restart the gane."); map.generateLevel(1); hero = new Hero(hero.getName(), hero.getQuip(), 10, 1, 100, map.findStartlocation()); } } else { String name; map.generateLevel(1); System.out.println("Welcome to the Dungeon of Dispair! Enter your name: "); name = input.nextLine(); hero = new Hero (name, "Haaa Yaaa!", 20, 1, 100, map.findStartlocation()); }
map.generateLevel(hero.getLevel());
while (hero.getHp() > 0 && hero.getLevel()
if (hero.getHp() > 0) { saveProgress(hero,f); } }
if ( hero.getLevel() == 4) { System.out.println("You have completed all 3 levels. You win!"); }
}
// Plays a level of the game
public static void playLevel (Hero hero, ItemGenerator itemGenerator, EnemyGenerator enemyGenerator, Level map ) { int direction; char room = 'a';
hero.setLocation(map.findStartlocation()); System.out.println("May the odds be in your favor. You are entering Level " + hero.getLevel() + ".");
if ( !hero.getItems().isEmpty()) { sellItems(hero); }
while (hero.getHp() > 0 && room != 'f') { direction = directionMenu(map, hero); room = goDirection(direction, hero, map);
if(room == 'm') { int action, flee; Random generator = new Random(); Enemy enemy = enemyGenerator.generateEnemy(hero.getLevel());
System.out.println(hero.getName() +" encounters a " + enemy.getName() + ". " );
enemy.attack(hero);
if(hero.getHp() > 0) { System.out.println(enemy.getName() + " has " + enemy.getHp() + " hp. ");
System.out.println(" What do you want to do? " + "1. Run away 2. Attack"); action = UserInput.getInt();
while (action 2) { System.out.println("Invalid entry. Try again."); action = UserInput.getInt(); }
if (action == 1) //run away but only once at a time { flee = generator.nextInt(4)+1; room = goDirection(flee, hero, map);
while (room == '0' || room == '*') { flee = generator.nextInt(4)+1; room = goDirection(flee, hero, map);
} System.out.println(hero.getName() + " has successfully ran away."); if( room == 'm') { monsterFight(hero, enemy); } } else { monsterFight(hero, enemy); } } else { System.out.println(hero.getName() + " has died."); } }
if(room == 'i') { itemRoom(hero, itemGenerator); } if (room == '0') { System.out.println("Cannot move in that direction. Try again."); } if (room == '*') { System.out.println("You have already been to that location."); } }
if (hero.getHp()
if (room == 'f') { System.out.println("Congratulations! You passed level " + hero.getLevel() + "!");
hero.increaseLevel(); hero.heal(20 * (int) (Math.pow(2,hero.getLevel()-1))); hero.display();
if(hero.getLevel()
// Displays a list of directions and allows user input
public static int directionMenu(Level map, Hero hero) { int i; map.displayMap(hero.getLocation()); System.out.println("Choose a direction:"); System.out.println("1. North 2. South 3. East 4. West"); i = UserInput.getInt();
while (i 4) { System.out.println("Invalid entry. Try again."); i = UserInput.getInt();
} return i; }
//Goes to another location based on the direction choice and returns the char of the
public static char goDirection (int i, Hero hero, Level map ) { char roomType; if (i == 1) { return roomType = hero.goNorth(map); } if (i == 2) { return roomType = hero.goSouth(map); } if (i == 3) { return roomType = hero.goEast(map); } if (i == 4) { return roomType = hero.goWest(map); } else {
return '0'; } }
// Activates a fight between the hero and the enemy
public static void monsterFight(Hero hero, Enemy enemy) { while (hero.getHp() > 0 && enemy.getHp() > 0) { hero.attack(enemy);
if(enemy.getHp() > 0) { enemy.attack(hero); }
if (potionExists(hero) == true) { usePotion(hero); } }
if (enemy.getHp()
if(hero.pickUpItem(item) == false) { System.out.println("The inventory is full. The enemy's item will be sold. " + "The item was sold for " + item.getValue() + "gold. "); hero.collectGold(item.getValue()); } System.out.println(hero.getName() +" sees gold from the " + enemy.getName() + "'s carcass."); hero.collectGold(gold); } else if(hero.getHp()
// Allows the user to sell their items
public static void sellItems(Hero hero) { int index, action = 1; Item item; ArrayList list;
System.out.println("Before you begin, you may sell your items.");
while (action == 1 && !hero.getItems().isEmpty()) { list = hero.getItems(); System.out.println("Pick which item to sell:"); for (int i = 0; i
System.out.println("You have chosen to sell " + item.getName() + " for " + item.getValue() + " gold."); System.out.println("You currently have " + hero.getGold() + " gold. "); hero.collectGold(item.getValue()); hero.removeItem(index-1); System.out.println("Do you want to sell another item? 1. Yes 2.No"); action = UserInput.getInt();
while (action 2) { System.out.println("Invalid entry. Try again."); action = UserInput.getInt(); } }
if (hero.getItems().isEmpty()) { System.out.println("You have sold all of your items."); }
}
// Allows the hero use their potion
public static void usePotion(Hero hero) { int action; System.out.println("Do you want to use your health potion? 1. Yes 2. No"); action = UserInput.getInt();
while (action 2) { System.out.println("Invalid entry. Try again."); action = UserInput.getInt(); }
if (action == 1) { int index=0; ArrayList items = hero.getItems();
for (int i=0; i
hero.heal(20 * (int) (Math.pow(2,hero.getLevel()-1))); hero.removeItem(index); System.out.println(hero.getName() + " took the potion and now has " + hero.getHp() + " hp."); } }
// Returns true if the hero's item list has the potion
public static boolean potionExists (Hero hero) { boolean exists = false; ArrayList items = hero.getItems();
for (int i=0; i
}
// Runs when the user enter a room with an item. The item will be sold if inventory is full
public static void itemRoom(Hero hero, ItemGenerator itemGenerator) { int action; Item item = itemGenerator.generateItem();
System.out.println(hero.getName() + " finds a " + item.getName());
if (hero.getItems().size()
while (action 2) { System.out.println("Invalid entry. Try again."); action = UserInput.getInt(); }
if (action == 1) //keep it { hero.pickUpItem(item); } else { System.out.println("You decided to sell the item."); hero.collectGold(item.getValue()); } } else { System.out.println("Your inventory is full. The item will be sold."); hero.collectGold(item.getValue()); } }
// Saves Hero into a dat file
public static void saveProgress(Hero hero, File f) { try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream( f )); out.writeObject( hero ); out.close(); System.out.println("Saving progress..."); } catch( IOException e ) { System.out.println("Error processing file."); } } } --------------------------------------------------------------------------------------------------------------------
public abstract class Entity { private String name;
private String quip;
private int level;
private int hp;
private int gold;
//Constructor constructs a Character with the necessary info
public Entity(String n, String q, int h, int l, int g) { name = n; quip = q; hp = h; level = l; gold = g; }
// Method allows a character to attack another character
public abstract void attack(Entity e);
// Gets the character's name
public String getName() { return name; }
// Gets the character's quip
public String getQuip() { return quip; }
// Gets the character's hit points
public int getHp() { if (hp > 0) { return hp; } else { return 0; } }
//Gets the character's level public int getLevel() { return level; }
// Gets the character's gold
public int getGold() { return gold; }
// Increments the character's level by 1
public void increaseLevel() { level++; }
// Adds points to the character's hit points to heal
public void heal (int h) { hp = h; }
// Subtracts points from the character's hit points to take damage
public void takeDamage (int h) { hp = hp - h; }
// Adds gold to the character's wealth
public void collectGold (int g) { gold = gold + g; System.out.println(name + " has collected " + g + " gold. " + name + " now has " + gold + " gold."); }
// Displays the character's condition
public void display() { System.out.println(getName() + "'s level is now at Level " + level); System.out.println(getName() + " has " + gold + " gold"); System.out.println(getName() + " now has " + hp + " hp"); } } ------------------------------------------------------------------------------------------------------------------------------------ import java.util.Random;
public class Enemy extends Entity { private Item item;
public Enemy (String n, String q, int h, int l, int g, Item i) { super(n, q, h, l, g); item = i; }
@Override public void attack(Entity e) { Random generator = new Random(); int attackPoints = generator.nextInt(getHp()) + 1; c.takeDamage(attackPoints);
System.out.println("\"" + getQuip() + "!\" " + getName() + " attacks " + c.getName() + " for " + attackPoints + " hp."); System.out.println(c.getName() + " now has " + c.getHp() + " hp. "); } public Item getItem() { return item; } } -------------------------------------------------------------------------------------------------------------------------- import java.io.File; import java.io.FileNotFoundException; import java.util.*;
public class EnemyGenerator { public ArrayList enemyList;
public EnemyGenerator() { enemyList = new ArrayList(); ItemGenerator itemList = new ItemGenerator();
try { Scanner read = new Scanner(new File("EnemyList.txt"));
do { Random generator = new Random(); int gold = (generator.nextInt(10) + 1) * 10; // randomizes by multiples of 10 up to 100
Item item = itemList.generateItem(); String line = read.nextLine(); String[] tokens = line.split(","); //use commas to split up the data in the line
Enemy enemy = new Enemy(tokens[0], tokens[1], Integer.parseInt(tokens[2]), 1, gold, item);
enemyList.add(enemy);
} while (read.hasNextLine());
read.close(); } catch (FileNotFoundException fnf) {
System.out.println("File was not found"); } }
public Enemy generateEnemy(int level) {
Random generator = new Random(); ItemGenerator itemList = new ItemGenerator(); int gold = (generator.nextInt(10) + 1) * 10; // randomizes by multiples of 10 up to 100
Item item = itemList.generateItem();
int index = generator.nextInt(enemyList.size());
Enemy enemy = enemyList.get(index); Enemy copy = new Enemy(enemy.getName(), enemy.getQuip(), enemy.getHp() * level, enemy.getLevel(), gold, item);
while (copy.getLevel()
public class Hero extends Entity { private ArrayList items;
private Point location;
public Hero (String n, String q, int h, int l, int g, Point start) { super(n, q, h, l, g); items = new ArrayList (); location = start; }
@Override public void attack(Character c) { Random generator = new Random(); int attackPoints = generator.nextInt(getHp()) + 1; c.takeDamage(attackPoints);
System.out.println("\"" + getQuip() + "!\" " + getName() + " attacks the " + c.getName() + " for " + attackPoints + " hp."); System.out.println("The " + c.getName() + " now has " + c.getHp() + " hp. "); }
public ArrayList getItems() { return items; }
public boolean pickUpItem(Item item) { if (items.size()
public void removeItem (Item item) { items.remove(item); }
public void removeItem (int i) { items.remove(i); }
public Point getLocation () { return location; }
public void setLocation (Point p) { location = p; }
public char goNorth (Level l) { int x = (int) location.getX(); int y = (int) location.getY(); Point north = new Point(x,y-1); char c = l.getRoom(north);
if (c != '0' && c != '*') { setLocation(north); }
return c; }
public char goSouth (Level l) { int x = (int) location.getX(); int y = (int) location.getY(); Point south = new Point(x,y+1); char c = l.getRoom(south);
if (c != '0' && c != '*') { setLocation(south); }
return c; }
public char goEast (Level l) { int x = (int) location.getX(); int y = (int) location.getY(); Point east = new Point(x+1,y); char c = l.getRoom(east);
if (c != '0' && c != '*') { setLocation(east); }
return c; }
public char goWest (Level l) { int x = (int) location.getX(); int y = (int) location.getY(); Point west = new Point(x-1,y); char c = l.getRoom(west);
if (c != '0' && c != '*') { setLocation(west); }
return c; } } -------------------------------------------------------------------------------------------------------------------
public class Item { public String name;
public int goldValue;
public Item (String n, int v) { name = n; goldValue = v; }
public String getName() { return name; }
public int getValue() { return goldValue; }
} --------------------------------------------------------------------------------------------------------------- import java.util.*; import java.io.*;
public class ItemGenerator { public ArrayList itemList;
public ItemGenerator() { itemList = new ArrayList (); try { Scanner read=new Scanner(new File("ItemList.txt"));
do { String line = read.nextLine(); String [] tokens = line.split(","); //use commas to split up the data in the line int gold =Integer.parseInt(tokens[1]); Item item = new Item(tokens[0], gold ); // input data in constructor
itemList.add(item);
}while(read.hasNextLine());
read.close(); } catch(FileNotFoundException fnf){
System.out.println("File was not found"); } }
public Item generateItem() { Random generator = new Random(); int index = generator.nextInt(itemList.size());
return itemList.get(index); } } ----------------------------------------------------------------------------------------------------------- import java.io.*;
import java.util.Scanner; import java.awt.Point;
public class Map { public char [][] level;
public Level () { level = new char [5][5]; }
public void generateLevel (int levelNum) { String lev = Integer.toString(levelNum);
try { Scanner read=new Scanner(new File("Level" + lev + ".txt"));
do { String line = read.nextLine(); String [] tokens = line.split(","); for(int i=0; i { for(int j = 0; j
} while(read.hasNextLine());
read.close(); } catch (FileNotFoundException fnf) { System.out.println("File was not found"); } }
public char getRoom (Point p) { try { int row = (int) p.getY(); int col = (int) p.getX();
return level[row][col]; } catch(ArrayIndexOutOfBoundsException oob) { return '0' ; } }
public void displayMap (Point p) { int row = (int) p.getY(); int col = (int) p.getX(); level[row][col] = '*'; System.out.println("__________"); for(int i = 0; i { System.out.print("|"); for(int j = 0; j { System.out.print(level[i][j] + " "); } System.out.print("|"); System.out.println(); } System.out.println("__________ "); }
public Point findStartlocation () { int x=0; int y=0;
for(int i = 0; i { for(int j = 0; j { if(level[i][j] == 's') { x = j; y = i; } } } Point start = new Point(x,y); return start; } } --------------------------------------------------------------------------------------------------------- import java.util.*;
public class UserInput {
public static int getInt() { Scanner in = new Scanner(System.in); boolean valid = false; int validNum = 0;
while( !valid) { if( in.hasNextInt() ) { validNum = in.nextInt(); valid = true; } else { in.next(); System.out.println("Invalid. Try again."); } } return validNum; } }


ItemGenerator ArrayList
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
