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) {
ArrayList<Pokemon> pokemon = readPokemon();
int initialPokemon = pokemon.size();
ArrayList<Pokemon> usersPokemon = randomTeam();
System.out.println("What is your name?");
String name = in.nextLine();
Trainer player = new Trainer(name, usersPokemon, createInventory());
Trainer player = new Trainer(name, randomTeam(), createInventory());
boolean done = false;
Pokemon opponentPokemon = null;
@ -79,6 +78,7 @@ public class Game {
availablePotions(player.getInventory().getPotions());
currentPotion = chosenPotion(player.getInventory().getPotions());
if (currentPotion == null) {
in.nextLine();
System.out.println("Invalid potion.");
} else {
if (currentPotion.needsAlive()) {
@ -105,14 +105,15 @@ public class Game {
availablePokeballs(player.getInventory().getPokeballs());
currentPokeball = chosenPokeball(player.getInventory().getPokeballs());
if (currentPokeball == null) {
in.nextLine();
System.out.println("Invalid pokeball.");
} else {
if (currentPokeball.getType() == Pokeball.Pokeballs.MASTERBALL) {
currentPokeball.use(opponentPokemon, pokemon, usersPokemon);
currentPokeball.use(opponentPokemon, pokemon, player);
opponentPokemon = randomPokemon(pokemon);
System.out.printf("A wild %s appeared.%n", opponentPokemon.getName());
} else {
boolean captured = currentPokeball.use(opponentPokemon, pokemon, usersPokemon);
boolean captured = currentPokeball.use(opponentPokemon, pokemon, player);
if (captured) {
opponentPokemon = randomPokemon(pokemon);
System.out.printf("A wild %s appeared.%n", opponentPokemon.getName());
@ -136,7 +137,7 @@ public class Game {
break;
case 's':
savePokemon(pokemon, "pokemon.save");
savePokemon(usersPokemon, "user.save");
savePokemon(player.getPokemon(), "user.save");
saveInventory(player.getInventory(), "inventory.save");
break;
case 'l':
@ -149,7 +150,7 @@ public class Game {
pokemon = loadedPokemon;
player.setPokemon(loadedUsersPokemon);
player.setInventory(loadedInventory);
if (pokemon.size() > 0 && usersPokemon.size() > 0) {
if (pokemon.size() > 0 && player.getPokemon().size() > 0) {
do {
availablePokemon(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.
* @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) {
this.amount--;
switch (this.type) {

View File

@ -91,7 +91,7 @@ public class Pokemon {
* @param catcher The list to send the pokemon on successfull capture.
* @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) {
this.capture(current, catcher);
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 catcher The pokemon list of the trainer.
*/
private void capture(ArrayList<Pokemon> current, ArrayList<Pokemon> catcher) {
catcher.add(this);
private void capture(ArrayList<Pokemon> current, Trainer trainer) {
trainer.addPokemon(this);
for (int i = 0; i < current.size(); i++) {
if (current.get(i) == this) {
current.remove(i);

View File

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