Fixed some errors and issue #14.

Capturing pokemon now happens inside the Trainer object. usersPokemon is no longer used.
This commit is contained in:
Kristian Knarvik 2017-11-27 11:11:31 +01:00
parent b242cfd183
commit 666c89fa8f
4 changed files with 15 additions and 10 deletions

View File

@ -15,11 +15,10 @@ public class Game {
public static void main(String[] args) { public static void main(String[] args) {
ArrayList<Pokemon> pokemon = readPokemon(); ArrayList<Pokemon> pokemon = readPokemon();
int initialPokemon = pokemon.size(); int initialPokemon = pokemon.size();
ArrayList<Pokemon> usersPokemon = randomTeam();
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, createInventory()); Trainer player = new Trainer(name, randomTeam(), createInventory());
boolean done = false; boolean done = false;
Pokemon opponentPokemon = null; Pokemon opponentPokemon = null;
@ -79,6 +78,7 @@ public class Game {
availablePotions(player.getInventory().getPotions()); availablePotions(player.getInventory().getPotions());
currentPotion = chosenPotion(player.getInventory().getPotions()); currentPotion = chosenPotion(player.getInventory().getPotions());
if (currentPotion == null) { if (currentPotion == null) {
in.nextLine();
System.out.println("Invalid potion."); System.out.println("Invalid potion.");
} else { } else {
if (currentPotion.needsAlive()) { if (currentPotion.needsAlive()) {
@ -105,14 +105,15 @@ public class Game {
availablePokeballs(player.getInventory().getPokeballs()); availablePokeballs(player.getInventory().getPokeballs());
currentPokeball = chosenPokeball(player.getInventory().getPokeballs()); currentPokeball = chosenPokeball(player.getInventory().getPokeballs());
if (currentPokeball == null) { if (currentPokeball == null) {
in.nextLine();
System.out.println("Invalid pokeball."); System.out.println("Invalid pokeball.");
} else { } else {
if (currentPokeball.getType() == Pokeball.Pokeballs.MASTERBALL) { if (currentPokeball.getType() == Pokeball.Pokeballs.MASTERBALL) {
currentPokeball.use(opponentPokemon, pokemon, usersPokemon); currentPokeball.use(opponentPokemon, pokemon, player);
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 = currentPokeball.use(opponentPokemon, pokemon, usersPokemon); boolean captured = currentPokeball.use(opponentPokemon, pokemon, player);
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());
@ -136,7 +137,7 @@ public class Game {
break; break;
case 's': case 's':
savePokemon(pokemon, "pokemon.save"); savePokemon(pokemon, "pokemon.save");
savePokemon(usersPokemon, "user.save"); savePokemon(player.getPokemon(), "user.save");
saveInventory(player.getInventory(), "inventory.save"); saveInventory(player.getInventory(), "inventory.save");
break; break;
case 'l': case 'l':
@ -149,7 +150,7 @@ public class Game {
pokemon = loadedPokemon; pokemon = loadedPokemon;
player.setPokemon(loadedUsersPokemon); player.setPokemon(loadedUsersPokemon);
player.setInventory(loadedInventory); player.setInventory(loadedInventory);
if (pokemon.size() > 0 && usersPokemon.size() > 0) { if (pokemon.size() > 0 && player.getPokemon().size() > 0) {
do { do {
availablePokemon(player.getConsciousPokemon()); availablePokemon(player.getConsciousPokemon());
trainersPokemon = usersChoice(player.getConsciousPokemon()); trainersPokemon = usersChoice(player.getConsciousPokemon());

View File

@ -41,7 +41,7 @@ public class Pokeball {
* @param catcher Where we send the pokemon on a successfull capture. * @param catcher Where we send the pokemon on a successfull capture.
* @return True if nothing went wrong. False otherwise. * @return True if nothing went wrong. False otherwise.
*/ */
public boolean use(Pokemon target, ArrayList<Pokemon> current, ArrayList<Pokemon> catcher) { public boolean use(Pokemon target, ArrayList<Pokemon> current, Trainer catcher) {
if (this.amount > 0) { if (this.amount > 0) {
this.amount--; this.amount--;
switch (this.type) { switch (this.type) {

View File

@ -91,7 +91,7 @@ public class Pokemon {
* @param catcher The list to send the pokemon on successfull capture. * @param catcher The list to send the pokemon on successfull capture.
* @return True on successfull capture. False otherwise. * @return True on successfull capture. False otherwise.
*/ */
public boolean tryCapture(ArrayList<Pokemon> current, ArrayList<Pokemon> catcher, int range) { public boolean tryCapture(ArrayList<Pokemon> current, Trainer catcher, int range) {
if (range == 0) { if (range == 0) {
this.capture(current, catcher); this.capture(current, catcher);
System.out.printf("%s was caught.%n", this.name); System.out.printf("%s was caught.%n", this.name);
@ -112,8 +112,8 @@ public class Pokemon {
* @param current The pokemon list the pokemon belongs to. * @param current The pokemon list the pokemon belongs to.
* @param catcher The pokemon list of the trainer. * @param catcher The pokemon list of the trainer.
*/ */
private void capture(ArrayList<Pokemon> current, ArrayList<Pokemon> catcher) { private void capture(ArrayList<Pokemon> current, Trainer trainer) {
catcher.add(this); trainer.addPokemon(this);
for (int i = 0; i < current.size(); i++) { for (int i = 0; i < current.size(); i++) {
if (current.get(i) == this) { if (current.get(i) == this) {
current.remove(i); current.remove(i);

View File

@ -23,6 +23,10 @@ public class Trainer {
return this.pokemon; return this.pokemon;
} }
public void addPokemon(Pokemon pokemon) {
this.pokemon.add(pokemon);
}
public ArrayList<Pokemon> getConsciousPokemon() { public ArrayList<Pokemon> getConsciousPokemon() {
ArrayList<Pokemon> pokemon = new ArrayList<Pokemon>(); ArrayList<Pokemon> pokemon = new ArrayList<Pokemon>();
for (Pokemon singlePokemon : this.pokemon) { for (Pokemon singlePokemon : this.pokemon) {