Advertisement
Guest User

yatzi gui

a guest
Mar 26th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.53 KB | None | 0 0
  1. package gui;
  2.  
  3.  
  4. import model.Yatzy;
  5.  
  6. import java.util.Arrays;
  7.  
  8.  
  9. import javafx.application.Application;
  10. import javafx.collections.ObservableList;
  11. import javafx.event.EventHandler;
  12. import javafx.geometry.Insets;
  13. import javafx.geometry.Pos;
  14. import javafx.scene.Node;
  15. import javafx.scene.Scene;
  16. import javafx.scene.control.Button;
  17. import javafx.scene.control.CheckBox;
  18. import javafx.scene.control.Label;
  19. import javafx.scene.control.TextField;
  20. import javafx.scene.input.MouseEvent;
  21. import javafx.scene.layout.HBox;
  22. import javafx.scene.layout.VBox;
  23. import javafx.stage.Stage;
  24.  
  25.  
  26. public class Gui extends Application{
  27. public static void main(String[] args) {
  28. launch();
  29. }
  30.  
  31. /*
  32. * We define all of our elements and containers as globals.
  33. * So that we interact with each element in our methods.
  34. * */
  35. HBox dices;
  36. HBox holds;
  37. Label rolled;
  38. VBox textFields;
  39. TextField txf;
  40. VBox sumTextFields;
  41. VBox totalTextFields;
  42. // global variables used when application start.
  43. boolean globalVisibility = false;
  44. private int numRolls = 0;
  45. private boolean hasClicked = false;
  46.  
  47. public void start(Stage stage) {
  48. stage.setTitle("Yatzy");
  49. stage.setResizable(false);
  50.  
  51. dices = new HBox(10);
  52. for(int i = 0; i < 5; i++) {
  53. TextField dice = new TextField("");
  54. dice.setPrefSize(80, 80);
  55. dice.setFocusTraversable(false);
  56. dice.setEditable(false);
  57. dices.getChildren().add(dice);
  58. }
  59.  
  60. holds = new HBox(43);
  61. holds.setStyle("-fx-padding: 10 0 0 12;");
  62.  
  63.  
  64. // CheckBox hold; // = new CheckBox("Hold");
  65.  
  66. for(int i = 0; i < 5; i++) {
  67. CheckBox hold = new CheckBox("Hold");
  68. hold.setFocusTraversable(false);
  69. holds.getChildren().add(hold);
  70. boolean disableOff = setDisableOff();
  71. hold.setDisable(disableOff);
  72. hold.setId(String.valueOf(i));
  73. hold.setOnAction(event -> {
  74. if (hold.isSelected()) {
  75. int parseID = Integer.parseInt(hold.getId());
  76. updateHold(parseID);
  77. }
  78. });
  79. }
  80.  
  81.  
  82.  
  83.  
  84.  
  85. HBox roll = new HBox(26);
  86. roll.setStyle("-fx-padding: 10 0 0 230;");
  87. Button btnRoll = new Button("Roll");
  88.  
  89. btnRoll.setOnAction(event -> this.roll());
  90.  
  91. btnRoll.setPrefSize(80, 40);
  92. btnRoll.setStyle("-fx-font-size: 20px;");
  93.  
  94. rolled = new Label("Rolled: " + numRolls);
  95. rolled.setStyle("-fx-padding: 12 0 0 0");
  96. roll.getChildren().addAll(btnRoll, rolled);
  97.  
  98. VBox topPanel = new VBox(dices, holds, roll);
  99. topPanel.setPadding(new Insets(10));
  100.  
  101. String[] labelTexts = { "1-s", "2-s", "3-s", "4-s", "5-s", "6-s", "One Pair", "Two Pairs", "Three Same", "Four Same", "Full House", "Small Straight", "Large Straight", "Chance", "Yatzy" };
  102. VBox labels = new VBox(16);
  103. labels.setStyle("-fx-padding: 3 10 0 0;");
  104. textFields = new VBox(8);
  105. textFields.setStyle("-fx-padding: 0 10 0 0;");
  106. VBox sumLabels = new VBox(280);
  107. sumLabels.setStyle("-fx-padding: 169 10 0 0;");
  108. sumTextFields = new VBox(272);
  109. sumTextFields.setStyle("-fx-padding: 166 10 0 0;");
  110. VBox totalLabels = new VBox(280);
  111. totalLabels.setStyle("-fx-padding: 169 10 0 0;");
  112. totalTextFields = new VBox(272);
  113. totalTextFields.setStyle("-fx-padding: 166 10 0 0;");
  114. Label lbl;
  115.  
  116. for(int i = 0; i < labelTexts.length; i++) {
  117. lbl = new Label(labelTexts[i]);
  118. labels.getChildren().add(lbl);
  119. txf = new TextField();
  120. String index = String.valueOf(i);
  121. txf.setId(index);
  122. int IAmGlobal = i;
  123. txf.setOnMouseClicked(e -> {
  124. if (!hasClicked) {
  125.  
  126. System.out.println("Clicked on " + IAmGlobal);
  127. System.out.println(holdChosenTextValues[IAmGlobal]);
  128. holdChosenTextValues[IAmGlobal] = true;
  129. boolean visibility = setValueOnSelectedElement(IAmGlobal);
  130. hasClicked = true;
  131. } else {
  132. System.out.println("you already clicked");
  133. }
  134. });
  135.  
  136.  
  137.  
  138.  
  139. txf.setPrefWidth(60);
  140. txf.setFocusTraversable(false);
  141. txf.setEditable(false);
  142. txf.setAlignment(Pos.CENTER_RIGHT);
  143. textFields.getChildren().add(txf);
  144. }
  145.  
  146. for(int n = 0; n < 2; n++) {
  147. lbl = new Label("Sum:");
  148. txf = new TextField("0");
  149. txf.setPrefWidth(60);
  150. txf.setFocusTraversable(false);
  151. txf.setEditable(false);
  152. txf.setAlignment(Pos.CENTER_RIGHT);
  153. txf.setStyle("-fx-text-inner-color: blue;");
  154. sumLabels.getChildren().add(lbl);
  155. sumTextFields.getChildren().add(txf);
  156. }
  157.  
  158. for(int j = 0; j < 2; j++) {
  159. lbl = new Label("Bonus:");
  160. txf = new TextField("0");
  161. txf.setPrefWidth(60);
  162. txf.setFocusTraversable(false);
  163. txf.setEditable(false);
  164. txf.setAlignment(Pos.CENTER_RIGHT);
  165. txf.setStyle("-fx-text-inner-color: blue;");
  166. if(j == 1) {
  167. lbl.setText("Total:");
  168. }
  169. totalLabels.getChildren().add(lbl);
  170. totalTextFields.getChildren().add(txf);
  171.  
  172.  
  173. }
  174.  
  175. HBox bottomPanel = new HBox(labels, textFields, sumLabels, sumTextFields, totalLabels, totalTextFields);
  176. bottomPanel.setPadding(new Insets(10));
  177. VBox rootPane = new VBox(10, topPanel, bottomPanel);
  178. rootPane.setPadding(new Insets(10));
  179. Scene scene = new Scene(rootPane);
  180. stage.setScene(scene);
  181. topPanel.setStyle("-fx-border-style: solid;");
  182. bottomPanel.setStyle("-fx-border-style: solid;");
  183.  
  184. stage.show();
  185. }
  186.  
  187. // global arrays.
  188. boolean[] holdArray = new boolean[] {false,false,false,false,false};
  189. boolean[] holdChosenTextValues = new boolean[]
  190. {false,false,false,false,false,false,false,false,
  191. false,false,false,false,false,false,false};
  192. private int[] disableValues = new int[15];
  193. // global variables
  194. boolean didUserSelect = false;
  195. private int upperSectionSum = 0;
  196. private int totalSum = 0;
  197. private int bonusSum = 0;
  198. private int sum = 0;
  199. int tempRollCounter = 0;
  200. // creates an instance of yatzy object.
  201. Yatzy yatzy = new Yatzy();
  202.  
  203. /*
  204. * Method to set disable off, if dice hasen't been rolled.
  205. * */
  206. public boolean setDisableOff() {
  207. if (yatzy.getThrowCount() == 0) {
  208. return true;
  209. } else {
  210. return false;
  211. }
  212. }
  213.  
  214.  
  215. /**
  216. * Method to roll the dices
  217. * */
  218. public void roll() {
  219. // checks if the player has selected a category
  220. // or if the player has rolled 3 times
  221. if(numRolls == 3 && hasClicked) {
  222. yatzy.resetThrowCount();
  223. didUserSelect = false;
  224. holdArray = new boolean[] {false,false,false,false,false};
  225. hasClicked = false;
  226. setSelectedOff();
  227. } else if (hasClicked || numRolls == 3) {
  228. /* if the user has selected a category or rolled 3 times
  229. * we reset the throw count
  230. * */
  231. yatzy.resetThrowCount();
  232. holdArray = new boolean[] {false,false,false,false,false};
  233. setSelectedOff();
  234. }
  235.  
  236.  
  237. // rolls the dice
  238. yatzy.throwDice(holdArray);
  239. int[] array = yatzy.getValues();
  240. getFaceValues(array);
  241. shownOrNot();
  242.  
  243. tempRollCounter++;
  244. numRolls = yatzy.getThrowCount();
  245. rolled.setText("rolled: " + numRolls);
  246. // Method calls
  247. setTextValues();
  248. updateUpperSum();
  249. setUpperSum();
  250. setLowerSum();
  251. setTotal();
  252.  
  253. }
  254. /*
  255. * Method to unhold each check box element, when the throw count resets
  256. * */
  257. public void setSelectedOff() {
  258. ObservableList<Node> checkboxes = holds.getChildren();
  259. int i = 0;
  260. for (Node element : checkboxes) {
  261. ((CheckBox) element).setSelected(holdArray[i]);
  262. i++;
  263. }
  264. }
  265. /*
  266. * Method to update the sum in upper section
  267. * */
  268. public void updateUpperSum() {
  269. upperSectionSum = 0;
  270. int sum = 0;
  271. // iteration over the first 6 elements in our disableValues array.
  272. for (int j = 0; j < 6; j++) {
  273. sum += disableValues[j];
  274. upperSectionSum += disableValues[j];
  275. }
  276.  
  277. /* if our sum is greater than or equal to 15
  278. * we set the bonus value*/
  279. if (sum >= 15)
  280. {
  281. // defines our bonus value
  282. bonusSum = 50;
  283. // Creates an observable list of nodes.
  284. ObservableList<Node> bonusTextfield = totalTextFields.getChildren();
  285. // Iteration over each element in nodelist
  286. for (Node element : bonusTextfield) {
  287. // we set our bonusSum value as inner Text on element.
  288. ((TextField)element).setText(""+bonusSum);
  289. break;
  290. }
  291. }
  292. }
  293. /* Method to show or not dice boxes and hold check boxes*/
  294. public void shownOrNot() {
  295. // Iteration over each check box to determine if shown or not.
  296. ObservableList<Node> checkboxes = holds.getChildren();
  297.  
  298. // foreach loop over each child in nodelist
  299. for (Node child : checkboxes) {
  300. // checks if our throwcount has been reset or not thrown at all,
  301. // if so we disable our elements.
  302. boolean showOrNot = setDisableOff();
  303. child.setDisable(showOrNot);
  304.  
  305. }
  306. }
  307. /**
  308. * Method to get face values of our dices
  309. * */
  310. public void getFaceValues(int[] array) {
  311.  
  312. // creates a increment variable, to increment in each iteration.
  313. int counter = 0;
  314. // grabs our node list of dice.
  315. ObservableList<Node> diceList = dices.getChildren();
  316. // foreach iteration over each txf element.
  317. for(Node textElement : diceList) {
  318. if(txf instanceof TextField) {
  319. // sets the text as innerText on each element.
  320. ((TextField)textElement).setText(String.valueOf(array[counter]));
  321. counter++;
  322. }
  323. }
  324. }
  325. /*
  326. * Method to calculate the upper sections sum
  327. * */
  328. public void setUpperSum() {
  329. // Grabs our node list of elements in our sumTextFields container.
  330. ObservableList<Node> sumTextField = sumTextFields.getChildren();
  331. // iteration over each element.
  332. for (Node element : sumTextField) {
  333. // sets the value
  334. ((TextField)element).setText("" + upperSectionSum);
  335. break;
  336. }
  337. }
  338. /*
  339. * Method to calculate the lower sections sum
  340. * */
  341. public void setLowerSum() {
  342.  
  343. totalSum = 0;
  344. // iteration over elements after index 6.
  345. for (int i = 6; i < disableValues.length; i++) {
  346. // grabs to total sum of those.
  347. totalSum += disableValues[i];
  348. }
  349. // grabs our node list from our sumTextField container
  350. ObservableList<Node> sumTextField = sumTextFields.getChildren();
  351. // iteration over each node and then after the first iteration, we make a sub list.
  352. // With the first element in our container and sets its innerText to our totalSum value.
  353. for (Node element : sumTextField.subList(1, sumTextField.size())) {
  354. ((TextField)element).setText("" + totalSum);
  355. }
  356. }
  357. /*
  358. * Method to calculate to total sum of all sums.
  359. * */
  360. public void setTotal() {
  361. // create an integer with the total value of all our sums
  362. int bonus = totalSum + upperSectionSum + bonusSum;
  363. // grabs our container and puts its children inside an observable list
  364. ObservableList<Node> bonusTextfield = totalTextFields.getChildren();
  365. // iteration over each element in list, and grabs our bonus field and sets its value to bonus.
  366. for (Node element : bonusTextfield.subList(1, bonusTextfield.size())) {
  367. ((TextField)element).setText("" + bonus);
  368. }
  369.  
  370. }
  371. /*
  372. * Method to update our hold array values to determine how many
  373. * dice are being held by the player
  374. * */
  375. public void updateHold(int num) {
  376. // creates an array of integers from our getValues method from our yatzy class.
  377. int[] values = yatzy.getValues();
  378. // then we grab the element at our num position in array.
  379. int getValue = values[num];
  380. // then we set element at n positions, value to true.
  381. holdArray[num] = true;
  382. }
  383.  
  384. /*
  385. * Method to feed our text elements with values.
  386. */
  387. public void setTextValues() {
  388. // test to make sure the user only is able to set one value at each round
  389. if (didUserSelect != true) {
  390. // create an node list from our text field containers children.
  391. ObservableList<Node> textFieldsElement = textFields.getChildren();
  392. // creates an array of integers from our getValues method from our yatzy class.
  393. int[] results = yatzy.getResults();
  394. // creates an integer increment variable
  395. int counter = 0;
  396. // for each loop over each node in node list
  397. for(Node temp : textFieldsElement) {
  398. // if the node is an instance of a TextField element.
  399. if(txf instanceof TextField) {
  400. // we feed the nodes with text as innerText.
  401. temp.setDisable(holdChosenTextValues[counter]);
  402. ((TextField)temp).setText("" + results[counter]);
  403. if (temp.isDisable()) {
  404. ((TextField)temp).setText("" + disableValues[counter]);
  405. }
  406. counter++;
  407. }
  408. }
  409. } else {
  410. System.out.println("You already selected something");
  411. }
  412. }
  413. /*
  414. * Method to hide the selected element, witch makes sure the user can't select the same
  415. * box again.
  416. * */
  417. public boolean setValueOnSelectedElement(int selectedIndex) {
  418. // creates an array of integers from our getValues method from our yatzy class.
  419. int[] results = yatzy.getResults();
  420. // with the value of our selectedIndex we pick the element at n position in our disableValues array
  421. // and sets its value equal to our results array n position value.
  422. disableValues[selectedIndex] = results[selectedIndex];
  423. // creates an boolean, to determine if we should hide our element.
  424. boolean hideElement = false;
  425. // we set the element at our holdChosenTextValues arrays position to true.
  426. holdChosenTextValues[selectedIndex] = true;
  427. // and then we feed that value to our hideElement.
  428. hideElement = holdChosenTextValues[selectedIndex];
  429. // returns our hideElement.
  430. return hideElement;
  431. }
  432.  
  433.  
  434.  
  435. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement