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 file
|
||||||
*.log
|
*.log
|
||||||
|
|
||||||
|
# Save files
|
||||||
|
*.save
|
||||||
|
|
||||||
# BlueJ files
|
# BlueJ files
|
||||||
*.ctxt
|
*.ctxt
|
||||||
|
|
||||||
|
158
Game.java
158
Game.java
@ -16,16 +16,16 @@ public class Game {
|
|||||||
ArrayList<Pokemon> pokemon = readPokemon();
|
ArrayList<Pokemon> pokemon = readPokemon();
|
||||||
int initialPokemon = pokemon.size();
|
int initialPokemon = pokemon.size();
|
||||||
ArrayList<Pokemon> usersPokemon = randomTeam();
|
ArrayList<Pokemon> usersPokemon = randomTeam();
|
||||||
ArrayList<Item> usersItems = prepareInventory();
|
|
||||||
|
|
||||||
System.out.println("What is your name?");
|
System.out.println("What is your name?");
|
||||||
String name = in.nextLine();
|
String name = in.nextLine();
|
||||||
Trainer player = new Trainer(name, usersPokemon, usersItems);
|
Trainer player = new Trainer(name, usersPokemon, createInventory());
|
||||||
|
|
||||||
boolean done = false;
|
boolean done = false;
|
||||||
Pokemon opponentPokemon = null;
|
Pokemon opponentPokemon = null;
|
||||||
Pokemon trainersPokemon = null;
|
Pokemon trainersPokemon = null;
|
||||||
Item currentItem = null;
|
Pokeball currentPokeball = null;
|
||||||
|
Potion currentPotion = null;
|
||||||
boolean fleeSuccess = false;
|
boolean fleeSuccess = false;
|
||||||
Pokemon pokemonToHeal = null;
|
Pokemon pokemonToHeal = null;
|
||||||
|
|
||||||
@ -75,13 +75,13 @@ public class Game {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
if (itemsLeft(usersItems, Item.Target.SELF)) {
|
if (player.getInventory().getPotions().size() > 0) {
|
||||||
availableItems(usersItems, Item.Target.SELF);
|
availablePotions(player.getInventory().getPotions());
|
||||||
currentItem = chosenItem(usersItems, Item.Target.SELF);
|
currentPotion = chosenPotion(player.getInventory().getPotions());
|
||||||
if (currentItem == null) {
|
if (currentPotion == null) {
|
||||||
System.out.println("Invalid item.");
|
System.out.println("Invalid potion.");
|
||||||
} else {
|
} else {
|
||||||
if (currentItem.needsAlive()) {
|
if (currentPotion.needsAlive()) {
|
||||||
availablePokemon(player.getConsciousPokemon());
|
availablePokemon(player.getConsciousPokemon());
|
||||||
pokemonToHeal = usersChoice(player.getConsciousPokemon());
|
pokemonToHeal = usersChoice(player.getConsciousPokemon());
|
||||||
} else {
|
} else {
|
||||||
@ -91,7 +91,7 @@ public class Game {
|
|||||||
if (pokemonToHeal == null) {
|
if (pokemonToHeal == null) {
|
||||||
System.out.println("That is not a valid pokemon");
|
System.out.println("That is not a valid pokemon");
|
||||||
} else {
|
} else {
|
||||||
if (currentItem.use(pokemonToHeal)) {
|
if (currentPotion.use(pokemonToHeal)) {
|
||||||
opponentPokemon.attack(trainersPokemon);
|
opponentPokemon.attack(trainersPokemon);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -101,18 +101,18 @@ public class Game {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
if (itemsLeft(usersItems, Item.Target.OTHER)) {
|
if (player.getInventory().getPokeballs().size() > 0) {
|
||||||
availableItems(usersItems, Item.Target.OTHER);
|
availablePokeballs(player.getInventory().getPokeballs());
|
||||||
currentItem = chosenItem(usersItems, Item.Target.OTHER);
|
currentPokeball = chosenPokeball(player.getInventory().getPokeballs());
|
||||||
if (currentItem == null) {
|
if (currentPokeball == null) {
|
||||||
System.out.println("Invalid pokeball.");
|
System.out.println("Invalid pokeball.");
|
||||||
} else {
|
} else {
|
||||||
if (currentItem.getName().toLowerCase().replace(" ", "").equals("masterball")) {
|
if (currentPokeball.getType() == Pokeball.Pokeballs.MASTERBALL) {
|
||||||
currentItem.use(opponentPokemon, pokemon, usersPokemon);
|
currentPokeball.use(opponentPokemon, pokemon, usersPokemon);
|
||||||
opponentPokemon = randomPokemon(pokemon);
|
opponentPokemon = randomPokemon(pokemon);
|
||||||
System.out.printf("A wild %s appeared.%n", opponentPokemon.getName());
|
System.out.printf("A wild %s appeared.%n", opponentPokemon.getName());
|
||||||
} else {
|
} else {
|
||||||
boolean captured = currentItem.use(opponentPokemon, pokemon, usersPokemon);
|
boolean captured = currentPokeball.use(opponentPokemon, pokemon, usersPokemon);
|
||||||
if (captured) {
|
if (captured) {
|
||||||
opponentPokemon = randomPokemon(pokemon);
|
opponentPokemon = randomPokemon(pokemon);
|
||||||
System.out.printf("A wild %s appeared.%n", opponentPokemon.getName());
|
System.out.printf("A wild %s appeared.%n", opponentPokemon.getName());
|
||||||
@ -137,18 +137,18 @@ public class Game {
|
|||||||
case 's':
|
case 's':
|
||||||
savePokemon(pokemon, "pokemon.save");
|
savePokemon(pokemon, "pokemon.save");
|
||||||
savePokemon(usersPokemon, "user.save");
|
savePokemon(usersPokemon, "user.save");
|
||||||
saveItems(usersItems, "items.save");
|
saveInventory(player.getInventory(), "inventory.save");
|
||||||
break;
|
break;
|
||||||
case 'l':
|
case 'l':
|
||||||
ArrayList<Pokemon> loadedPokemon = loadPokemon("pokemon.save");
|
ArrayList<Pokemon> loadedPokemon = loadPokemon("pokemon.save");
|
||||||
ArrayList<Pokemon> loadedUsersPokemon = loadPokemon("user.save");
|
ArrayList<Pokemon> loadedUsersPokemon = loadPokemon("user.save");
|
||||||
ArrayList<Item> loadedUsersItems = loadItems("items.save");
|
Inventory loadedInventory = loadInventory("inventory.save");
|
||||||
if (loadedPokemon == null || loadedUsersPokemon == null || loadedUsersItems == null) {
|
if (loadedPokemon == null || loadedUsersPokemon == null || loadedInventory == null) {
|
||||||
System.out.println("One or more savefiles seem corrupt. Please delete or fix the affected file(s).");
|
System.out.println("One or more savefiles seem corrupt. Please delete or fix the affected file(s).");
|
||||||
} else {
|
} else {
|
||||||
pokemon = loadedPokemon;
|
pokemon = loadedPokemon;
|
||||||
player.setPokemon(loadedUsersPokemon);
|
player.setPokemon(loadedUsersPokemon);
|
||||||
player.setItems(loadedUsersItems);
|
player.setInventory(loadedInventory);
|
||||||
if (pokemon.size() > 0 && usersPokemon.size() > 0) {
|
if (pokemon.size() > 0 && usersPokemon.size() > 0) {
|
||||||
do {
|
do {
|
||||||
availablePokemon(player.getConsciousPokemon());
|
availablePokemon(player.getConsciousPokemon());
|
||||||
@ -200,12 +200,17 @@ public class Game {
|
|||||||
* @param items List of all of the user's items.
|
* @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.
|
* @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:");
|
System.out.println("You may choose from these items:");
|
||||||
for (int i = 0; i < items.size(); i++) {
|
for (int i = 0; i < potions.size(); i++) {
|
||||||
if (items.get(i).getAmount() > 0 && items.get(i).getTarget().equals(target)) {
|
System.out.printf("%d: %s%n", i + 1, potions.get(i));
|
||||||
System.out.printf("%d: %s%n", i + 1, items.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(">");
|
System.out.print(">");
|
||||||
}
|
}
|
||||||
@ -214,33 +219,18 @@ public class Game {
|
|||||||
* Gives a trainer necessary starting items.
|
* Gives a trainer necessary starting items.
|
||||||
* @return A list of items.
|
* @return A list of items.
|
||||||
*/
|
*/
|
||||||
public static ArrayList<Item> prepareInventory() {
|
public static Inventory createInventory() {
|
||||||
ArrayList<Item> usersItems = new ArrayList<Item>();
|
Inventory inventory = new Inventory();
|
||||||
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));
|
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);
|
||||||
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));
|
inventory.addPokeball("Great ball", "A good, high-performance Poké Ball that provides a higher Pokémon catch rate than a standard Poké Ball.", 10);
|
||||||
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));
|
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);
|
||||||
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));
|
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);
|
||||||
usersItems.add(new Item("Potion", "Heals a pokemon for 20 HP.", 20, Item.Target.SELF));
|
inventory.addPotion("Potion", "Heals a pokemon for 20 HP.", 20, true);
|
||||||
usersItems.add(new Item("Super Potion", "Heals a pokemon for 50 HP.", 10, Item.Target.SELF));
|
inventory.addPotion("Super Potion", "Heals a pokemon for 50 HP.", 10, true);
|
||||||
usersItems.add(new Item("Hyper Potion", "Heals a pokemon for 200 HP.", 5, Item.Target.SELF));
|
inventory.addPotion("Hyper Potion", "Heals a pokemon for 200 HP.", 5, true);
|
||||||
usersItems.add(new Item("Max Potion", "Fully heals a pokemon.", 1, Item.Target.SELF));
|
inventory.addPotion("Max Potion", "Fully heals a pokemon.", 1, true);
|
||||||
usersItems.add(new Item("Revive", "Revives a fainted pokemon.", 2, Item.Target.SELF, false));
|
inventory.addPotion("Revive", "Revives a fainted pokemon.", 2, false);
|
||||||
return usersItems;
|
return inventory;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -280,13 +270,13 @@ public class Game {
|
|||||||
public static Pokemon usersChoice(ArrayList<Pokemon> pokemonList) {
|
public static Pokemon usersChoice(ArrayList<Pokemon> pokemonList) {
|
||||||
if (in.hasNextInt()) {
|
if (in.hasNextInt()) {
|
||||||
int choice = in.nextInt() - 1;
|
int choice = in.nextInt() - 1;
|
||||||
in.nextLine();
|
|
||||||
if (choice >= 0 && choice < pokemonList.size()) {
|
if (choice >= 0 && choice < pokemonList.size()) {
|
||||||
|
in.nextLine();
|
||||||
return pokemonList.get(choice);
|
return pokemonList.get(choice);
|
||||||
} else {
|
|
||||||
System.out.println("Invalid pokemon");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
System.out.println("Invalid pokemon");
|
||||||
|
in.nextLine();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -295,16 +285,26 @@ public class Game {
|
|||||||
* @param itemList Available items.
|
* @param itemList Available items.
|
||||||
* @return An Item object or null.
|
* @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()) {
|
if (in.hasNextInt()) {
|
||||||
int choice = in.nextInt() - 1;
|
int choice = in.nextInt() - 1;
|
||||||
in.nextLine();
|
if (choice >= 0 && choice < potions.size()) {
|
||||||
if (choice >= 0 && choice < itemList.size()) {
|
in.nextLine();
|
||||||
return itemList.get(choice);
|
return potions.get(choice);
|
||||||
} else {
|
|
||||||
System.out.println("Invalid item");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -370,12 +370,16 @@ public class Game {
|
|||||||
* @param itemList List of Item objects to save.
|
* @param itemList List of Item objects to save.
|
||||||
* @param savefile The file to write to.
|
* @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)) {
|
try (PrintWriter file = new PrintWriter(savefile)) {
|
||||||
for (Item item : itemList) {
|
for (Pokeball pokeball : inventory.getPokeballs()) {
|
||||||
file.println(item.saveString());
|
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) {
|
} catch (FileNotFoundException e) {
|
||||||
System.out.println("The savefile could not be written.");
|
System.out.println("The savefile could not be written.");
|
||||||
}
|
}
|
||||||
@ -415,13 +419,27 @@ public class Game {
|
|||||||
* @param savefile The file to write to.
|
* @param savefile The file to write to.
|
||||||
* @return A list of items or null on failiure.
|
* @return A list of items or null on failiure.
|
||||||
*/
|
*/
|
||||||
public static ArrayList<Item> loadItems(String savefile) {
|
public static Inventory loadInventory(String savefile) {
|
||||||
ArrayList<Item> items = new ArrayList<Item>();
|
ArrayList<Pokeball> pokeballs = new ArrayList<Pokeball>();
|
||||||
|
ArrayList<Potion> potions = new ArrayList<Potion>();
|
||||||
try (Scanner file = new Scanner(new File(savefile))) {
|
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()) {
|
while (file.hasNextLine()) {
|
||||||
try {
|
try {
|
||||||
String[] data = file.nextLine().split(";");
|
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) {
|
} catch (ArrayIndexOutOfBoundsException e) {
|
||||||
System.out.println("Invalid savefile: " + savefile);
|
System.out.println("Invalid savefile: " + savefile);
|
||||||
return null;
|
return null;
|
||||||
@ -431,7 +449,7 @@ public class Game {
|
|||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
System.out.println("You don't have a valid savefile.");
|
System.out.println("You don't have a valid savefile.");
|
||||||
}
|
}
|
||||||
return items;
|
return new Inventory(pokeballs, potions);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ArrayList<Pokemon> randomTeam() {
|
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,24 +1,25 @@
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
public class Item {
|
|
||||||
public enum Target { SELF, OTHER }
|
public class Potion {
|
||||||
|
public static enum Potions { POTION, SUPERPOTION, HYPERPOTION, MAXPOTION, REVIVE }
|
||||||
private String name;
|
private String name;
|
||||||
private String description;
|
private String description;
|
||||||
private Target target;
|
private Potions type;
|
||||||
private boolean alive;
|
private boolean alive;
|
||||||
private int amount;
|
private int amount;
|
||||||
|
|
||||||
public Item(String name, String description, int amount, Target target) {
|
public Potion(String name, String description, int amount, Potions type) {
|
||||||
|
this.type = type;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.target = target;
|
|
||||||
this.alive = true;
|
this.alive = true;
|
||||||
this.amount = amount;
|
this.amount = amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Item(String name, String description, int amount, Target target, boolean alive) {
|
public Potion(String name, String description, int amount, Potions type, boolean alive) {
|
||||||
|
this.type = Potions.valueOf(name.toUpperCase().replace(" ", ""));
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.target = target;
|
|
||||||
this.alive = alive;
|
this.alive = alive;
|
||||||
this.amount = amount;
|
this.amount = amount;
|
||||||
}
|
}
|
||||||
@ -28,17 +29,13 @@ public class Item {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String saveString() {
|
public String saveString() {
|
||||||
return String.format("%s;%s;%d;%s;%b", this.name, this.description, this.amount, this.target, this.alive);
|
return String.format("%s;%s;%d;%b", this.name, this.description, this.amount, this.alive);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getAmount() {
|
public int getAmount() {
|
||||||
return this.amount;
|
return this.amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Target getTarget() {
|
|
||||||
return this.target;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return this.name;
|
return this.name;
|
||||||
}
|
}
|
||||||
@ -55,17 +52,16 @@ public class Item {
|
|||||||
*/
|
*/
|
||||||
public boolean use(Pokemon target) {
|
public boolean use(Pokemon target) {
|
||||||
if (this.amount > 0) {
|
if (this.amount > 0) {
|
||||||
String name = this.name.toLowerCase().replace(" ", "");
|
switch (this.type) {
|
||||||
switch (name) {
|
case POTION:
|
||||||
case "potion":
|
|
||||||
return potion(target, 20);
|
return potion(target, 20);
|
||||||
case "superpotion":
|
case SUPERPOTION:
|
||||||
return potion(target, 50);
|
return potion(target, 50);
|
||||||
case "hyperpotion":
|
case HYPERPOTION:
|
||||||
return potion(target, 200);
|
return potion(target, 200);
|
||||||
case "maxpotion":
|
case MAXPOTION:
|
||||||
return potion(target, -1);
|
return potion(target, -1);
|
||||||
case "revive":
|
case REVIVE:
|
||||||
if (!target.isConscious()) {
|
if (!target.isConscious()) {
|
||||||
this.amount--;
|
this.amount--;
|
||||||
target.revive();
|
target.revive();
|
||||||
@ -97,6 +93,7 @@ public class Item {
|
|||||||
this.amount--;
|
this.amount--;
|
||||||
int healed = target.heal(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());
|
System.out.printf("%s was healed for %d HP and now has %d HP.%n", target.getName(), healed, target.getHP());
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
System.out.printf("%s has not taken damage, and does not require a potion.%n", target.getName());
|
System.out.printf("%s has not taken damage, and does not require a potion.%n", target.getName());
|
||||||
return false;
|
return false;
|
||||||
@ -105,34 +102,6 @@ public class Item {
|
|||||||
System.out.println("You can't heal a fainted pokemon.");
|
System.out.println("You can't heal a fainted pokemon.");
|
||||||
return false;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
20
Trainer.java
20
Trainer.java
@ -3,20 +3,20 @@ import java.util.ArrayList;
|
|||||||
public class Trainer {
|
public class Trainer {
|
||||||
String name;
|
String name;
|
||||||
private ArrayList<Pokemon> pokemon;
|
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.name = name;
|
||||||
this.pokemon = pokemon;
|
this.pokemon = pokemon;
|
||||||
this.items = items;
|
this.inventory = inventory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPokemon(ArrayList<Pokemon> pokemon) {
|
public void setPokemon(ArrayList<Pokemon> pokemon) {
|
||||||
this.pokemon = pokemon;
|
this.pokemon = pokemon;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setItems(ArrayList<Item> items) {
|
public void setInventory(Inventory inventory) {
|
||||||
this.items = items;
|
this.inventory = inventory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Pokemon> getPokemon() {
|
public ArrayList<Pokemon> getPokemon() {
|
||||||
@ -43,13 +43,7 @@ public class Trainer {
|
|||||||
return pokemon;
|
return pokemon;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Item> getItems() {
|
public Inventory getInventory() {
|
||||||
ArrayList<Item> items = new ArrayList<Item>();
|
return this.inventory;
|
||||||
for (Item item : this.items) {
|
|
||||||
if (item.getAmount() > 0) {
|
|
||||||
items.add(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return items;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user