Create a program that allows a user to play a dungeon maze game. Use the UML dia
ID: 3745533 • Letter: C
Question
Create a program that allows a user to play a dungeon maze game. Use the UML diag on the next page to help you create your classes. The dungeon maps, the item list, and the enemy list are all read in from text files and stored in their respective classes (do not assume the length of the enemy or item files). ram 1. Item - name, item's value Bag o' Gold - doesn't take up inventory, just add it to the hero's gold. b. a. Health Potion - buy/sell/heals for 25 c. Armor items- use an item's value for both maxHp and gold 2. Enemy - name, quip, hit points at level 1, magical or physical type creature a. Use the hero's level as a multiplier to calculate the hit points b. Construct enemy as magical or non-magical when generating 3. Map - 5x5 grid of characters a. s-start, f - finish, m-monster, i - item, n - nothing When the game starts, the hero is level 1 and begins at the start position of the first map Display a covered map to the user and have a marker to show where the hero is located Allow the user to choose a direction (North, South, East, or West) to explore the map 1. If the user enters a room with a monster, the user has the option of either attacking the monster or running away. If they attack, they choose to use swords or magic. They attack the monster for a random amount of damage based on the hero's level. The monster will attack back (if it's still alive), also for a random amount of damage based on the level. Repeat until the monster is dead or the user runs away. If they run away, then they will run in a valid random direction to an adjacent room (ie. not into a wall), and the monster stays where it is. If they defeat the monster, then they collect the item from the monster's corpse and the room is cleared. If they have a potion in their inventory, make a third option when fighting to use the potion to refill the hero's hit points, +25 or up to the hero's level max. If the hero dies, then the game is over. 2. If the user enters a room with an item, it is picked up and added to the hero's inventory and the room is cleared. The hero's inventory has a max capacity of 5 items, so if their inventory is already full, then the item is left where it was 3. If the user re-enters the start position, then they may sell the items in their inventory for the listed value, the gold is then added to the hero's gold. They also have the option to buy a potion for 25 gold if there is room in their inventory. 4. If the user enters the finish, then they have passed the level. The hero gains a level and the hero's maxHp increases by 10. The hero moves to the next map Re-load the first map for level 4, second map for level 5, etc Notes Item/EnemyGenerator classes read in the files in the constructor to make a list of templates. The generate functions randomly choose from the template list and construct new items/enemies (ie. do not return the template's reference). Make sure the enemies you create are of the right type (either physical or magical) When generating enemies, multiply by the hero's level to get the enemy's maxHp The armor in the hero's inventory adds the value to the hero's hit points ° . Use the Point class from the java.awt library to keep the location of the hero * Please so not add any extra data members or methods to the UMLExplanation / Answer
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() < 4)
{
playLevel(hero, itemGenerator, enemyGenerator, map);
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 <1 ||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("Cannor move in that direction. Try again.");
}
if (room == '*')
{
System.out.println("You have already been to that location.");
}
}
if (hero.getHp() <= 0)
{
System.out.println("Game over!");
}
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() < 4)
{
map.generateLevel(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 < 1 || 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() <= 0)
{
System.out.println(hero.getName() +" has successfully killed the " + enemy.getName() + ".");
Item item = enemy.getItem();
int gold = enemy.getGold();
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() <= 0)
{
System.out.println(hero.getName() + " has died");
}
}
// Allows the user to sell their items
public static void sellItems(Hero hero)
{
int index, action = 1;
Item item;
ArrayList <Item> 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 < list.size(); i++)
{
System.out.println( (i+1) + ". " + list.get(i).getName());
}
index = UserInput.getInt();
item = hero.getItems().get(index-1);
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 <1 ||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 <1 ||action > 2)
{
System.out.println("Invalid entry. Try again.");
action = UserInput.getInt();
}
if (action == 1)
{
int index=0;
ArrayList <Item> items = hero.getItems();
for (int i=0; i< items.size(); i++)
{
if (items.get(i).getName().equalsIgnoreCase("Health Potion"))
{
index = 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 <Item> items = hero.getItems();
for (int i=0; i< items.size(); i++)
{
if (items.get(i).getName().equalsIgnoreCase("Health Potion"))
{
exists = true;
}
}
return exists;
}
// 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() < 5)
{
System.out.println("What do you want to do? 1. Keep it 2. Sell it");
action = UserInput.getInt();
while (action <1 ||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.");
}
}
}
--------------------------------------------------------------------------------------------------------------------
import java.io.Serializable;
public abstract class Character implements Serializable
{
private String name;
private String quip;
private int level;
private int hp;
private int gold;
//Constructor constructs a Character with the necessary info
public Character(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(Character c);
// 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.io.Serializable;
import java.util.Random;
public class Enemy extends Character implements Serializable
{
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(Character c)
{
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.io.Serializable;
import java.util.*;
public class EnemyGenerator implements Serializable {
public ArrayList<Enemy> enemyList;
public EnemyGenerator() {
enemyList = new ArrayList<Enemy>();
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() < level) {
copy.increaseLevel();
}
return copy;
}
}
--------------------------------------------------------------------------------------------------------------------------
import java.io.*;
import java.util.*;
import java.awt.Point;
public class Hero extends Character implements Serializable
{
private ArrayList <Item> 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 <Item> ();
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 <Item> getItems()
{
return items;
}
public boolean pickUpItem(Item item)
{
if (items.size()<5)
{
items.add(item);
System.out.println(getName() + " has successfully picked up " + item.getName() + ".");
return true;
}
else return false;
}
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;
}
}
-------------------------------------------------------------------------------------------------------------------
import java.io.Serializable;
public class Item implements Serializable
{
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 implements Serializable
{
public ArrayList <Item> itemList;
public ItemGenerator()
{
itemList = new ArrayList <Item>();
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 Level implements Serializable
{
public char [][] level;
public Level ()
{
level = new char [4][4];
}
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<tokens.length; i++)
{
for(int j = 0; j < tokens[i].length(); j++)
{
level[i][j] = tokens[i].charAt(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<level.length; i++)
{
System.out.print("|");
for(int j = 0; j<level[0].length; 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<level.length; i++)
{
for(int j = 0; j<level[0].length; 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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.