Task 5 progress 4/6 just need to finish [unclaimedResourceStatement] and [player statement]

NOTE: There's a bug with the water board and stoneCircles tile generator. Will fix later hopefully lol
This commit is contained in:
Immanuel Alvaro Bhirawa 2023-03-27 10:36:53 +11:00
parent 08c2eee299
commit 8f204e274c

View File

@ -3,14 +3,18 @@ package comp1110.ass2.gui;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
@ -19,6 +23,9 @@ import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
import javax.swing.*;
import java.awt.*;
public class Viewer extends Application {
private static final int VIEWER_WIDTH = 1200;
@ -48,7 +55,7 @@ public class Viewer extends Application {
*
* gameState = gameArrangementStatement, " ", currentStateStatement, {" ", islandStatement}, " ", stonesStatement, " ", unclaimedResourcesAndStatuettesStatement, {" ", playerStatement}
*
* e.g. "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 1,8 2,6 2,7 2,8 3,7 3,8; i 6 7,12 8,11 9,11 9,12 10,10 10,11 11,10 11,11 11,12 12,10 12,11; i 8 0,9 0,10 0,11 1,10 1,11 1,12 2,10 2,11 3,10 3,11 3,12 4,10 4,11 5,11 5,12; i 8 4,0 5,0 5,1 6,0 6,1 7,0 7,1 7,2 8,0 8,1 8,2 9,0 9,1 9,2; i 8 10,3 10,4 11,0 11,1 11,2 11,3 11,4 11,5 12,0 12,1 12,2 12,3 12,4 12,5; i 10 3,3 3,4 3,5 4,2 4,3 4,4 4,5 5,3 5,4 5,5 5,6 6,3 6,4 6,5 6,6 7,4 7,5 7,6 8,4 8,5; i 10 5,8 5,9 6,8 6,9 7,8 7,9 7,10 8,7 8,8 8,9 9,7 9,8 9,9 10,6 10,7 10,8 11,7 11,8 12,7 12,8; s 0,0 0,5 0,9 1,4 1,8 1,12 2,1 3,5 3,7 3,10 3,12 4,0 4,2 5,9 5,11 6,3 6,6 7,0 7,8 7,12 8,2 8,5 9,0 9,9 10,3 10,6 10,10 11,0 11,5 12,2 12,8 12,11; r C B W P S; p 0 0 0 0 0 0 0 S T; p 1 0 0 0 0 0 0 S T;"
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 1,8 2,6 2,7 2,8 3,7 3,8; i 6 7,12 8,11 9,11 9,12 10,10 10,11 11,10 11,11 11,12 12,10 12,11; i 8 0,9 0,10 0,11 1,10 1,11 1,12 2,10 2,11 3,10 3,11 3,12 4,10 4,11 5,11 5,12; i 8 4,0 5,0 5,1 6,0 6,1 7,0 7,1 7,2 8,0 8,1 8,2 9,0 9,1 9,2; i 8 10,3 10,4 11,0 11,1 11,2 11,3 11,4 11,5 12,0 12,1 12,2 12,3 12,4 12,5; i 10 3,3 3,4 3,5 4,2 4,3 4,4 4,5 5,3 5,4 5,5 5,6 6,3 6,4 6,5 6,6 7,4 7,5 7,6 8,4 8,5; i 10 5,8 5,9 6,8 6,9 7,8 7,9 7,10 8,7 8,8 8,9 9,7 9,8 9,9 10,6 10,7 10,8 11,7 11,8 12,7 12,8; s 0,0 0,5 0,9 1,4 1,8 1,12 2,1 3,5 3,7 3,10 3,12 4,0 4,2 5,9 5,11 6,3 6,6 7,0 7,8 7,12 8,2 8,5 9,0 9,9 10,3 10,6 10,10 11,0 11,5 12,2 12,8 12,11; r C B W P S; p 0 0 0 0 0 0 0 S T; p 1 0 0 0 0 0 0 S T;
*
* ^ The initial board. Two players, player 0 to start.
*
@ -128,6 +135,317 @@ public class Viewer extends Application {
// to each other
// StackPane rootGroup = new StackPane();
// a 13 2; c 0 E; i 6 0,0 0,1 0,2 0,3 1,0 1,
// String gameArrangementState = parts[i];
// String[] gameArrangementStateParts = gameArrangementState.split(" ");
// int boardHeight = Integer.parseInt(gameArrangementStateParts[1]);
// int boardHeightPx = boardHeight * 45;
GridPane viewerGrid = new GridPane();
String[] parts = stateString.split("; ?");
int boardHeight = 0;
int boardHeightPx = 0;
for ( String part : parts) {
String[] parseSplit = part.split(" ");
String stateCases = parseSplit[0];
switch (stateCases) {
case "a":
boardHeight = Integer.parseInt(parseSplit[1]);
boardHeightPx = boardHeight * 45;
viewerGrid.setPrefWidth(boardHeightPx);
viewerGrid.setPrefHeight(boardHeightPx);
// row major order
for(int i = 0; i < boardHeight; i++){
for(int j = 0; j < boardHeight; j++){
Rectangle boardMap = new Rectangle(boardHeightPx/boardHeight,
boardHeightPx/boardHeight, Color.DARKBLUE);
viewerGrid.add(boardMap, i, j);
Label boardLabel = new Label(i + "," + j);
boardLabel.setFont(Font.font("Sans Serif", 12));
boardLabel.setTextFill(Color.WHITE);
viewerGrid.add(boardLabel, i, j);
}
}
break;
case "c":
int playerId = Integer.parseInt(parseSplit[1]);
String currentPhase = parseSplit[2];
switch (currentPhase) {
case "E":
currentPhase = "Exploration";
break;
case "S":
currentPhase = "Settler";
break;
}
Text currentStateText = new Text();
currentStateText.setText("The current player to move is player " +
playerId + " in the " + currentPhase +
" phase");
currentStateText.setFont(Font.font("Sans Serif",
FontWeight.BOLD, 20));
currentStateText.setX(VIEWER_WIDTH / boardHeight * 3.5);
currentStateText.setY(50);
currentStateText.setTextAlignment(TextAlignment.CENTER);
root.getChildren().add(currentStateText);
currentStateText.setFill(Color.GREEN);
break;
case "i":
for(int i = 2; i < parseSplit.length; i++){
Rectangle island = new Rectangle(boardHeightPx/boardHeight,
boardHeightPx/boardHeight, Color.GREEN);
String[] coords = parseSplit[i].split(",");
int xCoord = Integer.parseInt(coords[0]);
int yCoord = Integer.parseInt(coords[1]);
Label islandLabel = new Label(xCoord + "," + yCoord +
"\n b " + parseSplit[1]);
islandLabel.setTextFill(Color.GREENYELLOW);
islandLabel.setFont(Font.font("Sans Serif", 12));
viewerGrid.add(island, xCoord, yCoord);
viewerGrid.add(islandLabel, xCoord, yCoord);
// root.getChildren().add(viewerGrid);
}
break;
case "s":
for(int i = 1; i < parseSplit.length; i++){
Rectangle theRock = new Rectangle(boardHeightPx/boardHeight,
boardHeightPx/boardHeight, Color.GRAY);
String[] coords = parseSplit[i].split(",");
viewerGrid.add(theRock, Integer.parseInt(coords[0]),
Integer.parseInt(coords[1]));
}
break;
case "r":
// parts[i].matches(" c \\d [ES]")
//String[] parseSplit = part.split(" ");
break;
//r C 1,1 B 1,2 W P 1,4 S;
}
}
viewerGrid.relocate((VIEWER_WIDTH/2-viewerGrid.getPrefWidth()/2), (VIEWER_HEIGHT/2-viewerGrid.getPrefHeight()/2));
root.getChildren().add(viewerGrid);
// String currentArrangementState = parts[i];
//// String[] currentArrangementStateParts = currentArrangementState.split(" ");
//// int playerId = Integer.parseInt(currentArrangementStateParts[2]);
//// String currentArrangementPhaseParts = currentArrangementStateParts[3];
//// String currentArrangementActualPhase = "";
//// switch (currentArrangementPhaseParts) {
//// case "E": {
//// currentArrangementActualPhase = "Exploration";
//// break;
//// }
//// case "S": {
//// currentArrangementActualPhase = "Settler";
//// break;
//// }
//// }
////
//// Text currentStateText = new Text();
// currentStateText.setText("The current player to move is player " +
// playerId + " in the " + currentArrangementActualPhase +
// " phase");
// currentStateText.setFont(Font.font("Sans Serif",
// FontWeight.BOLD, 20));
// currentStateText.setX(VIEWER_WIDTH / boardHeight * 3.5);
// currentStateText.setY(50);
// currentStateText.setTextAlignment(TextAlignment.CENTER);
// root.getChildren().add(currentStateText);
// currentStateText.setFill(Color.GREEN);
//
// String[] parts = stateString.split(";");
// String gameArrangementState = parts[0];
// String[] gameArrangementStateParts = gameArrangementState.split(" ");
// int boardHeight = Integer.parseInt(gameArrangementStateParts[1]);
// int boardHeightPx = boardHeight * 45;
// int tilePxSize = (boardHeightPx-45) / boardHeight;
//
// Rectangle board = new Rectangle();
// board.setWidth(boardHeightPx); // width (assume for right now the map is a square
// board.setHeight(boardHeightPx); // height
// board.setX((VIEWER_WIDTH / 2) - board.getWidth() / 2); // starting x loc
// board.setY((VIEWER_HEIGHT / 2) - board.getHeight() / 2); // starting y loc
// board.setFill(Color.DARKBLUE);
// root.getChildren().add(board);
//
// for (int i = 0; i < parts.length; i++) {
//// if(parts[i].matches("a \\d{1,2} \\d")){
//// Rectangle board = new Rectangle();
//// board.setWidth(boardHeightPx); // width (assume for right now the map is a square
//// board.setHeight(boardHeightPx); // height
//// board.setX((VIEWER_WIDTH / 2) - board.getWidth()/2); // starting x loc
//// board.setY((VIEWER_HEIGHT / 2) - board.getHeight()/2); // starting y loc
//// board.setFill(Color.DARKBLUE);
//// root.getChildren().add(board);
//// }
//
// if (parts[i].matches(" c \\d [ES]")) {
// String currentArrangementState = parts[i];
// String[] currentArrangementStateParts = currentArrangementState.split(" ");
// int playerId = Integer.parseInt(currentArrangementStateParts[2]);
// String currentArrangementPhaseParts = currentArrangementStateParts[3];
// String currentArrangementActualPhase = "";
// switch (currentArrangementPhaseParts) {
// case "E": {
// currentArrangementActualPhase = "Exploration";
// break;
// }
// case "S": {
// currentArrangementActualPhase = "Settler";
// break;
// }
// }
//
// Text currentStateText = new Text();
// currentStateText.setText("The current player to move is player " +
// playerId + " in the " + currentArrangementActualPhase +
// " phase");
// currentStateText.setFont(Font.font("Sans Serif",
// FontWeight.BOLD, 20));
// currentStateText.setX(VIEWER_WIDTH / boardHeight * 3.5);
// currentStateText.setY(50);
// currentStateText.setTextAlignment(TextAlignment.CENTER);
// root.getChildren().add(currentStateText);
// currentStateText.setFill(Color.GREEN);
// }
//
// // i 6 0,0 0,1 0,2 0,3 1,0 1,1 1,2 1,3 1,4 2,0 2,1;
// //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
//// 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 1,8 2,6 2,7 2,8 3,7 3,8
//
// if (parts[i].matches(" i \\d{1,2} (\\d{1,2},\\d{1,2} )*\\d{1,2},\\d{1,2}")) {
// GridPane islandGrid = new GridPane();
// String[] islandString = parts[i].split(" ");
// for(int j = 3; j < islandString.length; j++){
// Rectangle islandTiles = new Rectangle(tilePxSize, tilePxSize, Color.GREEN);
// String[] coords = islandString[j].split(",");
// islandGrid.add(islandTiles, Integer.parseInt(coords[0]),
// Integer.parseInt(coords[1]));
// islandGrid.setTranslateX(board.getX());
// islandGrid.setTranslateY(board.getY());
// }
// root.getChildren().add(islandGrid);
// }
// }
// String[] islandString = parts[i].split(" ");
// for (int j = i; j < parts.length; j++) {
// String[] islandString = parts[i].split(" ");
// Text bonusPointIslandText = new Text();
// bonusPointIslandText.setText("The bonus point for this island is " +
// islandString[2]);
// bonusPointIslandText.setFont(Font.font("Sans Serif",
// FontWeight.BOLD, 10));
// bonusPointIslandText.setX(50);
// bonusPointIslandText.setY(150);
// bonusPointIslandText.setTextAlignment(TextAlignment.CENTER);
// root.getChildren().add(bonusPointIslandText);
// bonusPointIslandText.setFill(Color.RED);
//// System.out.println("Parts[i] : " + parts[j]);
// for (int k = 3; k < islandString.length; k++) {
// String[] islandCoords = islandString[k].split(",");
// System.out.println("IslandString: " + islandString[k]);
// Rectangle island = new Rectangle(
// Integer.parseInt(islandCoords[0]) * boardHeight,
// Integer.parseInt(islandCoords[1]) * boardHeight,
// (boardHeightPx - 100) / boardHeight,
// (boardHeightPx - 100) / boardHeight
// );
//// island.setX((VIEWER_WIDTH / 2) - (board.getWidth()/2) + 50);
//// island.setY((VIEWER_HEIGHT / 2) - (board.getHeight()/2) + 50);
// island.setTranslateX((VIEWER_WIDTH / 2) - board.getWidth() / 2 + 50);
// island.setTranslateY((VIEWER_HEIGHT / 2) - board.getHeight() / 2 + 50);
// island.setFill(Color.GREEN);
// root.getChildren().add(island);
// }
// }
// }
// if (k == 2) {
// Text bonusPointIslandText = new Text();
// bonusPointIslandText.setText("The bonus point for this island is " +
// islandString[2]);
// bonusPointIslandText.setFont(Font.font("Sans Serif",
// FontWeight.BOLD, 10));
// bonusPointIslandText.setX(50);
// bonusPointIslandText.setY(150);
// bonusPointIslandText.setTextAlignment(TextAlignment.CENTER);
// root.getChildren().add(bonusPointIslandText);
// bonusPointIslandText.setFill(Color.RED);
// } else {
// Label rectangleLabel = new Label();
// for(int j = i+1; j < islandString.length; j++){
// Label rectangleLabel = new Label();
// if(j == 3) {
// Text bonusPointIslandText = new Text();
// bonusPointIslandText.setText("The bonus point for this island is " +
// islandString[j]);
// bonusPointIslandText.setFont(Font.font("Sans Serif",
// FontWeight.BOLD, 20));
// bonusPointIslandText.setX(50);
// bonusPointIslandText.setY(150);
// bonusPointIslandText.setTextAlignment(TextAlignment.CENTER);
// root.getChildren().add(bonusPointIslandText);
// bonusPointIslandText.setFill(Color.GREEN);
// }
// String[] islandCoords = islandString[j].split(",");
// Rectangle island = new Rectangle(
// Integer.parseInt(islandCoords[0]) * boardHeight,
// Integer.parseInt(islandCoords[1]) * boardHeight,
// boardHeightPx - 100 / boardHeight,
// boardHeightPx - 100 / boardHeight
// );
// island.setX((VIEWER_WIDTH / 2) - (board.getWidth()/2) + 50);
// island.setY((VIEWER_HEIGHT / 2) - (board.getHeight()/2) + 50);
// island.setTranslateX((VIEWER_WIDTH / 2) - board.getWidth() / 2 + 50);
// island.setTranslateY((VIEWER_HEIGHT / 2) - board.getHeight() / 2 + 50);
// island.setFill(Color.GREEN);
// root.getChildren().add(island);
// }
// System.out.println("tod");
// for (int j = 2; j < parts.length; j++) {
// String[] islandString = parts[j].split(" ");
// System.out.println("Parts[j] : " + parts[j]);
// for (int k = 3; k < islandString.length; k++) {
// Label rectangleLabel = new Label();
// String[] islandCoords = islandString[k].split(",");
// System.out.println("IslandString: " + islandString[k]);
// Rectangle island = new Rectangle(
// Integer.parseInt(islandCoords[0]) * boardHeight,
// Integer.parseInt(islandCoords[1]) * boardHeight,
// (boardHeightPx - 100) / boardHeight,
// (boardHeightPx - 100) / boardHeight
// );
//// island.setX((VIEWER_WIDTH / 2) - (board.getWidth()/2) + 50);
//// island.setY((VIEWER_HEIGHT / 2) - (board.getHeight()/2) + 50);
// island.setTranslateX((VIEWER_WIDTH / 2) - board.getWidth() / 2 + 50);
// island.setTranslateY((VIEWER_HEIGHT / 2) - board.getHeight() / 2 + 50);
// island.setFill(Color.GREEN);
// root.getChildren().add(island);
// }
// }
// }
// switch(parts[i]){
// if()
// // case "a": {
// String gameArrangementState = parts[i];
// String[] gameArrangementStateParts = gameArrangementState.split(" ");
// int boardHeight = Integer.parseInt(gameArrangementStateParts[1]);
// int boardHeightPx = boardHeight * 45;
//
// Rectangle board = new Rectangle();
// board.setWidth(boardHeightPx); // width (assume for right now the map is a square
// board.setHeight(boardHeightPx); // height
// board.setX((VIEWER_WIDTH / 2) - board.getWidth()/2); // starting x loc
// board.setY((VIEWER_HEIGHT / 2) - board.getHeight()/2); // starting y loc
// board.setFill(Color.DARKBLUE);
// root.getChildren().add(board);
// }
// }
// }
/*
// Part 1 done (Game Arrangement State)
String[] parts = stateString.split(";");
String gameArrangementState = parts[0];
@ -178,9 +496,76 @@ public class Viewer extends Application {
// by that I mean, when the board size adjusted, the current state is also
// adjusted
// The input is below
// 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;
// statement = parts
// Part 3 (Many Island Statements)
// Make hexagonal tiles inside the board
// CoordsPx < coords * 45 as "*45" is the px constant for the board
for(int i = 2; i < parts.length; i++){
String[] islandString = parts[i].split(" ");
System.out.println("Parts[i] : " + parts[i]);
for(int j = 3; j < islandString.length; j++){
Label rectangleLabel = new Label();
String[] islandCoords = islandString[j].split(",");
System.out.println("IslandString: " + islandString[j]);
Rectangle island = new Rectangle(
Integer.parseInt(islandCoords[0]) * boardHeight,
Integer.parseInt(islandCoords[1]) * boardHeight,
(boardHeightPx - 100) / boardHeight,
(boardHeightPx - 100) / boardHeight
);
// island.setX((VIEWER_WIDTH / 2) - (board.getWidth()/2) + 50);
// island.setY((VIEWER_HEIGHT / 2) - (board.getHeight()/2) + 50);
island.setTranslateX((VIEWER_WIDTH / 2) - board.getWidth()/2 + 50);
island.setTranslateY((VIEWER_HEIGHT / 2) - board.getHeight()/2 + 50);
island.setFill(Color.GREEN);
root.getChildren().add(island);
}
}
*/
// TilePane pane = new TilePane();
// pane.setTileAlignment(Pos.CENTER);
//
// for(int i = 1; i <= boardHeight; i++){
// for(int j = 1; j <= boardHeight; j++){
// pane.getChildren().add(new Button(i + "," + j));
// }
// }
// root.getChildren().add(pane);
// JFrame frame = new JFrame();
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setSize(500,500);
// frame.setVisible(true);
// frame.setLayout(new GridLayout(boardHeight, boardHeight));
//
// for(int i = 1; i <= boardHeight; i++){
// for(int j = 1; j <= boardHeight; j++){
// frame.add(new JButton(i + "," + j));
// }
// }
//
// frame.setVisible(true);
//root.getChildren().add(frame);
// TilePane tile = new TilePane();
// tile.setHgap(8);
// tile.setPrefColumns(13);
// for (int i = 0; i < 20; i++){
// root.getChildren().add(tile);
// }
// TilePane tileVer = new TilePane(Orientation.VERTICAL);
// tile.setTileAlignment(Pos.CENTER_LEFT);
// tile.setPrefRows(10);
// for (int i = 0; i < 50; i++) {
// tileVer.getChildren().add(new ImageView(...));
// }
// Make hexagonal tiles inside the board
// CoordsPx < coords * 45 as "*45" is the px constant for the board
/*
[Many Island Statements]
@ -200,8 +585,9 @@ public class Viewer extends Application {
*
* ^ A sequence of three island statements appearing in the standard game string
*/
// FIXME Task 5
}
// FIXME Task 5
}
/**
* Create a basic text field for input and a refresh button.