import java.util.ArrayList;
import java.util.Iterator;
/**
* A PokemonSpecies entry in the Pokedex. Maintains the number of candies associated
* with the Pokemon species as well as the Trainer\'s inventory of Pokemon of this
* species.
*/
public class PokemonSpecies {
private int pokedexNumber;
private String speciesName;
private int candies;
/**
* Maintains the list of Pokemon of this species in the Trainer\'s inventory.
*/
private ArrayList caughtPokemon;
/**
* Constructor suitable for a newly encountered Pokemon species during the course of the
* game and for loading species data from a save file.
*
* @param pokedexNumber the Pokedex Number for the species
* @param speciesName the name of the species
* @param candies the number of candies the player has obtained by catching
* or transferring Pokemon of this species
*/
public PokemonSpecies(int pokedexNumber, String speciesName, int candies) {
this.pokedexNumber = pokedexNumber;
this.speciesName = speciesName;
this.candies = candies;
// construct caughtPokemon
caughtPokemon = new ArrayList();
}
/**
* Getter methods
*/
public Integer getPokedexNumber() {
return pokedexNumber;
}
public String getSpeciesName() {
return speciesName;
}
public int getCandies() {
return candies;
}
/**
* Add a newly caught Pokemon to the player\'s inventory and
* increase the number of candies for this PokemonSpecies
*
* @param pokemon the newly caught Pokemon
*/
public void addNewPokemon(Pokemon pokemon) {
// TODO
caughtPokemon.add(pokemon);
this.candies++;
}
/**
* Helper function to load Pokemon from a save file into the player\'s inventory for this
* Pokemon Species
*
* @param pokemon the pokemon to add to this species
*/
public void loadPokemon(Pokemon pokemon) {
caughtPokemon.add(pokemon);
}
/**
* Find a Pokemon of the given combatPower in the player\'s inventory for this species.
*
* @param cp the combatPower of the Pokemon to find
* @throws PokedexException [Config.POKEMON_NOT_FOUND] if there is no Pokemon
with the
* given combatPower in the player\'s inventory.
* @return the first Pokemon with the provided combatPower
*/
public Pokemon findPokemon(int cp) throws PokedexException {
// TODO
for(int i=0;i 0){
return true;
}
return false;
}
/**
* Increment candies when a new pokemon is caught
*/
private void addNewPokemonCandies() {
this.candies += PokemonGO.NEW_POKEMON_CANDIES;
}
/**
* Increment candies when a pokemon is transferred to the professor
*/
private void addTransferCandies() {
this.candies += PokemonGO.TRANSFER_POKEMON_CANDIES;
}
/**
* Prepare a listing of all the Pokemon of this species that are currently in the
* player\'s inventory.
*
* @return a String of the form
* ...
*/
public String caughtPokemonToString() {
// TODO
return pokedexNumber+\" Name: \"+speciesName+\" Candies\"+candies;
}
/**
* Prepare a String representing this entire PokemonSpecies. This is used to
* save the PokemonSpecies (one part of the Pokedex) to a file to
* save the player\'s game.
*
* @return a String of the form
* [, ,.
1. import java.util.ArrayList;
import java.util.Iterator;
/**
* A PokemonSpecies entry in the Pokedex. Maintains the number of candies associated
* with the Pokemon species as well as the Trainer's inventory of Pokemon of this
* species.
*/
public class PokemonSpecies {
private int pokedexNumber;
private String speciesName;
private int candies;
/**
* Maintains the list of Pokemon of this species in the Trainer's inventory.
*/
private ArrayList caughtPokemon;
/**
* Constructor suitable for a newly encountered Pokemon species during the course of the
* game and for loading species data from a save file.
*
* @param pokedexNumber the Pokedex Number for the species
* @param speciesName the name of the species
* @param candies the number of candies the player has obtained by catching
* or transferring Pokemon of this species
*/
public PokemonSpecies(int pokedexNumber, String speciesName, int candies) {
this.pokedexNumber = pokedexNumber;
this.speciesName = speciesName;
this.candies = candies;
// construct caughtPokemon
caughtPokemon = new ArrayList();
}
/**
* Getter methods
*/
2. public Integer getPokedexNumber() {
return pokedexNumber;
}
public String getSpeciesName() {
return speciesName;
}
public int getCandies() {
return candies;
}
/**
* Add a newly caught Pokemon to the player's inventory and
* increase the number of candies for this PokemonSpecies
*
* @param pokemon the newly caught Pokemon
*/
public void addNewPokemon(Pokemon pokemon) {
// TODO
caughtPokemon.add(pokemon);
this.candies++;
}
/**
* Helper function to load Pokemon from a save file into the player's inventory for this
* Pokemon Species
*
* @param pokemon the pokemon to add to this species
*/
public void loadPokemon(Pokemon pokemon) {
caughtPokemon.add(pokemon);
}
/**
* Find a Pokemon of the given combatPower in the player's inventory for this species.
*
* @param cp the combatPower of the Pokemon to find
* @throws PokedexException [Config.POKEMON_NOT_FOUND] if there is no Pokemon
with the
* given combatPower in the player's inventory.
3. * @return the first Pokemon with the provided combatPower
*/
public Pokemon findPokemon(int cp) throws PokedexException {
// TODO
for(int i=0;i 0){
return true;
}
return false;
}
/**
* Increment candies when a new pokemon is caught
*/
private void addNewPokemonCandies() {
this.candies += PokemonGO.NEW_POKEMON_CANDIES;
}
/**
* Increment candies when a pokemon is transferred to the professor
*/
private void addTransferCandies() {
this.candies += PokemonGO.TRANSFER_POKEMON_CANDIES;
}
/**
* Prepare a listing of all the Pokemon of this species that are currently in the
* player's inventory.
*
* @return a String of the form
* ...
*/
public String caughtPokemonToString() {
// TODO
return pokedexNumber+" Name: "+speciesName+" Candies"+candies;
}
/**
4. * Prepare a String representing this entire PokemonSpecies. This is used to
* save the PokemonSpecies (one part of the Pokedex) to a file to
* save the player's game.
*
* @return a String of the form
* [, , ...]
*/
public String toString() {
// TODO
return pokedexNumber+" Name: "+speciesName+" Candies"+candies;
}
}
--------------------------------------------------------------------------------------------------------------------
-------------------------------------
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.InputMismatchException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
/**
* The main class. Provides the main method which is responsible for game play.
* Also provides assistance for gameplay with functions to generate Pokemon, and
* save a player's progress.
*/
public class PokemonGO {
public static final int NEW_POKEMON_CANDIES = 3;
public static final int TRANSFER_POKEMON_CANDIES = 1;
/**
* The game begins here! The set of Pokemon that the player will encounter
* must be provided as the first and only command-line argument which will
* be loaded into the {@link PokemonDB}
*
* Players are prompted to enter their name which will be used to save their
* progress. Players are then prompted with a set of menu options which can
5. * be selected by entering a single character e.g. 's' for "search". Options
* include search for Pokemon, display encountered Pokemon, display caught Pokemon,
* transfer caught Pokemon, and quit.
*
* @param args the command line arguments. args[0] must be a file which is loaded
* by {@link PokemonDB}
*/
public static void main(String[] args) throws FileNotFoundException {
// Interpret command-line arguments and use them to load PokemonDB
if(args.length != 1) {
System.err.println("Invalid arguments");
System.exit(1);
}
PokemonDB db = new PokemonDB(args[0]);
// Start
System.out.println(Config.WELCOME);
// Prompt user to enter player name
System.out.println(Config.USER_PROMPT);
// Take name of Pokemon trainer
String playerName = Config.getNextLine();
// Provide a name for a txt file which will be used to save the player's progress
String playerFileName = playerName + ".txt";
Pokedex pokedex = new Pokedex();
try {
pokedex.loadFromFile(playerFileName);
} catch (FileNotFoundException e) {
// then the player has not saved any progress yet, start a new game!
}
PokemonTrainer pokemonTrainer = new PokemonTrainer(playerName, pokedex);
System.out.println("Hi " + playerName);
// main menu for the game. accept commands until the player enters 'q' to quit
String option = null;
6. while ((!"Q".equals(option)) && (!"q".equals(option))) {
// Ask user to choose
// [C] display caught Pokemon
// [D]isplay encountered Pokemon
// [S]earch for Pokemon
// [T]ransfer Pokemon to the Professor
// [Q]uit
System.out.println(Config.MENU_PROMPT);
option = Config.getNextLine();
switch(option) {
case "C":
case "c":
System.out.println(pokemonTrainer.getPokedex().caughtPokemonMenu());
break;
case "D":
case "d":
System.out.println(pokemonTrainer.getPokedex().seenPokemonMenu());
break;
case "S":
case "s":
Pokemon wildPokemon = encounterPokemon(db);
// Provide alternative guessing options
int[] pokedexNumbers = new int[Config.DIFFICULTY];
pokedexNumbers[0] = wildPokemon.getPokedexNumber();
for(int i = 1; i < pokedexNumbers.length; i++) {
pokedexNumbers[i] = db.generatePokedexNumber();
}
Arrays.sort(pokedexNumbers);
// Prompt user for input
System.out.println(String.format(
Config.ENCOUNTERED_POKEMON,
wildPokemon.getSpecies(),
wildPokemon.getCombatPower(),
Arrays.toString(pokedexNumbers)));
8. case "t":
// Select Pokemon species to transfer
System.out.println(Config.TRANSFER_PROMPT);
pokedex = pokemonTrainer.getPokedex();
String speciesName = Config.getNextLine();
if(speciesName.toLowerCase().equals("cancel")) {
break;
}
try {
db.lookupPokedexNumber(speciesName);
} catch (PokedexException e) {
System.out.println(e.toString());
break;
}
// Begin transfer of selected species
PokemonSpecies species = null;
try {
species = pokedex.findCaughtSpeciesData(speciesName);
} catch (PokedexException e) {
System.out.println(e.toString());
break;
}
String transferPokemonName = species.getSpeciesName();
// Select Pokemon of that species to transfer
System.out.println(String.format(
Config.TRANSFER_CP_PROMPT,
transferPokemonName,
species.caughtPokemonToString()));
int transferPokemonCp = -1;
while(transferPokemonCp == -1) {
try {
transferPokemonCp = Config.getNextInteger();
if(transferPokemonCp < 0) {
System.out.println(Config.INVALID_CP_INPUT);
transferPokemonCp = -1;
9. }
} catch (InputMismatchException e) {
System.out.println(Config.INVALID_CP_INPUT);
transferPokemonCp = -1;
Config.getNextLine();
} catch (RuntimeException e) {
System.out.println(Config.INVALID_CP_INPUT);
transferPokemonCp = -1;
}
}
if(transferPokemonCp == 0) {
break;
}
try {
// Call transfer function; should throw exceptions within transfer but are to be caught here
pokemonTrainer.transferPokemon(transferPokemonName, transferPokemonCp);
System.out.println(String.format(
Config.SUCCESSFUL_TRANSFER,
transferPokemonName,
transferPokemonCp));
}
catch (PokedexException pokedexException) {
System.out.println (pokedexException.toString());
}
break;
case "Q":
case "q":
break;
default:
System.out.println(Config.INVALID_RESPONSE);
}
}
// Save the game when the player quits
File outFile = new File(playerFileName);
saveGame(outFile, pokemonTrainer);
10. System.out.println(String.format(
Config.QUIT_MESSAGE,
playerName));
}
/**
* A wild has appeared! ~*~ battle music plays ~*~
*
* @param db the PokemonDB to generate a Pokemon from
* @return a Pokemon for the encounter
*/
public static Pokemon encounterPokemon(PokemonDB db) {
// random number to pick pokemon
int pokedexNumber = db.generatePokedexNumber();
String species = db.lookupSpeciesName(pokedexNumber);
// random number to decide CP
int cp = Config.CP_GENERATOR.nextInt(Config.MAX_CP-1)+1; // adjustments for origin 0/1
Pokemon wildPokemon = new Pokemon(pokedexNumber, species, cp);
return wildPokemon;
}
/**
* Save the game by writing the Pokedex into a file. The game can be loaded again
* by functions provided by the {@link Pokedex} class
*
* @param outFile the file handle to write the game progress to
* @param pokemonTrainer the player whose game we are saving
*/
public static void saveGame(File outFile, PokemonTrainer pokemonTrainer) throws
FileNotFoundException {
// TODO
outFile.println(pokemonTrainer);
}
}
11. --------------------------------------------------------------------------------------------------------------------
-----------------------------------------
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
/**
* This class provides functions for interacting with the set of Pokemon that the player
* will encounter during the course of the game.
*/
public class PokemonDB {
public static final int NUM_POKEMON = 151;
public static final int WILD_CANDIES = 3;
private static ArrayList pokemonSpeciesNames;
/**
* Create a PokemonDB by parsing the Pokemon file in @path@
*
* @param path the file path that contains the Pokemon used for this game
*/
public PokemonDB(String path) throws FileNotFoundException {
pokemonSpeciesNames = new ArrayList();
File infile = new File(path);
loadFile(infile);
}
/**
* Parse Pokemon file with format
*
* File lines must be ordered from PokedexNumber and start from 1
*
* @param infile the file with all the Pokemon that will be used for this game
*/
public void loadFile(File infile) throws FileNotFoundException {
Scanner sc = new Scanner(infile);
while(sc.hasNextLine()) {
12. String line = sc.nextLine();
String[] tokens = line.split("s+");
String name = tokens[1];
name = name.toLowerCase(); // to make sure all cases are acceptable
pokemonSpeciesNames.add(name);
}
sc.close();
}
/**
* Generate a Pokedex Number to (1) provide a Pokemon for an encounter and (2)
* provide a set of alternative options for the Pokedex Number guessing game to
* capture the encountered Pokemon
*
* @return the Pokedex Number generated
*/
public int generatePokedexNumber() {
int pokedexNumber =
Config.POKEDEX_NUMBER_GENERATOR.nextInt(PokemonDB.NUM_POKEMON-1)+1; //
adjustments for origin 0/1
return pokedexNumber;
}
/**
* Lookup a Pokemon species name using its Pokedex Number (the index+1 of the
* string in pokemonSpeciesNames which matches the parameter pokedexNumber)
*
* @param pokedexNumber find the Pokemon species name associated with this number
* @throws PokedexException thrown if the pokedexNumber is not in the range 1 to
* NUM_POKEMON (inclusive)
*/
public String lookupSpeciesName(int pokedexNumber) throws PokedexException {
// TODO
for(int i=0;i
Solution
import java.util.ArrayList;
import java.util.Iterator;
13. /**
* A PokemonSpecies entry in the Pokedex. Maintains the number of candies associated
* with the Pokemon species as well as the Trainer's inventory of Pokemon of this
* species.
*/
public class PokemonSpecies {
private int pokedexNumber;
private String speciesName;
private int candies;
/**
* Maintains the list of Pokemon of this species in the Trainer's inventory.
*/
private ArrayList caughtPokemon;
/**
* Constructor suitable for a newly encountered Pokemon species during the course of the
* game and for loading species data from a save file.
*
* @param pokedexNumber the Pokedex Number for the species
* @param speciesName the name of the species
* @param candies the number of candies the player has obtained by catching
* or transferring Pokemon of this species
*/
public PokemonSpecies(int pokedexNumber, String speciesName, int candies) {
this.pokedexNumber = pokedexNumber;
this.speciesName = speciesName;
this.candies = candies;
// construct caughtPokemon
caughtPokemon = new ArrayList();
}
/**
* Getter methods
*/
public Integer getPokedexNumber() {
return pokedexNumber;
}
14. public String getSpeciesName() {
return speciesName;
}
public int getCandies() {
return candies;
}
/**
* Add a newly caught Pokemon to the player's inventory and
* increase the number of candies for this PokemonSpecies
*
* @param pokemon the newly caught Pokemon
*/
public void addNewPokemon(Pokemon pokemon) {
// TODO
caughtPokemon.add(pokemon);
this.candies++;
}
/**
* Helper function to load Pokemon from a save file into the player's inventory for this
* Pokemon Species
*
* @param pokemon the pokemon to add to this species
*/
public void loadPokemon(Pokemon pokemon) {
caughtPokemon.add(pokemon);
}
/**
* Find a Pokemon of the given combatPower in the player's inventory for this species.
*
* @param cp the combatPower of the Pokemon to find
* @throws PokedexException [Config.POKEMON_NOT_FOUND] if there is no Pokemon
with the
* given combatPower in the player's inventory.
* @return the first Pokemon with the provided combatPower
*/
public Pokemon findPokemon(int cp) throws PokedexException {
15. // TODO
for(int i=0;i 0){
return true;
}
return false;
}
/**
* Increment candies when a new pokemon is caught
*/
private void addNewPokemonCandies() {
this.candies += PokemonGO.NEW_POKEMON_CANDIES;
}
/**
* Increment candies when a pokemon is transferred to the professor
*/
private void addTransferCandies() {
this.candies += PokemonGO.TRANSFER_POKEMON_CANDIES;
}
/**
* Prepare a listing of all the Pokemon of this species that are currently in the
* player's inventory.
*
* @return a String of the form
* ...
*/
public String caughtPokemonToString() {
// TODO
return pokedexNumber+" Name: "+speciesName+" Candies"+candies;
}
/**
* Prepare a String representing this entire PokemonSpecies. This is used to
* save the PokemonSpecies (one part of the Pokedex) to a file to
* save the player's game.
16. *
* @return a String of the form
* [, , ...]
*/
public String toString() {
// TODO
return pokedexNumber+" Name: "+speciesName+" Candies"+candies;
}
}
--------------------------------------------------------------------------------------------------------------------
-------------------------------------
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.InputMismatchException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
/**
* The main class. Provides the main method which is responsible for game play.
* Also provides assistance for gameplay with functions to generate Pokemon, and
* save a player's progress.
*/
public class PokemonGO {
public static final int NEW_POKEMON_CANDIES = 3;
public static final int TRANSFER_POKEMON_CANDIES = 1;
/**
* The game begins here! The set of Pokemon that the player will encounter
* must be provided as the first and only command-line argument which will
* be loaded into the {@link PokemonDB}
*
* Players are prompted to enter their name which will be used to save their
* progress. Players are then prompted with a set of menu options which can
* be selected by entering a single character e.g. 's' for "search". Options
* include search for Pokemon, display encountered Pokemon, display caught Pokemon,
* transfer caught Pokemon, and quit.
17. *
* @param args the command line arguments. args[0] must be a file which is loaded
* by {@link PokemonDB}
*/
public static void main(String[] args) throws FileNotFoundException {
// Interpret command-line arguments and use them to load PokemonDB
if(args.length != 1) {
System.err.println("Invalid arguments");
System.exit(1);
}
PokemonDB db = new PokemonDB(args[0]);
// Start
System.out.println(Config.WELCOME);
// Prompt user to enter player name
System.out.println(Config.USER_PROMPT);
// Take name of Pokemon trainer
String playerName = Config.getNextLine();
// Provide a name for a txt file which will be used to save the player's progress
String playerFileName = playerName + ".txt";
Pokedex pokedex = new Pokedex();
try {
pokedex.loadFromFile(playerFileName);
} catch (FileNotFoundException e) {
// then the player has not saved any progress yet, start a new game!
}
PokemonTrainer pokemonTrainer = new PokemonTrainer(playerName, pokedex);
System.out.println("Hi " + playerName);
// main menu for the game. accept commands until the player enters 'q' to quit
String option = null;
while ((!"Q".equals(option)) && (!"q".equals(option))) {
// Ask user to choose
// [C] display caught Pokemon
18. // [D]isplay encountered Pokemon
// [S]earch for Pokemon
// [T]ransfer Pokemon to the Professor
// [Q]uit
System.out.println(Config.MENU_PROMPT);
option = Config.getNextLine();
switch(option) {
case "C":
case "c":
System.out.println(pokemonTrainer.getPokedex().caughtPokemonMenu());
break;
case "D":
case "d":
System.out.println(pokemonTrainer.getPokedex().seenPokemonMenu());
break;
case "S":
case "s":
Pokemon wildPokemon = encounterPokemon(db);
// Provide alternative guessing options
int[] pokedexNumbers = new int[Config.DIFFICULTY];
pokedexNumbers[0] = wildPokemon.getPokedexNumber();
for(int i = 1; i < pokedexNumbers.length; i++) {
pokedexNumbers[i] = db.generatePokedexNumber();
}
Arrays.sort(pokedexNumbers);
// Prompt user for input
System.out.println(String.format(
Config.ENCOUNTERED_POKEMON,
wildPokemon.getSpecies(),
wildPokemon.getCombatPower(),
Arrays.toString(pokedexNumbers)));
int guessedId = 0;
while(guessedId < 1 || guessedId > 151) {
// then prompt is invalid
19. try {
guessedId = Config.getNextInteger();
if(guessedId < 1 || guessedId > 151) {
throw new RuntimeException(Config.INVALID_INT_INPUT);
}
} catch (InputMismatchException e) {
System.out.println(Config.INVALID_INT_INPUT);
Config.getNextLine();
} catch (RuntimeException e) {
System.out.println(Config.INVALID_INT_INPUT);
}
}
if (guessedId == wildPokemon.getPokedexNumber()) {
// guessed correctly, pokemon is captured
pokemonTrainer.capturePokemon(wildPokemon);
System.out.println(String.format(
Config.SUCCESSFUL_CAPTURE,
playerName,
wildPokemon.getSpecies(),
wildPokemon.getCombatPower()));
}
else {
// guessed incorrectly, pokemon escapes
pokemonTrainer.seePokemon(wildPokemon);
System.out.println(String.format(
Config.FAILED_CAPTURE,
playerName,
wildPokemon.getSpecies()));
}
break;
case "T":
case "t":
// Select Pokemon species to transfer
System.out.println(Config.TRANSFER_PROMPT);
20. pokedex = pokemonTrainer.getPokedex();
String speciesName = Config.getNextLine();
if(speciesName.toLowerCase().equals("cancel")) {
break;
}
try {
db.lookupPokedexNumber(speciesName);
} catch (PokedexException e) {
System.out.println(e.toString());
break;
}
// Begin transfer of selected species
PokemonSpecies species = null;
try {
species = pokedex.findCaughtSpeciesData(speciesName);
} catch (PokedexException e) {
System.out.println(e.toString());
break;
}
String transferPokemonName = species.getSpeciesName();
// Select Pokemon of that species to transfer
System.out.println(String.format(
Config.TRANSFER_CP_PROMPT,
transferPokemonName,
species.caughtPokemonToString()));
int transferPokemonCp = -1;
while(transferPokemonCp == -1) {
try {
transferPokemonCp = Config.getNextInteger();
if(transferPokemonCp < 0) {
System.out.println(Config.INVALID_CP_INPUT);
transferPokemonCp = -1;
}
} catch (InputMismatchException e) {
System.out.println(Config.INVALID_CP_INPUT);
21. transferPokemonCp = -1;
Config.getNextLine();
} catch (RuntimeException e) {
System.out.println(Config.INVALID_CP_INPUT);
transferPokemonCp = -1;
}
}
if(transferPokemonCp == 0) {
break;
}
try {
// Call transfer function; should throw exceptions within transfer but are to be caught here
pokemonTrainer.transferPokemon(transferPokemonName, transferPokemonCp);
System.out.println(String.format(
Config.SUCCESSFUL_TRANSFER,
transferPokemonName,
transferPokemonCp));
}
catch (PokedexException pokedexException) {
System.out.println (pokedexException.toString());
}
break;
case "Q":
case "q":
break;
default:
System.out.println(Config.INVALID_RESPONSE);
}
}
// Save the game when the player quits
File outFile = new File(playerFileName);
saveGame(outFile, pokemonTrainer);
System.out.println(String.format(
Config.QUIT_MESSAGE,
playerName));
22. }
/**
* A wild has appeared! ~*~ battle music plays ~*~
*
* @param db the PokemonDB to generate a Pokemon from
* @return a Pokemon for the encounter
*/
public static Pokemon encounterPokemon(PokemonDB db) {
// random number to pick pokemon
int pokedexNumber = db.generatePokedexNumber();
String species = db.lookupSpeciesName(pokedexNumber);
// random number to decide CP
int cp = Config.CP_GENERATOR.nextInt(Config.MAX_CP-1)+1; // adjustments for origin 0/1
Pokemon wildPokemon = new Pokemon(pokedexNumber, species, cp);
return wildPokemon;
}
/**
* Save the game by writing the Pokedex into a file. The game can be loaded again
* by functions provided by the {@link Pokedex} class
*
* @param outFile the file handle to write the game progress to
* @param pokemonTrainer the player whose game we are saving
*/
public static void saveGame(File outFile, PokemonTrainer pokemonTrainer) throws
FileNotFoundException {
// TODO
outFile.println(pokemonTrainer);
}
}
--------------------------------------------------------------------------------------------------------------------
-----------------------------------------
import java.io.File;
23. import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
/**
* This class provides functions for interacting with the set of Pokemon that the player
* will encounter during the course of the game.
*/
public class PokemonDB {
public static final int NUM_POKEMON = 151;
public static final int WILD_CANDIES = 3;
private static ArrayList pokemonSpeciesNames;
/**
* Create a PokemonDB by parsing the Pokemon file in @path@
*
* @param path the file path that contains the Pokemon used for this game
*/
public PokemonDB(String path) throws FileNotFoundException {
pokemonSpeciesNames = new ArrayList();
File infile = new File(path);
loadFile(infile);
}
/**
* Parse Pokemon file with format
*
* File lines must be ordered from PokedexNumber and start from 1
*
* @param infile the file with all the Pokemon that will be used for this game
*/
public void loadFile(File infile) throws FileNotFoundException {
Scanner sc = new Scanner(infile);
while(sc.hasNextLine()) {
String line = sc.nextLine();
String[] tokens = line.split("s+");
String name = tokens[1];
24. name = name.toLowerCase(); // to make sure all cases are acceptable
pokemonSpeciesNames.add(name);
}
sc.close();
}
/**
* Generate a Pokedex Number to (1) provide a Pokemon for an encounter and (2)
* provide a set of alternative options for the Pokedex Number guessing game to
* capture the encountered Pokemon
*
* @return the Pokedex Number generated
*/
public int generatePokedexNumber() {
int pokedexNumber =
Config.POKEDEX_NUMBER_GENERATOR.nextInt(PokemonDB.NUM_POKEMON-1)+1; //
adjustments for origin 0/1
return pokedexNumber;
}
/**
* Lookup a Pokemon species name using its Pokedex Number (the index+1 of the
* string in pokemonSpeciesNames which matches the parameter pokedexNumber)
*
* @param pokedexNumber find the Pokemon species name associated with this number
* @throws PokedexException thrown if the pokedexNumber is not in the range 1 to
* NUM_POKEMON (inclusive)
*/
public String lookupSpeciesName(int pokedexNumber) throws PokedexException {
// TODO
for(int i=0;i