Rewrote some methods as non-static.

Functions for getting potions, pokeballs and pokemon are now in non-static classes.
This will make it easier to manage other npc trainers in the future.
This commit is contained in:
Kristian Knarvik 2017-11-27 20:48:24 +01:00
parent b39f03c447
commit 8860f39f2c
4 changed files with 112 additions and 118 deletions

136
Game.java
View File

@ -38,13 +38,13 @@ public class Game {
+ "There really is nothing more to do here.%n", initialPokemon); + "There really is nothing more to do here.%n", initialPokemon);
return; return;
} }
if (!consciousPokemon(player.getPokemon())) { if (player.getConsciousPokemon().size() < 1) {
System.out.println("All your pokemon have fainted. Your journey ends here."); System.out.println("All your pokemon have fainted. Your journey ends here.");
return; return;
} }
while (trainersPokemon == null || !trainersPokemon.isConscious()) { while (trainersPokemon == null || !trainersPokemon.isConscious()) {
availablePokemon(player.getConsciousPokemon()); player.availablePokemon(true);
trainersPokemon = usersChoice(player.getConsciousPokemon()); trainersPokemon = player.choosePokemon(true);
} }
System.out.printf("Opponent: %s%nWhat will you do?%n", opponentPokemon); System.out.printf("Opponent: %s%nWhat will you do?%n", opponentPokemon);
System.out.printf("b: battle" System.out.printf("b: battle"
@ -75,18 +75,18 @@ public class Game {
break; break;
case 'h': case 'h':
if (player.getInventory().getPotions().size() > 0) { if (player.getInventory().getPotions().size() > 0) {
availablePotions(player.getInventory().getPotions()); player.getInventory().availablePotions();
currentPotion = chosenPotion(player.getInventory().getPotions()); currentPotion = player.getInventory().chosenPotion();
if (currentPotion == null) { if (currentPotion == null) {
in.nextLine(); in.nextLine();
System.out.println("Invalid potion."); System.out.println("Invalid potion.");
} else { } else {
if (currentPotion.needsAlive()) { if (currentPotion.needsAlive()) {
availablePokemon(player.getConsciousPokemon()); player.availablePokemon(true);
pokemonToHeal = usersChoice(player.getConsciousPokemon()); pokemonToHeal = player.choosePokemon(true);
} else { } else {
availablePokemon(player.getFaintedPokemon()); player.availablePokemon(false);
pokemonToHeal = usersChoice(player.getFaintedPokemon()); pokemonToHeal = player.choosePokemon(false);
} }
if (pokemonToHeal == null) { if (pokemonToHeal == null) {
System.out.println("That is not a valid pokemon"); System.out.println("That is not a valid pokemon");
@ -102,8 +102,8 @@ public class Game {
break; break;
case 't': case 't':
if (player.getInventory().getPokeballs().size() > 0) { if (player.getInventory().getPokeballs().size() > 0) {
availablePokeballs(player.getInventory().getPokeballs()); player.getInventory().availablePokeballs();
currentPokeball = chosenPokeball(player.getInventory().getPokeballs()); currentPokeball = player.getInventory().chosenPokeball();
if (currentPokeball == null) { if (currentPokeball == null) {
in.nextLine(); in.nextLine();
System.out.println("Invalid pokeball."); System.out.println("Invalid pokeball.");
@ -127,11 +127,11 @@ public class Game {
} }
break; break;
case 'c': case 'c':
availablePokemon(player.getConsciousPokemon()); player.availablePokemon(true);
trainersPokemon = usersChoice(player.getConsciousPokemon()); trainersPokemon = player.choosePokemon(true);
while (trainersPokemon == null) { while (trainersPokemon == null) {
availablePokemon(player.getConsciousPokemon()); player.availablePokemon(true);
trainersPokemon = usersChoice(player.getConsciousPokemon()); trainersPokemon = player.choosePokemon(true);
} }
opponentPokemon.attack(trainersPokemon); opponentPokemon.attack(trainersPokemon);
break; break;
@ -152,8 +152,8 @@ public class Game {
player.setInventory(loadedInventory); player.setInventory(loadedInventory);
if (pokemon.size() > 0 && player.getPokemon().size() > 0) { if (pokemon.size() > 0 && player.getPokemon().size() > 0) {
do { do {
availablePokemon(player.getConsciousPokemon()); player.availablePokemon(true);
trainersPokemon = usersChoice(player.getConsciousPokemon()); trainersPokemon = player.choosePokemon(true);
} while (trainersPokemon == null || !trainersPokemon.isConscious()); } while (trainersPokemon == null || !trainersPokemon.isConscious());
opponentPokemon = randomPokemon(pokemon); opponentPokemon = randomPokemon(pokemon);
} }
@ -171,8 +171,7 @@ public class Game {
} }
break; break;
case 'v': case 'v':
availablePokemon(player.getPokemon()); player.printPokemon();
System.out.println();
break; break;
case 'q': case 'q':
done = true; done = true;
@ -184,7 +183,7 @@ public class Game {
} }
/** /**
* Prevents wild fainted pokemon to ever be encountered again. * Prevents wild fainted pokemon from ever being encountered again.
* @param pokemonList List of pokemon to search. * @param pokemonList List of pokemon to search.
* @param target The pokemon to remove. * @param target The pokemon to remove.
*/ */
@ -196,26 +195,6 @@ public class Game {
} }
} }
/**
* Lists all currently available items for the user.
* @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 availablePotions(ArrayList<Potion> potions) {
System.out.println("You may choose from these items:");
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(">");
}
/** /**
* Gives a trainer necessary starting items. * Gives a trainer necessary starting items.
* @return A list of items. * @return A list of items.
@ -234,83 +213,6 @@ public class Game {
return inventory; return inventory;
} }
/**
* Lists all currently available pokemon for the user.
* @param pokemonList List of all the user's pokemon.
* @param type Should we only include a certain type of pokemon.
*/
public static void availablePokemon(ArrayList<Pokemon> pokemonList) {
System.out.println("You may choose from these pokemon:");
for (int i = 0; i < pokemonList.size(); i++) {
System.out.printf("%d: %s%n", i + 1, pokemonList.get(i));
}
System.out.print(">");
}
/**
* Checks if all of the pokemon in a list have fainted.
* @param pokemonList List of the user's pokemon.
* @return True if the user has at least one pokemon left. False otherwise.
*/
public static boolean consciousPokemon(ArrayList<Pokemon> pokemonList) {
int pokemonLeft = 0;
for (Pokemon pokemon : pokemonList) {
if (pokemon.isConscious()) {
pokemonLeft++;
}
}
return pokemonLeft > 0;
}
/**
* Asks the user for the name of a pokemon and returns it.
* @param pokemonList A list of available pokemon
* @return A pokemon object or null.
*/
public static Pokemon usersChoice(ArrayList<Pokemon> pokemonList) {
if (in.hasNextInt()) {
int choice = in.nextInt() - 1;
if (choice >= 0 && choice < pokemonList.size()) {
in.nextLine();
return pokemonList.get(choice);
}
}
System.out.println("Invalid pokemon");
in.nextLine();
return null;
}
/**
* Asks the user for an item, and validates it.
* @param itemList Available items.
* @return An Item object or null.
*/
public static Potion chosenPotion(ArrayList<Potion> potions) {
if (in.hasNextInt()) {
int choice = in.nextInt() - 1;
if (choice >= 0 && choice < potions.size()) {
in.nextLine();
return potions.get(choice);
}
} else {
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);
}
} else {
in.nextLine();
}
return null;
}
/** /**
* Chooses a random pokemon from a list. * Chooses a random pokemon from a list.
* @param pokemon The list to choose from. * @param pokemon The list to choose from.

View File

@ -1,6 +1,8 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Scanner;
public class Inventory { public class Inventory {
public static Scanner in = new Scanner(System.in);
private ArrayList<Pokeball> pokeballs; private ArrayList<Pokeball> pokeballs;
private ArrayList<Potion> potions; private ArrayList<Potion> potions;
@ -47,4 +49,49 @@ public class Inventory {
} }
return potions; return potions;
} }
public void availablePokeballs() {
System.out.println("You may choose from these items:");
for (int i = 0; i < this.pokeballs.size(); i++) {
System.out.printf("%d: %s%n", i + 1, this.pokeballs.get(i));
}
System.out.print(">");
}
public void availablePotions() {
System.out.println("You may choose from these items:");
ArrayList<Potion> potionList = this.getPotions();
for (int i = 0; i < potionList.size(); i++) {
System.out.printf("%d: %s%n", i + 1, potionList.get(i));
}
System.out.print(">");
}
public Potion chosenPotion() {
ArrayList<Potion> potions = this.getPotions();
if (in.hasNextInt()) {
int choice = in.nextInt() - 1;
if (choice >= 0 && choice < potions.size()) {
in.nextLine();
return potions.get(choice);
}
} else {
in.nextLine();
}
return null;
}
public Pokeball chosenPokeball() {
ArrayList<Pokeball> pokeballs = this.getPokeballs();
if (in.hasNextInt()) {
int choice = in.nextInt() - 1;
if (choice >= 0 && choice < pokeballs.size()) {
in.nextLine();
return pokeballs.get(choice);
}
} else {
in.nextLine();
}
return null;
}
} }

View File

@ -103,5 +103,4 @@ public class Potion {
return false; return false;
} }
} }
} }

View File

@ -1,6 +1,8 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Scanner;
public class Trainer { public class Trainer {
public static Scanner in = new Scanner(System.in);
String name; String name;
private ArrayList<Pokemon> pokemon; private ArrayList<Pokemon> pokemon;
private Inventory inventory; private Inventory inventory;
@ -50,4 +52,48 @@ public class Trainer {
public Inventory getInventory() { public Inventory getInventory() {
return this.inventory; return this.inventory;
} }
/** Lists all currently available pokemon for the trainer.*/
public void availablePokemon(boolean alive) {
ArrayList<Pokemon> pokemonList = null;
if (alive) {
pokemonList = this.getConsciousPokemon();
} else {
pokemonList = this.getFaintedPokemon();
}
System.out.println("You may choose from these pokemon:");
for (int i = 0; i < pokemonList.size(); i++) {
System.out.printf("%d: %s%n", i + 1, pokemonList.get(i));
}
System.out.print(">");
}
public void printPokemon() {
for (Pokemon pokemon : this.pokemon) {
System.out.println(pokemon);
}
}
/**
* Asks the user for the name of a pokemon and returns it.
* @param pokemonList A list of available pokemon
* @return A pokemon object or null.
*/
public Pokemon choosePokemon(boolean alive) {
ArrayList<Pokemon> pokemonList = null;
if (alive) {
pokemonList = this.getConsciousPokemon();
} else {
pokemonList = this.getFaintedPokemon();
}
if (in.hasNextInt()) {
int choice = in.nextInt() - 1;
if (choice >= 0 && choice < pokemonList.size()) {
in.nextLine();
return pokemonList.get(choice);
}
}
in.nextLine();
return null;
}
} }