Created a trainer class (#12)

Added a trainer class with a name, a list of pokemon and a list of items. Game.java has been changed to treat the player as a Trainer.
This commit is contained in:
Kristian Knarvik 2017-11-25 22:28:17 +01:00 committed by GitHub
parent bdb871d35c
commit 1d2de15c8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 101 additions and 53 deletions

View File

@ -10,23 +10,24 @@ import java.text.ParseException;
/** Simulates the game Pokémon. */ /** Simulates the game Pokémon. */
public class Game { public class Game {
public static Scanner in = new Scanner(System.in);
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> temporaryPokemonList = readPokemon(); ArrayList<Pokemon> usersPokemon = randomTeam();
ArrayList<Pokemon> usersPokemon = new ArrayList<Pokemon>();
for (int i = 1; i <= 6; i++) {
usersPokemon.add(pick(temporaryPokemonList));
}
temporaryPokemonList.clear();
ArrayList<Item> usersItems = prepareInventory(); ArrayList<Item> usersItems = prepareInventory();
System.out.println("What is your name?");
String name = in.nextLine();
Trainer player = new Trainer(name, usersPokemon, usersItems);
boolean done = false; boolean done = false;
Pokemon opponentPokemon = null; Pokemon opponentPokemon = null;
Pokemon trainersPokemon = null; Pokemon trainersPokemon = null;
Item currentItem = null; Item currentItem = null;
boolean fleeSuccess = false; boolean fleeSuccess = false;
Pokemon pokemonToHeal = null; Pokemon pokemonToHeal = null;
Scanner in = new Scanner(System.in);
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());
@ -38,13 +39,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(usersPokemon)) { if (!consciousPokemon(player.getPokemon())) {
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(usersPokemon, "conscious"); availablePokemon(player.getConsciousPokemon());
trainersPokemon = usersChoice(usersPokemon, true); trainersPokemon = usersChoice(player.getConsciousPokemon());
} }
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"
@ -81,11 +82,11 @@ public class Game {
System.out.println("Invalid item."); System.out.println("Invalid item.");
} else { } else {
if (currentItem.needsAlive()) { if (currentItem.needsAlive()) {
availablePokemon(usersPokemon, "conscious"); availablePokemon(player.getConsciousPokemon());
pokemonToHeal = usersChoice(usersPokemon, true); pokemonToHeal = usersChoice(player.getConsciousPokemon());
} else { } else {
availablePokemon(usersPokemon, "fainted"); availablePokemon(player.getFaintedPokemon());
pokemonToHeal = usersChoice(usersPokemon, false); pokemonToHeal = usersChoice(player.getFaintedPokemon());
} }
if (pokemonToHeal == null) { if (pokemonToHeal == null) {
System.out.println("That is not a valid pokemon"); System.out.println("That is not a valid pokemon");
@ -125,11 +126,11 @@ public class Game {
} }
break; break;
case 'c': case 'c':
availablePokemon(usersPokemon, "conscious"); availablePokemon(player.getConsciousPokemon());
trainersPokemon = usersChoice(usersPokemon, true); trainersPokemon = usersChoice(player.getConsciousPokemon());
while (trainersPokemon == null) { while (trainersPokemon == null) {
availablePokemon(usersPokemon, "conscious"); availablePokemon(player.getConsciousPokemon());
trainersPokemon = usersChoice(usersPokemon, true); trainersPokemon = usersChoice(player.getConsciousPokemon());
} }
opponentPokemon.attack(trainersPokemon); opponentPokemon.attack(trainersPokemon);
break; break;
@ -146,12 +147,12 @@ public class Game {
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;
usersPokemon = loadedUsersPokemon; player.setPokemon(loadedUsersPokemon);
usersItems = loadedUsersItems; player.setItems(loadedUsersItems);
if (pokemon.size() > 0 && usersPokemon.size() > 0) { if (pokemon.size() > 0 && usersPokemon.size() > 0) {
do { do {
availablePokemon(usersPokemon, "conscious"); availablePokemon(player.getConsciousPokemon());
trainersPokemon = usersChoice(usersPokemon, true); trainersPokemon = usersChoice(player.getConsciousPokemon());
} while (trainersPokemon == null || !trainersPokemon.isConscious()); } while (trainersPokemon == null || !trainersPokemon.isConscious());
opponentPokemon = randomPokemon(pokemon); opponentPokemon = randomPokemon(pokemon);
} }
@ -169,7 +170,7 @@ public class Game {
} }
break; break;
case 'v': case 'v':
availablePokemon(usersPokemon, "all"); availablePokemon(player.getPokemon());
System.out.println(); System.out.println();
break; break;
case 'q': case 'q':
@ -247,23 +248,10 @@ public class Game {
* @param pokemonList List of all the user's pokemon. * @param pokemonList List of all the user's pokemon.
* @param type Should we only include a certain type of pokemon. * @param type Should we only include a certain type of pokemon.
*/ */
public static void availablePokemon(ArrayList<Pokemon> pokemonList, String type) { public static void availablePokemon(ArrayList<Pokemon> pokemonList) {
System.out.println("You may choose from these pokemon:"); System.out.println("You may choose from these pokemon:");
for (int i = 0; i < pokemonList.size(); i++) { for (int i = 0; i < pokemonList.size(); i++) {
switch (type) { System.out.printf("%d: %s%n", i + 1, pokemonList.get(i));
case "conscious":
if (pokemonList.get(i).isConscious()) {
System.out.printf("%d: %s%n", i + 1, pokemonList.get(i));
}
break;
case "fainted":
if (!pokemonList.get(i).isConscious()) {
System.out.printf("%d: %s%n", i + 1, pokemonList.get(i));
}
break;
default:
System.out.printf("%d: %s%n", i + 1, pokemonList.get(i));
}
} }
System.out.print(">"); System.out.print(">");
@ -289,14 +277,11 @@ public class Game {
* @param pokemonList A list of available pokemon * @param pokemonList A list of available pokemon
* @return A pokemon object or null. * @return A pokemon object or null.
*/ */
public static Pokemon usersChoice(ArrayList<Pokemon> pokemonList, boolean alive) { public static Pokemon usersChoice(ArrayList<Pokemon> pokemonList) {
Scanner in = new Scanner(System.in);
if (in.hasNextInt()) { if (in.hasNextInt()) {
int choice = in.nextInt() - 1; int choice = in.nextInt() - 1;
in.nextLine(); in.nextLine();
if (choice >= 0 && choice < pokemonList.size() && alive && pokemonList.get(choice).isConscious()) { if (choice >= 0 && choice < pokemonList.size()) {
return pokemonList.get(choice);
} else if (choice >= 0 && choice < pokemonList.size() && !alive && !pokemonList.get(choice).isConscious()) {
return pokemonList.get(choice); return pokemonList.get(choice);
} else { } else {
System.out.println("Invalid pokemon"); System.out.println("Invalid pokemon");
@ -311,7 +296,6 @@ public class Game {
* @return An Item object or null. * @return An Item object or null.
*/ */
public static Item chosenItem(ArrayList<Item> itemList, Item.Target target) { public static Item chosenItem(ArrayList<Item> itemList, Item.Target target) {
Scanner in = new Scanner(System.in);
if (in.hasNextInt()) { if (in.hasNextInt()) {
int choice = in.nextInt() - 1; int choice = in.nextInt() - 1;
in.nextLine(); in.nextLine();
@ -343,9 +327,9 @@ public class Game {
*/ */
public static ArrayList<Pokemon> readPokemon() { public static ArrayList<Pokemon> readPokemon() {
ArrayList<Pokemon> pokemon = new ArrayList<Pokemon>(); ArrayList<Pokemon> pokemon = new ArrayList<Pokemon>();
try (Scanner in = new Scanner(new File("Pokemon.txt"))) { try (Scanner file = new Scanner(new File("Pokemon.txt"))) {
while (in.hasNextLine()) { while (file.hasNextLine()) {
pokemon.add(new Pokemon(in.nextLine())); pokemon.add(new Pokemon(file.nextLine()));
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
System.out.println("You seem to be missing one of the necessary files to run this program."); System.out.println("You seem to be missing one of the necessary files to run this program.");
@ -404,10 +388,10 @@ public class Game {
*/ */
public static ArrayList<Pokemon> loadPokemon(String savefile) { public static ArrayList<Pokemon> loadPokemon(String savefile) {
ArrayList<Pokemon> pokemon = new ArrayList<Pokemon>(); ArrayList<Pokemon> pokemon = new ArrayList<Pokemon>();
try (Scanner in = new Scanner(new File(savefile))) { try (Scanner file = new Scanner(new File(savefile))) {
NumberFormat format = NumberFormat.getInstance(Locale.ENGLISH); NumberFormat format = NumberFormat.getInstance(Locale.ENGLISH);
while (in.hasNextLine()) { while (file.hasNextLine()) {
String[] data = in.nextLine().split(";"); String[] data = file.nextLine().split(";");
try { try {
pokemon.add(new Pokemon(data[0], Integer.parseInt(data[1]), Integer.parseInt(data[2]), Integer.parseInt(data[3]), format.parse(data[4]).doubleValue(), Integer.parseInt(data[5]), Integer.parseInt(data[6]), Integer.parseInt(data[7]))); pokemon.add(new Pokemon(data[0], Integer.parseInt(data[1]), Integer.parseInt(data[2]), Integer.parseInt(data[3]), format.parse(data[4]).doubleValue(), Integer.parseInt(data[5]), Integer.parseInt(data[6]), Integer.parseInt(data[7])));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
@ -433,10 +417,10 @@ public class Game {
*/ */
public static ArrayList<Item> loadItems(String savefile) { public static ArrayList<Item> loadItems(String savefile) {
ArrayList<Item> items = new ArrayList<Item>(); ArrayList<Item> items = new ArrayList<Item>();
try (Scanner in = new Scanner(new File(savefile))) { try (Scanner file = new Scanner(new File(savefile))) {
while (in.hasNextLine()) { while (file.hasNextLine()) {
try { try {
String[] data = in.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]))); items.add(new Item(data[0], data[1], Integer.parseInt(data[2]), Item.Target.valueOf(data[3]), Boolean.parseBoolean(data[4])));
} catch (ArrayIndexOutOfBoundsException e) { } catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Invalid savefile: " + savefile); System.out.println("Invalid savefile: " + savefile);
@ -449,4 +433,13 @@ public class Game {
} }
return items; return items;
} }
public static ArrayList<Pokemon> randomTeam() {
ArrayList<Pokemon> temporary = readPokemon();
ArrayList<Pokemon> pokemon = new ArrayList<Pokemon>();
for (int i = 1; i <= 6; i++) {
pokemon.add(pick(temporary));
}
return pokemon;
}
} }

55
Trainer.java Normal file
View File

@ -0,0 +1,55 @@
import java.util.ArrayList;
public class Trainer {
String name;
private ArrayList<Pokemon> pokemon;
private ArrayList<Item> items;
public Trainer(String name, ArrayList<Pokemon> pokemon, ArrayList<Item> items) {
this.name = name;
this.pokemon = pokemon;
this.items = items;
}
public void setPokemon(ArrayList<Pokemon> pokemon) {
this.pokemon = pokemon;
}
public void setItems(ArrayList<Item> items) {
this.items = items;
}
public ArrayList<Pokemon> getPokemon() {
return this.pokemon;
}
public ArrayList<Pokemon> getConsciousPokemon() {
ArrayList<Pokemon> pokemon = new ArrayList<Pokemon>();
for (Pokemon singlePokemon : this.pokemon) {
if (singlePokemon.isConscious()) {
pokemon.add(singlePokemon);
}
}
return pokemon;
}
public ArrayList<Pokemon> getFaintedPokemon() {
ArrayList<Pokemon> pokemon = new ArrayList<Pokemon>();
for (Pokemon singlePokemon : this.pokemon) {
if (!singlePokemon.isConscious()) {
pokemon.add(singlePokemon);
}
}
return pokemon;
}
public ArrayList<Item> getItems() {
ArrayList<Item> items = new ArrayList<Item>();
for (Item item : this.items) {
if (item.getAmount() > 0) {
items.add(item);
}
}
return items;
}
}