From f937a017cd2ac2392b210e5046fc74366eb6c267 Mon Sep 17 00:00:00 2001 From: jslightham <31053827+jslightham@users.noreply.github.com> Date: Sat, 1 Jun 2019 02:05:04 -0400 Subject: [PATCH 1/2] comments --- src/com/bayviewglen/zork/Combat.java | 27 ++++++++- src/com/bayviewglen/zork/Command.java | 11 +--- src/com/bayviewglen/zork/CommandWords.java | 14 +++-- src/com/bayviewglen/zork/Entities/Entity.java | 4 ++ src/com/bayviewglen/zork/Game.java | 55 +++++++++++++++++-- src/com/bayviewglen/zork/Parser.java | 20 ++++--- src/com/bayviewglen/zork/Room.java | 8 ++- 7 files changed, 106 insertions(+), 33 deletions(-) diff --git a/src/com/bayviewglen/zork/Combat.java b/src/com/bayviewglen/zork/Combat.java index a439ef9..b8f7510 100644 --- a/src/com/bayviewglen/zork/Combat.java +++ b/src/com/bayviewglen/zork/Combat.java @@ -7,7 +7,10 @@ import com.bayviewglen.zork.Entities.Player; import com.bayviewglen.zork.Entities.Enemies.Enemy; import com.bayviewglen.zork.Items.Item; import com.bayviewglen.zork.Items.Shavingcream; - +/* + * This combat class stores information, and handles turns for both the player and enemy that are in combat. + * Whenever combat takes place, this class is instantiated and stored in the Game class. + */ public class Combat { private Player player; private Enemy enemy; @@ -17,7 +20,12 @@ public class Combat { this.player = player; this.enemy = enemy; } - // return new health of enemy + /* + * Handles the attack made by a player. Called whenever a player uses the command word "attack" + * This method takes the item from the command as a string, and turns it into an object so that it can find info about the item. + * When the player attacks, there is a 10% chance of missing, 10% chance of a critical hit, and an 80% chance of a normal hit. + * Returns the new health of the enemy. + */ public double playerAttack(String item) { Class clazz; Item object; @@ -27,6 +35,7 @@ public class Combat { object = (Item) ctor.newInstance(); double rand = Math.random(); + // Special case when weapon is shaving cream, blind enemy if(object.equals(new Shavingcream())) { System.out.println("You blinded " + enemy.getName()); player.removeFromInventory(new Shavingcream()); @@ -44,6 +53,7 @@ public class Combat { } else { enemy.setHealth(enemy.getHealth()-object.getDamage()); + // Ensure health is not negative if(enemy.getHealth() < 0) enemy.setHealth(0); System.out.println("You did " + object.getDamage() + " damage! " + enemy.getName() + " is now at " + enemy.getHealth() + "% health."); @@ -55,9 +65,14 @@ public class Combat { turn = 1; return enemy.getHealth(); } - + /* + * Much like the playerAttack() method, this method handles attacks for the enemy. + * Same attack probabilities as player. + * Returns new health of the player. + */ public double enemyAttack() { double rand = Math.random(); + // If the enemy is blind, there is a 40% chance of the enemy beocoming unblinded. if(enemy.getBlinded()) { if(rand <0.4) { System.out.println(enemy.getName() + " is no longer blinded!"); @@ -70,14 +85,17 @@ public class Combat { System.out.println(enemy.getName() + " missed!"); }else if(rand < 0.20) { player.setHealth(player.getHealth()-enemy.getDamage()*1.5); + // ensure health does not drop below 0 if(player.getHealth() < 0) player.setHealth(0); System.out.println(enemy.getName() + " hit you with a critical hit, doing " + enemy.getDamage()*1.5 + " damage! Your health is now " + player.getHealth() + "%"); + // Set the player's bleeding status to true, when the enemy makes a critical hit System.out.println("You are now bleeding."); player.setBleeding(true); } else { player.setHealth(player.getHealth()-enemy.getDamage()); + // ensure health does not drop below 0 if(player.getHealth() < 0) player.setHealth(0); System.out.println(enemy.getName() + " did " + enemy.getDamage() + " damage to you! Your health is now " + player.getHealth() + "%"); @@ -86,6 +104,9 @@ public class Combat { return player.getHealth(); } + /* + * Getters and setters for this class. + */ public int getTurn() { return this.turn; } diff --git a/src/com/bayviewglen/zork/Command.java b/src/com/bayviewglen/zork/Command.java index c3f883f..65a6613 100644 --- a/src/com/bayviewglen/zork/Command.java +++ b/src/com/bayviewglen/zork/Command.java @@ -32,9 +32,9 @@ class Command { private String riddler; /** - * Create a command object. First and second word must be supplied, but - * either one (or both) can be null. The command word should be null to - * indicate that this was a command that is not recognised by this game. + * Create a command object. + * The command object is created by the parser, and contains all the known words that were in the input line. + * The command word should be null to indicate that this was a command that is not recognised by this game. */ public Command(String firstWord, ArrayList otherWords, String direction, String item, String enemy, String riddler) { commandWord = firstWord; @@ -72,11 +72,6 @@ class Command { public ArrayList getOtherWords() { return otherWords; } - /* - public String getSecondWord() { - return otherWords.get(0); - } - */ /** * Return true if this command was not understood. diff --git a/src/com/bayviewglen/zork/CommandWords.java b/src/com/bayviewglen/zork/CommandWords.java index 472c84f..6fd09da 100644 --- a/src/com/bayviewglen/zork/CommandWords.java +++ b/src/com/bayviewglen/zork/CommandWords.java @@ -15,7 +15,7 @@ import java.util.Scanner; * This class is part of the "Zork" game. */ class CommandWords { - // a constant array that holds all valid command words + // Hashmaps that store known words and their types, as well as synonyms to the known words private static HashMap m_words = new HashMap(); private static HashMap m_synonyms = new HashMap(); /** @@ -34,7 +34,7 @@ class CommandWords { }catch (Exception e) { e.printStackTrace(); } - + // import synonyms into hashmap try { Scanner in = new Scanner(new File("data/synonyms.dat")); while(in.hasNext()){ @@ -68,7 +68,7 @@ class CommandWords { return false; } } - + // check if given string is item public static boolean isItem(String aString){ try { return m_words.get(aString).equals("item"); @@ -76,7 +76,7 @@ class CommandWords { return false; } } - + // check if given string is enemy public static boolean isEnemy(String aString) { try { return m_words.get(aString).equals("enemy"); @@ -84,7 +84,7 @@ class CommandWords { return false; } } - + // check if given string is riddler public static boolean isRiddler(String aString) { try { return m_words.get(aString).equals("riddler"); @@ -103,7 +103,9 @@ class CommandWords { } System.out.println(); } - + /* + * If a known word exists for the synonym of the word given, return it. Otherwise return the given word. + */ public static String replaceSynonym(String word) { try { String words = m_synonyms.get(word); diff --git a/src/com/bayviewglen/zork/Entities/Entity.java b/src/com/bayviewglen/zork/Entities/Entity.java index 789b306..10d821c 100644 --- a/src/com/bayviewglen/zork/Entities/Entity.java +++ b/src/com/bayviewglen/zork/Entities/Entity.java @@ -1,6 +1,10 @@ package com.bayviewglen.zork.Entities; public class Entity { + /* + * The base class of any character in the game. + * All characters have a health, which is taken care of by this class. Hunger is not used. + */ protected double hunger; protected double health; diff --git a/src/com/bayviewglen/zork/Game.java b/src/com/bayviewglen/zork/Game.java index aa6c506..7851dfe 100644 --- a/src/com/bayviewglen/zork/Game.java +++ b/src/com/bayviewglen/zork/Game.java @@ -40,12 +40,15 @@ class Game { // masterRoomMap.get("GREAT_ROOM") will return the Room Object that is the // Great Room (assuming you have one). private HashMap masterRoomMap; + // Stores where all enemies are currently private HashMap masterEnemyMap; + /* + * Stores the current combat that is taking place. + * If there is a combat, this contains an instance of the combat class, if not it is null. + */ private Combat currentCombat = null; - // private HashMap itemsInRooms = new HashMap(); private void initRooms(String fileName) throws Exception { - // itemsInRooms.put(new Candlestick(), "Candlestick"); masterRoomMap = new HashMap(); Scanner roomScanner; try { @@ -139,7 +142,9 @@ class Game { e.printStackTrace(); } } - +/* + * Initiate enemies from the data file into the hashmap. + */ private void initEnemies(String fileName) throws Exception { masterEnemyMap = new HashMap(); Scanner enemyScanner = null; @@ -227,7 +232,9 @@ class Game { System.out.println(currentRoom.exitString()); boolean finished = false; while (!finished) { + // Checks if there is a current combat, if so perform the enemy's action, and check for deaths. if (currentCombat != null) { + // If enemy dies if (currentCombat.getEnemy().getHealth() <= 0.0) { System.out.print("You destroyed " + currentCombat.getEnemy().getName() + "! "); System.out.println(currentCombat.getEnemy().getName() + " seems to have dropped a " + currentCombat.getEnemy().getLoot()); @@ -243,6 +250,7 @@ class Game { currentRoom.addItem(object); masterEnemyMap.values().remove(currentRoom.getRoomName()); currentCombat = null; + // If enemy's turn - signified by turn = 1 } else if (currentCombat.getTurn() == 1) { currentCombat.enemyAttack(); if (currentCombat.getPlayer().getHealth() <= 0.0) { @@ -252,6 +260,7 @@ class Game { player.removeFromInventory(player.getInventory().get(i)); i--; } + // On player death, reset everything currentRoom = masterRoomMap.get("CIRCLE_ROOM"); System.out.println( "Poof! You looked pretty banged up there, so I brought you back to the circle room. Your items are where you died."); @@ -266,11 +275,13 @@ class Game { } } } + // Checks if the player is bleeding, if they are subtract 2 from health each turn if(player.getBleeding()) { player.setHealth(player.getHealth()-2); System.out.println("You are bleeding. Find, and use bandages to stop bleeding."); System.out.println("Your health is now " + player.getHealth() + "%"); } + // Checks if the player dies, if so reset all values, and drop items if(player.getHealth() <= 0) { for (int i = 0; i < player.getInventory().size(); i++) { currentRoom.addItem(player.getInventory().get(i)); @@ -287,6 +298,7 @@ class Game { finished = processCommand(command); } } + // Printed when game ends System.out.println("Thank you for playing. Goodbye!"); } @@ -301,11 +313,14 @@ class Game { return false; } String commandWord = command.getCommandWord(); + // Switch that handles all commands switch (commandWord) { case "open": case "unlock": + // Command to open or unlock a door boolean hasLockPick = false; boolean hasKey = false; + // Loop to check if player's inventory contains key or lockpick for (int i = 0; i < player.getInventory().size(); i++) { if (player.getInventory().get(i).equals(new Lockpick())) { hasLockPick = true; @@ -316,7 +331,7 @@ class Game { break; } } - + // Check if the room can be unlocked, if so unlock it if (command.hasDirection() && (hasLockPick || hasKey)) { Room nextRoom = currentRoom.nextRoom(command.getDirection()); try { @@ -345,6 +360,8 @@ class Game { } else { System.out.println("What do you want to open the door with?"); } + + // Similar with the above key and lockpick process, but with crowbars and battering rams for boarded doors. boolean hasCrowbar = false; boolean hasBatteringRam = false; for (int i = 0; i < player.getInventory().size(); i++) { @@ -394,6 +411,7 @@ class Game { case "down": case "d": case "u": + // if player is not in combat, go in direction given if (currentCombat == null) goRoom(command); else @@ -411,7 +429,7 @@ class Game { System.out.println("If you insist... \nPoof! You're gone. You're out of the castle now, but now a new, grand new adventure begins..."); return true; case "eat": - + // convert the item string in the command (if existent) into an item to get info about the item, and to remove from inventory if (command.hasItem()) { Class clazz; Item object; @@ -422,11 +440,13 @@ class Game { Constructor ctor = clazz.getConstructor(); object = (Item) ctor.newInstance(); boolean hasItem = false; + // check if player has item for (int i = 0; i < player.getInventory().size(); i++) { if (object.equals(player.getInventory().get(i))) { hasItem = true; } } + // If item can be eaten, and player has, eat item. if (object.isConsumable() && hasItem) { System.out.println("Nom Nom Nom..."); player.eat(); @@ -447,6 +467,7 @@ class Game { } break; case "talk": + // Talking with riddler command if (currentCombat == null) { if (currentRoom.hasRiddler()) { Scanner rScanner = new Scanner(System.in); @@ -455,7 +476,9 @@ class Game { String answer = currentRoom.getRiddler().getRiddle().getAnswer(); System.out.println(message + "\n\nHere's my riddle: " + riddle); System.out.print("Enter your guess here: "); + // wait for riddle response String guess = rScanner.nextLine(); + // check if guess has any part of answer in it. And if so administer prize. if (guess.toLowerCase().indexOf(answer.toLowerCase()) >= 0) { Item prize = currentRoom.getRiddler().getPrize(); String prizeName = prize.getName(); @@ -511,12 +534,15 @@ class Game { } break; case "take": + // Take the given item, or take all items boolean hasAll = false; + // check if player used word all, if so take all. for (String a : command.getOtherWords()) { if (a.equals("all")) hasAll = true; } if (hasAll) { + // Iterate through all items in room, remove from room inventory and add to player inventory. for (int i = 0; i < currentRoom.getItems().size(); i++) { if (player.addToInventory(currentRoom.getItem(i))) { currentRoom.removeItem(i); @@ -554,6 +580,7 @@ class Game { break; case "look": + // Print out descriptions of rooms System.out.print(currentRoom.longDescription()); System.out.println(currentRoom.itemString()); System.out.println(currentRoom.exitString()); @@ -561,8 +588,10 @@ class Game { case "inventory": case "i": + // Check if player has items, if so display items, if not display message boolean hasPlayerItems = false; String itemsP = ""; + // Iterate through to see if player has items, and add items to string to be printed for (Item i : player.getInventory()) { hasPlayerItems = true; itemsP += i.getName() + " "; @@ -576,6 +605,7 @@ class Game { } break; case "drop": + // if an item is given, convert string into item so that it can be removed from inventory if (command.hasItem()) { Class clazz; Item object; @@ -606,9 +636,11 @@ class Game { } break; case "attack": + // If there is currently no combat if (currentCombat == null) { if (command.hasEnemy()) { Enemy enemy = null; + // Using hashmap backwards for (Enemy i : masterEnemyMap.keySet()) { if (masterEnemyMap.get(i).equals(currentRoom.getRoomName())) { enemy = i; @@ -618,11 +650,13 @@ class Game { if (command.hasItem()) { boolean has = false; for (Item i : player.getInventory()) { + // Removes all spaces, capital letters from name if (i.getName().toLowerCase().replaceAll("\\s+", "").equals(command.getItem())) { has = true; } } if (has) { + // if the player has the specified item, and the enemy exists in the room set currentCombat to be the combat between the given enemy and player, and perform the first player attack currentCombat = new Combat(player, enemy); currentCombat.playerAttack(command.getItem()); } else { @@ -638,6 +672,7 @@ class Game { System.out.println("Attack what?"); } } else { + // Ran when combat is already taking place so that a new combat is not created each time an attack is made if (command.hasItem()) { boolean has = false; for (Item i : player.getInventory()) { @@ -646,6 +681,7 @@ class Game { } } if (has) { + // player attack on enemy. currentCombat.playerAttack(command.getItem()); } else { System.out.println("You do not have that weapon!"); @@ -656,6 +692,7 @@ class Game { } break; case "craft": + // if the item is craftable, and the player has the required components give the crafted item to the player, and remove the materials if(command.hasItem()) { Class clazz; CraftableItem object; @@ -666,6 +703,7 @@ class Game { if(object.isCraftable()) { boolean playerHasItems = true; boolean hasItem = false; + // Nested for loops to check if player has all required items for(Item i : object.getMaterials()) { hasItem = false; for(Item pi : player.getInventory()) { @@ -710,9 +748,11 @@ class Game { + command.getItem().substring(1).trim()); Constructor ctor = clazz.getConstructor(); object = (Item) ctor.newInstance(); + // If the item is not a bandage, run what is in the catch part of the try catch if(!object.equals(new Bandage())) throw new Exception(); boolean hasBandage = false; + // iterate through player inventory to see if bandage exists for(Item i : player.getInventory()) { if(i.equals(new Bandage())) { hasBandage = true; @@ -762,12 +802,14 @@ class Game { Room nextRoom = currentRoom.nextRoom(direction); if (nextRoom == null) System.out.println("There is no door!"); - else if (nextRoom.getLocked() && nextRoom.getBoarded()) { + else if (nextRoom.getLocked() && nextRoom.getBoarded()) { // check if the room is boarded or locked, if so do not allow the player to enter System.out.println("The door is locked and boarded shut. You need to find a key and crowbar to open it."); } else if (nextRoom.getLocked()) { System.out.println("The door is locked. You need a key to open it."); } else { + // This is run when there are no problems leaving the room currentRoom = nextRoom; + // print description and enemies in the room from the hashmap System.out.print(currentRoom.longDescription()); boolean hasEnemy = false; Enemy enemy = null; @@ -783,6 +825,7 @@ class Game { enemy = i; } } + // if the room has an enemy, display it. if (hasEnemy) { System.out.println(enemy.getName() + ", " + enemy.getDescription() + " has appeared!"); System.out.println(currentRoom.itemString()); diff --git a/src/com/bayviewglen/zork/Parser.java b/src/com/bayviewglen/zork/Parser.java index eb80057..ae9a223 100644 --- a/src/com/bayviewglen/zork/Parser.java +++ b/src/com/bayviewglen/zork/Parser.java @@ -31,15 +31,17 @@ class Parser { } public Command getCommand() { + // If the following are blank, this means that the input line did not contain that specific type of word. String inputLine = ""; // will hold the full input line - String verb = ""; - String direction = ""; - String item = ""; - String enemy = ""; - String riddler = ""; + String verb = ""; // holds verb of command + String direction = ""; // holds direction of command + String item = ""; // holds item of command + String enemy = ""; // holds enemies in command + String riddler = ""; // holds riddler in command boolean open = false; - //String word2; + //Store all the words in the input line in an arraylist ArrayList words = new ArrayList(); + // Where unknown words are stored ArrayList otherWords = new ArrayList(); System.out.print("> "); // print prompt BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); @@ -48,12 +50,16 @@ class Parser { } catch (java.io.IOException exc) { System.out.println("There was an error during reading: " + exc.getMessage()); } + // convert inputLine into tokens, and put this into the ArrayList of words StringTokenizer tokenizer = new StringTokenizer(inputLine.toLowerCase()); while(tokenizer.hasMoreTokens()) { words.add(tokenizer.nextToken()); } + // For each word, check what type it is, and store it in its respective String variable if it matches. for(int i=0; i exits; // stores exits of this room. - private ArrayList items; + private ArrayList items; // The inventory of the room private Riddler riddler; //needs to altered outside of the class so that riddler can be set to null. private boolean locked; // Otherwise you can repeatedly solve the riddle and get unlimited items - private boolean boarded; + private boolean boarded; /** * Create a room described "description". Initially, it has no exits. @@ -193,7 +193,9 @@ class Room { } return returnString; } - + /* + * Return a string of items to be printed + */ public String itemString(){ boolean hasItems = false; String items = ""; From d941264ec1c5f7b768069a3f58b503288d8199b0 Mon Sep 17 00:00:00 2001 From: jslightham <31053827+jslightham@users.noreply.github.com> Date: Sat, 1 Jun 2019 02:05:37 -0400 Subject: [PATCH 2/2] comments --- src/com/bayviewglen/zork/Entities/Enemies/Enemy.java | 6 +++++- src/com/bayviewglen/zork/Entities/Player.java | 4 ++++ src/com/bayviewglen/zork/Entities/Riddle.java | 3 +++ src/com/bayviewglen/zork/Entities/Riddler.java | 5 +++++ src/com/bayviewglen/zork/Items/CraftableItem.java | 6 +++++- src/com/bayviewglen/zork/Items/Item.java | 4 ++++ 6 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/com/bayviewglen/zork/Entities/Enemies/Enemy.java b/src/com/bayviewglen/zork/Entities/Enemies/Enemy.java index bf5e804..5709318 100644 --- a/src/com/bayviewglen/zork/Entities/Enemies/Enemy.java +++ b/src/com/bayviewglen/zork/Entities/Enemies/Enemy.java @@ -1,7 +1,10 @@ package com.bayviewglen.zork.Entities.Enemies; import com.bayviewglen.zork.Entities.Entity; - +/* + * This is the enemy class, which extends the entity class. + * The enemy class is instantiated in the initiate enemies method of the game class, and stores information such as damage given, name, description, room, loot, and whether or not it is blinded. + */ public class Enemy extends Entity{ private int damageGiven; private String name; @@ -11,6 +14,7 @@ public class Enemy extends Entity{ private boolean blinded; public Enemy(){ + // Call super constructor to store health and hunger. super(100.0, 100.0); damageGiven = 10; blinded = false; diff --git a/src/com/bayviewglen/zork/Entities/Player.java b/src/com/bayviewglen/zork/Entities/Player.java index d869a81..dd8dfd0 100644 --- a/src/com/bayviewglen/zork/Entities/Player.java +++ b/src/com/bayviewglen/zork/Entities/Player.java @@ -6,6 +6,10 @@ import java.lang.reflect.Constructor; import java.util.ArrayList; public class Player extends Entity{ + /* + * Player class that extends the main entity class. + * Takes care of inventory and health of the player as well as bleeding status. + */ private ArrayList inventory = new ArrayList(); private final int INVENTORY_CAPACITY = 120; private int currentInventoryWeight; diff --git a/src/com/bayviewglen/zork/Entities/Riddle.java b/src/com/bayviewglen/zork/Entities/Riddle.java index 541ff6d..fb92001 100644 --- a/src/com/bayviewglen/zork/Entities/Riddle.java +++ b/src/com/bayviewglen/zork/Entities/Riddle.java @@ -1,6 +1,9 @@ package com.bayviewglen.zork.Entities; public class Riddle { + /* + * A simple riddle object that stores a question, and the answer to that question that is used in the riddler class. + */ private String question; private String answer; diff --git a/src/com/bayviewglen/zork/Entities/Riddler.java b/src/com/bayviewglen/zork/Entities/Riddler.java index 28f1e95..8f526b2 100644 --- a/src/com/bayviewglen/zork/Entities/Riddler.java +++ b/src/com/bayviewglen/zork/Entities/Riddler.java @@ -2,6 +2,11 @@ package com.bayviewglen.zork.Entities; import com.bayviewglen.zork.Items.Item; public class Riddler extends Entity { + /* + * Create the riddler character, which is an entity. + * Holds a riddle, and has an item as a prize. + * Riddlers are instantiated by the room loading method at the begining of the game class. + */ Riddle riddle; String message; Item prize; diff --git a/src/com/bayviewglen/zork/Items/CraftableItem.java b/src/com/bayviewglen/zork/Items/CraftableItem.java index 5fffe2e..ac8554c 100644 --- a/src/com/bayviewglen/zork/Items/CraftableItem.java +++ b/src/com/bayviewglen/zork/Items/CraftableItem.java @@ -1,13 +1,17 @@ package com.bayviewglen.zork.Items; import java.util.ArrayList; - +/* + * Extends the item class, and is extended by all items that can be crafted. + * Stores the items required to craft the specific item. + */ public class CraftableItem extends Item{ private ArrayList materials; public CraftableItem(int id, String name, String description, boolean isConsumable, int health, int weight) { super(id, name, description, isConsumable, health, weight); materials = new ArrayList(); } + // override the isCraftable method from the item class, stating that craftable items are craftable. public boolean isCraftable() { return true; } diff --git a/src/com/bayviewglen/zork/Items/Item.java b/src/com/bayviewglen/zork/Items/Item.java index 7e922bb..16773f3 100644 --- a/src/com/bayviewglen/zork/Items/Item.java +++ b/src/com/bayviewglen/zork/Items/Item.java @@ -1,6 +1,10 @@ package com.bayviewglen.zork.Items; public class Item { + /* + * The main class that all items, and types of items extend. + * Items are their own classes due to the way that they are used in this game, created and destroyed multiple times. + */ private int id; private String name; private String description;