diff --git a/src/comp1110/ass2/Coord.java b/src/comp1110/ass2/Coord.java new file mode 100644 index 0000000..a02f0dd --- /dev/null +++ b/src/comp1110/ass2/Coord.java @@ -0,0 +1,70 @@ +package comp1110.ass2; + +/** + * Object to store coordinates + * This stores the x and y coordinates of a point + */ +public class Coord { + private int x; + private int y; + + /** + * Constructor for the Coord object + * @param x x coordinate + * @param y y coordinate + */ + public Coord(int x, int y) { + this.x = x; + this.y = y; + } + + /** + * Get the x coordinate + * @return int x coordinate + */ + public int getX() { + return x; + } + + /** + * Get the y coordinate + * @return int y coordinate + */ + public int getY() { + return y; + } + + /** + * Set the x coordinate + * @param x int x coordinate + */ + public void setX(int x) { + this.x = x; + } + + /** + * Set the y coordinate + * @param y int y coordinate + */ + public void setY(int y) { + this.y = y; + } + + /** + * Check if two coordinates are equal + * @param coord Coord object to compare to + * @return boolean true if equal, false otherwise + */ + public boolean equals(Coord coord) { + return (this.x == coord.x && this.y == coord.y); + } + + /** + * Get a string representation of the coordinate + * @return String representation of the coordinate + */ + @Override + public String toString() { + return "(" + x + ", " + y + ")"; + } +} diff --git a/src/comp1110/ass2/Island.java b/src/comp1110/ass2/Island.java new file mode 100644 index 0000000..e55a874 --- /dev/null +++ b/src/comp1110/ass2/Island.java @@ -0,0 +1,82 @@ +package comp1110.ass2; + +/** + * Island class + * This class is used to store the information of an island + */ + +public class Island { + final int bonus; + private Coord[] coords; + + /** + * Constructor for Island class + * This creates an island with a bonus and an empty array of coordinates + * @param bonus the bonus of the island + */ + public Island(int bonus) { + this.bonus = bonus; + this.coords = new Coord[0]; + } + + /** + * Get the bonus of the island + * @return int bonus of the island + */ + public int getBonus() { + return bonus; + } + + /** + * Get the coordinates of the island + * @return Coord[] coordinates of the island + */ + public Coord[] getCoords() { + return coords; + } + + /** + * Check if the island contains a coordinate + * @param coord the coordinate to be checked + * @return boolean true if the island contains the coordinate + */ + public boolean containsCoord(Coord coord) { + for (Coord c : this.coords) { + if (c.equals(coord)) { + return true; + } + } + return false; + } + + /** + * Add a coordinate to the island + * @param coord the coordinate to be added + */ + public void addCoord(Coord coord) { + + if (this.containsCoord(coord)) { + return; + } + Coord[] newCoords = new Coord[this.coords.length + 1]; + for (int i = 0; i < this.coords.length; i++) { + newCoords[i] = this.coords[i]; + } + newCoords[this.coords.length] = coord; + this.coords = newCoords; + } + + /** + * Check if island is equal to another island + * @param island the island to be compared to + * @return boolean true if the island is equal to the other island + */ + public boolean equals(Island island) { + if (this.bonus != island.bonus) return false; + if (this.coords.length != island.coords.length) return false; + for (int i = 0; i < this.coords.length; i++) { + if (!this.containsCoord(island.coords[i])) return false; + } + return true; + } +} diff --git a/src/comp1110/ass2/Player.java b/src/comp1110/ass2/Player.java new file mode 100644 index 0000000..4f79019 --- /dev/null +++ b/src/comp1110/ass2/Player.java @@ -0,0 +1,203 @@ +package comp1110.ass2; + +/** + * Player class + * This class is used to store the information of a player + * This includes the player's ID, score, resources, settlers and villages + */ +public class Player { + final int playerID; + private int score; + private int numCoconuts; + private int numBamboo; + private int numWater; + private int numPreciousStones; + private int numStatuette; + private Coord[] settlers; + private Coord[] villages; + + /** + * Constructor for Player class + * This creates a player with a player ID and initialises the player's score, resources, settlers and villages + * @param playerID int player ID + */ + public Player(int playerID) { + this.playerID = playerID; + this.score = 0; + this.numCoconuts = 0; + this.numBamboo = 0; + this.numWater = 0; + this.numPreciousStones = 0; + this.numStatuette = 0; + this.settlers = new Coord[0]; + this.villages = new Coord[0]; + } + + /** + * Get the player's ID + * @return int player ID + */ + public int getPlayerID() { + return playerID; + } + + /** + * Get the player's score + * @return int player score + */ + public int getScore() { + return score; + } + + /** + * Add a score to the player's score + * @param score int score to be added + */ + public void addScore(int score) { + this.score += score; + } + + /** + * Get the number of a resource the player has of a type + * @param resourceType char resource type + * @return int number of the resource the player has + */ + public int getNumResource(char resourceType) { + switch (resourceType) { + case 'C': + return numCoconuts; + case 'B': + return numBamboo; + case 'W': + return numWater; + case 'P': + return numPreciousStones; + case 'S': + return numStatuette; + default: + return 0; + } + } + + /** + * Add a resource to the player's resources + * @param numResource int number of the resource to be added + * @param resourceType char resource type + */ + public void addResource(int numResource, char resourceType) { + switch (resourceType) { + case 'C': + numCoconuts += numResource; + break; + case 'B': + numBamboo += numResource; + break; + case 'W': + numWater += numResource; + break; + case 'P': + numPreciousStones += numResource; + break; + case 'S': + numStatuette += numResource; + break; + } + } + + /** + * Remove a resource from the player's resources + * @param numResource int number of the resource to be removed + * @param resourceType char resource type + */ + public void removeResource(int numResource, char resourceType) { + switch (resourceType) { + case 'C': + numCoconuts -= numResource; + break; + case 'B': + numBamboo -= numResource; + break; + case 'W': + numWater -= numResource; + break; + case 'P': + numPreciousStones -= numResource; + break; + case 'S': + numStatuette -= numResource; + break; + } + } + + /** + * Get the player's settlers + * @return Coord[] list of the player's settlers coords + */ + public Coord[] getSettlers() { + return settlers; + } + + /** + * Get the player's villages + * @return Coord[] list of the player's villages coords + */ + public Coord[] getVillages() { + return villages; + } + + /** + * Add settlers to the player's settlers + * @param coord Coord coord of the settler to be added + */ + public void addSettler(Coord coord) { + Coord[] newSettlers = new Coord[settlers.length + 1]; + for (int i = 0; i < settlers.length; i++) { + newSettlers[i] = settlers[i]; + } + newSettlers[settlers.length] = coord; + settlers = newSettlers; + } + + /** + * Add a village to the player's villages + * @param coord Coord coord of the village to be added + */ + public void addVillage(Coord coord) { + // Check if the player already has the maximum number of villages + if (villages.length >= 5) { + return; + } + + Coord[] newVillages = new Coord[villages.length + 1]; + for (int i = 0; i < villages.length; i++) { + newVillages[i] = villages[i]; + } + newVillages[villages.length] = coord; + villages = newVillages; + } + + /** + * Check if player is equal to another player + * @param player Player player to be compared to + * @return true if the players are equal, false otherwise + */ + public boolean equals(Player player) { + if (player.getPlayerID() != playerID) return false; + if (player.getScore() != score) return false; + if (player.getNumResource('C') != numCoconuts) return false; + if (player.getNumResource('B') != numBamboo) return false; + if (player.getNumResource('W') != numWater) return false; + if (player.getNumResource('P') != numPreciousStones) return false; + if (player.getNumResource('S') != numStatuette) return false; + if (player.getSettlers().length != settlers.length) return false; + if (player.getVillages().length != villages.length) return false; + + for (int i = 0; i < settlers.length; i++) { + if (!player.getSettlers()[i].equals(settlers[i])) return false; + } + for (int i = 0; i < villages.length; i++) { + if (!player.getVillages()[i].equals(villages[i])) return false; + } + return true; + } +} diff --git a/src/comp1110/ass2/Resource.java b/src/comp1110/ass2/Resource.java new file mode 100644 index 0000000..ad44b10 --- /dev/null +++ b/src/comp1110/ass2/Resource.java @@ -0,0 +1,64 @@ +package comp1110.ass2; + +/** + * Resource class + * This class is used to store the information of a resource + * This stores the type and the coordinate of the resource + */ +public class Resource { + private char type; + private Coord coord; + + /** + * Constructor for Resource class + * This creates a resource with a type and a coordinate + * @param type the type of the resource + * @param coord the coordinate of the resource + */ + public Resource(char type, Coord coord) { + this.type = type; + this.coord = coord; + } + + /** + * Get the type of the resource + * @return char type of the resource + */ + public char getType() { + return type; + } + + /** + * Get the coordinate of the resource + * @return Coord coordinate of the resource + */ + public Coord getCoord() { + return coord; + } + + /** + * Set the type of the resource + * @param type char type of the resource + */ + public void setType(char type) { + this.type = type; + } + + /** + * Set the coordinate of the resource + * @param coord Coord coordinate of the resource + */ + public void setCoord(Coord coord) { + this.coord = coord; + } + + /** + * Check if the resource is equal to another resource + * @param resource Resource resource to be compared + * @return boolean true if the resources are equal + */ + public boolean equals(Resource resource) { + return (this.type == resource.type && this.coord.equals(resource.coord)); + } + +} diff --git a/src/comp1110/ass2/State.java b/src/comp1110/ass2/State.java new file mode 100644 index 0000000..c4896fb --- /dev/null +++ b/src/comp1110/ass2/State.java @@ -0,0 +1,234 @@ +package comp1110.ass2; + +/** + * Object to store the game state + * This stores the state of the game in a way that is easy to access and modify + * It stores the board height, number of players, current player, current phase, islands, stones, + * unclaimed resources and players (with their data) + * + */ +public class State { + final int boardHeight; + private int numPlayers; + private int currentPlayer; + private int currentPhase; // 0 for exploration, 1 for settlement + + private Island[] islands; + private Coord[] stonesCoords; + private Resource[] unclaimedResources; + private Player[] players; + + /** + * Constructor for the state object + * This takes a string containing the state of the game and initialises the object + * @param stateString String containing the state of the game + */ + public State(String stateString) { + + // Split the state string into its components + String[] components = stateString.split(";"); + + // For the game initialisation part + String[] gameInitComponents = components[0].trim().split(" "); + + boardHeight = Integer.parseInt(gameInitComponents[1]); + numPlayers = Integer.parseInt(gameInitComponents[2]); + + + // Current state part + String[] currentStateComponents = components[1].trim().split(" "); + currentPlayer = Integer.parseInt(currentStateComponents[1]); + + if (currentStateComponents[2].equals("E")) currentPhase = 0; + else currentPhase = 1; + + // Islands part + int islandcount = 0; + for (int i=2; i< components.length; i++) + { + // Split island part + String[] islandComponents = components[i].trim().split(" "); + // Check if the component is still an island + if (!islandComponents[0].equals("i")) break; + + Island tmpIsland = new Island(Integer.parseInt(islandComponents[1])); + + // Add each coord + for (int j=2; j