player: Cleaned up code

This commit is contained in:
Nathan Woodburn 2023-05-02 14:28:03 +10:00
parent a13e93c339
commit e52d16d01a
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1

View File

@ -77,20 +77,14 @@ public class Player {
* @return int number of the resource the player has * @return int number of the resource the player has
*/ */
public int getNumResource(char resourceType) { public int getNumResource(char resourceType) {
switch (resourceType) { return switch (resourceType) {
case 'C': case 'C' -> numCoconuts;
return numCoconuts; case 'B' -> numBamboo;
case 'B': case 'W' -> numWater;
return numBamboo; case 'P' -> numPreciousStones;
case 'W': case 'S' -> numStatuette;
return numWater; default -> 0;
case 'P': };
return numPreciousStones;
case 'S':
return numStatuette;
default:
return 0;
}
} }
/** /**
@ -100,21 +94,11 @@ public class Player {
*/ */
public void addResource(int numResource, char resourceType) { public void addResource(int numResource, char resourceType) {
switch (resourceType) { switch (resourceType) {
case 'C': case 'C' -> numCoconuts += numResource;
numCoconuts += numResource; case 'B' -> numBamboo += numResource;
break; case 'W' -> numWater += numResource;
case 'B': case 'P' -> numPreciousStones += numResource;
numBamboo += numResource; case 'S' -> numStatuette += numResource;
break;
case 'W':
numWater += numResource;
break;
case 'P':
numPreciousStones += numResource;
break;
case 'S':
numStatuette += numResource;
break;
} }
} }
@ -153,9 +137,7 @@ public class Player {
*/ */
public void addSettler(Coord coord) { public void addSettler(Coord coord) {
Coord[] newSettlers = new Coord[settlers.length + 1]; Coord[] newSettlers = new Coord[settlers.length + 1];
for (int i = 0; i < settlers.length; i++) { System.arraycopy(settlers, 0, newSettlers, 0, settlers.length);
newSettlers[i] = settlers[i];
}
newSettlers[settlers.length] = coord; newSettlers[settlers.length] = coord;
settlers = newSettlers; settlers = newSettlers;
lastMove = coord; lastMove = coord;
@ -172,9 +154,7 @@ public class Player {
} }
Coord[] newVillages = new Coord[villages.length + 1]; Coord[] newVillages = new Coord[villages.length + 1];
for (int i = 0; i < villages.length; i++) { System.arraycopy(villages, 0, newVillages, 0, villages.length);
newVillages[i] = villages[i];
}
newVillages[villages.length] = coord; newVillages[villages.length] = coord;
villages = newVillages; villages = newVillages;
lastMove = coord; lastMove = coord;
@ -193,9 +173,9 @@ public class Player {
public void removeVillage(Coord coord) { public void removeVillage(Coord coord) {
Coord[] newVillages = new Coord[villages.length - 1]; Coord[] newVillages = new Coord[villages.length - 1];
int j = 0; int j = 0;
for (int i = 0; i < villages.length; i++) { for (Coord village : villages) {
if (villages[i] != coord) { if (village != coord) {
newVillages[j] = villages[i]; newVillages[j] = village;
j++; j++;
} }
} }
@ -209,18 +189,14 @@ public class Player {
*/ */
public Coord[] getPieces() { public Coord[] getPieces() {
Coord[] pieces = new Coord[settlers.length + villages.length]; Coord[] pieces = new Coord[settlers.length + villages.length];
for (int i = 0; i < settlers.length; i++) { System.arraycopy(settlers, 0, pieces, 0, settlers.length);
pieces[i] = settlers[i]; System.arraycopy(villages, 0, pieces, settlers.length, villages.length);
}
for (int i = 0; i < villages.length; i++) {
pieces[settlers.length + i] = villages[i];
}
return pieces; return pieces;
} }
/** /**
* Get number of pieces on island * Get number of pieces on island
* @param island Island island to check * @param island Island to check
* @return int number of pieces on island * @return int number of pieces on island
*/ */
public int getNumPiecesOnIsland(Island island) { public int getNumPiecesOnIsland(Island island) {
@ -376,9 +352,9 @@ public class Player {
if (i == randomMove) { if (i == randomMove) {
char pieceType = move.charAt(0); char pieceType = move.charAt(0);
String coordStr = move.substring(2); String coordStr = move.substring(2);
int x = Integer.parseInt(coordStr.split(",")[0]); int y = Integer.parseInt(coordStr.split(",")[0]);
int y = Integer.parseInt(coordStr.split(",")[1]); int x = Integer.parseInt(coordStr.split(",")[1]);
Coord coord = new Coord(x, y); Coord coord = new Coord(y, x);
lastMove = coord; lastMove = coord;
state.placePiece(coord, pieceType); state.placePiece(coord, pieceType);
state.nextPlayer(); state.nextPlayer();
@ -427,9 +403,9 @@ public class Player {
String bestMove = createAIMove(state); String bestMove = createAIMove(state);
char pieceType = bestMove.charAt(0); char pieceType = bestMove.charAt(0);
String coordStr = bestMove.substring(2); String coordStr = bestMove.substring(2);
int x = Integer.parseInt(coordStr.split(",")[0]); int y = Integer.parseInt(coordStr.split(",")[0]);
int y = Integer.parseInt(coordStr.split(",")[1]); int x = Integer.parseInt(coordStr.split(",")[1]);
Coord coord = new Coord(x, y); Coord coord = new Coord(y, x);
lastMove = coord; lastMove = coord;
state.placePiece(coord, pieceType); state.placePiece(coord, pieceType);
state.nextPlayer(); state.nextPlayer();
@ -470,7 +446,6 @@ public class Player {
} }
// Check if adding this piece will make player have the most pieces on the island // Check if adding this piece will make player have the most pieces on the island
int ties = 0; int ties = 0;
int wins = 0;
int loses = 0; int loses = 0;
int myPieces = this.getNumPiecesOnIsland(island); int myPieces = this.getNumPiecesOnIsland(island);
for (int i = 0; i < state.getNumPlayers(); i++) { for (int i = 0; i < state.getNumPlayers(); i++) {
@ -480,9 +455,6 @@ public class Player {
int otherPlayerPieces = state.getPlayer(i).getNumPiecesOnIsland(island); int otherPlayerPieces = state.getPlayer(i).getNumPiecesOnIsland(island);
if (otherPlayerPieces > myPieces+1) { if (otherPlayerPieces > myPieces+1) {
loses++; loses++;
} else
if (otherPlayerPieces == myPieces) {
wins++;
} else if (otherPlayerPieces == myPieces + 1) { } else if (otherPlayerPieces == myPieces + 1) {
ties++; ties++;
} }
@ -568,7 +540,7 @@ public class Player {
*/ */
@Override @Override
public String toString() { public String toString() {
String str = "p " + playerID + " " + score + " " + numCoconuts + " " + numBamboo + " " + numWater + " " + numPreciousStones + " " + numStatuette + " S"; StringBuilder str = new StringBuilder("p " + playerID + " " + score + " " + numCoconuts + " " + numBamboo + " " + numWater + " " + numPreciousStones + " " + numStatuette + " S");
// Get the coords of the player's pieces in row major order // Get the coords of the player's pieces in row major order
@ -620,13 +592,13 @@ public class Player {
for (Coord coord : settlersCoords) { for (Coord coord : settlersCoords) {
str += " " + coord.toString(); str.append(" ").append(coord.toString());
} }
str += " T"; str.append(" T");
for (Coord coord : villagesCoords) { for (Coord coord : villagesCoords) {
str += " " + coord.toString(); str.append(" ").append(coord.toString());
} }
return str; return str.toString();
} }
} }