2017-11-24 16:02:57 +01:00
import java.util.Scanner ;
import java.util.ArrayList ;
import java.io.File ;
import java.io.FileNotFoundException ;
import java.io.PrintWriter ;
import java.lang.NumberFormatException ;
import java.text.NumberFormat ;
import java.util.Locale ;
import java.text.ParseException ;
/** Simulates the game Pokémon. */
public class Game {
2017-11-25 22:28:17 +01:00
public static Scanner in = new Scanner ( System . in ) ;
2017-11-24 16:02:57 +01:00
public static void main ( String [ ] args ) {
ArrayList < Pokemon > pokemon = readPokemon ( ) ;
int initialPokemon = pokemon . size ( ) ;
2017-11-25 22:28:17 +01:00
System . out . println ( " What is your name? " ) ;
String name = in . nextLine ( ) ;
2017-11-27 11:11:31 +01:00
Trainer player = new Trainer ( name , randomTeam ( ) , createInventory ( ) ) ;
2017-11-25 22:28:17 +01:00
2017-11-24 16:02:57 +01:00
boolean done = false ;
Pokemon opponentPokemon = null ;
Pokemon trainersPokemon = null ;
2017-11-26 14:27:38 +01:00
Pokeball currentPokeball = null ;
Potion currentPotion = null ;
2017-11-24 16:02:57 +01:00
boolean fleeSuccess = false ;
Pokemon pokemonToHeal = null ;
opponentPokemon = randomPokemon ( pokemon ) ;
System . out . printf ( " A wild %s appeared.%n " , opponentPokemon . getName ( ) ) ;
while ( ! done ) {
if ( opponentPokemon = = null ) {
System . out . printf ( " You have brutally murdered %d pokemon.%n "
+ " The only ones left are the ones in your posession.%n "
+ " There really is nothing more to do here.%n " , initialPokemon ) ;
return ;
}
2017-11-27 20:48:24 +01:00
if ( player . getConsciousPokemon ( ) . size ( ) < 1 ) {
2017-11-24 16:02:57 +01:00
System . out . println ( " All your pokemon have fainted. Your journey ends here. " ) ;
return ;
}
while ( trainersPokemon = = null | | ! trainersPokemon . isConscious ( ) ) {
2017-11-27 20:48:24 +01:00
player . availablePokemon ( true ) ;
trainersPokemon = player . choosePokemon ( true ) ;
2017-11-24 16:02:57 +01:00
}
System . out . printf ( " Opponent: %s%nWhat will you do?%n " , opponentPokemon ) ;
System . out . printf ( " b: battle "
+ " %nh: heal or revive "
+ " %nt: throw pokeball "
+ " %nc: change pokemon "
+ " %nf: flee "
+ " %nv: view my pokemon "
+ " %ns: save "
+ " %nl: load "
+ " %nq: quit%n> " ) ;
char command = in . next ( ) . toLowerCase ( ) . charAt ( 0 ) ;
switch ( command ) {
case 'b' :
if ( opponentPokemon . isConscious ( ) & & trainersPokemon . isConscious ( ) ) {
trainersPokemon . attack ( opponentPokemon ) ;
if ( opponentPokemon . isConscious ( ) ) {
opponentPokemon . attack ( trainersPokemon ) ;
if ( ! trainersPokemon . isConscious ( ) ) {
System . out . println ( " Your pokemon fainted. " ) ;
}
} else {
pokemonFainted ( pokemon , opponentPokemon ) ;
System . out . println ( " The opponent pokemon fainted. " ) ;
opponentPokemon = randomPokemon ( pokemon ) ;
}
}
break ;
case 'h' :
2017-11-26 14:27:38 +01:00
if ( player . getInventory ( ) . getPotions ( ) . size ( ) > 0 ) {
2017-11-27 20:48:24 +01:00
player . getInventory ( ) . availablePotions ( ) ;
currentPotion = player . getInventory ( ) . chosenPotion ( ) ;
2017-11-26 14:27:38 +01:00
if ( currentPotion = = null ) {
2017-11-27 11:11:31 +01:00
in . nextLine ( ) ;
2017-11-26 14:27:38 +01:00
System . out . println ( " Invalid potion. " ) ;
2017-11-24 16:02:57 +01:00
} else {
2017-11-26 14:27:38 +01:00
if ( currentPotion . needsAlive ( ) ) {
2017-11-27 20:48:24 +01:00
player . availablePokemon ( true ) ;
pokemonToHeal = player . choosePokemon ( true ) ;
2017-11-24 16:02:57 +01:00
} else {
2017-11-27 20:48:24 +01:00
player . availablePokemon ( false ) ;
pokemonToHeal = player . choosePokemon ( false ) ;
2017-11-24 16:02:57 +01:00
}
if ( pokemonToHeal = = null ) {
System . out . println ( " That is not a valid pokemon " ) ;
} else {
2017-11-26 14:27:38 +01:00
if ( currentPotion . use ( pokemonToHeal ) ) {
2017-11-24 16:02:57 +01:00
opponentPokemon . attack ( trainersPokemon ) ;
}
}
}
} else {
System . out . println ( " You have used all your healing items. " ) ;
}
break ;
case 't' :
2017-11-26 14:27:38 +01:00
if ( player . getInventory ( ) . getPokeballs ( ) . size ( ) > 0 ) {
2017-11-27 20:48:24 +01:00
player . getInventory ( ) . availablePokeballs ( ) ;
currentPokeball = player . getInventory ( ) . chosenPokeball ( ) ;
2017-11-26 14:27:38 +01:00
if ( currentPokeball = = null ) {
2017-11-27 11:11:31 +01:00
in . nextLine ( ) ;
2017-11-24 16:02:57 +01:00
System . out . println ( " Invalid pokeball. " ) ;
} else {
2017-11-26 14:27:38 +01:00
if ( currentPokeball . getType ( ) = = Pokeball . Pokeballs . MASTERBALL ) {
2017-11-27 11:11:31 +01:00
currentPokeball . use ( opponentPokemon , pokemon , player ) ;
2017-11-24 16:02:57 +01:00
opponentPokemon = randomPokemon ( pokemon ) ;
System . out . printf ( " A wild %s appeared.%n " , opponentPokemon . getName ( ) ) ;
} else {
2017-11-27 11:11:31 +01:00
boolean captured = currentPokeball . use ( opponentPokemon , pokemon , player ) ;
2017-11-24 16:02:57 +01:00
if ( captured ) {
opponentPokemon = randomPokemon ( pokemon ) ;
System . out . printf ( " A wild %s appeared.%n " , opponentPokemon . getName ( ) ) ;
} else {
opponentPokemon . attack ( trainersPokemon ) ;
}
}
}
} else {
System . out . println ( " You have used all your pokeballs. " ) ;
}
break ;
case 'c' :
2017-11-27 20:48:24 +01:00
player . availablePokemon ( true ) ;
trainersPokemon = player . choosePokemon ( true ) ;
2017-11-24 16:02:57 +01:00
while ( trainersPokemon = = null ) {
2017-11-27 20:48:24 +01:00
player . availablePokemon ( true ) ;
trainersPokemon = player . choosePokemon ( true ) ;
2017-11-24 16:02:57 +01:00
}
opponentPokemon . attack ( trainersPokemon ) ;
break ;
case 's' :
savePokemon ( pokemon , " pokemon.save " ) ;
2017-11-27 11:11:31 +01:00
savePokemon ( player . getPokemon ( ) , " user.save " ) ;
2017-11-26 14:27:38 +01:00
saveInventory ( player . getInventory ( ) , " inventory.save " ) ;
2017-11-24 16:02:57 +01:00
break ;
case 'l' :
ArrayList < Pokemon > loadedPokemon = loadPokemon ( " pokemon.save " ) ;
ArrayList < Pokemon > loadedUsersPokemon = loadPokemon ( " user.save " ) ;
2017-11-26 14:27:38 +01:00
Inventory loadedInventory = loadInventory ( " inventory.save " ) ;
if ( loadedPokemon = = null | | loadedUsersPokemon = = null | | loadedInventory = = null ) {
2017-11-24 16:02:57 +01:00
System . out . println ( " One or more savefiles seem corrupt. Please delete or fix the affected file(s). " ) ;
} else {
pokemon = loadedPokemon ;
2017-11-25 22:28:17 +01:00
player . setPokemon ( loadedUsersPokemon ) ;
2017-11-26 14:27:38 +01:00
player . setInventory ( loadedInventory ) ;
2017-11-27 11:11:31 +01:00
if ( pokemon . size ( ) > 0 & & player . getPokemon ( ) . size ( ) > 0 ) {
2017-11-24 16:02:57 +01:00
do {
2017-11-27 20:48:24 +01:00
player . availablePokemon ( true ) ;
trainersPokemon = player . choosePokemon ( true ) ;
2017-11-24 16:02:57 +01:00
} while ( trainersPokemon = = null | | ! trainersPokemon . isConscious ( ) ) ;
opponentPokemon = randomPokemon ( pokemon ) ;
}
}
break ;
case 'f' :
fleeSuccess = trainersPokemon . flee ( opponentPokemon ) ;
if ( fleeSuccess ) {
System . out . println ( " You fled the battle. " ) ;
opponentPokemon = randomPokemon ( pokemon ) ;
System . out . printf ( " A wild %s appeared.%n " , opponentPokemon . getName ( ) ) ;
} else {
System . out . printf ( " Failed to flee from %s.%n " , opponentPokemon . getName ( ) ) ;
opponentPokemon . attack ( trainersPokemon ) ;
}
break ;
case 'v' :
2017-11-27 20:48:24 +01:00
player . printPokemon ( ) ;
2017-11-24 16:02:57 +01:00
break ;
case 'q' :
done = true ;
break ;
default :
System . out . println ( " [Invalid command] " ) ;
}
}
}
/ * *
2017-11-27 20:48:24 +01:00
* Prevents wild fainted pokemon from ever being encountered again .
2017-11-24 16:02:57 +01:00
* @param pokemonList List of pokemon to search .
* @param target The pokemon to remove .
* /
public static void pokemonFainted ( ArrayList < Pokemon > pokemonList , Pokemon target ) {
for ( int i = 0 ; i < pokemonList . size ( ) ; i + + ) {
if ( pokemonList . get ( i ) . equals ( target ) ) {
pokemonList . remove ( i ) ;
}
}
}
/ * *
* Gives a trainer necessary starting items .
* @return A list of items .
* /
2017-11-26 14:27:38 +01:00
public static Inventory createInventory ( ) {
Inventory inventory = new Inventory ( ) ;
inventory . addPokeball ( " Poke Ball " , " A device for catching wild Pokémon. It's thrown like a ball at a Pokémon, comfortably encapsulating its target. " , 15 ) ;
inventory . addPokeball ( " Great ball " , " A good, high-performance Poké Ball that provides a higher Pokémon catch rate than a standard Poké Ball. " , 10 ) ;
inventory . addPokeball ( " Ultra ball " , " An ultra-high-performance Poké Ball that provides a higher success rate for catching Pokémon than a Great Ball. " , 5 ) ;
inventory . addPokeball ( " Master ball " , " The best Poké Ball with the ultimate level of performance. With it, you will catch any wild Pokémon without fail. " , 1 ) ;
inventory . addPotion ( " Potion " , " Heals a pokemon for 20 HP. " , 20 , true ) ;
inventory . addPotion ( " Super Potion " , " Heals a pokemon for 50 HP. " , 10 , true ) ;
inventory . addPotion ( " Hyper Potion " , " Heals a pokemon for 200 HP. " , 5 , true ) ;
inventory . addPotion ( " Max Potion " , " Fully heals a pokemon. " , 1 , true ) ;
inventory . addPotion ( " Revive " , " Revives a fainted pokemon. " , 2 , false ) ;
return inventory ;
2017-11-24 16:02:57 +01:00
}
/ * *
* Chooses a random pokemon from a list .
* @param pokemon The list to choose from .
* @return A Pokemon object , or null if the list is empty .
* /
public static Pokemon randomPokemon ( ArrayList < Pokemon > pokemon ) {
if ( pokemon . size ( ) > 0 ) {
return pokemon . get ( ( int ) ( Math . random ( ) * pokemon . size ( ) ) ) ;
} else {
return null ;
}
}
/ * *
* Reads pokemon from Pokemon . txt to an ArrayList .
* @return An ArrayList of pokemon objects .
* /
public static ArrayList < Pokemon > readPokemon ( ) {
ArrayList < Pokemon > pokemon = new ArrayList < Pokemon > ( ) ;
2017-11-25 22:28:17 +01:00
try ( Scanner file = new Scanner ( new File ( " Pokemon.txt " ) ) ) {
while ( file . hasNextLine ( ) ) {
pokemon . add ( new Pokemon ( file . nextLine ( ) ) ) ;
2017-11-24 16:02:57 +01:00
}
} catch ( FileNotFoundException e ) {
System . out . println ( " You seem to be missing one of the necessary files to run this program. " ) ;
}
2017-11-28 13:58:53 +01:00
/** If the file is compiled as jar, this will prevent an empty list. */
if ( pokemon . size ( ) < 1 ) {
try ( Scanner file = new Scanner ( Game . class . getResourceAsStream ( " /Pokemon.txt " ) ) ) {
while ( file . hasNextLine ( ) ) {
pokemon . add ( new Pokemon ( file . nextLine ( ) ) ) ;
}
}
}
2017-11-24 16:02:57 +01:00
return pokemon ;
}
/ * *
* Picks a random pokemon from a list .
* @param pokemon A list of pokemon objects .
* @return A pokemon object .
* /
public static Pokemon pick ( ArrayList < Pokemon > pokemon ) {
int index = ( int ) ( Math . random ( ) * ( pokemon . size ( ) ) ) ;
Pokemon randomPokemon = pokemon . get ( index ) ;
pokemon . remove ( index ) ;
return randomPokemon ;
}
/ * *
* Saves all pokemon in a list with stats to a text file .
* @param pokemonList List of Pokemon objects to save .
* @param savefile The file to write to .
* /
public static void savePokemon ( ArrayList < Pokemon > pokemonList , String savefile ) {
try ( PrintWriter file = new PrintWriter ( savefile ) ) {
for ( Pokemon pokemon : pokemonList ) {
file . println ( pokemon . saveString ( ) ) ;
}
System . out . println ( " Successfully saved pokemon. " ) ;
} catch ( FileNotFoundException e ) {
System . out . println ( " The savefile could not be written. " ) ;
}
}
/ * *
* Saves all items in a list to a text file .
* @param itemList List of Item objects to save .
* @param savefile The file to write to .
* /
2017-11-26 14:27:38 +01:00
public static void saveInventory ( Inventory inventory , String savefile ) {
2017-11-24 16:02:57 +01:00
try ( PrintWriter file = new PrintWriter ( savefile ) ) {
2017-11-26 14:27:38 +01:00
for ( Pokeball pokeball : inventory . getPokeballs ( ) ) {
file . println ( pokeball . saveString ( ) ) ;
}
file . println ( " :NEXTLIST: " ) ;
for ( Potion potion : inventory . getPotions ( ) ) {
file . println ( potion . saveString ( ) ) ;
2017-11-24 16:02:57 +01:00
}
2017-11-26 14:27:38 +01:00
System . out . println ( " Successfully saved inventory. " ) ;
2017-11-24 16:02:57 +01:00
} catch ( FileNotFoundException e ) {
System . out . println ( " The savefile could not be written. " ) ;
}
}
/ * *
* Loads pokemon from a text file .
* @param savefile The file to write to .
* @return A list of pokemon or null on failiure .
* /
public static ArrayList < Pokemon > loadPokemon ( String savefile ) {
ArrayList < Pokemon > pokemon = new ArrayList < Pokemon > ( ) ;
2017-11-25 22:28:17 +01:00
try ( Scanner file = new Scanner ( new File ( savefile ) ) ) {
2017-11-24 16:02:57 +01:00
NumberFormat format = NumberFormat . getInstance ( Locale . ENGLISH ) ;
2017-11-25 22:28:17 +01:00
while ( file . hasNextLine ( ) ) {
String [ ] data = file . nextLine ( ) . split ( " ; " ) ;
2017-11-24 16:02:57 +01:00
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 ] ) ) ) ;
} catch ( NumberFormatException e ) {
System . out . println ( " Malformed number " + e ) ;
} catch ( ParseException e ) {
System . out . println ( " Malformed number " + e ) ;
} catch ( ArrayIndexOutOfBoundsException e ) {
System . out . println ( " Invalid savefile: " + savefile ) ;
return null ;
}
}
System . out . println ( " Successfully loaded pokemon. " ) ;
} catch ( FileNotFoundException e ) {
System . out . println ( " You don't have a valid savefile. " ) ;
}
return pokemon ;
}
/ * *
* Loads items from a text file .
* @param savefile The file to write to .
* @return A list of items or null on failiure .
* /
2017-11-26 14:27:38 +01:00
public static Inventory loadInventory ( String savefile ) {
ArrayList < Pokeball > pokeballs = new ArrayList < Pokeball > ( ) ;
ArrayList < Potion > potions = new ArrayList < Potion > ( ) ;
2017-11-25 22:28:17 +01:00
try ( Scanner file = new Scanner ( new File ( savefile ) ) ) {
2017-11-26 14:27:38 +01:00
String next = " " ;
while ( file . hasNextLine ( ) & & ! next . equals ( " :NEXTLIST: " ) ) {
try {
next = file . nextLine ( ) ;
if ( ! next . equals ( " :NEXTLIST: " ) ) {
String [ ] data = next . split ( " ; " ) ;
pokeballs . add ( new Pokeball ( data [ 0 ] , data [ 1 ] , Integer . parseInt ( data [ 2 ] ) , Pokeball . Pokeballs . valueOf ( data [ 0 ] . toUpperCase ( ) . replace ( " " , " " ) ) ) ) ;
}
} catch ( ArrayIndexOutOfBoundsException e ) {
System . out . println ( " Invalid savefile: " + savefile ) ;
return null ;
}
}
2017-11-25 22:28:17 +01:00
while ( file . hasNextLine ( ) ) {
2017-11-24 16:02:57 +01:00
try {
2017-11-25 22:28:17 +01:00
String [ ] data = file . nextLine ( ) . split ( " ; " ) ;
2017-11-26 14:27:38 +01:00
potions . add ( new Potion ( data [ 0 ] , data [ 1 ] , Integer . parseInt ( data [ 2 ] ) , Potion . Potions . valueOf ( data [ 0 ] . toUpperCase ( ) . replace ( " " , " " ) ) ) ) ;
2017-11-24 16:02:57 +01:00
} catch ( ArrayIndexOutOfBoundsException e ) {
System . out . println ( " Invalid savefile: " + savefile ) ;
return null ;
}
}
System . out . println ( " Successfully loaded items. " ) ;
} catch ( FileNotFoundException e ) {
System . out . println ( " You don't have a valid savefile. " ) ;
}
2017-11-26 14:27:38 +01:00
return new Inventory ( pokeballs , potions ) ;
2017-11-24 16:02:57 +01:00
}
2017-11-25 22:28:17 +01:00
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 ;
}
2017-11-28 14:52:16 +01:00
}