Task 5 done hopefully

Signed-off-by: Immanuel Alvaro Bhirawa <u7280427@anu.edu.au>
This commit is contained in:
Immanuel Alvaro Bhirawa 2023-04-11 01:10:55 +10:00
parent 809701c5f6
commit 37b09c5576

View File

@ -22,7 +22,8 @@ import javafx.scene.text.FontWeight;
import javafx.scene.text.Text; import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment; import javafx.scene.text.TextAlignment;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.scene.shape.Polygon;
import javafx.scene.paint.Paint;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -126,36 +127,55 @@ public class Viewer extends Application {
* *
* a 13 2; c 0 E; i 6 0,0 0,1 0,2 0,3 1,0 1,1 1,2 1,3 1,4 2,0 2,1; i 6 0,5 0,6 0,7 1,6 1,7 * a 13 2; c 0 E; i 6 0,0 0,1 0,2 0,3 1,0 1,1 1,2 1,3 1,4 2,0 2,1; i 6 0,5 0,6 0,7 1,6 1,7
*/ */
void displayState(String stateString) { void displayState(String stateString) {
// When refreshing, it clears the whole thing and update it // When refreshing, it clears the whole thing and update it
root.getChildren().clear(); root.getChildren().clear();
root.getChildren().add(controls); root.getChildren().add(controls);
// Intizialiation of the grid
GridPane viewerGrid = new GridPane(); GridPane viewerGrid = new GridPane();
String[] parts = stateString.split("; ?"); String[] parts = stateString.split("; ?");
int boardHeight = 0; int boardHeight = 0;
int boardHeightPx = 0; int boardHeightPx = 0;
int tileSize = 0;
for ( String part : parts) { for ( String part : parts) {
String[] parseSplit = part.split(" "); String[] parseSplit = part.split(" ");
String stateCases = parseSplit[0]; String stateCases = parseSplit[0];
// Splitting of the states of the game as per the encoded string
switch (stateCases) { switch (stateCases) {
// Game Arrangement Statement
case "a": case "a":
// Settting up the board height value and the tile size
boardHeight = Integer.parseInt(parseSplit[1]); boardHeight = Integer.parseInt(parseSplit[1]);
boardHeightPx = 575; boardHeightPx = 650;
tileSize = boardHeightPx / boardHeight;
viewerGrid.setPrefWidth(boardHeightPx); viewerGrid.setPrefWidth(boardHeightPx);
viewerGrid.setPrefHeight(boardHeightPx); viewerGrid.setPrefHeight(boardHeightPx);
// row major order // Generating the water tiles ( the background water map )
for(int i = 0; i < boardHeight; i++){ for(int i = 0; i < boardHeight; i++){
for(int j = 0; j < boardHeight; j++){ for(int j = 0; j < boardHeight; j++){
Rectangle boardMap = new Rectangle(boardHeightPx/boardHeight, addBoardTile(viewerGrid, boardHeightPx/boardHeight,
boardHeightPx/boardHeight, Color.DARKBLUE); String.format("%s,%s", i, j), Color.BLUE);
viewerGrid.add(boardMap, i, j);
} }
} }
break; break;
// Current state statement
case "c": case "c":
// Getting the player ID from the string
int playerId = Integer.parseInt(parseSplit[1]); int playerId = Integer.parseInt(parseSplit[1]);
// Getting the two mode of current state either Exploration
// or settler
String currentPhase = parseSplit[2]; String currentPhase = parseSplit[2];
switch (currentPhase) { switch (currentPhase) {
case "E": case "E":
@ -165,6 +185,8 @@ public class Viewer extends Application {
currentPhase = "Settler"; currentPhase = "Settler";
break; break;
} }
// 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 + " in the " + currentPhase + playerId + " in the " + currentPhase +
@ -177,196 +199,236 @@ public class Viewer extends Application {
root.getChildren().add(currentStateText); root.getChildren().add(currentStateText);
currentStateText.setFill(Color.GREEN); currentStateText.setFill(Color.GREEN);
break; break;
// Generating the Island tiles from the Island State Statement
case "i": case "i":
for(int i = 2; i < parseSplit.length; i++){ for(int i = 2; i < parseSplit.length; i++){
Rectangle island = new Rectangle(boardHeightPx/boardHeight, addBoardTile(viewerGrid, tileSize, parseSplit[i], Color.GREEN);
boardHeightPx/boardHeight, Color.GREEN);
String[] coords = parseSplit[i].split(",");
int xCoord = Integer.parseInt(coords[0]);
int yCoord = Integer.parseInt(coords[1]);
viewerGrid.add(island, xCoord, yCoord);
} }
break; break;
// Generating the Stone Tiles from the Stone Statement
case "s": case "s":
for(int i = 1; i < parseSplit.length; i++){ for(int i = 1; i < parseSplit.length; i++){
Rectangle theRock = new Rectangle(boardHeightPx/boardHeight addTileToBoard(viewerGrid, tileSize, parseSplit[i], Color.GRAY);
- 15, boardHeightPx/boardHeight - 15, Color.GRAY);
String[] coords = parseSplit[i].split(",");
theRock.setTranslateX(5);
viewerGrid.add(theRock, Integer.parseInt(coords[0]),
Integer.parseInt(coords[1]));
} }
break; break;
// Generating the Resources Tiles from the Resource Statement
case "r": case "r":
for(int i = 1; i < parseSplit.length; i++){ for(int i = 1; i < parseSplit.length; i++){
switch(parseSplit[i]) { switch(parseSplit[i]) {
// Generating Resource: Coconut Tiles
case "C": case "C":
i++; i++; // To Skip the "C" itself and go to the numbers in the string
while(!parseSplit[i].equals("B")){ while(!parseSplit[i].equals("B")){
String[] coords = parseSplit[i].split(",");
Rectangle coconut = new Rectangle(boardHeightPx/boardHeight // Tile generation
- 15, boardHeightPx/boardHeight - 15, Color.BROWN); addTileToBoard(viewerGrid, tileSize, parseSplit[i], Color.BROWN);
coconut.setTranslateX(5);
Label coconutLabel = new Label("C"); // Label for the tiles generation
coconutLabel.setTextFill(Color.WHITE); addLabelToTile(viewerGrid,tileSize,parseSplit[i], Color.WHITE, "C");
coconutLabel.setFont(Font.font("Sans Serif", 12)); i++; // To continue the iteration for the while loops
coconutLabel.setTranslateX(15);
viewerGrid.add(coconut, Integer.parseInt(coords[0]),
Integer.parseInt(coords[1]));
viewerGrid.add(coconutLabel, Integer.parseInt(coords[0]),
Integer.parseInt(coords[1]));
i++;
} }
i--; i--; // So that i does not go straight to the coords after the letters instead
// i stop at the letter "B"
break; break;
// Generating Resource: Bamboo Tiles
case "B": case "B":
i++; i++; // To Skip the "B" itself and go to the numbers in the string
while(!parseSplit[i].equals("W")){ while(!parseSplit[i].equals("W")){
String[] coords = parseSplit[i].split(",");
Rectangle bamboo = new Rectangle(boardHeightPx/boardHeight // Tile generation
- 15, boardHeightPx/boardHeight - 15, Color.YELLOW); addTileToBoard(viewerGrid, tileSize, parseSplit[i], Color.YELLOW);
bamboo.setTranslateX(5);
Label bambooLabel = new Label("B"); // Label for the tiles generation
bambooLabel.setTextFill(Color.BLACK); addLabelToTile(viewerGrid,tileSize,parseSplit[i], Color.BLACK, "B");
bambooLabel.setFont(Font.font("Sans Serif", 12)); i++; // To continue the iteration for the while loops
bambooLabel.setTranslateX(15);
viewerGrid.add(bamboo, Integer.parseInt(coords[0]),
Integer.parseInt(coords[1]));
viewerGrid.add(bambooLabel, Integer.parseInt(coords[0]),
Integer.parseInt(coords[1]));
i++;
} }
i--; i--; // So that i does not go straight to the coords after the letters instead
// i stop at the letter "W"
break; break;
// Generating Resource: Water tiles
case "W": case "W":
i++; i++; // To Skip the "W" itself and go to the numbers in the string
while(!parseSplit[i].equals("P")){ while(!parseSplit[i].equals("P")){
String[] coords = parseSplit[i].split(","); // Tile Generation
Rectangle water = new Rectangle(boardHeightPx/boardHeight addTileToBoard(viewerGrid,tileSize, parseSplit[i], Color.CYAN);
- 15, boardHeightPx/boardHeight - 15, Color.CYAN);
water.setTranslateX(5); // Label for the tiles generation
Label waterLabel = new Label("W"); addLabelToTile(viewerGrid,tileSize,parseSplit[i], Color.BLACK, "W");
waterLabel.setTextFill(Color.BLACK); i++; // To continue the iteration for the while loops
waterLabel.setFont(Font.font("Sans Serif", 12));
waterLabel.setTranslateX(15);
viewerGrid.add(water, Integer.parseInt(coords[0]),
Integer.parseInt(coords[1]));
viewerGrid.add(waterLabel, Integer.parseInt(coords[0]),
Integer.parseInt(coords[1]));
i++;
} }
i--; i--; // So that i does not go straight to the coords after the letters instead
// i stop at the letter "P"
break; break;
// Generating Resource: Precious Stone tiles
case "P": case "P":
i++; i++; // To Skip the "P" itself and go to the numbers in the string
while(!parseSplit[i].equals("S")){ while(!parseSplit[i].equals("S")){
String[] coords = parseSplit[i].split(",");
Rectangle precious = new Rectangle(boardHeightPx/boardHeight // Tile Generation
- 15, boardHeightPx/boardHeight - 15, Color.GOLD); addTileToBoard(viewerGrid,tileSize,parseSplit[i], Color.GOLD);
precious.setTranslateX(5);
Label preciousLabel = new Label("P"); // Label for the tiles generation
preciousLabel.setTextFill(Color.BLACK); addLabelToTile(viewerGrid, tileSize, parseSplit[i], Color.BLACK, "P");
preciousLabel.setFont(Font.font("Sans Serif", 12)); i++; // To continue the iteration for the while loops
preciousLabel.setTranslateX(15);
viewerGrid.add(precious, Integer.parseInt(coords[0]),
Integer.parseInt(coords[1]));
viewerGrid.add(preciousLabel, Integer.parseInt(coords[0]),
Integer.parseInt(coords[1]));
i++;
} }
i--; i--; // So that i does not go straight to the coords after the letters instead
// i stop at the letter "S"
break; break;
//
// Generating Resource: Statuettes tiles
case "S": case "S":
i++; i++; // To Skip the "P" itself and go to the numbers in the string
while(i < parseSplit.length){ while(i < parseSplit.length){
String[] coords = parseSplit[i].split(",");
Rectangle precious = new Rectangle(boardHeightPx/boardHeight // Tile Generation
- 15, boardHeightPx/boardHeight - 15, Color.SILVER); addTileToBoard(viewerGrid, tileSize, parseSplit[i], Color.SILVER);
precious.setTranslateX(5);
Label preciousLabel = new Label("S"); // Label for the tiles generation
preciousLabel.setTextFill(Color.BLACK); addLabelToTile(viewerGrid, tileSize, parseSplit[i], Color.BLACK, "S");
preciousLabel.setFont(Font.font("Sans Serif", 12)); i++; // To continue the iteration for the while loops
preciousLabel.setTranslateX(15);
viewerGrid.add(precious, Integer.parseInt(coords[0]),
Integer.parseInt(coords[1]));
viewerGrid.add(preciousLabel, Integer.parseInt(coords[0]),
Integer.parseInt(coords[1]));
i++;
} }
break; break;
} }
} }
break; break;
// Player Statement
case "p": case "p":
// A container for the String that is printed at the end
String container = ""; String container = "";
container += "player " + parseSplit[1] + " with score: " + container += "player " + parseSplit[1] + " with score: " +
parseSplit[2] + ", coconuts: " + parseSplit[3] + parseSplit[2] + ", coconuts: " + parseSplit[3] +
", bambooo: " + parseSplit[4] + ", water: " + ", bambooo: " + parseSplit[4] + ", water: " +
parseSplit[5] + ", precious stone: " + parseSplit[6] + parseSplit[5] + ", precious stone: " + parseSplit[6] +
", statuettes: " + parseSplit[7] + ", placed settlers at "; ", statuettes: " + parseSplit[7] + ", placed settlers at ";
// Setller tile generator
int i = 9; int i = 9;
int counter = 0; int counter = 0;
while(!parseSplit[i].equals("T")){ while(!parseSplit[i].equals("T")){
counter++; counter++;
String[] coords = parseSplit[i].split(","); String[] coords = parseSplit[i].split(",");
Rectangle settlers = new Rectangle(boardHeightPx/boardHeight
- 15, boardHeightPx/boardHeight - 15, Color.PINK); // Tile generator
settlers.setTranslateX(5); addTileToBoard(viewerGrid, tileSize, parseSplit[i], Color.PINK);
Label settlerLabel = new Label("Settlers");
settlerLabel.setTextFill(Color.BLACK); // Label generator
settlerLabel.setFont(Font.font("Sans Serif", 9)); addSmallLabelToTile(viewerGrid, tileSize, parseSplit[i], Color.BLACK, "Settlers");
settlerLabel.setTranslateX(5);
viewerGrid.add(settlers, Integer.parseInt(coords[0]), // if there's more than 1 settler placed
Integer.parseInt(coords[1]));
viewerGrid.add(settlerLabel, Integer.parseInt(coords[0]),
Integer.parseInt(coords[1]));
if(counter > 1) container += " and "; if(counter > 1) container += " and ";
container += Integer.parseInt(coords[0]) + "," + container += Integer.parseInt(coords[0]) + "," +
Integer.parseInt(coords[1]); Integer.parseInt(coords[1]);
i++; i++; // To iterate the while loop
} }
i++; i++; // To continue past the "T" keyword
// Village tile generator
container += ", placed villages at "; container += ", placed villages at ";
int counterT = 0; int counterT = 0;
while(i < parseSplit.length){ while(i < parseSplit.length){
counterT++; counterT++;
String[] coords = parseSplit[i].split(","); String[] coords = parseSplit[i].split(",");
Rectangle village = new Rectangle(boardHeightPx/boardHeight
- 15, boardHeightPx/boardHeight - 15, Color.LIGHTGOLDENRODYELLOW); // Tile generator
village.setTranslateX(5); addTileToBoard(viewerGrid, tileSize, parseSplit[i], Color. LIGHTGOLDENRODYELLOW);
Label villageLabel = new Label("Village");
villageLabel.setTextFill(Color.BLACK); // Label generator
villageLabel.setFont(Font.font("Sans Serif", 9)); addSmallLabelToTile(viewerGrid, tileSize, parseSplit[i], Color.BLACK, "Village");
villageLabel.setTranslateX(5);
viewerGrid.add(village, Integer.parseInt(coords[0]), // If there's more than 1 village placed
Integer.parseInt(coords[1]));
viewerGrid.add(villageLabel, Integer.parseInt(coords[0]),
Integer.parseInt(coords[1]));
if(counterT > 1) container += " and "; if(counterT > 1) container += " and ";
container += Integer.parseInt(coords[0]) + "," + container += Integer.parseInt(coords[0]) + "," +
Integer.parseInt(coords[1]); Integer.parseInt(coords[1]);
i++; i++;
} }
// Adding the player Statement Text to the window
Text playerStateText = new Text(); Text playerStateText = new Text();
playerStateText.setText(container); playerStateText.setText(container);
playerStateText.setFont(Font.font("Sans Serif", playerStateText.setFont(Font.font("Sans Serif",
FontWeight.BOLD, 12)); FontWeight.BOLD, 15));
playerStateText.setX(200); playerStateText.setX(125);
playerStateText.setY(645); playerStateText.setY(600);
playerStateText.setTextAlignment(TextAlignment.CENTER); playerStateText.setTextAlignment(TextAlignment.CENTER);
root.getChildren().add(playerStateText); root.getChildren().add(playerStateText);
playerStateText.setFill(Color.BLACK); playerStateText.setFill(Color.BLACK);
// } // }
} }
} }
viewerGrid.relocate((VIEWER_WIDTH/2-viewerGrid.getPrefWidth()/2), (VIEWER_HEIGHT/2-viewerGrid.getPrefHeight()/2));
// Relocate the grid to be more center
viewerGrid.relocate((VIEWER_WIDTH/2-viewerGrid.getPrefWidth()/2), ((VIEWER_HEIGHT+100)/2-viewerGrid.getPrefHeight()/2));
root.getChildren().add(viewerGrid); root.getChildren().add(viewerGrid);
// FIXME Task 5 // FIXME Task 5
} }
void addBoardTile(GridPane board, int tileSize, String coordString, Color color) {
if (coordString.equals("")) return;
String[] coords = coordString.split(",");
Hexagon hex = new Hexagon(tileSize, color);
if (Integer.parseInt(coords[0]) % 2 == 0) hex.setTranslateX(tileSize/2);
hex.setTranslateY(Integer.parseInt(coords[0]) * -0.25 * tileSize);
board.add(hex, Integer.parseInt(coords[1]), Integer.parseInt(coords[0]));
}
void addTileToBoard(GridPane board, int tileSize, String coordString, Color color) {
int tileSize2 = tileSize;
tileSize2 -= 15;
if (coordString.equals("")) return;
String[] coords = coordString.split(",");
Hexagon hex = new Hexagon(tileSize2, color);
if (Integer.parseInt(coords[0]) % 2 == 0) hex.setTranslateX(tileSize/2);
hex.setTranslateY(Integer.parseInt(coords[0]) * -0.25 * tileSize);
hex.setTranslateX(7 + hex.getTranslateX());
board.add(hex, Integer.parseInt(coords[1]), Integer.parseInt(coords[0]));
}
void addLabelToTile(GridPane board, int tileSize, String coordString, Color color, String labelName){
if (coordString.equals("")) return;
String[] coords = coordString.split(",");
Label newLabel = new Label(labelName);
newLabel.setTextFill(color);
newLabel.setFont(Font.font("Sans Serif", 12));
if (Integer.parseInt(coords[0]) % 2 == 0) newLabel.setTranslateX(tileSize/2);
newLabel.setTranslateY(Integer.parseInt(coords[0]) * -0.25 * tileSize);
newLabel.setTranslateX(20 + newLabel.getTranslateX());
board.add(newLabel, Integer.parseInt(coords[1]), Integer.parseInt(coords[0]));
}
void addSmallLabelToTile(GridPane board, int tileSize, String coordString, Color color, String labelName){
if (coordString.equals("")) return;
String[] coords = coordString.split(",");
Label newLabel = new Label(labelName);
newLabel.setTextFill(color);
newLabel.setFont(Font.font("Sans Serif", 9));
if (Integer.parseInt(coords[0]) % 2 == 0) newLabel.setTranslateX(tileSize/2);
newLabel.setTranslateY(Integer.parseInt(coords[0]) * -0.25 * tileSize);
newLabel.setTranslateX(9 + newLabel.getTranslateX());
board.add(newLabel, Integer.parseInt(coords[1]), Integer.parseInt(coords[0]));
}
class Hexagon extends Polygon {
public Hexagon(double side, Paint fill) {
super(
0.0, side / 2.0,
side / 2.0, side / 4.0,
side / 2.0, -side / 4.0,
0.0, -side / 2.0,
-side / 2.0, -side / 4.0,
-side / 2.0, side / 4.0
);
this.setFill(fill);
}
}
/** /**
* Create a basic text field for input and a refresh button. * Create a basic text field for input and a refresh button.