game: Cleaned up code

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

View File

@ -21,6 +21,8 @@ import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment; import javafx.scene.text.TextAlignment;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.util.Objects;
public class Game extends Application { public class Game extends Application {
// region Variables // region Variables
@ -46,7 +48,7 @@ public class Game extends Application {
* *
* @param stage the primary stage for this application, onto which * @param stage the primary stage for this application, onto which
* the application scene can be set. * the application scene can be set.
* @throws Exception * @throws Exception if there is an error
*/ */
@Override @Override
public void start(Stage stage) throws Exception { public void start(Stage stage) throws Exception {
@ -64,12 +66,12 @@ public class Game extends Application {
// Set some app properties // Set some app properties
stage.setScene(scene); stage.setScene(scene);
stage.setTitle("Blue Lagoon"); stage.setTitle("Blue Lagoon");
stage.getIcons().add(new javafx.scene.image.Image(Game.class.getResourceAsStream("favicon.png"))); stage.getIcons().add(new javafx.scene.image.Image(Objects.requireNonNull(
Game.class.getResourceAsStream("favicon.png"))));
stage.setResizable(false); stage.setResizable(false);
stage.show(); stage.show();
// Create a new game // Create a new game
newGame(2); newGame(2);
} }
@ -91,16 +93,14 @@ public class Game extends Application {
} }
// Add additional players as needed // Add additional players as needed
switch (numPlayers){ switch (numPlayers) {
case 3: case 3 -> currentGame.addPlayer();
currentGame.addPlayer(); case 4 -> {
break;
case 4:
currentGame.addPlayer(); currentGame.addPlayer();
currentGame.addPlayer(); currentGame.addPlayer();
break; }
default: default -> {
break; }
} }
if (AI >= numPlayers){ if (AI >= numPlayers){
@ -164,11 +164,11 @@ public class Game extends Application {
// If the move was a stone, send a message about it // If the move was a stone, send a message about it
Coord lastMove = selectedTile; Coord lastMove = selectedTile;
String message = ""; StringBuilder message = new StringBuilder();
if (currentGame.isStone(lastMove)){ if (currentGame.isStone(lastMove)){
for (Resource resource : currentGame.getResources()) { for (Resource resource : currentGame.getResources()) {
if (resource.getCoord().equals(lastMove) ) { if (resource.getCoord().equals(lastMove) ) {
message = "Player " + currentGame.getCurrentPlayerID() +" picked up a " + resource.getTypeString().toLowerCase(); message = new StringBuilder("Player " + currentGame.getCurrentPlayerID() + " picked up a " + resource.getTypeString().toLowerCase());
} }
} }
} }
@ -177,11 +177,11 @@ public class Game extends Application {
currentGame.nextPlayer(); currentGame.nextPlayer();
selectedTile = new Coord(-1,-1); selectedTile = new Coord(-1,-1);
while (currentGame.getCurrentPlayer().isAI()) { while (currentGame.getCurrentPlayer().isAI()) {
message += "\n"+ doAIMove(); message.append("\n").append(doAIMove());
} }
// Send the message to the user // Send the message to the user
sendMessage(message); sendMessage(message.toString());
} }
else { else {
sendMessage("Invalid move",true); sendMessage("Invalid move",true);
@ -194,9 +194,9 @@ public class Game extends Application {
if (currentGame.getCurrentPhase() == 'E') { if (currentGame.getCurrentPhase() == 'E') {
currentGame.cleanBoard(); currentGame.cleanBoard();
currentGame.distributeResources(); currentGame.distributeResources();
String AI = ""; StringBuilder AI = new StringBuilder();
while (currentGame.getCurrentPlayer().isAI()) { while (currentGame.getCurrentPlayer().isAI()) {
AI += "\n"+ doAIMove(); AI.append("\n").append(doAIMove());
} }
sendMessage("Next phase!\n" + AI); sendMessage("Next phase!\n" + AI);
} }
@ -242,11 +242,11 @@ public class Game extends Application {
currentGame.cleanBoard(); currentGame.cleanBoard();
currentGame.distributeResources(); currentGame.distributeResources();
currentGame.nextPhase(); currentGame.nextPhase();
String AI = "Next phase!\n"; StringBuilder AI = new StringBuilder("Next phase!\n");
while (currentGame.getCurrentPlayer().isAI()) { while (currentGame.getCurrentPlayer().isAI()) {
AI += "\n"+ doAIMove(); AI.append("\n").append(doAIMove());
} }
message = AI; message = AI.toString();
} }
else { else {
message = "Game over!"; message = "Game over!";
@ -368,22 +368,18 @@ public class Game extends Application {
// Getting the two mode of current state either Exploration // Getting the two mode of current state either Exploration
// or settler // or settler
char currentPhaseChar = currentGame.getCurrentPhase(); char currentPhaseChar = currentGame.getCurrentPhase();
String currentPhase = ""; String currentPhase = switch (currentPhaseChar) {
switch (currentPhaseChar) { case 'E' -> "Exploration";
case 'E': case 'S' -> "Settlement";
currentPhase = "Exploration"; default -> "";
break; };
case 'S':
currentPhase = "Settlement";
break;
}
// Making the Current State Statement text on the window // Making the Current State Statement text on the window
Text currentStateText = new Text(); Text currentStateText = new Text();
currentStateText.setText("The current player to move is player " + currentStateText.setText("The current player to move is player " +
playerId + "\nCurrent Phase: " + currentPhase); playerId + "\nCurrent Phase: " + currentPhase);
currentStateText.setFont(Font.font("Sans Serif", FontWeight.BOLD, 20)); currentStateText.setFont(Font.font("Sans Serif", FontWeight.BOLD, 20));
currentStateText.setX(WINDOW_WIDTH / 2 + (WINDOW_WIDTH/5) - 175); currentStateText.setX((double) WINDOW_WIDTH / 2 + ((double) WINDOW_WIDTH /5) - 175);
currentStateText.setY(25); currentStateText.setY(25);
currentStateText.setTextAlignment(TextAlignment.CENTER); currentStateText.setTextAlignment(TextAlignment.CENTER);
root.getChildren().add(currentStateText); root.getChildren().add(currentStateText);
@ -401,13 +397,13 @@ public class Game extends Application {
addStoneTileToBoard(viewerGrid, tileSize, stoneCircle.toString(), Color.GRAY); addStoneTileToBoard(viewerGrid, tileSize, stoneCircle.toString(), Color.GRAY);
} }
String playerData = "Scores:"; StringBuilder playerData = new StringBuilder("Scores:");
// For each player add their settlements and roads // For each player add their settlements and roads
for (int i = 0; i < currentGame.getNumPlayers(); i++){ for (int i = 0; i < currentGame.getNumPlayers(); i++){
Player currentPlayer = currentGame.getPlayer(i); Player currentPlayer = currentGame.getPlayer(i);
// Add the player's score to the playerData string // Add the player's score to the playerData string
if (currentPlayer.isAI()) playerData += "\nAI " + i + ": " + currentPlayer.getScore(); if (currentPlayer.isAI()) playerData.append("\nAI ").append(i).append(": ").append(currentPlayer.getScore());
else playerData += "\nPlayer " + i + ": " + currentPlayer.getScore(); else playerData.append("\nPlayer ").append(i).append(": ").append(currentPlayer.getScore());
// Settler tile generator // Settler tile generator
for (Coord c: currentPlayer.getSettlers()){ for (Coord c: currentPlayer.getSettlers()){
@ -416,7 +412,7 @@ public class Game extends Application {
// Label generator // Label generator
if (currentPlayer.isAI()) if (currentPlayer.isAI())
addLabelToTile(viewerGrid, tileSize, c.toString(), Color.BLACK, "AI "+i); addLabelToTile(viewerGrid, tileSize, c.toString(), Color.GREEN, "AI "+i);
else else
addLabelToTile(viewerGrid, tileSize, c.toString(), Color.BLACK, "P "+i); addLabelToTile(viewerGrid, tileSize, c.toString(), Color.BLACK, "P "+i);
} }
@ -434,7 +430,7 @@ public class Game extends Application {
} }
// Adding the player Statement Text to the window // Adding the player Statement Text to the window
Text playerStateText = new Text(); Text playerStateText = new Text();
playerStateText.setText(playerData); playerStateText.setText(playerData.toString());
playerStateText.setFont(Font.font("Sans Serif", FontWeight.BOLD, 25)); playerStateText.setFont(Font.font("Sans Serif", FontWeight.BOLD, 25));
playerStateText.setX(0); playerStateText.setX(0);
playerStateText.setY(100); playerStateText.setY(100);
@ -442,8 +438,8 @@ public class Game extends Application {
root.getChildren().add(playerStateText); root.getChildren().add(playerStateText);
// Add the grid to the root // Add the grid to the root
viewerGrid.relocate((WINDOW_WIDTH/2-viewerGrid.getPrefWidth()/2) + (WINDOW_WIDTH/5), viewerGrid.relocate(((double) WINDOW_WIDTH /2-viewerGrid.getPrefWidth()/2) + ((double) WINDOW_WIDTH /5),
((WINDOW_HEIGHT+100)/2-viewerGrid.getPrefHeight()/2)); ((double) (WINDOW_HEIGHT + 100) /2-viewerGrid.getPrefHeight()/2));
root.getChildren().add(viewerGrid); root.getChildren().add(viewerGrid);
// Add selected tile // Add selected tile
@ -492,25 +488,13 @@ public class Game extends Application {
mapSelector.setOnAction(new EventHandler<ActionEvent>() { mapSelector.setOnAction(new EventHandler<ActionEvent>() {
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
switch (mapSelector.getValue().toString()){ switch (mapSelector.getValue().toString()) {
case "Default": case "Default" -> game_selected = 0;
game_selected = 0; case "Wheels" -> game_selected = 1;
break; case "Face" -> game_selected = 2;
case "Wheels": case "Sides" -> game_selected = 3;
game_selected = 1; case "Space Invaders" -> game_selected = 4;
break; default -> game_selected = 0;
case "Face":
game_selected = 2;
break;
case "Sides":
game_selected = 3;
break;
case "Space Invaders":
game_selected = 4;
break;
default:
game_selected = 0;
break;
} }
} }
}); });
@ -576,7 +560,7 @@ public class Game extends Application {
Hexagon hex = new Hexagon(tileSize, color); Hexagon hex = new Hexagon(tileSize, color);
// if the row is even, translate the tile to the right by tileSize/2 // if the row is even, translate the tile to the right by tileSize/2
if (Integer.parseInt(coords[0]) % 2 == 0) hex.setTranslateX(tileSize/2); if (Integer.parseInt(coords[0]) % 2 == 0) hex.setTranslateX((double) tileSize /2);
// Translate the whole tile's Y axis downwards so they connect and there's no gap // Translate the whole tile's Y axis downwards so they connect and there's no gap
hex.setTranslateY(Integer.parseInt(coords[0]) * -0.25 * tileSize); hex.setTranslateY(Integer.parseInt(coords[0]) * -0.25 * tileSize);
@ -606,7 +590,7 @@ public class Game extends Application {
Hexagon hex = new Hexagon(tileSize2, color); Hexagon hex = new Hexagon(tileSize2, color);
// if the row is even, translate the tile to the right by tileSize/2 // if the row is even, translate the tile to the right by tileSize/2
if (Integer.parseInt(coords[0]) % 2 == 0) hex.setTranslateX(tileSize/2); if (Integer.parseInt(coords[0]) % 2 == 0) hex.setTranslateX((double) tileSize /2);
// Translate the whole tile's Y axis downwards so they connect and there's no gap // Translate the whole tile's Y axis downwards so they connect and there's no gap
hex.setTranslateY(Integer.parseInt(coords[0]) * -0.25 * tileSize); hex.setTranslateY(Integer.parseInt(coords[0]) * -0.25 * tileSize);
@ -637,7 +621,7 @@ public class Game extends Application {
newLabel.setFont(Font.font("Sans Serif", 12)); newLabel.setFont(Font.font("Sans Serif", 12));
// Following the tile's pos format // Following the tile's pos format
if (Integer.parseInt(coords[0]) % 2 == 0) newLabel.setTranslateX(tileSize/2); if (Integer.parseInt(coords[0]) % 2 == 0) newLabel.setTranslateX((double) tileSize /2);
newLabel.setTranslateY(Integer.parseInt(coords[0]) * -0.25 * tileSize); newLabel.setTranslateY(Integer.parseInt(coords[0]) * -0.25 * tileSize);
// Making the label center // Making the label center
@ -677,7 +661,7 @@ public class Game extends Application {
// Adjust the label's position // Adjust the label's position
if (Integer.parseInt(coords[0]) % 2 == 0){ if (Integer.parseInt(coords[0]) % 2 == 0){
newLabel.setTranslateX(tileSize/2 + width); newLabel.setTranslateX((double) tileSize /2 + width);
} else } else
{ {
newLabel.setTranslateX(width); newLabel.setTranslateX(width);
@ -696,7 +680,7 @@ public class Game extends Application {
* A hexagon shape with a given side length and fill. * A hexagon shape with a given side length and fill.
* Used to create the tiles on the board. * Used to create the tiles on the board.
*/ */
class Hexagon extends Polygon { static class Hexagon extends Polygon {
/** /**
* Create a hexagon with a given side length and fill. * Create a hexagon with a given side length and fill.
* @param side double The length of a side of the hexagon. * @param side double The length of a side of the hexagon.