In: Computer Science
Java code. You will need to make classes that could be used to stage a battle as described below.
You have been tasked to create a number of items that can be used in an online game. All items can be used on a game character who will have a name, a number of health-points and a bag to hold the items.
Here are examples of items that can be made:
Item | type of item | effectOnHealth | other info |
---|---|---|---|
Widow's Cloak | Armor | +15 | |
Gladiator's Shield | Armor | +8 | |
Rusty Knife | Weapon | -3 | |
Protection Spell | Spell | 1 | has magic words that can be read |
Note from Zelda | Letter | 0 | says "Beware of the Witch" |
Here are examples of characters that can be made:
name | health points |
---|---|
Godwin | 30 |
Marilyn | 40 |
All items can be added to any character's bag and also used on any character. (For instance the Rusty Knife could be used on a character to lower their health by 3 points.)
Armor and weapons can be equipped (worn) and removed. Spells can also be equipped (memorized) which casts a glow around the character and removed (forgotten) which removes the glow. In the future, any time a character equips or removes any item, their appearance will be altered. These methods -- for now -- can just have a body which says either "equipping" or "removing."
Both spells and letters have some text that could be read. Whenever a spell or letter is used, the text will also be read (displayed). However these items can be read independently from their use on a character.
Build these items using good interface and abstract class design as appropriate. Set the toString() method of the Character class to return a character's name and current health. You should have a good object oriented structure and good method signatures.
To illustrate this, in a driver class, two characters, Godwin and Marilyn are created. They begin by having items added to their bags (both Godwin and Marilyn get a copy of the Note from Zelda). Then the items they have are used on themselves or one another as follows:
This is a sample of how a Driver could work, staging a battle between the defensive Godwin and the offensive Marilyn.
- The Protection Spell is used on Godwin (by Godwin)
- The Rusty Knife is used on Godwin (by Marilyn)
- The Gladiator's Shield is used on Godwin (by Godwin)
- The Rusty Knife is used on Godwin (by Marilyn)
file-name : Character.java ------------------------------- import java.util.ArrayList; public class Character extends Items{ String name; int healthPoints; ArrayList<String> bags; Character(String name, int healthPoints) { super(); this.name = name; this.healthPoints = healthPoints; bags = new ArrayList<>(); bags.add("Zelda"); } public String getName() { return name; } public int getHealthPoints() { return healthPoints; } public void setHealthPoints(int healthPoints) { this.healthPoints = healthPoints; } public String toString() { String str = "Name: "+this.getName() +"\nHealth: "+getHealthPoints()+"\n---------\n"; return str; } public void rustyKnife(Character c) { System.out.println("Using knife weapon on "+c.getName()); c.setHealthPoints(c.getHealthPoints()-3); } // END of rustyKnife method @Override public void useSpell() { System.out.println("Avada Kedavra"); this.setHealthPoints(this.getHealthPoints()+1); this.bags.add("Spell"); } @Override public void zelda() { System.out.println("Beware of Witch"); } @Override public void wearCloak() { System.out.println(this.getName() + " Equipping Widow's Cloak"); this.bags.add("Cloak"); this.setHealthPoints(this.getHealthPoints()+8); } @Override public void wearShield() { System.out.println(this.getName()+" Equipping Gladiator's Shield"); this.bags.add("Shield"); this.setHealthPoints(this.getHealthPoints()+15); } @Override public void removeCloak() { // only if the character wears a cloak, then only he can remove it if(this.bags.contains("Cloak")) { System.out.println(this.getName() + " Removing Widow's Cloak"); this.bags.remove("Cloak"); this.setHealthPoints(this.getHealthPoints() - 8); } else { System.out.println("Not wearing any cloak"); } } @Override public void removeShield() { if(this.bags.contains("Shield")) { System.out.println(this.getName()+" Removing Gladiator's Shield"); this.bags.remove("Shield"); this.setHealthPoints(this.getHealthPoints() - 15); } else { System.out.println("Not wearing any shield"); } } } //END of class
-------------------------------------------------------------------------------------------------------
file-name: Items.java ---------------------- public abstract class Items { String itemName; int points; Items(String name, int p) { this.itemName = name; this.points = p; } public Items() { } public abstract void rustyKnife(Character c); public abstract void wearCloak(); public abstract void wearShield(); public abstract void useSpell(); public abstract void zelda(); public abstract void removeCloak(); public abstract void removeShield(); }
-----------------------------------------------------------------------------------------------------------
file-name: TestMain.java ------------------------------ public class TestMain { public static void main(String[] args) { // creating characters Character p1 = new Character("Godwin", 30); Character p2 = new Character("Marilyn", 40); p1.useSpell(); p2.rustyKnife(p1); p1.wearShield(); p2.rustyKnife(p1); System.out.println("----------"); // I am showing how some other methods work // p1 character not wearing any cloak // but still trying to remove so prints error message. p1.removeCloak(); System.out.println(p1); System.out.println(p2); } // END of main( ) method } // END of class
==================================================
OUTPUT :
Hey If you want me to modify code, I will. Thank you!!. Please UPVOTE if you like my effort.