Replaced items with inventory. This makes it easier to validate items and add new items, but we need to remove some code repetition.
This commit is contained in:
parent
948b26edf3
commit
63859fe4ae
3
.gitignore
vendored
3
.gitignore
vendored
@ -4,6 +4,9 @@
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# Save files
|
||||
*.save
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
|
158
Game.java
158
Game.java
@ -16,16 +16,16 @@ public class Game {
|
||||
ArrayList<Pokemon> pokemon = readPokemon();
|
||||
int initialPokemon = pokemon.size();
|
||||
ArrayList<Pokemon> usersPokemon = randomTeam();
|
||||
ArrayList<Item> usersItems = prepareInventory();
|
||||
|
||||
System.out.println("What is your name?");
|
||||
String name = in.nextLine();
|
||||
Trainer player = new Trainer(name, usersPokemon, usersItems);
|
||||
Trainer player = new Trainer(name, usersPokemon, createInventory());
|
||||
|
||||
boolean done = false;
|
||||
Pokemon opponentPokemon = null;
|
||||
Pokemon trainersPokemon = null;
|
||||
Item currentItem = null;
|
||||
Pokeball currentPokeball = null;
|
||||
Potion currentPotion = null;
|
||||
boolean fleeSuccess = false;
|
||||
Pokemon pokemonToHeal = null;
|
||||
|
||||
@ -75,13 +75,13 @@ public class Game {
|
||||
}
|
||||
break;
|
||||
case 'h':
|
||||
if (itemsLeft(usersItems, Item.Target.SELF)) {
|
||||
availableItems(usersItems, Item.Target.SELF);
|
||||
currentItem = chosenItem(usersItems, Item.Target.SELF);
|
||||
if (currentItem == null) {
|
||||
System.out.println("Invalid item.");
|
||||
if (player.getInventory().getPotions().size() > 0) {
|
||||
availablePotions(player.getInventory().getPotions());
|
||||
currentPotion = chosenPotion(player.getInventory().getPotions());
|
||||
if (currentPotion == null) {
|
||||
System.out.println("Invalid potion.");
|
||||
} else {
|
||||
if (currentItem.needsAlive()) {
|
||||
if (currentPotion.needsAlive()) {
|
||||
availablePokemon(player.getConsciousPokemon());
|
||||
pokemonToHeal = usersChoice(player.getConsciousPokemon());
|
||||
} else {
|
||||
@ -91,7 +91,7 @@ public class Game {
|
||||
if (pokemonToHeal == null) {
|
||||
System.out.println("That is not a valid pokemon");
|
||||
} else {
|
||||
if (currentItem.use(pokemonToHeal)) {
|
||||
if (currentPotion.use(pokemonToHeal)) {
|
||||
opponentPokemon.attack(trainersPokemon);
|
||||
}
|
||||
}
|
||||
@ -101,18 +101,18 @@ public class Game {
|
||||
}
|
||||
break;
|
||||
case 't':
|
||||
if (itemsLeft(usersItems, Item.Target.OTHER)) {
|
||||
availableItems(usersItems, Item.Target.OTHER);
|
||||
currentItem = chosenItem(usersItems, Item.Target.OTHER);
|
||||
if (currentItem == null) {
|
||||
if (player.getInventory().getPokeballs().size() > 0) {
|
||||
availablePokeballs(player.getInventory().getPokeballs());
|
||||
currentPokeball = chosenPokeball(player.getInventory().getPokeballs());
|
||||
if (currentPokeball == null) {
|
||||
System.out.println("Invalid pokeball.");
|
||||
} else {
|
||||
if (currentItem.getName().toLowerCase().replace(" ", "").equals("masterball")) {
|
||||
currentItem.use(opponentPokemon, pokemon, usersPokemon);
|
||||
if (currentPokeball.getType() == Pokeball.Pokeballs.MASTERBALL) {
|
||||
currentPokeball.use(opponentPokemon, pokemon, usersPokemon);
|
||||
opponentPokemon = randomPokemon(pokemon);
|
||||
System.out.printf("A wild %s appeared.%n", opponentPokemon.getName());
|
||||
} else {
|
||||
boolean captured = currentItem.use(opponentPokemon, pokemon, usersPokemon);
|
||||
boolean captured = currentPokeball.use(opponentPokemon, pokemon, usersPokemon);
|
||||
if (captured) {
|
||||
opponentPokemon = randomPokemon(pokemon);
|
||||
System.out.printf("A wild %s appeared.%n", opponentPokemon.getName());
|
||||
@ -137,18 +137,18 @@ public class Game {
|
||||
case 's':
|
||||
savePokemon(pokemon, "pokemon.save");
|
||||
savePokemon(usersPokemon, "user.save");
|
||||
saveItems(usersItems, "items.save");
|
||||
saveInventory(player.getInventory(), "inventory.save");
|
||||
break;
|
||||
case 'l':
|
||||
ArrayList<Pokemon> loadedPokemon = loadPokemon("pokemon.save");
|
||||
ArrayList<Pokemon> loadedUsersPokemon = loadPokemon("user.save");
|
||||
ArrayList<Item> loadedUsersItems = loadItems("items.save");
|
||||
if (loadedPokemon == null || loadedUsersPokemon == null || loadedUsersItems == null) {
|
||||
Inventory loadedInventory = loadInventory("inventory.save");
|
||||
if (loadedPokemon == null || loadedUsersPokemon == null || loadedInventory == null) {
|
||||
System.out.println("One or more savefiles seem corrupt. Please delete or fix the affected file(s).");
|
||||
} else {
|
||||
pokemon = loadedPokemon;
|
||||
player.setPokemon(loadedUsersPokemon);
|
||||
player.setItems(loadedUsersItems);
|
||||
player.setInventory(loadedInventory);
|
||||
if (pokemon.size() > 0 && usersPokemon.size() > 0) {
|
||||
do {
|
||||
availablePokemon(player.getConsciousPokemon());
|
||||
@ -200,12 +200,17 @@ public class Game {
|
||||
* @param items List of all of the user's items.
|
||||
* @param target We are either listing items targeted at an opponent pokemon or at our own pokemon.
|
||||
*/
|
||||
public static void availableItems(ArrayList<Item> items, Item.Target target) {
|
||||
public static void availablePotions(ArrayList<Potion> potions) {
|
||||
System.out.println("You may choose from these items:");
|
||||
for (int i = 0; i < items.size(); i++) {
|
||||
if (items.get(i).getAmount() > 0 && items.get(i).getTarget().equals(target)) {
|
||||
System.out.printf("%d: %s%n", i + 1, items.get(i));
|
||||
}
|
||||
for (int i = 0; i < potions.size(); i++) {
|
||||
System.out.printf("%d: %s%n", i + 1, potions.get(i));
|
||||
}
|
||||
System.out.print(">");
|
||||
}
|
||||
public static void availablePokeballs(ArrayList<Pokeball> pokeballs) {
|
||||
System.out.println("You may choose from these items:");
|
||||
for (int i = 0; i < pokeballs.size(); i++) {
|
||||
System.out.printf("%d: %s%n", i + 1, pokeballs.get(i));
|
||||
}
|
||||
System.out.print(">");
|
||||
}
|
||||
@ -214,33 +219,18 @@ public class Game {
|
||||
* Gives a trainer necessary starting items.
|
||||
* @return A list of items.
|
||||
*/
|
||||
public static ArrayList<Item> prepareInventory() {
|
||||
ArrayList<Item> usersItems = new ArrayList<Item>();
|
||||
usersItems.add(new Item("Poke Ball", "A device for catching wild Pokémon. It's thrown like a ball at a Pokémon, comfortably encapsulating its target.", 15, Item.Target.OTHER));
|
||||
usersItems.add(new Item("Great ball", "A good, high-performance Poké Ball that provides a higher Pokémon catch rate than a standard Poké Ball.", 10, Item.Target.OTHER));
|
||||
usersItems.add(new Item("Ultra ball", "An ultra-high-performance Poké Ball that provides a higher success rate for catching Pokémon than a Great Ball.", 5, Item.Target.OTHER));
|
||||
usersItems.add(new Item("Master ball", "The best Poké Ball with the ultimate level of performance. With it, you will catch any wild Pokémon without fail.", 1, Item.Target.OTHER));
|
||||
usersItems.add(new Item("Potion", "Heals a pokemon for 20 HP.", 20, Item.Target.SELF));
|
||||
usersItems.add(new Item("Super Potion", "Heals a pokemon for 50 HP.", 10, Item.Target.SELF));
|
||||
usersItems.add(new Item("Hyper Potion", "Heals a pokemon for 200 HP.", 5, Item.Target.SELF));
|
||||
usersItems.add(new Item("Max Potion", "Fully heals a pokemon.", 1, Item.Target.SELF));
|
||||
usersItems.add(new Item("Revive", "Revives a fainted pokemon.", 2, Item.Target.SELF, false));
|
||||
return usersItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the user has any item left of any type.
|
||||
* @param items List of all of the user's items.
|
||||
* @return True if any items are left. False otherwise.
|
||||
*/
|
||||
public static boolean itemsLeft(ArrayList<Item> items, Item.Target target) {
|
||||
int count = 0;
|
||||
for (Item item : items) {
|
||||
if (item.getAmount() > 0 && item.getTarget().equals(target)) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count > 0;
|
||||
public static Inventory createInventory() {
|
||||
Inventory inventory = new Inventory();
|
||||
inventory.addPokeball("Poke Ball", "A device for catching wild Pokémon. It's thrown like a ball at a Pokémon, comfortably encapsulating its target.", 15);
|
||||
inventory.addPokeball("Great ball", "A good, high-performance Poké Ball that provides a higher Pokémon catch rate than a standard Poké Ball.", 10);
|
||||
inventory.addPokeball("Ultra ball", "An ultra-high-performance Poké Ball that provides a higher success rate for catching Pokémon than a Great Ball.", 5);
|
||||
inventory.addPokeball("Master ball", "The best Poké Ball with the ultimate level of performance. With it, you will catch any wild Pokémon without fail.", 1);
|
||||
inventory.addPotion("Potion", "Heals a pokemon for 20 HP.", 20, true);
|
||||
inventory.addPotion("Super Potion", "Heals a pokemon for 50 HP.", 10, true);
|
||||
inventory.addPotion("Hyper Potion", "Heals a pokemon for 200 HP.", 5, true);
|
||||
inventory.addPotion("Max Potion", "Fully heals a pokemon.", 1, true);
|
||||
inventory.addPotion("Revive", "Revives a fainted pokemon.", 2, false);
|
||||
return inventory;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -280,13 +270,13 @@ public class Game {
|
||||
public static Pokemon usersChoice(ArrayList<Pokemon> pokemonList) {
|
||||
if (in.hasNextInt()) {
|
||||
int choice = in.nextInt() - 1;
|
||||
in.nextLine();
|
||||
if (choice >= 0 && choice < pokemonList.size()) {
|
||||
in.nextLine();
|
||||
return pokemonList.get(choice);
|
||||
} else {
|
||||
System.out.println("Invalid pokemon");
|
||||
}
|
||||
}
|
||||
System.out.println("Invalid pokemon");
|
||||
in.nextLine();
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -295,16 +285,26 @@ public class Game {
|
||||
* @param itemList Available items.
|
||||
* @return An Item object or null.
|
||||
*/
|
||||
public static Item chosenItem(ArrayList<Item> itemList, Item.Target target) {
|
||||
public static Potion chosenPotion(ArrayList<Potion> potions) {
|
||||
if (in.hasNextInt()) {
|
||||
int choice = in.nextInt() - 1;
|
||||
in.nextLine();
|
||||
if (choice >= 0 && choice < itemList.size()) {
|
||||
return itemList.get(choice);
|
||||
} else {
|
||||
System.out.println("Invalid item");
|
||||
if (choice >= 0 && choice < potions.size()) {
|
||||
in.nextLine();
|
||||
return potions.get(choice);
|
||||
}
|
||||
}
|
||||
in.nextLine();
|
||||
return null;
|
||||
}
|
||||
public static Pokeball chosenPokeball(ArrayList<Pokeball> pokeball) {
|
||||
if (in.hasNextInt()) {
|
||||
int choice = in.nextInt() - 1;
|
||||
if (choice >= 0 && choice < pokeball.size()) {
|
||||
in.nextLine();
|
||||
return pokeball.get(choice);
|
||||
}
|
||||
}
|
||||
in.nextLine();
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -370,12 +370,16 @@ public class Game {
|
||||
* @param itemList List of Item objects to save.
|
||||
* @param savefile The file to write to.
|
||||
*/
|
||||
public static void saveItems(ArrayList<Item> itemList, String savefile) {
|
||||
public static void saveInventory(Inventory inventory, String savefile) {
|
||||
try (PrintWriter file = new PrintWriter(savefile)) {
|
||||
for (Item item : itemList) {
|
||||
file.println(item.saveString());
|
||||
for (Pokeball pokeball : inventory.getPokeballs()) {
|
||||
file.println(pokeball.saveString());
|
||||
}
|
||||
System.out.println("Successfully saved items.");
|
||||
file.println(":NEXTLIST:");
|
||||
for (Potion potion : inventory.getPotions()) {
|
||||
file.println(potion.saveString());
|
||||
}
|
||||
System.out.println("Successfully saved inventory.");
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("The savefile could not be written.");
|
||||
}
|
||||
@ -415,13 +419,27 @@ public class Game {
|
||||
* @param savefile The file to write to.
|
||||
* @return A list of items or null on failiure.
|
||||
*/
|
||||
public static ArrayList<Item> loadItems(String savefile) {
|
||||
ArrayList<Item> items = new ArrayList<Item>();
|
||||
public static Inventory loadInventory(String savefile) {
|
||||
ArrayList<Pokeball> pokeballs = new ArrayList<Pokeball>();
|
||||
ArrayList<Potion> potions = new ArrayList<Potion>();
|
||||
try (Scanner file = new Scanner(new File(savefile))) {
|
||||
String next = "";
|
||||
while (file.hasNextLine() && !next.equals(":NEXTLIST:")) {
|
||||
try {
|
||||
next = file.nextLine();
|
||||
if (!next.equals(":NEXTLIST:")) {
|
||||
String[] data = next.split(";");
|
||||
pokeballs.add(new Pokeball(data[0], data[1], Integer.parseInt(data[2]), Pokeball.Pokeballs.valueOf(data[0].toUpperCase().replace(" ", ""))));
|
||||
}
|
||||
} catch (ArrayIndexOutOfBoundsException e) {
|
||||
System.out.println("Invalid savefile: " + savefile);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
while (file.hasNextLine()) {
|
||||
try {
|
||||
String[] data = file.nextLine().split(";");
|
||||
items.add(new Item(data[0], data[1], Integer.parseInt(data[2]), Item.Target.valueOf(data[3]), Boolean.parseBoolean(data[4])));
|
||||
potions.add(new Potion(data[0], data[1], Integer.parseInt(data[2]), Potion.Potions.valueOf(data[0].toUpperCase().replace(" ", ""))));
|
||||
} catch (ArrayIndexOutOfBoundsException e) {
|
||||
System.out.println("Invalid savefile: " + savefile);
|
||||
return null;
|
||||
@ -431,7 +449,7 @@ public class Game {
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("You don't have a valid savefile.");
|
||||
}
|
||||
return items;
|
||||
return new Inventory(pokeballs, potions);
|
||||
}
|
||||
|
||||
public static ArrayList<Pokemon> randomTeam() {
|
||||
|
50
Inventory.java
Normal file
50
Inventory.java
Normal file
@ -0,0 +1,50 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Inventory {
|
||||
private ArrayList<Pokeball> pokeballs;
|
||||
private ArrayList<Potion> potions;
|
||||
|
||||
public Inventory() {
|
||||
this.pokeballs = new ArrayList<Pokeball>();
|
||||
this.potions = new ArrayList<Potion>();
|
||||
}
|
||||
|
||||
public Inventory(ArrayList<Pokeball> pokeballs, ArrayList<Potion> potions) {
|
||||
this.pokeballs = pokeballs;
|
||||
this.potions = potions;
|
||||
}
|
||||
|
||||
public void addPokeball(String name, String description, int amount) {
|
||||
Pokeball pokeball = new Pokeball(name, description, amount, Pokeball.Pokeballs.valueOf(name.toUpperCase().replace(" ", "")));
|
||||
if (pokeball != null) {
|
||||
this.pokeballs.add(pokeball);
|
||||
}
|
||||
}
|
||||
|
||||
public void addPotion(String name, String description, int amount, boolean alive) {
|
||||
Potion potion = new Potion(name, description, amount, Potion.Potions.valueOf(name.toUpperCase().replace(" ", "")), alive);
|
||||
if (potion != null) {
|
||||
this.potions.add(potion);
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList<Pokeball> getPokeballs() {
|
||||
ArrayList<Pokeball> pokeballs = new ArrayList<Pokeball>();
|
||||
for (Pokeball pokeball : this.pokeballs) {
|
||||
if (pokeball.getAmount() > 0) {
|
||||
pokeballs.add(pokeball);
|
||||
}
|
||||
}
|
||||
return pokeballs;
|
||||
}
|
||||
|
||||
public ArrayList<Potion> getPotions() {
|
||||
ArrayList<Potion> potions = new ArrayList<Potion>();
|
||||
for (Potion potion : this.potions) {
|
||||
if (potion.getAmount() > 0) {
|
||||
potions.add(potion);
|
||||
}
|
||||
}
|
||||
return potions;
|
||||
}
|
||||
}
|
62
Pokeball.java
Normal file
62
Pokeball.java
Normal file
@ -0,0 +1,62 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Pokeball {
|
||||
public static enum Pokeballs { POKEBALL, GREATBALL, ULTRABALL, MASTERBALL }
|
||||
private String name;
|
||||
private String description;
|
||||
private Pokeballs type;
|
||||
private int amount;
|
||||
|
||||
public Pokeball(String name, String description, int amount, Pokeballs type) {
|
||||
this.type = type;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return String.format("(%d) %s: %s", this.amount, this.name, this.description);
|
||||
}
|
||||
|
||||
public String saveString() {
|
||||
return String.format("%s;%s;%d", this.name, this.description, this.amount);
|
||||
}
|
||||
|
||||
public int getAmount() {
|
||||
return this.amount;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public Pokeballs getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses a pokeball on an opponent.
|
||||
* @param target Which pokemon to target.
|
||||
* @param current Current list the pokemon belongs to.
|
||||
* @param catcher Where we send the pokemon on a successfull capture.
|
||||
* @return True if nothing went wrong. False otherwise.
|
||||
*/
|
||||
public boolean use(Pokemon target, ArrayList<Pokemon> current, ArrayList<Pokemon> catcher) {
|
||||
if (this.amount > 0) {
|
||||
this.amount--;
|
||||
switch (this.type) {
|
||||
case POKEBALL:
|
||||
return target.tryCapture(current, catcher, 255);
|
||||
case GREATBALL:
|
||||
return target.tryCapture(current, catcher, 200);
|
||||
case ULTRABALL:
|
||||
return target.tryCapture(current, catcher, 150);
|
||||
case MASTERBALL:
|
||||
return target.tryCapture(current, catcher, 0);
|
||||
}
|
||||
} else {
|
||||
System.out.println("No cheating!");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -1,138 +1,107 @@
|
||||
import java.util.ArrayList;
|
||||
public class Item {
|
||||
public enum Target { SELF, OTHER }
|
||||
private String name;
|
||||
private String description;
|
||||
private Target target;
|
||||
private boolean alive;
|
||||
private int amount;
|
||||
|
||||
public Item(String name, String description, int amount, Target target) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.target = target;
|
||||
this.alive = true;
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public Item(String name, String description, int amount, Target target, boolean alive) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.target = target;
|
||||
this.alive = alive;
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return String.format("(%d) %s: %s", this.amount, this.name, this.description);
|
||||
}
|
||||
|
||||
public String saveString() {
|
||||
return String.format("%s;%s;%d;%s;%b", this.name, this.description, this.amount, this.target, this.alive);
|
||||
}
|
||||
|
||||
public int getAmount() {
|
||||
return this.amount;
|
||||
}
|
||||
|
||||
public Target getTarget() {
|
||||
return this.target;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/** Checks if an item should be used on alive or fainted pokemon. */
|
||||
public boolean needsAlive() {
|
||||
return alive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Spends an item and does something based on the type of item.
|
||||
* @param target Which pokemon should the item be used on.
|
||||
* @return True if nothing went wrong. False otherwise.
|
||||
*/
|
||||
public boolean use(Pokemon target) {
|
||||
if (this.amount > 0) {
|
||||
String name = this.name.toLowerCase().replace(" ", "");
|
||||
switch (name) {
|
||||
case "potion":
|
||||
return potion(target, 20);
|
||||
case "superpotion":
|
||||
return potion(target, 50);
|
||||
case "hyperpotion":
|
||||
return potion(target, 200);
|
||||
case "maxpotion":
|
||||
return potion(target, -1);
|
||||
case "revive":
|
||||
if (!target.isConscious()) {
|
||||
this.amount--;
|
||||
target.revive();
|
||||
System.out.printf("%s was revived.%n", target.getName());
|
||||
return true;
|
||||
} else {
|
||||
System.out.println("You can't revive a conscious pokemon.");
|
||||
return false;
|
||||
}
|
||||
default:
|
||||
System.out.printf("Invalid item %s%n", name);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
System.out.println("No cheating!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a pokemon is able to, and in need of a potion. If it is, heal it.
|
||||
* @param target The pokemon to heal.
|
||||
* @param amount The amount to heal the pokemon.
|
||||
* @return True if nothing went wrong. False otherwise.
|
||||
*/
|
||||
private boolean potion(Pokemon target, int amount) {
|
||||
if (target.isConscious()) {
|
||||
if (target.isDamaged()) {
|
||||
this.amount--;
|
||||
int healed = target.heal(amount);
|
||||
System.out.printf("%s was healed for %d HP and now has %d HP.%n", target.getName(), healed, target.getHP());
|
||||
} else {
|
||||
System.out.printf("%s has not taken damage, and does not require a potion.%n", target.getName());
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
System.out.println("You can't heal a fainted pokemon.");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses a pokeball on an opponent.
|
||||
* @param target Which pokemon to target.
|
||||
* @param current Current list the pokemon belongs to.
|
||||
* @param catcher Where we send the pokemon on a successfull capture.
|
||||
* @return True if nothing went wrong. False otherwise.
|
||||
*/
|
||||
public boolean use(Pokemon target, ArrayList<Pokemon> current, ArrayList<Pokemon> catcher) {
|
||||
if (this.amount > 0) {
|
||||
this.amount--;
|
||||
switch (this.name.toLowerCase().replace(" ", "")) {
|
||||
case "pokeball":
|
||||
return target.tryCapture(current, catcher, 255);
|
||||
case "greatball":
|
||||
return target.tryCapture(current, catcher, 200);
|
||||
case "ultraball":
|
||||
return target.tryCapture(current, catcher, 150);
|
||||
case "masterball":
|
||||
return target.tryCapture(current, catcher, 0);
|
||||
default:
|
||||
System.out.println("That item does not exist.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("No cheating!");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Potion {
|
||||
public static enum Potions { POTION, SUPERPOTION, HYPERPOTION, MAXPOTION, REVIVE }
|
||||
private String name;
|
||||
private String description;
|
||||
private Potions type;
|
||||
private boolean alive;
|
||||
private int amount;
|
||||
|
||||
public Potion(String name, String description, int amount, Potions type) {
|
||||
this.type = type;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.alive = true;
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public Potion(String name, String description, int amount, Potions type, boolean alive) {
|
||||
this.type = Potions.valueOf(name.toUpperCase().replace(" ", ""));
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.alive = alive;
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return String.format("(%d) %s: %s", this.amount, this.name, this.description);
|
||||
}
|
||||
|
||||
public String saveString() {
|
||||
return String.format("%s;%s;%d;%b", this.name, this.description, this.amount, this.alive);
|
||||
}
|
||||
|
||||
public int getAmount() {
|
||||
return this.amount;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/** Checks if an item should be used on alive or fainted pokemon. */
|
||||
public boolean needsAlive() {
|
||||
return alive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Spends an item and does something based on the type of item.
|
||||
* @param target Which pokemon should the item be used on.
|
||||
* @return True if nothing went wrong. False otherwise.
|
||||
*/
|
||||
public boolean use(Pokemon target) {
|
||||
if (this.amount > 0) {
|
||||
switch (this.type) {
|
||||
case POTION:
|
||||
return potion(target, 20);
|
||||
case SUPERPOTION:
|
||||
return potion(target, 50);
|
||||
case HYPERPOTION:
|
||||
return potion(target, 200);
|
||||
case MAXPOTION:
|
||||
return potion(target, -1);
|
||||
case REVIVE:
|
||||
if (!target.isConscious()) {
|
||||
this.amount--;
|
||||
target.revive();
|
||||
System.out.printf("%s was revived.%n", target.getName());
|
||||
return true;
|
||||
} else {
|
||||
System.out.println("You can't revive a conscious pokemon.");
|
||||
return false;
|
||||
}
|
||||
default:
|
||||
System.out.printf("Invalid item %s%n", name);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
System.out.println("No cheating!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a pokemon is able to, and in need of a potion. If it is, heal it.
|
||||
* @param target The pokemon to heal.
|
||||
* @param amount The amount to heal the pokemon.
|
||||
* @return True if nothing went wrong. False otherwise.
|
||||
*/
|
||||
private boolean potion(Pokemon target, int amount) {
|
||||
if (target.isConscious()) {
|
||||
if (target.isDamaged()) {
|
||||
this.amount--;
|
||||
int healed = target.heal(amount);
|
||||
System.out.printf("%s was healed for %d HP and now has %d HP.%n", target.getName(), healed, target.getHP());
|
||||
return true;
|
||||
} else {
|
||||
System.out.printf("%s has not taken damage, and does not require a potion.%n", target.getName());
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
System.out.println("You can't heal a fainted pokemon.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
20
Trainer.java
20
Trainer.java
@ -3,20 +3,20 @@ import java.util.ArrayList;
|
||||
public class Trainer {
|
||||
String name;
|
||||
private ArrayList<Pokemon> pokemon;
|
||||
private ArrayList<Item> items;
|
||||
private Inventory inventory;
|
||||
|
||||
public Trainer(String name, ArrayList<Pokemon> pokemon, ArrayList<Item> items) {
|
||||
public Trainer(String name, ArrayList<Pokemon> pokemon, Inventory inventory) {
|
||||
this.name = name;
|
||||
this.pokemon = pokemon;
|
||||
this.items = items;
|
||||
this.inventory = inventory;
|
||||
}
|
||||
|
||||
public void setPokemon(ArrayList<Pokemon> pokemon) {
|
||||
this.pokemon = pokemon;
|
||||
}
|
||||
|
||||
public void setItems(ArrayList<Item> items) {
|
||||
this.items = items;
|
||||
public void setInventory(Inventory inventory) {
|
||||
this.inventory = inventory;
|
||||
}
|
||||
|
||||
public ArrayList<Pokemon> getPokemon() {
|
||||
@ -43,13 +43,7 @@ public class Trainer {
|
||||
return pokemon;
|
||||
}
|
||||
|
||||
public ArrayList<Item> getItems() {
|
||||
ArrayList<Item> items = new ArrayList<Item>();
|
||||
for (Item item : this.items) {
|
||||
if (item.getAmount() > 0) {
|
||||
items.add(item);
|
||||
}
|
||||
}
|
||||
return items;
|
||||
public Inventory getInventory() {
|
||||
return this.inventory;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user