Guest User

Untitled

a guest
Feb 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.61 KB | None | 0 0
  1. package com.basix.mühle;
  2.  
  3. import javafx.application.Application;
  4. import javafx.scene.Group;
  5. import javafx.scene.Scene;
  6. import javafx.stage.Stage;
  7. import javafx.beans.value.ChangeListener;
  8. import javafx.beans.value.ObservableValue;
  9. import javafx.scene.control.ToggleButton;
  10. import javafx.scene.control.Toggle;
  11. import javafx.scene.control.ToggleGroup;
  12. import javafx.scene.control.Label;
  13. import javafx.scene.layout.HBox;
  14. import javafx.scene.layout.VBox;
  15. import javafx.scene.paint.Color;
  16. import javafx.scene.shape.Line;
  17. import javafx.scene.text.Font;
  18.  
  19. import java.util.ArrayList;
  20.  
  21. /**
  22. * Main class for the game.
  23. *
  24. * @author K.Grueneberg
  25. *
  26. */
  27. public class Game extends Application
  28. {
  29.  
  30. public Group root;
  31.  
  32. /**
  33. * Array of togglebuttons whose are being displayed to the public.
  34. */
  35. private ToggleButton[][] btnVertices = new ToggleButton[7][7];
  36.  
  37. /**
  38. * Is player1 currently picking?
  39. */
  40. private boolean player1Turn = true;
  41. /**
  42. * Is player2 currently picking?
  43. */
  44. private boolean player2Turn = false;
  45.  
  46. /**
  47. * Label to display who is currently picking
  48. */
  49. private Label turnLabel = new Label();
  50. /**
  51. * Label to display how many pieces player1 has still left.
  52. */
  53. private Label pl1Pieces = new Label();
  54. /**
  55. * Label to display how many pieces player2 has still left.
  56. */
  57. private Label pl2Pieces = new Label();
  58.  
  59. /**
  60. * Allow removing an enemys piece.
  61. */
  62. private boolean allowRemove = false;
  63.  
  64. /**
  65. * Available pieces for player1.
  66. */
  67. private int player1Pieces = 3;
  68. /**
  69. * Available pieces for player2.
  70. */
  71. private int player2Pieces = 3;
  72.  
  73. /**
  74. * This box contains the main interface.
  75. */
  76. private VBox boxMain;
  77.  
  78. /**
  79. * Contains a list of all selected fields to compare it with previous picks.
  80. */
  81. private ArrayList<fieldPicked> fieldsPicked = new ArrayList<fieldPicked>();
  82.  
  83. /**
  84. * A new field, that is being added to the list of selected fields.
  85. */
  86. private fieldPicked newFP = new fieldPicked();
  87.  
  88. /**
  89. * Draws a line on the interface.
  90. *
  91. * @param LayoutX
  92. * X-Position
  93. * @param LayoutY
  94. * Y-Position
  95. * @param StartX
  96. * X-Startposition
  97. * @param EndX
  98. * X-Endposition
  99. * @param StartY
  100. * Y-Startposition
  101. * @param EndY
  102. * Y-Endposition
  103. * @param color
  104. * The lines color
  105. */
  106. private void addLine(int LayoutX, int LayoutY, int StartX, int EndX, int StartY, int EndY, Color color)
  107. {
  108. Line line = new Line();
  109. line.setLayoutY(LayoutY);
  110. line.setLayoutX(LayoutX);
  111. line.setStartX(StartX);
  112. line.setEndX(EndX);
  113. line.setStartY(StartY);
  114. line.setEndY(EndY);
  115. line.setFill(null);
  116. line.setStroke(color);
  117. line.setStrokeWidth(2);
  118.  
  119. root.getChildren().add(line);
  120. }
  121.  
  122. /**
  123. * Initiates the interface.
  124. *
  125. * @param primaryStage
  126. * The stage the interface will be build on.
  127. */
  128. private void init(Stage primaryStage)
  129. {
  130. root = new Group();
  131. primaryStage.setScene(new Scene(root, 650, 480));
  132.  
  133. // Space between each button
  134. int verticeTranslationX = 20;
  135. int verticeTranslationY = 20;
  136.  
  137. // Add a new vbox
  138. boxMain = new VBox();
  139. boxMain.setStyle("-fx-background-color: DAE6F3");
  140. boxMain.setMinSize(650, 480);
  141.  
  142. // This is needed to handle the click event later on
  143. ToggleGroup group = new ToggleGroup();
  144.  
  145. // Loop through array of buttons and add it to the vbox
  146. for (int i = 0; i < 7; i++)
  147. {
  148. // Create on height box for each row(7 buttons ea row)
  149. HBox boxVertices = new HBox();
  150. boxVertices.setSpacing(50);
  151. boxVertices.setTranslateY(verticeTranslationY);
  152. boxVertices.setTranslateX(verticeTranslationX);
  153.  
  154. for (int x = 0; x < 7; x++)
  155. {
  156. // Create the togglebutton and add it to the heightbox
  157. btnVertices[i][x] = new ToggleButton();
  158. btnVertices[i][x].setId(i + "," + x);
  159. btnVertices[i][x].setStyle("-fx-base: white;");
  160. btnVertices[i][x].setToggleGroup(group);
  161.  
  162. boxVertices.getChildren().add(btnVertices[i][x]);
  163. }
  164.  
  165. // Space between the rows on the Y-Axis
  166. verticeTranslationY += 50;
  167.  
  168. // Finally add each heightbox to the main vbox
  169. boxMain.getChildren().add(boxVertices);
  170. }
  171.  
  172. // Eventhandler for the buttons
  173. group.selectedToggleProperty().addListener(new ChangeListener<Toggle>()
  174. {
  175. @Override
  176. public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle selectedToggle)
  177. {
  178. // If the button isn't being deselected
  179. if (selectedToggle != null)
  180. {
  181. // Get the X and Y Coordinates, they are passed commaseparated
  182. String str = ((ToggleButton) selectedToggle).getId();
  183. String[] strSplit = str.split(",");
  184.  
  185. // Run the chooseField sub
  186. chooseField(Integer.parseInt(strSplit[0]), Integer.parseInt(strSplit[1]));
  187. }
  188. }
  189. }
  190.  
  191. );
  192.  
  193. // Add the vbox to the main interface
  194. root.getChildren().add(boxMain);
  195.  
  196. // Draw lines that connect the different buttons together
  197. addLine(30, 30, 10, 200, 0, 0, Color.BLACK);
  198. addLine(240, 30, 10, 200, 0, 0, Color.BLACK);
  199. addLine(30, 455, 10, 200, 0, 0, Color.BLACK);
  200. addLine(240, 455, 10, 200, 0, 0, Color.BLACK);
  201.  
  202. addLine(30, 33, 0, 0, 10, 200, Color.BLACK);
  203. addLine(30, 245, 0, 0, 10, 200, Color.BLACK);
  204. addLine(450, 33, 0, 0, 10, 200, Color.BLACK);
  205. addLine(450, 245, 0, 0, 10, 200, Color.BLACK);
  206.  
  207. addLine(100, 103, 0, 0, 10, 130, Color.BLACK);
  208. addLine(100, 245, 0, 0, 10, 130, Color.BLACK);
  209. addLine(380, 103, 0, 0, 10, 130, Color.BLACK);
  210. addLine(380, 245, 0, 0, 10, 130, Color.BLACK);
  211.  
  212. addLine(240, 103, 10, 130, 0, 0, Color.BLACK);
  213. addLine(100, 103, 10, 130, 0, 0, Color.BLACK);
  214. addLine(240, 385, 10, 130, 0, 0, Color.BLACK);
  215. addLine(100, 385, 10, 130, 0, 0, Color.BLACK);
  216.  
  217. addLine(170, 170, 10, 60, 0, 0, Color.BLACK);
  218. addLine(240, 170, 10, 60, 0, 0, Color.BLACK);
  219. addLine(170, 315, 10, 60, 0, 0, Color.BLACK);
  220. addLine(240, 315, 10, 60, 0, 0, Color.BLACK);
  221. addLine(310, 245, 10, 60, 0, 0, Color.BLACK);
  222. addLine(380, 245, 10, 60, 0, 0, Color.BLACK);
  223. addLine(30, 245, 10, 60, 0, 0, Color.BLACK);
  224. addLine(100, 245, 10, 60, 0, 0, Color.BLACK);
  225.  
  226. addLine(170, 173, 0, 0, 10, 60, Color.BLACK);
  227. addLine(170, 243, 0, 0, 10, 60, Color.BLACK);
  228. addLine(310, 173, 0, 0, 10, 60, Color.BLACK);
  229. addLine(310, 243, 0, 0, 10, 60, Color.BLACK);
  230. addLine(240, 30, 0, 0, 10, 60, Color.BLACK);
  231. addLine(240, 100, 0, 0, 10, 60, Color.BLACK);
  232. addLine(240, 315, 0, 0, 10, 60, Color.BLACK);
  233. addLine(240, 385, 0, 0, 10, 60, Color.BLACK);
  234.  
  235. // Main Header Label
  236. Label lblHeader = new Label("Mühle");
  237. lblHeader.setFont(new Font("Arial", 30));
  238. lblHeader.setLayoutX(520);
  239. lblHeader.setLayoutY(30);
  240.  
  241. // Label to see who is currently picking
  242. turnLabel.setFont(new Font("Arial", 14));
  243. turnLabel.setLayoutX(530);
  244. turnLabel.setLayoutY(100);
  245.  
  246. // Label to see how many pieces player 1 still has left
  247. pl1Pieces.setFont(new Font("Arial", 14));
  248. pl1Pieces.setLayoutX(500);
  249. pl1Pieces.setLayoutY(130);
  250.  
  251. // Label to see how many pieces player 2 still has left
  252. pl2Pieces.setFont(new Font("Arial", 14));
  253. pl2Pieces.setLayoutX(500);
  254. pl2Pieces.setLayoutY(150);
  255.  
  256. // Add all labels to the main interface
  257. root.getChildren().add(lblHeader);
  258. root.getChildren().add(turnLabel);
  259. root.getChildren().add(pl1Pieces);
  260. root.getChildren().add(pl2Pieces);
  261.  
  262. // Hide a few unnecessary buttons
  263. // btnVertices[Y][X]
  264. btnVertices[0][1].setVisible(false);
  265. btnVertices[0][2].setVisible(false);
  266. btnVertices[0][4].setVisible(false);
  267. btnVertices[0][5].setVisible(false);
  268.  
  269. btnVertices[1][0].setVisible(false);
  270. btnVertices[2][0].setVisible(false);
  271. btnVertices[4][0].setVisible(false);
  272. btnVertices[5][0].setVisible(false);
  273.  
  274. btnVertices[1][6].setVisible(false);
  275. btnVertices[2][6].setVisible(false);
  276. btnVertices[4][6].setVisible(false);
  277. btnVertices[5][6].setVisible(false);
  278.  
  279. btnVertices[0][1].setVisible(false);
  280. btnVertices[2][1].setVisible(false);
  281. btnVertices[4][1].setVisible(false);
  282. btnVertices[6][1].setVisible(false);
  283.  
  284. btnVertices[0][5].setVisible(false);
  285. btnVertices[2][5].setVisible(false);
  286. btnVertices[4][5].setVisible(false);
  287. btnVertices[6][5].setVisible(false);
  288.  
  289. btnVertices[0][2].setVisible(false);
  290. btnVertices[1][2].setVisible(false);
  291. btnVertices[5][2].setVisible(false);
  292. btnVertices[6][2].setVisible(false);
  293.  
  294. btnVertices[0][4].setVisible(false);
  295. btnVertices[1][4].setVisible(false);
  296. btnVertices[5][4].setVisible(false);
  297. btnVertices[6][4].setVisible(false);
  298.  
  299. btnVertices[3][3].setVisible(false);
  300. }
  301.  
  302. /**
  303. * This function registers, which fields have been picked to compare previous picked fields.
  304. *
  305. * @param x
  306. * X-Coordinate
  307. * @param y
  308. * Y-Coordinate
  309. */
  310. private void addField(int x, int y)
  311. {
  312. // Reinitialise the fieldpick
  313. newFP = new fieldPicked();
  314. // Set the coordinates
  315. newFP.x = x;
  316. newFP.y = y;
  317. // Add it to the list of fields selected
  318. fieldsPicked.add(newFP);
  319. }
  320.  
  321. /**
  322. * Checks if the player has built a mill.
  323. *
  324. * @param x
  325. * X-Coordinate
  326. * @param y
  327. * Y-Coordinate
  328. * @param colour
  329. * The player's colour.
  330. * @return Boolean, if the player has built a mill.
  331. */
  332. private boolean isMill(int x, int y, String colour)
  333. {
  334. boolean b = false;
  335.  
  336. if (x < 5)
  337. {
  338. if (btnVertices[x][y].getStyle() == colour && btnVertices[x + 1][y].getStyle() == colour && btnVertices[x + 2][y].getStyle() == colour)
  339. {
  340. b = true;
  341. }
  342. }
  343.  
  344. if (x > 0 && x < 6)
  345. {
  346. if (btnVertices[x - 1][y].getStyle() == colour && btnVertices[x][y].getStyle() == colour && btnVertices[x + 1][y].getStyle() == colour)
  347. {
  348. b = true;
  349. }
  350. }
  351.  
  352. if (x > 1)
  353. {
  354. if (btnVertices[x - 1][y].getStyle() == colour && btnVertices[x - 2][y].getStyle() == colour && btnVertices[x][y].getStyle() == colour)
  355. {
  356. b = true;
  357. }
  358. }
  359.  
  360. if (y < 5)
  361. {
  362. if (btnVertices[x][y].getStyle() == colour && btnVertices[x][y + 1].getStyle() == colour && btnVertices[x][y + 2].getStyle() == colour)
  363. {
  364. b = true;
  365. }
  366. }
  367.  
  368. if (y > 0 && y < 6)
  369. {
  370. if (btnVertices[x][y - 1].getStyle() == colour && btnVertices[x][y].getStyle() == colour && btnVertices[x][y + 1].getStyle() == colour)
  371. {
  372. b = true;
  373. }
  374. }
  375.  
  376. if (y > 1)
  377. {
  378. if (btnVertices[x][y - 2].getStyle() == colour && btnVertices[x][y - 1].getStyle() == colour && btnVertices[x][y].getStyle() == colour)
  379. {
  380. b = true;
  381. }
  382. }
  383.  
  384. return b;
  385. }
  386.  
  387. /**
  388. * Checks if the move is possible at all(disallow jumping).
  389. *
  390. * @param x
  391. * X-Coordinate
  392. * @param y
  393. * Y-Coordinate
  394. * @param previousx
  395. * Previously picked X-Coordinate
  396. * @param previousy
  397. * Previously picked Y-Coordinate
  398. * @return Boolean, if the move is possible.
  399. */
  400. private boolean isMovePossible(int x, int y, int previousx, int previousy)
  401. {
  402. boolean isPossible = false;
  403.  
  404. if (previousx - x == 0 || previousx - x == x || previousx - x == -1)
  405. {
  406. isPossible = true;
  407. }
  408.  
  409. if (previousy - y == 0 || previousy - y == y || previousy - y == -1)
  410. {
  411. isPossible = true;
  412. }
  413.  
  414. return isPossible;
  415. }
  416.  
  417. /**
  418. * Checks if the game is already over. The game is over when one of the two players on has 2
  419. * pieces left.
  420. *
  421. * @return Boolean, if the game is over.
  422. */
  423. private boolean isGameOver()
  424. {
  425. boolean isOver = false;
  426.  
  427. if (player1Pieces == 0 && player2Pieces == 0)
  428. {
  429. int leftPlayer1 = 0;
  430. int leftPlayer2 = 0;
  431.  
  432. // Loop through all buttons and see how many pieces each player has left
  433. for (int i = 0; i < 7; i++)
  434. {
  435. for (int x = 0; x < 7; x++)
  436. {
  437. if (btnVertices[i][x].getStyle() == "-fx-base: green;")
  438. {
  439. leftPlayer1++;
  440. }
  441. else if (btnVertices[i][x].getStyle() == "-fx-base: blue;")
  442. {
  443. leftPlayer2++;
  444. }
  445. }
  446. }
  447.  
  448. // Set the labels according to the pieces left
  449. pl1Pieces.setText("Spieler 1: " + leftPlayer1 + " übrig");
  450. pl2Pieces.setText("Spieler 2: " + leftPlayer2 + " übrig");
  451.  
  452. // If one of the players has only 2 pieces left, the game is lost for that specific
  453. // player
  454. if (leftPlayer1 == 2 || leftPlayer2 == 2)
  455. {
  456. isOver = true;
  457. }
  458. }
  459.  
  460. return isOver;
  461. }
  462.  
  463. /**
  464. * This is the main action. If you choose any field, this void is called and checks through
  465. * various things.
  466. *
  467. * @param x
  468. * Selected X-Coordinate
  469. * @param y
  470. * Selected Y-Coordinate
  471. */
  472. private void chooseField(int x, int y)
  473. {
  474. // Get the previously picked coordinates
  475. int previousX = 0;
  476. int previousY = 0;
  477.  
  478. // Add the field to the list of selected togglebuttons
  479. addField(x, y);
  480.  
  481. // Assign the previously picked coordinates
  482. if (fieldsPicked.size() > 1)
  483. {
  484. previousX = fieldsPicked.get(fieldsPicked.size() - 2).x;
  485. previousY = fieldsPicked.get(fieldsPicked.size() - 2).y;
  486. }
  487.  
  488. // Assign the player's color
  489. String styleColour = player1Turn ? "-fx-base: green;" : "-fx-base: blue;";
  490. // Assign the enemy player's color
  491. String styleEnemy = player1Turn ? "-fx-base: blue;" : "-fx-base: green;";
  492. // Check how many pieces the player has still left
  493. int playerPieces = player1Turn ? player1Pieces : player2Pieces;
  494.  
  495. if (playerPieces > 0)
  496. {
  497. // If the player has more than zero pieces to set
  498. if (btnVertices[x][y].getStyle() == "-fx-base: white;")
  499. {
  500. // just setting first 9 pieces, decrease pieces
  501. if (player1Turn)
  502. {
  503. player1Pieces--;
  504. }
  505. else
  506. {
  507. player2Pieces--;
  508. }
  509.  
  510. btnVertices[x][y].setStyle(styleColour);
  511. player1Turn = player1Turn ? false : true;
  512. setPlayer2Turn(!player1Turn);
  513. }
  514. }
  515. else
  516. {
  517. // Else, the player is allowed to move one of his pieces
  518. if (allowRemove)
  519. {
  520. // If he has just built a mill, he can remove a piece of the enemy
  521. if (btnVertices[x][y].getStyle() == styleEnemy)
  522. {
  523. btnVertices[x][y].setStyle("-fx-base: white;");
  524. allowRemove = false;
  525. player1Turn = player1Turn ? false : true;
  526. setPlayer2Turn(!player1Turn);
  527. }
  528. }
  529. // move piece
  530. else if (btnVertices[previousX][previousY].getStyle() == styleColour)
  531. {
  532. // otherwise he can move his piece
  533. if (isMovePossible(x, y, previousX, previousY))
  534. {
  535. // but only if the move is possible at all
  536. if (btnVertices[x][y].getStyle() == "-fx-base: white;")
  537. {
  538. btnVertices[x][y].setStyle(styleColour);
  539. btnVertices[previousX][previousY].setStyle("-fx-base: white;");
  540. if (!isMill(x, y, styleColour))
  541. {
  542. player1Turn = player1Turn ? false : true;
  543. setPlayer2Turn(!player1Turn);
  544. }
  545. else
  546. {
  547. allowRemove = true;
  548. }
  549. }
  550. }
  551. }
  552. }
  553.  
  554. // Set the labels turn
  555. turnLabel.setText(player1Turn ? "Spieler 1" : "Spieler 2");
  556.  
  557. // Check if the game is over yet
  558. if (isGameOver())
  559. {
  560. turnLabel.setText("Game Over");
  561. for (int i = 0; i < 7; i++)
  562. {
  563. for (int k = 0; k < 7; k++)
  564. {
  565. btnVertices[i][k].setStyle("-fx-base: black;");
  566. }
  567. }
  568. }
  569. }
  570.  
  571. @Override
  572. public void start(Stage primaryStage) throws Exception
  573. {
  574. init(primaryStage);
  575. primaryStage.show();
  576. }
  577.  
  578. /**
  579. * Launch the application
  580. */
  581. public static void main(String[] args)
  582. {
  583. launch(args);
  584. }
  585.  
  586. /**
  587. * Setter for the player2's turn.
  588. *
  589. * @param player2Turn
  590. */
  591. public void setPlayer2Turn(boolean player2Turn)
  592. {
  593. this.player2Turn = player2Turn;
  594. }
  595.  
  596. /**
  597. * Getter for the player2's turn.
  598. *
  599. * @return playerTurn2 as boolean.
  600. */
  601. public boolean isPlayer2Turn()
  602. {
  603. return player2Turn;
  604. }
  605. }
Add Comment
Please, Sign In to add comment