//File: Simulator.java //Name: Trai Pham //Date: 03/02/2020 //Course: CSE 8B /** simulate the actions that can be done with our code, which is catching, battling, and storage modification **/ import java.util.Scanner; import java.util.List; import java.util.ArrayList; import java.io.File; import java.io.FileNotFoundException; public class Simulator { // Used if user selects bulbasaur as starter private static final int BULBASAUR = 2; // Used to parse the level of a Pokemon private static final int LEVEL_INDEX = 2; // Number of required arguments private static final int REQUIRED_ARGS = 2; // Used to split pokemon file / user input private static final String SEPARATOR = ","; private static final String PROMPT_SEP = " "; // Used to index through user arguments private static final int BOX_INDEX = 1; private static final int POS_INDEX = 2; private static final int TO_BOX_INDEX = 3; private static final int TO_POS_INDEX = 4; // Different choices user can make private static final String OPTION_0 = "0"; private static final String OPTION_1 = "1"; private static final String OPTION_2 = "2"; private static final String USER_PROMPT = "> "; private static final String USAGE_PROMPT = "\nUsage: java Simulator [0|1|2] [filename]\n" + "0 - Charmander, 1 - Squirtle, 2 - Bulbasaur\n\n"; private static final String MAIN_PROMPT = "\nWhat would you like to do?\n" + "[0] - Go into the wild!\n" + "[1] - View your PC!\n\n"; private static final String PC_PROMPT = "\nCurrently viewing someone's PC\n" + "[0] b - View box b (specify a number)\n" + "[1] b1 p1 b2 p2 - Move Pokemon at box b1, pos p1 to box b2, p2\n" + "[2] b p - Release Pokemon at box b, pos p\n\n"; private static final String WILD_PROMPT = "\nYou have encountered a level %d %s!\n" + "[0] - Catch\n" + "[1] - Run\n\n"; private static final String CAUGHT_PROMPT = "\nSuccessfully caught %s!\n"; private static final String RUN_PROMPT = "\nPhew... ran away!\n"; private static final String BATTLE_INTRO = "\nBattling against your rival!\n" + "Your rival sent out %s.\n" + "Go! %s!\n" + "--------------------------------------\n"; private static final String BATTLE_MAIN = "Your rival has dealt %d damage!\n" + "You dealt %d damage!\n\n"; private static final String BATTLE_WIN = "You won!\n"; private static final String BATTLE_LOSE = "You lost! Smell you later!\n"; private static final String EMPTY_WILD = "No more pokemon in the wild!\n"; private static final String SUCCESSFUL_MOVE = "Successful move!\n"; private static final String SUCCESSFUL_RELEASE = "Successful release!\n"; private static final String UNRECOGNIZED_PROMPT = "Unrecognized command. Please try again.\n\n"; private static final String FILE_NOT_FOUND = "File: %s could not be found!\n\n"; // One storage, one scanner (reinitialize scanner as necessary) private static PokemonStorageSystem storage; private static Scanner scanner; private static int magic5 = 5; private static int magic3 = 3; /** @param Pokemon type that represents your starter, Pokemon type represent rival's pokemon @return nothing This initiates a battle between the user and their rival. **/ private static void handleBattle(Pokemon starter, Pokemon rival) { // Initial battle text System.out.printf(BATTLE_INTRO, rival.getName(), starter.getName()); /** TODO Implement rival battle logic */ int starterDamage = starter.attack(); int rivalDamage = rival.attack(); System.out.printf(BATTLE_MAIN, rivalDamage, starterDamage); //check to see whose damages more, this is based on randomized numbers if(starterDamage > rivalDamage){ System.out.printf(BATTLE_WIN); } else{ System.out.printf(BATTLE_LOSE); } } /** @param Pokemon type that represents a wild pokemon @return nothing This allows user to encounter a wild pokemon. The user has two options to catch the pokemon or run away from it. **/ private static void handleWild(Pokemon wild) { // Use the wild pokemon that was passed in System.out.printf(WILD_PROMPT, wild.getLevel(), wild.getName()); // Parse user's next decision String line; boolean invalid = true; try { // Keep prompting user until a valid action has been made while(invalid) { System.out.print(USER_PROMPT); line = scanner.nextLine().toUpperCase().trim(); //going through the options of encountering pokemons in the wild //user can choose to catch or run away from the pokemon switch(line) { case OPTION_0: invalid = false; storage.deposit(wild); System.out.printf(CAUGHT_PROMPT, wild.getName()); break; case OPTION_1: invalid = false; System.out.printf(RUN_PROMPT); break; default: System.out.printf(UNRECOGNIZED_PROMPT); break; } } } //catches the exception when there are no more space in storage when catching //pokemons catch(NoStorageSpaceException ex){ System.out.println("NoStorageSpaceException"); } /** TODO Add catch statement here. No `Exception e` */ } /** @param nothing @return nothing This allows the user to access the storage system. The user can choose to see the data of a box, move pokemon around to specific boxes and positions, or release a pokemon from their storage **/ private static void handlePC() { System.out.printf(PC_PROMPT); String line; String[] splitLine; // Keep looping until we have a valid input boolean invalid = true; try { while(invalid) { System.out.print(USER_PROMPT); line = scanner.nextLine().trim(); splitLine = line.split(PROMPT_SEP); // Check to ensure number of required args is correct // If so, then parse accordingly // Assumes that inputs are numbers; Not handling invalid cases switch(splitLine[0].toUpperCase()) { //Goes through the option when viewing the PC case OPTION_0: { if(splitLine.length != REQUIRED_ARGS) { System.out.printf(UNRECOGNIZED_PROMPT); break; } //this will allow you to get the box number based on the second argument that //the user inputs invalid = false; int num = Integer.parseInt(splitLine[1]); String output = storage.getBox(num); System.out.printf(output); /** TODO System.out.printf output of getBox */ break; } case OPTION_1: { //5 args are needed in order for this option to run if(splitLine.length != magic5) { System.out.printf(UNRECOGNIZED_PROMPT); break; } //The user inputs after the first arg will be the position of where the Pokemon //will be moving to invalid = false; int a = Integer.parseInt(splitLine[1]); int b = Integer.parseInt(splitLine[2]); int c = Integer.parseInt(splitLine[3]); int d = Integer.parseInt(splitLine[4]); storage.move(a, b, c, d); /** TODO Parse arguments and pass into move */ System.out.printf(SUCCESSFUL_MOVE); break; } case OPTION_2: { //The args have to be that of 3 for this option to run if(splitLine.length != magic3) { System.out.printf(UNRECOGNIZED_PROMPT); break; } //this allows user to prompt the specific box and position to release a pokemon invalid = false; int a = Integer.parseInt(splitLine[0]); int b = Integer.parseInt(splitLine[1]); storage.release(a, b); /** TODO Parse arguments and pass into release */ System.out.printf(SUCCESSFUL_RELEASE); break; } default: System.out.printf(UNRECOGNIZED_PROMPT); break; } } } //catches the Exception if the user's inputs are out of bound catch(OutOfBoundsException ex){ System.out.println("OutOfBoundsException"); } /** TODO Add catch statement */ } /** @param String the name of the file @return List a list that contains pokemons This method reads a file to use as wild encounters. **/ public static List parsePokemonFile(String filename){ ArrayList pokemonList = new ArrayList(); try{ //reads the file with scanner File inputFile = new File(filename); Scanner scnr = new Scanner(inputFile); String line; String[] splitLine; //goes through the file and set the individual content within file to that of // a pokemon type while(scnr.hasNextLine()){ line = scnr.nextLine().trim(); splitLine = line.split(SEPARATOR); //creating a new pokemon from file Pokemon pokemon = new Pokemon(splitLine[0], splitLine[1], Integer.parseInt(splitLine[2])); //adding pokemon to list pokemonList.add(pokemon); } return pokemonList; } //catch blocks catch(MinLevelException minEx){ return null; } catch(MaxLevelException maxEx){ return null; } catch(FileNotFoundException ex){ System.out.printf(FILE_NOT_FOUND, filename); } return pokemonList; } /** @param String array @return nothing main method, that starts the simulation. **/ public static void main(String[] args) { if(args.length == REQUIRED_ARGS) { System.out.printf(USAGE_PROMPT); return; } storage = new PokemonStorageSystem(); /** TODO Initialize global pokemon storage */ // Choose your starter pokemon int choice = Integer.parseInt(args[0]); Pokemon starter = null; Pokemon rival = null; //based on the user's inputs the starter pokemon will be set try{ if(choice == Integer.parseInt(OPTION_0)){ starter = new Charmander(); rival = new Squirtle(); } else if(choice == Integer.parseInt(OPTION_1)){ starter = new Squirtle(); rival = new Bulbasaur(); } else if(choice == Integer.parseInt(OPTION_2)){ starter = new Bulbasaur(); rival = new Charmander(); } else{ return; } } //catches the exceptions that may occur Min and Max lvl exceptions catch(MinLevelException minEx){ System.out.println("MinLevelException"); } catch(MaxLevelException maxEx){ System.out.println("MaxLevelException"); } /** TODO Initialize the starter and rival variables accordingly */ storage.setPartyMember(starter); /** TODO Start battle against the opposing rival pokemon */ handleBattle(starter, rival); // Retrieve the filename of all the Pokemon that can be attainable List allPokemon = parsePokemonFile("sample_wild_pokemon_file"); /** TODO */ if(allPokemon == null) { return; } // Used to index through allPokemon int currIndex = 0; // Interactive mode System.out.printf(MAIN_PROMPT); System.out.print(USER_PROMPT); scanner = new Scanner(System.in); String line; // Keep looping until ctrl+D while(scanner.hasNextLine()) { // Decide whether to go into the wild or view PC line = scanner.nextLine().toUpperCase().trim(); switch(line) { case OPTION_0: if(currIndex == allPokemon.size()) { System.out.printf(EMPTY_WILD); } else { handleWild(allPokemon.get(currIndex)); currIndex++; /** TODO Call on handleWild */ } break; case OPTION_1: handlePC(); /** TODO Call on handlePC */ break; default: System.out.printf(UNRECOGNIZED_PROMPT); break; } System.out.printf(MAIN_PROMPT); System.out.print(USER_PROMPT); } } }