diff --git a/src/comp1110/ass2/BlueLagoon.java b/src/comp1110/ass2/BlueLagoon.java index 8f1e8f3..b80754b 100644 --- a/src/comp1110/ass2/BlueLagoon.java +++ b/src/comp1110/ass2/BlueLagoon.java @@ -616,7 +616,8 @@ public class BlueLagoon { * @return true if the state is at the end of either phase and false otherwise */ public static boolean isPhaseOver(String stateString){ - return false; // FIXME Task 9 + State state = new State(stateString); + return state.isPhaseOver(); } /** @@ -631,7 +632,14 @@ public class BlueLagoon { * @return a new state string achieved by placing the move on the board */ public static String placePiece(String stateString, String moveString){ - return ""; // FIXME Task 10 + State state = new State(stateString); + char pieceType = moveString.charAt(0); + String coordStr = moveString.substring(2); + int x = Integer.parseInt(coordStr.split(",")[0]); + int y = Integer.parseInt(coordStr.split(",")[1]); + Coord coord = new Coord(x, y); + state.placePiece(coord, pieceType); + return state.toString(); } diff --git a/src/comp1110/ass2/Coord.java b/src/comp1110/ass2/Coord.java index 438960c..41bdeed 100644 --- a/src/comp1110/ass2/Coord.java +++ b/src/comp1110/ass2/Coord.java @@ -13,7 +13,7 @@ public class Coord { * @param x x coordinate * @param y y coordinate */ - public Coord(int x, int y) { + public Coord(int y, int x) { this.x = x; this.y = y; } @@ -65,6 +65,6 @@ public class Coord { */ @Override public String toString() { - return x + "," + y; + return y + "," + x; } } diff --git a/src/comp1110/ass2/Player.java b/src/comp1110/ass2/Player.java index e9ae6ce..bae76b1 100644 --- a/src/comp1110/ass2/Player.java +++ b/src/comp1110/ass2/Player.java @@ -273,16 +273,90 @@ public class Player { } } + private int maxRow(Coord[] coords){ + int maxRow = 0; + for (Coord coord : coords) { + if (coord.getY() > maxRow) { + maxRow = coord.getY(); + } + } + + return maxRow; + } + private int maxCol(Coord[] coords){ + int maxCol = 0; + for (Coord coord : coords) { + if (coord.getX() > maxCol) { + maxCol = coord.getX(); + } + } + return maxCol; + } + + @Override public String toString() { String str = "p " + playerID + " " + score + " " + numCoconuts + " " + numBamboo + " " + numWater + " " + numPreciousStones + " " + numStatuette + " S"; - for (Coord coord : settlers) { + + + // Get the coords of the player's pieces in row major order + Coord[] settlersCoords = new Coord[settlers.length]; + if (settlers.length != 0) { + int row = 0; + int col = 0; + int i = 0; + int maxrow = maxRow(settlers); + int maxcol = maxCol(settlers); + + while (settlersCoords[settlers.length - 1] == null) { + for (Coord coord : settlers) { + if (coord.getX() == col && coord.getY() == row) { + settlersCoords[i] = coord; + i++; + } + } + col++; + if (col > maxcol) { + col = 0; + row++; + } + } + } + + Coord[] villagesCoords = new Coord[villages.length]; + if (villages.length != 0) { + int row = 0; + int col = 0; + int i = 0; + int maxrow = maxRow(villages); + int maxcol = maxCol(villages); + + while (villagesCoords[villages.length-1] == null){ + for (Coord coord : villages) { + if (coord.getX() == col && coord.getY() == row) { + villagesCoords[i] = coord; + i++; + } + } + col++; + if (col > maxcol) { + col = 0; + row++; + } + } + } + + + + + for (Coord coord : settlersCoords) { str += " " + coord.toString(); + } str += " T"; - for (Coord coord : villages) { + for (Coord coord : villagesCoords) { str += " " + coord.toString(); } return str; diff --git a/src/comp1110/ass2/State.java b/src/comp1110/ass2/State.java index 1b98ec1..dde77be 100644 --- a/src/comp1110/ass2/State.java +++ b/src/comp1110/ass2/State.java @@ -558,13 +558,12 @@ public class State { * @return int score */ public int scoreStatuettes(int playerID) { - int score = 0; - return score; //! TODO + return players[playerID].getNumResource('S') * 4; } // endregion - + // region String conversion @Override public String toString() { String str = "a " + boardHeight + " " + getNumPlayers() + "; c " + getCurrentPlayerID() + " " + getCurrentPhase() + "; "; @@ -581,7 +580,7 @@ public class State { for (char type : types) { str += " " + type; for (Resource resource : resources) { - if (resource.getType() == type) str += " " + resource.getCoord().toString(); + if (resource.getType() == type && !resource.isClaimed()) str += " " + resource.getCoord().toString(); } } str += ";"; @@ -591,4 +590,14 @@ public class State { return str; } + public String scoreString() { + String str = ""; + for (Player player : players) { + str += "Player " + player.getPlayerID() + "'s score is " + player.getScore() + "\n"; + } + return str.substring(0, str.length() - 1); + } + + // endregion + } diff --git a/tests/comp1110/ass2/StateTest.java b/tests/comp1110/ass2/StateTest.java index 9fda78e..07e7538 100644 --- a/tests/comp1110/ass2/StateTest.java +++ b/tests/comp1110/ass2/StateTest.java @@ -76,11 +76,15 @@ public class StateTest { // Test endPhase while (!state.isPhaseOver()) { - if (!state.getCurrentPlayer().canPlay(state)) state.nextPlayer(); + if (!state.getCurrentPlayer().canPlay(state)) { + System.out.println("Player " + state.getCurrentPlayerID() + " can't play"); + state.nextPlayer(); + } state.getCurrentPlayer().doRandomMove(state); } state.scorePhase(); - System.out.println(state); + System.out.println(state.getCurrentPhase()); + System.out.println(state.scoreString()); }