Improved error checks for Map
This commit is contained in:
parent
a16a07846b
commit
a135ec80ec
4
.gitignore
vendored
4
.gitignore
vendored
@ -27,3 +27,7 @@ hs_err_pid*
|
|||||||
# Script files
|
# Script files
|
||||||
*.bat
|
*.bat
|
||||||
*.sh
|
*.sh
|
||||||
|
|
||||||
|
#IDEA files
|
||||||
|
*.xml
|
||||||
|
*.iml
|
21
Test/MapTest.java
Normal file
21
Test/MapTest.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
class MapTest {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Tile rock = new Tile('■', true, "NONE");
|
||||||
|
Tile empty = new Tile(' ', false, "NONE");
|
||||||
|
Map map = new Map(10, 10, 5, 5, empty, rock);
|
||||||
|
Tile wallTopRight = new Tile('╗', true, "NONE");
|
||||||
|
Tile wallTopLeft = new Tile('╔', true, "NONE");
|
||||||
|
Tile wallBottomRight = new Tile('╝', true, "NONE");
|
||||||
|
Tile wallBottomLeft = new Tile('╚', true, "NONE");
|
||||||
|
Tile wallSide = new Tile('║', true, "NONE");
|
||||||
|
Tile wallLying = new Tile('═', true, "NONE");
|
||||||
|
Tile roof = new Tile(' ', true, "NONE");
|
||||||
|
Tile door = new Tile('╼', false, "TELEPORT", 10, 10);
|
||||||
|
Tile player = new Tile('P', false, "NONE");
|
||||||
|
Tile[][] tiles = {{wallTopLeft, wallSide, wallBottomLeft}, {wallLying, roof, door}, {wallTopRight, wallSide, wallBottomRight}};
|
||||||
|
new Structure("House", tiles);
|
||||||
|
map.generateStructure("House", 4, 2);
|
||||||
|
map.placeTile(player, 8, 8);
|
||||||
|
System.out.println(map);
|
||||||
|
}
|
||||||
|
}
|
3
java/Direction.java
Normal file
3
java/Direction.java
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
public enum Direction {
|
||||||
|
NORTH, SOUTH, EAST, WEST;
|
||||||
|
}
|
@ -10,7 +10,7 @@ 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);
|
private static final 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();
|
||||||
@ -21,12 +21,12 @@ public class Game {
|
|||||||
Trainer player = new Trainer(name, randomTeam(), createInventory());
|
Trainer player = new Trainer(name, randomTeam(), createInventory());
|
||||||
|
|
||||||
boolean done = false;
|
boolean done = false;
|
||||||
Pokemon opponentPokemon = null;
|
Pokemon opponentPokemon;
|
||||||
Pokemon trainersPokemon = null;
|
Pokemon trainersPokemon = null;
|
||||||
Pokeball currentPokeball = null;
|
Pokeball currentPokeball;
|
||||||
Potion currentPotion = null;
|
Potion currentPotion;
|
||||||
boolean fleeSuccess = false;
|
boolean fleeSuccess;
|
||||||
Pokemon pokemonToHeal = null;
|
Pokemon pokemonToHeal;
|
||||||
|
|
||||||
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());
|
||||||
@ -187,7 +187,7 @@ public class Game {
|
|||||||
* @param pokemonList List of pokemon to search.
|
* @param pokemonList List of pokemon to search.
|
||||||
* @param target The pokemon to remove.
|
* @param target The pokemon to remove.
|
||||||
*/
|
*/
|
||||||
public static void pokemonFainted(ArrayList<Pokemon> pokemonList, Pokemon target) {
|
private static void pokemonFainted(ArrayList<Pokemon> pokemonList, Pokemon target) {
|
||||||
for (int i = 0; i < pokemonList.size(); i++) {
|
for (int i = 0; i < pokemonList.size(); i++) {
|
||||||
if (pokemonList.get(i).equals(target)) {
|
if (pokemonList.get(i).equals(target)) {
|
||||||
pokemonList.remove(i);
|
pokemonList.remove(i);
|
||||||
@ -199,7 +199,7 @@ public class Game {
|
|||||||
* Gives a trainer necessary starting items.
|
* Gives a trainer necessary starting items.
|
||||||
* @return A list of items.
|
* @return A list of items.
|
||||||
*/
|
*/
|
||||||
public static Inventory createInventory() {
|
private static Inventory createInventory() {
|
||||||
Inventory inventory = new Inventory();
|
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("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("Great ball", "A good, high-performance Poké Ball that provides a higher Pokémon catch rate than a standard Poké Ball.", 10);
|
||||||
@ -218,7 +218,7 @@ public class Game {
|
|||||||
* @param pokemon The list to choose from.
|
* @param pokemon The list to choose from.
|
||||||
* @return A Pokemon object, or null if the list is empty.
|
* @return A Pokemon object, or null if the list is empty.
|
||||||
*/
|
*/
|
||||||
public static Pokemon randomPokemon(ArrayList<Pokemon> pokemon) {
|
private static Pokemon randomPokemon(ArrayList<Pokemon> pokemon) {
|
||||||
if (pokemon.size() > 0) {
|
if (pokemon.size() > 0) {
|
||||||
return pokemon.get((int)(Math.random() * pokemon.size()));
|
return pokemon.get((int)(Math.random() * pokemon.size()));
|
||||||
} else {
|
} else {
|
||||||
@ -230,14 +230,14 @@ public class Game {
|
|||||||
* Reads pokemon from Pokemon.txt to an ArrayList.
|
* Reads pokemon from Pokemon.txt to an ArrayList.
|
||||||
* @return An ArrayList of pokemon objects.
|
* @return An ArrayList of pokemon objects.
|
||||||
*/
|
*/
|
||||||
public static ArrayList<Pokemon> readPokemon() {
|
private static ArrayList<Pokemon> readPokemon() {
|
||||||
ArrayList<Pokemon> pokemon = new ArrayList<Pokemon>();
|
ArrayList<Pokemon> pokemon = new ArrayList<>();
|
||||||
try (Scanner file = new Scanner(new File("config/Pokemon.txt"))) {
|
try (Scanner file = new Scanner(new File("config/Pokemon.txt"))) {
|
||||||
while (file.hasNextLine()) {
|
while (file.hasNextLine()) {
|
||||||
pokemon.add(new Pokemon(file.nextLine()));
|
pokemon.add(new Pokemon(file.nextLine()));
|
||||||
}
|
}
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
/** If the file is compiled as jar, this will prevent an empty list. */
|
/* If the file is compiled as jar, this will prevent an empty list. */
|
||||||
try (Scanner file = new Scanner(Game.class.getResourceAsStream("/config/Pokemon.txt"))) {
|
try (Scanner file = new Scanner(Game.class.getResourceAsStream("/config/Pokemon.txt"))) {
|
||||||
while (file.hasNextLine()) {
|
while (file.hasNextLine()) {
|
||||||
pokemon.add(new Pokemon(file.nextLine()));
|
pokemon.add(new Pokemon(file.nextLine()));
|
||||||
@ -252,7 +252,7 @@ public class Game {
|
|||||||
* @param pokemon A list of pokemon objects.
|
* @param pokemon A list of pokemon objects.
|
||||||
* @return A pokemon object.
|
* @return A pokemon object.
|
||||||
*/
|
*/
|
||||||
public static Pokemon pick(ArrayList<Pokemon> pokemon) {
|
private static Pokemon pick(ArrayList<Pokemon> pokemon) {
|
||||||
int index = (int)(Math.random() * (pokemon.size()));
|
int index = (int)(Math.random() * (pokemon.size()));
|
||||||
Pokemon randomPokemon = pokemon.get(index);
|
Pokemon randomPokemon = pokemon.get(index);
|
||||||
pokemon.remove(index);
|
pokemon.remove(index);
|
||||||
@ -264,7 +264,7 @@ public class Game {
|
|||||||
* @param pokemonList List of Pokemon objects to save.
|
* @param pokemonList List of Pokemon objects to save.
|
||||||
* @param savefile The file to write to.
|
* @param savefile The file to write to.
|
||||||
*/
|
*/
|
||||||
public static void savePokemon(ArrayList<Pokemon> pokemonList, String savefile) {
|
private static void savePokemon(ArrayList<Pokemon> pokemonList, String savefile) {
|
||||||
try (PrintWriter file = new PrintWriter(savefile)) {
|
try (PrintWriter file = new PrintWriter(savefile)) {
|
||||||
for (Pokemon pokemon : pokemonList) {
|
for (Pokemon pokemon : pokemonList) {
|
||||||
file.println(pokemon.saveString());
|
file.println(pokemon.saveString());
|
||||||
@ -277,10 +277,10 @@ public class Game {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves all items in a list to a text file.
|
* Saves all items in a list to a text file.
|
||||||
* @param itemList List of Item objects to save.
|
* @param inventory The items of a player
|
||||||
* @param savefile The file to write to.
|
* @param savefile The file to write to.
|
||||||
*/
|
*/
|
||||||
public static void saveInventory(Inventory inventory, String savefile) {
|
private static void saveInventory(Inventory inventory, String savefile) {
|
||||||
try (PrintWriter file = new PrintWriter(savefile)) {
|
try (PrintWriter file = new PrintWriter(savefile)) {
|
||||||
for (Pokeball pokeball : inventory.getPokeballs()) {
|
for (Pokeball pokeball : inventory.getPokeballs()) {
|
||||||
file.println(pokeball.saveString());
|
file.println(pokeball.saveString());
|
||||||
@ -300,17 +300,15 @@ public class Game {
|
|||||||
* @param savefile The file to write to.
|
* @param savefile The file to write to.
|
||||||
* @return A list of pokemon or null on failiure.
|
* @return A list of pokemon or null on failiure.
|
||||||
*/
|
*/
|
||||||
public static ArrayList<Pokemon> loadPokemon(String savefile) {
|
private static ArrayList<Pokemon> loadPokemon(String savefile) {
|
||||||
ArrayList<Pokemon> pokemon = new ArrayList<Pokemon>();
|
ArrayList<Pokemon> pokemon = new ArrayList<>();
|
||||||
try (Scanner file = 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 (file.hasNextLine()) {
|
while (file.hasNextLine()) {
|
||||||
String[] data = file.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 | ParseException e) {
|
||||||
System.out.println("Malformed number " + e);
|
|
||||||
} catch (ParseException e) {
|
|
||||||
System.out.println("Malformed number " + e);
|
System.out.println("Malformed number " + e);
|
||||||
} catch (ArrayIndexOutOfBoundsException e) {
|
} catch (ArrayIndexOutOfBoundsException e) {
|
||||||
System.out.println("Invalid savefile: " + savefile);
|
System.out.println("Invalid savefile: " + savefile);
|
||||||
@ -330,9 +328,9 @@ public class Game {
|
|||||||
* @param savefile The file to write to.
|
* @param savefile The file to write to.
|
||||||
* @return A list of items or null on failiure.
|
* @return A list of items or null on failiure.
|
||||||
*/
|
*/
|
||||||
public static Inventory loadInventory(String savefile) {
|
private static Inventory loadInventory(String savefile) {
|
||||||
ArrayList<Pokeball> pokeballs = new ArrayList<Pokeball>();
|
ArrayList<Pokeball> pokeballs = new ArrayList<>();
|
||||||
ArrayList<Potion> potions = new ArrayList<Potion>();
|
ArrayList<Potion> potions = new ArrayList<>();
|
||||||
try (Scanner file = new Scanner(new File(savefile))) {
|
try (Scanner file = new Scanner(new File(savefile))) {
|
||||||
String next = "";
|
String next = "";
|
||||||
while (file.hasNextLine() && !next.equals(":NEXTLIST:")) {
|
while (file.hasNextLine() && !next.equals(":NEXTLIST:")) {
|
||||||
@ -364,9 +362,9 @@ public class Game {
|
|||||||
return new Inventory(pokeballs, potions);
|
return new Inventory(pokeballs, potions);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ArrayList<Pokemon> randomTeam() {
|
private static ArrayList<Pokemon> randomTeam() {
|
||||||
ArrayList<Pokemon> temporary = readPokemon();
|
ArrayList<Pokemon> temporary = readPokemon();
|
||||||
ArrayList<Pokemon> pokemon = new ArrayList<Pokemon>();
|
ArrayList<Pokemon> pokemon = new ArrayList<>();
|
||||||
for (int i = 1; i <= 6; i++) {
|
for (int i = 1; i <= 6; i++) {
|
||||||
pokemon.add(pick(temporary));
|
pokemon.add(pick(temporary));
|
||||||
}
|
}
|
||||||
|
@ -2,13 +2,13 @@ import java.util.ArrayList;
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Inventory {
|
public class Inventory {
|
||||||
public static Scanner in = new Scanner(System.in);
|
private static final Scanner in = new Scanner(System.in);
|
||||||
private ArrayList<Pokeball> pokeballs;
|
private final ArrayList<Pokeball> pokeballs;
|
||||||
private ArrayList<Potion> potions;
|
private final ArrayList<Potion> potions;
|
||||||
|
|
||||||
public Inventory() {
|
public Inventory() {
|
||||||
this.pokeballs = new ArrayList<Pokeball>();
|
this.pokeballs = new ArrayList<>();
|
||||||
this.potions = new ArrayList<Potion>();
|
this.potions = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Inventory(ArrayList<Pokeball> pokeballs, ArrayList<Potion> potions) {
|
public Inventory(ArrayList<Pokeball> pokeballs, ArrayList<Potion> potions) {
|
||||||
@ -18,20 +18,16 @@ public class Inventory {
|
|||||||
|
|
||||||
public void addPokeball(String name, String description, int amount) {
|
public void addPokeball(String name, String description, int amount) {
|
||||||
Pokeball pokeball = new Pokeball(name, description, amount, Pokeball.Pokeballs.valueOf(name.toUpperCase().replace(" ", "")));
|
Pokeball pokeball = new Pokeball(name, description, amount, Pokeball.Pokeballs.valueOf(name.toUpperCase().replace(" ", "")));
|
||||||
if (pokeball != null) {
|
|
||||||
this.pokeballs.add(pokeball);
|
this.pokeballs.add(pokeball);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void addPotion(String name, String description, int amount, boolean alive) {
|
public void addPotion(String name, String description, int amount, boolean alive) {
|
||||||
Potion potion = new Potion(name, description, amount, Potion.Potions.valueOf(name.toUpperCase().replace(" ", "")), alive);
|
Potion potion = new Potion(name, description, amount, Potion.Potions.valueOf(name.toUpperCase().replace(" ", "")), alive);
|
||||||
if (potion != null) {
|
|
||||||
this.potions.add(potion);
|
this.potions.add(potion);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList<Pokeball> getPokeballs() {
|
public ArrayList<Pokeball> getPokeballs() {
|
||||||
ArrayList<Pokeball> pokeballs = new ArrayList<Pokeball>();
|
ArrayList<Pokeball> pokeballs = new ArrayList<>();
|
||||||
for (Pokeball pokeball : this.pokeballs) {
|
for (Pokeball pokeball : this.pokeballs) {
|
||||||
if (pokeball.getAmount() > 0) {
|
if (pokeball.getAmount() > 0) {
|
||||||
pokeballs.add(pokeball);
|
pokeballs.add(pokeball);
|
||||||
@ -41,7 +37,7 @@ public class Inventory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Potion> getPotions() {
|
public ArrayList<Potion> getPotions() {
|
||||||
ArrayList<Potion> potions = new ArrayList<Potion>();
|
ArrayList<Potion> potions = new ArrayList<>();
|
||||||
for (Potion potion : this.potions) {
|
for (Potion potion : this.potions) {
|
||||||
if (potion.getAmount() > 0) {
|
if (potion.getAmount() > 0) {
|
||||||
potions.add(potion);
|
potions.add(potion);
|
||||||
|
@ -1,17 +1,15 @@
|
|||||||
public class Map {
|
class Map {
|
||||||
private Tile[][] tiles;
|
private final Tile[][] tiles;
|
||||||
private int wWidth;
|
private int fullWidth;
|
||||||
private int wHeight;
|
private int fullHeight;
|
||||||
|
|
||||||
public Map(int width, int height, int wWidth, int wHeight, Tile empty, Tile wall) {
|
public Map(int width, int height, int wWidth, int wHeight, Tile empty, Tile wall) {
|
||||||
this.wWidth = wWidth;
|
this.fullHeight = height + (2 * wHeight);
|
||||||
this.wHeight = wHeight;
|
this.fullWidth = width + (2 * wWidth);
|
||||||
int fullHeight = height + (2 * wHeight);
|
Tile[][] map = new Tile[this.fullWidth][this.fullHeight];
|
||||||
int fullWidth = width + (2 * wWidth);
|
for (int i = 0; i < this.fullWidth; i++) {
|
||||||
Tile[][] map = new Tile[fullHeight][fullWidth];
|
for (int j = 0; j < this.fullHeight; j++) {
|
||||||
for (int i = 0; i < fullHeight; i++) {
|
if (i < wWidth || i >= width + wWidth || j < wHeight || j >= height + wHeight) {
|
||||||
for (int j = 0; j < fullWidth; j++) {
|
|
||||||
if (i < wHeight || i >= height + wHeight || j < wWidth || j >= width + wWidth) {
|
|
||||||
map[i][j] = wall;
|
map[i][j] = wall;
|
||||||
} else {
|
} else {
|
||||||
map[i][j] = empty;
|
map[i][j] = empty;
|
||||||
@ -21,32 +19,71 @@ public class Map {
|
|||||||
this.tiles = map;
|
this.tiles = map;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tile[][] getTiles() {
|
private Tile[][] getTiles() {
|
||||||
return this.tiles;
|
return this.tiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void generateStructure(String name, int x, int y) {
|
public void generateStructure(String name, int x, int y) throws IllegalArgumentException {
|
||||||
for (Structure structure : Structure.getStructures()) {
|
for (Structure structure : Structure.getStructures()) {
|
||||||
if (name.equals(structure.getName())) {
|
if (name.equals(structure.getName())) {
|
||||||
this.placeStructure(structure, x, y);
|
this.placeStructure(structure, x, y);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println("Invalid structure name.");
|
throw new IllegalArgumentException("Invalid structure name.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void placeStructure(Structure structure, int x, int y) {
|
private void placeStructure(Structure structure, int x, int y) throws IllegalArgumentException {
|
||||||
|
if (x < 0 || y < 0 || this.fullWidth < x + structure.getWidth() || this.fullHeight < y + structure.getHeight()) {
|
||||||
|
throw new IllegalArgumentException("The structure is misplaced or does not fit.");
|
||||||
|
}
|
||||||
|
Tile[][] tiles = structure.getTiles();
|
||||||
|
for (int i = 0; i < structure.getWidth(); i++) {
|
||||||
|
for (int j = 0; j < structure.getHeight(); j++) {
|
||||||
|
if (!tiles[i][j].isSolid()) {
|
||||||
|
switch (structure.getDoorDirection()) {
|
||||||
|
case NORTH:
|
||||||
|
if (this.tiles[x+i][y+j-1].isSolid()) {
|
||||||
|
throw new IllegalArgumentException("A structure is blocked");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SOUTH:
|
||||||
|
if (this.tiles[x+i][y+j+1].isSolid()) {
|
||||||
|
throw new IllegalArgumentException("A structure is blocked");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case WEST:
|
||||||
|
if (this.tiles[x+i-1][y+j].isSolid()) {
|
||||||
|
throw new IllegalArgumentException("A structure is blocked");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case EAST:
|
||||||
|
if (this.tiles[x+i+1][y+j].isSolid()) {
|
||||||
|
throw new IllegalArgumentException("A structure is blocked");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.tiles[x+i][y+j] = tiles[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void placeTile(Tile tile, int x, int y) {
|
||||||
|
if (x < 0 || y < 0 || x >= this.tiles.length || y >= this.tiles[0].length) {
|
||||||
|
throw new IllegalArgumentException("Invalid tile position");
|
||||||
|
}
|
||||||
|
this.tiles[x][y] = tile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
Tile[][] tiles = this.getTiles();
|
Tile[][] tiles = this.getTiles();
|
||||||
String str = "";
|
StringBuilder str = new StringBuilder();
|
||||||
for (int i = 0; i < tiles.length; i++) {
|
for (int i = 0; i < tiles.length; i++) {
|
||||||
for (int j = 0; j < tiles[i].length; j++) {
|
for (int j = 0; j < tiles.length; j++) {
|
||||||
str += tiles[i][j].toChar();
|
str.append(tiles[j][i].toChar());
|
||||||
}
|
}
|
||||||
str += "\n";
|
str.append("\n");
|
||||||
}
|
}
|
||||||
return str;
|
return str.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,10 +1,10 @@
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Pokeball {
|
public class Pokeball {
|
||||||
public static enum Pokeballs { POKEBALL, GREATBALL, ULTRABALL, MASTERBALL }
|
public enum Pokeballs { POKEBALL, GREATBALL, ULTRABALL, MASTERBALL }
|
||||||
private String name;
|
private final String name;
|
||||||
private String description;
|
private final String description;
|
||||||
private Pokeballs type;
|
private final Pokeballs type;
|
||||||
private int amount;
|
private int amount;
|
||||||
|
|
||||||
public Pokeball(String name, String description, int amount, Pokeballs type) {
|
public Pokeball(String name, String description, int amount, Pokeballs type) {
|
||||||
|
@ -2,13 +2,13 @@ import java.util.Random;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Pokemon {
|
public class Pokemon {
|
||||||
private String name;
|
private final String name;
|
||||||
private int healthPoints;
|
private int healthPoints;
|
||||||
private int maxHealthPoints;
|
private int maxHealthPoints;
|
||||||
private int strength;
|
private int strength;
|
||||||
private double criticalChance;
|
private final double criticalChance;
|
||||||
private Random random;
|
private final Random random;
|
||||||
private int catchRate;
|
private final int catchRate;
|
||||||
private int exp;
|
private int exp;
|
||||||
private int level;
|
private int level;
|
||||||
private int fleeCount;
|
private int fleeCount;
|
||||||
@ -79,7 +79,7 @@ public class Pokemon {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if a pokemon has not taken any damage or is fully healed.
|
* Checks if a pokemon has not taken any damage or is fully healed.
|
||||||
* @param return True if health is full. False otherwise.
|
* @return True if health is full. False otherwise.
|
||||||
*/
|
*/
|
||||||
public boolean isDamaged() {
|
public boolean isDamaged() {
|
||||||
return this.healthPoints < this.maxHealthPoints;
|
return this.healthPoints < this.maxHealthPoints;
|
||||||
@ -110,7 +110,7 @@ public class Pokemon {
|
|||||||
/**
|
/**
|
||||||
* Captures a wild pokemon.
|
* Captures a wild pokemon.
|
||||||
* @param current The pokemon list the pokemon belongs to.
|
* @param current The pokemon list the pokemon belongs to.
|
||||||
* @param catcher The pokemon list of the trainer.
|
* @param trainer The trainer who is capturing.
|
||||||
*/
|
*/
|
||||||
private void capture(ArrayList<Pokemon> current, Trainer trainer) {
|
private void capture(ArrayList<Pokemon> current, Trainer trainer) {
|
||||||
trainer.addPokemon(this);
|
trainer.addPokemon(this);
|
||||||
@ -136,16 +136,16 @@ public class Pokemon {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Damages a pokemon.
|
* Damages a pokemon.
|
||||||
* @param How many hitpoints are to be deducted.
|
* @param damageTaken How many hitpoints are to be deducted.
|
||||||
*/
|
*/
|
||||||
public void damage(int damageTaken) {
|
private void damage(int damageTaken) {
|
||||||
this.healthPoints = Math.max(this.healthPoints -= damageTaken, 0);
|
this.healthPoints = Math.max(this.healthPoints -= damageTaken, 0);
|
||||||
System.out.printf("%s takes %d damage and is left with %d/%d HP%n", this.name, damageTaken, this.healthPoints, this.maxHealthPoints);
|
System.out.printf("%s takes %d damage and is left with %d/%d HP%n", this.name, damageTaken, this.healthPoints, this.maxHealthPoints);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gives a pokemon exp after each successfull battle. Also handles leveling up.
|
* Gives a pokemon exp after each successfull battle. Also handles leveling up.
|
||||||
* @target Which pokemon did we beat.
|
* @param target Which pokemon did we beat.
|
||||||
*/
|
*/
|
||||||
private void giveEXP(Pokemon target) {
|
private void giveEXP(Pokemon target) {
|
||||||
int exp = (100 * target.level)/7;
|
int exp = (100 * target.level)/7;
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public class Potion {
|
public class Potion {
|
||||||
public static enum Potions { POTION, SUPERPOTION, HYPERPOTION, MAXPOTION, REVIVE }
|
public enum Potions { POTION, SUPERPOTION, HYPERPOTION, MAXPOTION, REVIVE }
|
||||||
private String name;
|
private final String name;
|
||||||
private String description;
|
private final String description;
|
||||||
private Potions type;
|
private final Potions type;
|
||||||
private boolean alive;
|
private final boolean alive;
|
||||||
private int amount;
|
private int amount;
|
||||||
|
|
||||||
public Potion(String name, String description, int amount, Potions type) {
|
public Potion(String name, String description, int amount, Potions type) {
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Structure {
|
class Structure {
|
||||||
private static ArrayList<Structure> structures = new ArrayList<Structure>();
|
private static final ArrayList<Structure> structures = new ArrayList<>();
|
||||||
private Tile[][] tiles;
|
private final Tile[][] tiles;
|
||||||
private String name;
|
private final String name;
|
||||||
|
|
||||||
public Structure(String name, Tile[][] tiles) {
|
public Structure(String name, Tile[][] tiles) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
@ -27,6 +27,25 @@ public class Structure {
|
|||||||
return this.tiles.length;
|
return this.tiles.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Direction getDoorDirection() {
|
||||||
|
for (int x = 0; x < this.getWidth(); x++) {
|
||||||
|
for (int y = 0; y < this.getHeight(); y++) {
|
||||||
|
if (!tiles[x][y].isSolid()) {
|
||||||
|
if (y == tiles[x].length - 1) {
|
||||||
|
return Direction.SOUTH;
|
||||||
|
} else if (y == 0) {
|
||||||
|
return Direction.NORTH;
|
||||||
|
} else if (x == 0) {
|
||||||
|
return Direction.WEST;
|
||||||
|
} else if (x == tiles.length - 1) {
|
||||||
|
return Direction.EAST;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public int getHeight() {
|
public int getHeight() {
|
||||||
int max = 0;
|
int max = 0;
|
||||||
for (Tile[] tile : this.tiles) {
|
for (Tile[] tile : this.tiles) {
|
||||||
|
@ -1,13 +0,0 @@
|
|||||||
public class Test {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
Tile rock = new Tile('#', true, "NONE");
|
|
||||||
Tile empty = new Tile(' ', false, "NONE");
|
|
||||||
Map map = new Map(50, 5, 5, 5, empty, rock);
|
|
||||||
Tile wall = new Tile('=', true, "NONE");
|
|
||||||
Tile door = new Tile('_', false, "TELEPORT", 0, 0);
|
|
||||||
Tile[][] tiles = {{wall, wall, wall}, {wall, wall, wall}, {wall, door, wall}};
|
|
||||||
Structure structure = new Structure("House", tiles);
|
|
||||||
map.generateStructure("House", 10, 10);
|
|
||||||
System.out.println(map);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +1,8 @@
|
|||||||
public class Tile {
|
public class Tile {
|
||||||
private char representation;
|
private final char representation;
|
||||||
private boolean solid;
|
private final boolean solid;
|
||||||
private enum Action { TELEPORT, NONE, ENCOUNTER, MENUBATTLE, MENUSHOP }
|
private enum Action { TELEPORT, NONE, ENCOUNTER, MENUBATTLE, MENUSHOP }
|
||||||
private Action action;
|
private final Action action;
|
||||||
private int[] teleportTarget;
|
private int[] teleportTarget;
|
||||||
|
|
||||||
public Tile (char representation, boolean solid, String action) throws IllegalArgumentException {
|
public Tile (char representation, boolean solid, String action) throws IllegalArgumentException {
|
||||||
@ -21,8 +21,7 @@ public class Tile {
|
|||||||
this.solid = solid;
|
this.solid = solid;
|
||||||
this.action = Action.valueOf(action.toUpperCase());
|
this.action = Action.valueOf(action.toUpperCase());
|
||||||
if (this.action == Action.TELEPORT) {
|
if (this.action == Action.TELEPORT) {
|
||||||
int[] intArray = {x, y};
|
this.teleportTarget = new int[]{x, y};
|
||||||
this.teleportTarget = intArray;
|
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("A non-teleport tile can't have a teleport target.");
|
throw new IllegalArgumentException("A non-teleport tile can't have a teleport target.");
|
||||||
}
|
}
|
||||||
@ -36,6 +35,10 @@ public class Tile {
|
|||||||
return this.teleportTarget;
|
return this.teleportTarget;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isSolid() {
|
||||||
|
return this.solid;
|
||||||
|
}
|
||||||
|
|
||||||
public int onWalk() {
|
public int onWalk() {
|
||||||
if (this.solid) {
|
if (this.solid) {
|
||||||
System.out.println("You bumped against an immovable structure.");
|
System.out.println("You bumped against an immovable structure.");
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Trainer {
|
class Trainer {
|
||||||
public static Scanner in = new Scanner(System.in);
|
private static final Scanner in = new Scanner(System.in);
|
||||||
String name;
|
private final String name;
|
||||||
private ArrayList<Pokemon> pokemon;
|
private ArrayList<Pokemon> pokemon;
|
||||||
private Inventory inventory;
|
private Inventory inventory;
|
||||||
|
|
||||||
public Trainer(String name, ArrayList<Pokemon> pokemon, Inventory inventory) {
|
Trainer(String name, ArrayList<Pokemon> pokemon, Inventory inventory) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.pokemon = pokemon;
|
this.pokemon = pokemon;
|
||||||
this.inventory = inventory;
|
this.inventory = inventory;
|
||||||
@ -30,7 +30,7 @@ public class Trainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Pokemon> getConsciousPokemon() {
|
public ArrayList<Pokemon> getConsciousPokemon() {
|
||||||
ArrayList<Pokemon> pokemon = new ArrayList<Pokemon>();
|
ArrayList<Pokemon> pokemon = new ArrayList<>();
|
||||||
for (Pokemon singlePokemon : this.pokemon) {
|
for (Pokemon singlePokemon : this.pokemon) {
|
||||||
if (singlePokemon.isConscious()) {
|
if (singlePokemon.isConscious()) {
|
||||||
pokemon.add(singlePokemon);
|
pokemon.add(singlePokemon);
|
||||||
@ -39,8 +39,8 @@ public class Trainer {
|
|||||||
return pokemon;
|
return pokemon;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Pokemon> getFaintedPokemon() {
|
private ArrayList<Pokemon> getFaintedPokemon() {
|
||||||
ArrayList<Pokemon> pokemon = new ArrayList<Pokemon>();
|
ArrayList<Pokemon> pokemon = new ArrayList<>();
|
||||||
for (Pokemon singlePokemon : this.pokemon) {
|
for (Pokemon singlePokemon : this.pokemon) {
|
||||||
if (!singlePokemon.isConscious()) {
|
if (!singlePokemon.isConscious()) {
|
||||||
pokemon.add(singlePokemon);
|
pokemon.add(singlePokemon);
|
||||||
@ -55,7 +55,7 @@ public class Trainer {
|
|||||||
|
|
||||||
/** Lists all currently available pokemon for the trainer.*/
|
/** Lists all currently available pokemon for the trainer.*/
|
||||||
public void availablePokemon(boolean alive) {
|
public void availablePokemon(boolean alive) {
|
||||||
ArrayList<Pokemon> pokemonList = null;
|
ArrayList<Pokemon> pokemonList;
|
||||||
if (alive) {
|
if (alive) {
|
||||||
pokemonList = this.getConsciousPokemon();
|
pokemonList = this.getConsciousPokemon();
|
||||||
} else {
|
} else {
|
||||||
@ -76,11 +76,11 @@ public class Trainer {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Asks the user for the name of a pokemon and returns it.
|
* Asks the user for the name of a pokemon and returns it.
|
||||||
* @param pokemonList A list of available pokemon
|
* @param alive Are we looking for alive pokemon?
|
||||||
* @return A pokemon object or null.
|
* @return A pokemon object or null.
|
||||||
*/
|
*/
|
||||||
public Pokemon choosePokemon(boolean alive) {
|
public Pokemon choosePokemon(boolean alive) {
|
||||||
ArrayList<Pokemon> pokemonList = null;
|
ArrayList<Pokemon> pokemonList;
|
||||||
if (alive) {
|
if (alive) {
|
||||||
pokemonList = this.getConsciousPokemon();
|
pokemonList = this.getConsciousPokemon();
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user