Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.basix.mühle;
- import javafx.application.Application;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.stage.Stage;
- import javafx.beans.value.ChangeListener;
- import javafx.beans.value.ObservableValue;
- import javafx.scene.control.ToggleButton;
- import javafx.scene.control.Toggle;
- import javafx.scene.control.ToggleGroup;
- import javafx.scene.control.Label;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.VBox;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Line;
- import javafx.scene.text.Font;
- import java.util.ArrayList;
- /**
- * Main class for the game.
- *
- * @author K.Grueneberg
- *
- */
- public class Game extends Application
- {
- public Group root;
- /**
- * Array of togglebuttons whose are being displayed to the public.
- */
- private ToggleButton[][] btnVertices = new ToggleButton[7][7];
- /**
- * Is player1 currently picking?
- */
- private boolean player1Turn = true;
- /**
- * Is player2 currently picking?
- */
- private boolean player2Turn = false;
- /**
- * Label to display who is currently picking
- */
- private Label turnLabel = new Label();
- /**
- * Label to display how many pieces player1 has still left.
- */
- private Label pl1Pieces = new Label();
- /**
- * Label to display how many pieces player2 has still left.
- */
- private Label pl2Pieces = new Label();
- /**
- * Allow removing an enemys piece.
- */
- private boolean allowRemove = false;
- /**
- * Available pieces for player1.
- */
- private int player1Pieces = 3;
- /**
- * Available pieces for player2.
- */
- private int player2Pieces = 3;
- /**
- * This box contains the main interface.
- */
- private VBox boxMain;
- /**
- * Contains a list of all selected fields to compare it with previous picks.
- */
- private ArrayList<fieldPicked> fieldsPicked = new ArrayList<fieldPicked>();
- /**
- * A new field, that is being added to the list of selected fields.
- */
- private fieldPicked newFP = new fieldPicked();
- /**
- * Draws a line on the interface.
- *
- * @param LayoutX
- * X-Position
- * @param LayoutY
- * Y-Position
- * @param StartX
- * X-Startposition
- * @param EndX
- * X-Endposition
- * @param StartY
- * Y-Startposition
- * @param EndY
- * Y-Endposition
- * @param color
- * The lines color
- */
- private void addLine(int LayoutX, int LayoutY, int StartX, int EndX, int StartY, int EndY, Color color)
- {
- Line line = new Line();
- line.setLayoutY(LayoutY);
- line.setLayoutX(LayoutX);
- line.setStartX(StartX);
- line.setEndX(EndX);
- line.setStartY(StartY);
- line.setEndY(EndY);
- line.setFill(null);
- line.setStroke(color);
- line.setStrokeWidth(2);
- root.getChildren().add(line);
- }
- /**
- * Initiates the interface.
- *
- * @param primaryStage
- * The stage the interface will be build on.
- */
- private void init(Stage primaryStage)
- {
- root = new Group();
- primaryStage.setScene(new Scene(root, 650, 480));
- // Space between each button
- int verticeTranslationX = 20;
- int verticeTranslationY = 20;
- // Add a new vbox
- boxMain = new VBox();
- boxMain.setStyle("-fx-background-color: DAE6F3");
- boxMain.setMinSize(650, 480);
- // This is needed to handle the click event later on
- ToggleGroup group = new ToggleGroup();
- // Loop through array of buttons and add it to the vbox
- for (int i = 0; i < 7; i++)
- {
- // Create on height box for each row(7 buttons ea row)
- HBox boxVertices = new HBox();
- boxVertices.setSpacing(50);
- boxVertices.setTranslateY(verticeTranslationY);
- boxVertices.setTranslateX(verticeTranslationX);
- for (int x = 0; x < 7; x++)
- {
- // Create the togglebutton and add it to the heightbox
- btnVertices[i][x] = new ToggleButton();
- btnVertices[i][x].setId(i + "," + x);
- btnVertices[i][x].setStyle("-fx-base: white;");
- btnVertices[i][x].setToggleGroup(group);
- boxVertices.getChildren().add(btnVertices[i][x]);
- }
- // Space between the rows on the Y-Axis
- verticeTranslationY += 50;
- // Finally add each heightbox to the main vbox
- boxMain.getChildren().add(boxVertices);
- }
- // Eventhandler for the buttons
- group.selectedToggleProperty().addListener(new ChangeListener<Toggle>()
- {
- @Override
- public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle selectedToggle)
- {
- // If the button isn't being deselected
- if (selectedToggle != null)
- {
- // Get the X and Y Coordinates, they are passed commaseparated
- String str = ((ToggleButton) selectedToggle).getId();
- String[] strSplit = str.split(",");
- // Run the chooseField sub
- chooseField(Integer.parseInt(strSplit[0]), Integer.parseInt(strSplit[1]));
- }
- }
- }
- );
- // Add the vbox to the main interface
- root.getChildren().add(boxMain);
- // Draw lines that connect the different buttons together
- addLine(30, 30, 10, 200, 0, 0, Color.BLACK);
- addLine(240, 30, 10, 200, 0, 0, Color.BLACK);
- addLine(30, 455, 10, 200, 0, 0, Color.BLACK);
- addLine(240, 455, 10, 200, 0, 0, Color.BLACK);
- addLine(30, 33, 0, 0, 10, 200, Color.BLACK);
- addLine(30, 245, 0, 0, 10, 200, Color.BLACK);
- addLine(450, 33, 0, 0, 10, 200, Color.BLACK);
- addLine(450, 245, 0, 0, 10, 200, Color.BLACK);
- addLine(100, 103, 0, 0, 10, 130, Color.BLACK);
- addLine(100, 245, 0, 0, 10, 130, Color.BLACK);
- addLine(380, 103, 0, 0, 10, 130, Color.BLACK);
- addLine(380, 245, 0, 0, 10, 130, Color.BLACK);
- addLine(240, 103, 10, 130, 0, 0, Color.BLACK);
- addLine(100, 103, 10, 130, 0, 0, Color.BLACK);
- addLine(240, 385, 10, 130, 0, 0, Color.BLACK);
- addLine(100, 385, 10, 130, 0, 0, Color.BLACK);
- addLine(170, 170, 10, 60, 0, 0, Color.BLACK);
- addLine(240, 170, 10, 60, 0, 0, Color.BLACK);
- addLine(170, 315, 10, 60, 0, 0, Color.BLACK);
- addLine(240, 315, 10, 60, 0, 0, Color.BLACK);
- addLine(310, 245, 10, 60, 0, 0, Color.BLACK);
- addLine(380, 245, 10, 60, 0, 0, Color.BLACK);
- addLine(30, 245, 10, 60, 0, 0, Color.BLACK);
- addLine(100, 245, 10, 60, 0, 0, Color.BLACK);
- addLine(170, 173, 0, 0, 10, 60, Color.BLACK);
- addLine(170, 243, 0, 0, 10, 60, Color.BLACK);
- addLine(310, 173, 0, 0, 10, 60, Color.BLACK);
- addLine(310, 243, 0, 0, 10, 60, Color.BLACK);
- addLine(240, 30, 0, 0, 10, 60, Color.BLACK);
- addLine(240, 100, 0, 0, 10, 60, Color.BLACK);
- addLine(240, 315, 0, 0, 10, 60, Color.BLACK);
- addLine(240, 385, 0, 0, 10, 60, Color.BLACK);
- // Main Header Label
- Label lblHeader = new Label("Mühle");
- lblHeader.setFont(new Font("Arial", 30));
- lblHeader.setLayoutX(520);
- lblHeader.setLayoutY(30);
- // Label to see who is currently picking
- turnLabel.setFont(new Font("Arial", 14));
- turnLabel.setLayoutX(530);
- turnLabel.setLayoutY(100);
- // Label to see how many pieces player 1 still has left
- pl1Pieces.setFont(new Font("Arial", 14));
- pl1Pieces.setLayoutX(500);
- pl1Pieces.setLayoutY(130);
- // Label to see how many pieces player 2 still has left
- pl2Pieces.setFont(new Font("Arial", 14));
- pl2Pieces.setLayoutX(500);
- pl2Pieces.setLayoutY(150);
- // Add all labels to the main interface
- root.getChildren().add(lblHeader);
- root.getChildren().add(turnLabel);
- root.getChildren().add(pl1Pieces);
- root.getChildren().add(pl2Pieces);
- // Hide a few unnecessary buttons
- // btnVertices[Y][X]
- btnVertices[0][1].setVisible(false);
- btnVertices[0][2].setVisible(false);
- btnVertices[0][4].setVisible(false);
- btnVertices[0][5].setVisible(false);
- btnVertices[1][0].setVisible(false);
- btnVertices[2][0].setVisible(false);
- btnVertices[4][0].setVisible(false);
- btnVertices[5][0].setVisible(false);
- btnVertices[1][6].setVisible(false);
- btnVertices[2][6].setVisible(false);
- btnVertices[4][6].setVisible(false);
- btnVertices[5][6].setVisible(false);
- btnVertices[0][1].setVisible(false);
- btnVertices[2][1].setVisible(false);
- btnVertices[4][1].setVisible(false);
- btnVertices[6][1].setVisible(false);
- btnVertices[0][5].setVisible(false);
- btnVertices[2][5].setVisible(false);
- btnVertices[4][5].setVisible(false);
- btnVertices[6][5].setVisible(false);
- btnVertices[0][2].setVisible(false);
- btnVertices[1][2].setVisible(false);
- btnVertices[5][2].setVisible(false);
- btnVertices[6][2].setVisible(false);
- btnVertices[0][4].setVisible(false);
- btnVertices[1][4].setVisible(false);
- btnVertices[5][4].setVisible(false);
- btnVertices[6][4].setVisible(false);
- btnVertices[3][3].setVisible(false);
- }
- /**
- * This function registers, which fields have been picked to compare previous picked fields.
- *
- * @param x
- * X-Coordinate
- * @param y
- * Y-Coordinate
- */
- private void addField(int x, int y)
- {
- // Reinitialise the fieldpick
- newFP = new fieldPicked();
- // Set the coordinates
- newFP.x = x;
- newFP.y = y;
- // Add it to the list of fields selected
- fieldsPicked.add(newFP);
- }
- /**
- * Checks if the player has built a mill.
- *
- * @param x
- * X-Coordinate
- * @param y
- * Y-Coordinate
- * @param colour
- * The player's colour.
- * @return Boolean, if the player has built a mill.
- */
- private boolean isMill(int x, int y, String colour)
- {
- boolean b = false;
- if (x < 5)
- {
- if (btnVertices[x][y].getStyle() == colour && btnVertices[x + 1][y].getStyle() == colour && btnVertices[x + 2][y].getStyle() == colour)
- {
- b = true;
- }
- }
- if (x > 0 && x < 6)
- {
- if (btnVertices[x - 1][y].getStyle() == colour && btnVertices[x][y].getStyle() == colour && btnVertices[x + 1][y].getStyle() == colour)
- {
- b = true;
- }
- }
- if (x > 1)
- {
- if (btnVertices[x - 1][y].getStyle() == colour && btnVertices[x - 2][y].getStyle() == colour && btnVertices[x][y].getStyle() == colour)
- {
- b = true;
- }
- }
- if (y < 5)
- {
- if (btnVertices[x][y].getStyle() == colour && btnVertices[x][y + 1].getStyle() == colour && btnVertices[x][y + 2].getStyle() == colour)
- {
- b = true;
- }
- }
- if (y > 0 && y < 6)
- {
- if (btnVertices[x][y - 1].getStyle() == colour && btnVertices[x][y].getStyle() == colour && btnVertices[x][y + 1].getStyle() == colour)
- {
- b = true;
- }
- }
- if (y > 1)
- {
- if (btnVertices[x][y - 2].getStyle() == colour && btnVertices[x][y - 1].getStyle() == colour && btnVertices[x][y].getStyle() == colour)
- {
- b = true;
- }
- }
- return b;
- }
- /**
- * Checks if the move is possible at all(disallow jumping).
- *
- * @param x
- * X-Coordinate
- * @param y
- * Y-Coordinate
- * @param previousx
- * Previously picked X-Coordinate
- * @param previousy
- * Previously picked Y-Coordinate
- * @return Boolean, if the move is possible.
- */
- private boolean isMovePossible(int x, int y, int previousx, int previousy)
- {
- boolean isPossible = false;
- if (previousx - x == 0 || previousx - x == x || previousx - x == -1)
- {
- isPossible = true;
- }
- if (previousy - y == 0 || previousy - y == y || previousy - y == -1)
- {
- isPossible = true;
- }
- return isPossible;
- }
- /**
- * Checks if the game is already over. The game is over when one of the two players on has 2
- * pieces left.
- *
- * @return Boolean, if the game is over.
- */
- private boolean isGameOver()
- {
- boolean isOver = false;
- if (player1Pieces == 0 && player2Pieces == 0)
- {
- int leftPlayer1 = 0;
- int leftPlayer2 = 0;
- // Loop through all buttons and see how many pieces each player has left
- for (int i = 0; i < 7; i++)
- {
- for (int x = 0; x < 7; x++)
- {
- if (btnVertices[i][x].getStyle() == "-fx-base: green;")
- {
- leftPlayer1++;
- }
- else if (btnVertices[i][x].getStyle() == "-fx-base: blue;")
- {
- leftPlayer2++;
- }
- }
- }
- // Set the labels according to the pieces left
- pl1Pieces.setText("Spieler 1: " + leftPlayer1 + " übrig");
- pl2Pieces.setText("Spieler 2: " + leftPlayer2 + " übrig");
- // If one of the players has only 2 pieces left, the game is lost for that specific
- // player
- if (leftPlayer1 == 2 || leftPlayer2 == 2)
- {
- isOver = true;
- }
- }
- return isOver;
- }
- /**
- * This is the main action. If you choose any field, this void is called and checks through
- * various things.
- *
- * @param x
- * Selected X-Coordinate
- * @param y
- * Selected Y-Coordinate
- */
- private void chooseField(int x, int y)
- {
- // Get the previously picked coordinates
- int previousX = 0;
- int previousY = 0;
- // Add the field to the list of selected togglebuttons
- addField(x, y);
- // Assign the previously picked coordinates
- if (fieldsPicked.size() > 1)
- {
- previousX = fieldsPicked.get(fieldsPicked.size() - 2).x;
- previousY = fieldsPicked.get(fieldsPicked.size() - 2).y;
- }
- // Assign the player's color
- String styleColour = player1Turn ? "-fx-base: green;" : "-fx-base: blue;";
- // Assign the enemy player's color
- String styleEnemy = player1Turn ? "-fx-base: blue;" : "-fx-base: green;";
- // Check how many pieces the player has still left
- int playerPieces = player1Turn ? player1Pieces : player2Pieces;
- if (playerPieces > 0)
- {
- // If the player has more than zero pieces to set
- if (btnVertices[x][y].getStyle() == "-fx-base: white;")
- {
- // just setting first 9 pieces, decrease pieces
- if (player1Turn)
- {
- player1Pieces--;
- }
- else
- {
- player2Pieces--;
- }
- btnVertices[x][y].setStyle(styleColour);
- player1Turn = player1Turn ? false : true;
- setPlayer2Turn(!player1Turn);
- }
- }
- else
- {
- // Else, the player is allowed to move one of his pieces
- if (allowRemove)
- {
- // If he has just built a mill, he can remove a piece of the enemy
- if (btnVertices[x][y].getStyle() == styleEnemy)
- {
- btnVertices[x][y].setStyle("-fx-base: white;");
- allowRemove = false;
- player1Turn = player1Turn ? false : true;
- setPlayer2Turn(!player1Turn);
- }
- }
- // move piece
- else if (btnVertices[previousX][previousY].getStyle() == styleColour)
- {
- // otherwise he can move his piece
- if (isMovePossible(x, y, previousX, previousY))
- {
- // but only if the move is possible at all
- if (btnVertices[x][y].getStyle() == "-fx-base: white;")
- {
- btnVertices[x][y].setStyle(styleColour);
- btnVertices[previousX][previousY].setStyle("-fx-base: white;");
- if (!isMill(x, y, styleColour))
- {
- player1Turn = player1Turn ? false : true;
- setPlayer2Turn(!player1Turn);
- }
- else
- {
- allowRemove = true;
- }
- }
- }
- }
- }
- // Set the labels turn
- turnLabel.setText(player1Turn ? "Spieler 1" : "Spieler 2");
- // Check if the game is over yet
- if (isGameOver())
- {
- turnLabel.setText("Game Over");
- for (int i = 0; i < 7; i++)
- {
- for (int k = 0; k < 7; k++)
- {
- btnVertices[i][k].setStyle("-fx-base: black;");
- }
- }
- }
- }
- @Override
- public void start(Stage primaryStage) throws Exception
- {
- init(primaryStage);
- primaryStage.show();
- }
- /**
- * Launch the application
- */
- public static void main(String[] args)
- {
- launch(args);
- }
- /**
- * Setter for the player2's turn.
- *
- * @param player2Turn
- */
- public void setPlayer2Turn(boolean player2Turn)
- {
- this.player2Turn = player2Turn;
- }
- /**
- * Getter for the player2's turn.
- *
- * @return playerTurn2 as boolean.
- */
- public boolean isPlayer2Turn()
- {
- return player2Turn;
- }
- }
Add Comment
Please, Sign In to add comment