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);
return;
}
if (!consciousPokemon(player.getPokemon())) {
if (player.getConsciousPokemon().size() < 1) {
System.out.println("All your pokemon have fainted. Your journey ends here.");
return;
}
while (trainersPokemon == null || !trainersPokemon.isConscious()) {
availablePokemon(player.getConsciousPokemon());
trainersPokemon = usersChoice(player.getConsciousPokemon());
player.availablePokemon(true);
trainersPokemon = player.choosePokemon(true);
}
System.out.printf("Opponent: %s%nWhat will you do?%n", opponentPokemon);
System.out.printf("b: battle"
@ -75,18 +75,18 @@ public class Game {
break;
case 'h':
if (player.getInventory().getPotions().size() > 0) {
availablePotions(player.getInventory().getPotions());
currentPotion = chosenPotion(player.getInventory().getPotions());
player.getInventory().availablePotions();
currentPotion = player.getInventory().chosenPotion();
if (currentPotion == null) {
in.nextLine();
System.out.println("Invalid potion.");
} else {
if (currentPotion.needsAlive()) {
availablePokemon(player.getConsciousPokemon());
pokemonToHeal = usersChoice(player.getConsciousPokemon());
player.availablePokemon(true);
pokemonToHeal = player.choosePokemon(true);
} else {
availablePokemon(player.getFaintedPokemon());
pokemonToHeal = usersChoice(player.getFaintedPokemon());
player.availablePokemon(false);
pokemonToHeal = player.choosePokemon(false);
}
if (pokemonToHeal == null) {
System.out.println("That is not a valid pokemon");
@ -102,8 +102,8 @@ public class Game {
break;
case 't':
if (player.getInventory().getPokeballs().size() > 0) {
availablePokeballs(player.getInventory().getPokeballs());
currentPokeball = chosenPokeball(player.getInventory().getPokeballs());
player.getInventory().availablePokeballs();
currentPokeball = player.getInventory().chosenPokeball();
if (currentPokeball == null) {
in.nextLine();
System.out.println("Invalid pokeball.");
@ -127,11 +127,11 @@ public class Game {
}
break;
case 'c':
availablePokemon(player.getConsciousPokemon());
trainersPokemon = usersChoice(player.getConsciousPokemon());
player.availablePokemon(true);
trainersPokemon = player.choosePokemon(true);
while (trainersPokemon == null) {
availablePokemon(player.getConsciousPokemon());
trainersPokemon = usersChoice(player.getConsciousPokemon());
player.availablePokemon(true);
trainersPokemon = player.choosePokemon(true);
}
opponentPokemon.attack(trainersPokemon);
break;
@ -152,8 +152,8 @@ public class Game {
player.setInventory(loadedInventory);
if (pokemon.size() > 0 && player.getPokemon().size() > 0) {
do {
availablePokemon(player.getConsciousPokemon());
trainersPokemon = usersChoice(player.getConsciousPokemon());
player.availablePokemon(true);
trainersPokemon = player.choosePokemon(true);
} while (trainersPokemon == null || !trainersPokemon.isConscious());
opponentPokemon = randomPokemon(pokemon);
}
@ -171,8 +171,7 @@ public class Game {
}
break;
case 'v':
availablePokemon(player.getPokemon());
System.out.println();
player.printPokemon();
break;
case 'q':
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 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.
* @return A list of items.
@ -234,83 +213,6 @@ public class Game {
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.
* @param pokemon The list to choose from.

View File

@ -1,6 +1,8 @@
import java.util.ArrayList;
import java.util.Scanner;
public class Inventory {
public static Scanner in = new Scanner(System.in);
private ArrayList<Pokeball> pokeballs;
private ArrayList<Potion> potions;
@ -47,4 +49,49 @@ public class Inventory {
}
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;
}
}
}

View File

@ -1,6 +1,8 @@
import java.util.ArrayList;
import java.util.Scanner;
public class Trainer {
public static Scanner in = new Scanner(System.in);
String name;
private ArrayList<Pokemon> pokemon;
private Inventory inventory;
@ -50,4 +52,48 @@ public class Trainer {
public Inventory getInventory() {
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;
}
}