task 9 and 10: Added using state class

This commit is contained in:
Nathan Woodburn 2023-04-24 17:36:20 +10:00
parent 8ed19d75bd
commit 899497d037
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
5 changed files with 107 additions and 12 deletions

View File

@ -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();
}

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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
}

View File

@ -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());
}