Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gui;
- import model.Yatzy;
- import java.util.Arrays;
- import javafx.application.Application;
- import javafx.collections.ObservableList;
- import javafx.event.EventHandler;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Node;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.CheckBox;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextField;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.VBox;
- import javafx.stage.Stage;
- public class Gui extends Application{
- public static void main(String[] args) {
- launch();
- }
- /*
- * We define all of our elements and containers as globals.
- * So that we interact with each element in our methods.
- * */
- HBox dices;
- HBox holds;
- Label rolled;
- VBox textFields;
- TextField txf;
- VBox sumTextFields;
- VBox totalTextFields;
- // global variables used when application start.
- boolean globalVisibility = false;
- private int numRolls = 0;
- private boolean hasClicked = false;
- public void start(Stage stage) {
- stage.setTitle("Yatzy");
- stage.setResizable(false);
- dices = new HBox(10);
- for(int i = 0; i < 5; i++) {
- TextField dice = new TextField("");
- dice.setPrefSize(80, 80);
- dice.setFocusTraversable(false);
- dice.setEditable(false);
- dices.getChildren().add(dice);
- }
- holds = new HBox(43);
- holds.setStyle("-fx-padding: 10 0 0 12;");
- // CheckBox hold; // = new CheckBox("Hold");
- for(int i = 0; i < 5; i++) {
- CheckBox hold = new CheckBox("Hold");
- hold.setFocusTraversable(false);
- holds.getChildren().add(hold);
- boolean disableOff = setDisableOff();
- hold.setDisable(disableOff);
- hold.setId(String.valueOf(i));
- hold.setOnAction(event -> {
- if (hold.isSelected()) {
- int parseID = Integer.parseInt(hold.getId());
- updateHold(parseID);
- }
- });
- }
- HBox roll = new HBox(26);
- roll.setStyle("-fx-padding: 10 0 0 230;");
- Button btnRoll = new Button("Roll");
- btnRoll.setOnAction(event -> this.roll());
- btnRoll.setPrefSize(80, 40);
- btnRoll.setStyle("-fx-font-size: 20px;");
- rolled = new Label("Rolled: " + numRolls);
- rolled.setStyle("-fx-padding: 12 0 0 0");
- roll.getChildren().addAll(btnRoll, rolled);
- VBox topPanel = new VBox(dices, holds, roll);
- topPanel.setPadding(new Insets(10));
- 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" };
- VBox labels = new VBox(16);
- labels.setStyle("-fx-padding: 3 10 0 0;");
- textFields = new VBox(8);
- textFields.setStyle("-fx-padding: 0 10 0 0;");
- VBox sumLabels = new VBox(280);
- sumLabels.setStyle("-fx-padding: 169 10 0 0;");
- sumTextFields = new VBox(272);
- sumTextFields.setStyle("-fx-padding: 166 10 0 0;");
- VBox totalLabels = new VBox(280);
- totalLabels.setStyle("-fx-padding: 169 10 0 0;");
- totalTextFields = new VBox(272);
- totalTextFields.setStyle("-fx-padding: 166 10 0 0;");
- Label lbl;
- for(int i = 0; i < labelTexts.length; i++) {
- lbl = new Label(labelTexts[i]);
- labels.getChildren().add(lbl);
- txf = new TextField();
- String index = String.valueOf(i);
- txf.setId(index);
- int IAmGlobal = i;
- txf.setOnMouseClicked(e -> {
- if (!hasClicked) {
- System.out.println("Clicked on " + IAmGlobal);
- System.out.println(holdChosenTextValues[IAmGlobal]);
- holdChosenTextValues[IAmGlobal] = true;
- boolean visibility = setValueOnSelectedElement(IAmGlobal);
- hasClicked = true;
- } else {
- System.out.println("you already clicked");
- }
- });
- txf.setPrefWidth(60);
- txf.setFocusTraversable(false);
- txf.setEditable(false);
- txf.setAlignment(Pos.CENTER_RIGHT);
- textFields.getChildren().add(txf);
- }
- for(int n = 0; n < 2; n++) {
- lbl = new Label("Sum:");
- txf = new TextField("0");
- txf.setPrefWidth(60);
- txf.setFocusTraversable(false);
- txf.setEditable(false);
- txf.setAlignment(Pos.CENTER_RIGHT);
- txf.setStyle("-fx-text-inner-color: blue;");
- sumLabels.getChildren().add(lbl);
- sumTextFields.getChildren().add(txf);
- }
- for(int j = 0; j < 2; j++) {
- lbl = new Label("Bonus:");
- txf = new TextField("0");
- txf.setPrefWidth(60);
- txf.setFocusTraversable(false);
- txf.setEditable(false);
- txf.setAlignment(Pos.CENTER_RIGHT);
- txf.setStyle("-fx-text-inner-color: blue;");
- if(j == 1) {
- lbl.setText("Total:");
- }
- totalLabels.getChildren().add(lbl);
- totalTextFields.getChildren().add(txf);
- }
- HBox bottomPanel = new HBox(labels, textFields, sumLabels, sumTextFields, totalLabels, totalTextFields);
- bottomPanel.setPadding(new Insets(10));
- VBox rootPane = new VBox(10, topPanel, bottomPanel);
- rootPane.setPadding(new Insets(10));
- Scene scene = new Scene(rootPane);
- stage.setScene(scene);
- topPanel.setStyle("-fx-border-style: solid;");
- bottomPanel.setStyle("-fx-border-style: solid;");
- stage.show();
- }
- // global arrays.
- boolean[] holdArray = new boolean[] {false,false,false,false,false};
- boolean[] holdChosenTextValues = new boolean[]
- {false,false,false,false,false,false,false,false,
- false,false,false,false,false,false,false};
- private int[] disableValues = new int[15];
- // global variables
- boolean didUserSelect = false;
- private int upperSectionSum = 0;
- private int totalSum = 0;
- private int bonusSum = 0;
- private int sum = 0;
- int tempRollCounter = 0;
- // creates an instance of yatzy object.
- Yatzy yatzy = new Yatzy();
- /*
- * Method to set disable off, if dice hasen't been rolled.
- * */
- public boolean setDisableOff() {
- if (yatzy.getThrowCount() == 0) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * Method to roll the dices
- * */
- public void roll() {
- // checks if the player has selected a category
- // or if the player has rolled 3 times
- if(numRolls == 3 && hasClicked) {
- yatzy.resetThrowCount();
- didUserSelect = false;
- holdArray = new boolean[] {false,false,false,false,false};
- hasClicked = false;
- setSelectedOff();
- } else if (hasClicked || numRolls == 3) {
- /* if the user has selected a category or rolled 3 times
- * we reset the throw count
- * */
- yatzy.resetThrowCount();
- holdArray = new boolean[] {false,false,false,false,false};
- setSelectedOff();
- }
- // rolls the dice
- yatzy.throwDice(holdArray);
- int[] array = yatzy.getValues();
- getFaceValues(array);
- shownOrNot();
- tempRollCounter++;
- numRolls = yatzy.getThrowCount();
- rolled.setText("rolled: " + numRolls);
- // Method calls
- setTextValues();
- updateUpperSum();
- setUpperSum();
- setLowerSum();
- setTotal();
- }
- /*
- * Method to unhold each check box element, when the throw count resets
- * */
- public void setSelectedOff() {
- ObservableList<Node> checkboxes = holds.getChildren();
- int i = 0;
- for (Node element : checkboxes) {
- ((CheckBox) element).setSelected(holdArray[i]);
- i++;
- }
- }
- /*
- * Method to update the sum in upper section
- * */
- public void updateUpperSum() {
- upperSectionSum = 0;
- int sum = 0;
- // iteration over the first 6 elements in our disableValues array.
- for (int j = 0; j < 6; j++) {
- sum += disableValues[j];
- upperSectionSum += disableValues[j];
- }
- /* if our sum is greater than or equal to 15
- * we set the bonus value*/
- if (sum >= 15)
- {
- // defines our bonus value
- bonusSum = 50;
- // Creates an observable list of nodes.
- ObservableList<Node> bonusTextfield = totalTextFields.getChildren();
- // Iteration over each element in nodelist
- for (Node element : bonusTextfield) {
- // we set our bonusSum value as inner Text on element.
- ((TextField)element).setText(""+bonusSum);
- break;
- }
- }
- }
- /* Method to show or not dice boxes and hold check boxes*/
- public void shownOrNot() {
- // Iteration over each check box to determine if shown or not.
- ObservableList<Node> checkboxes = holds.getChildren();
- // foreach loop over each child in nodelist
- for (Node child : checkboxes) {
- // checks if our throwcount has been reset or not thrown at all,
- // if so we disable our elements.
- boolean showOrNot = setDisableOff();
- child.setDisable(showOrNot);
- }
- }
- /**
- * Method to get face values of our dices
- * */
- public void getFaceValues(int[] array) {
- // creates a increment variable, to increment in each iteration.
- int counter = 0;
- // grabs our node list of dice.
- ObservableList<Node> diceList = dices.getChildren();
- // foreach iteration over each txf element.
- for(Node textElement : diceList) {
- if(txf instanceof TextField) {
- // sets the text as innerText on each element.
- ((TextField)textElement).setText(String.valueOf(array[counter]));
- counter++;
- }
- }
- }
- /*
- * Method to calculate the upper sections sum
- * */
- public void setUpperSum() {
- // Grabs our node list of elements in our sumTextFields container.
- ObservableList<Node> sumTextField = sumTextFields.getChildren();
- // iteration over each element.
- for (Node element : sumTextField) {
- // sets the value
- ((TextField)element).setText("" + upperSectionSum);
- break;
- }
- }
- /*
- * Method to calculate the lower sections sum
- * */
- public void setLowerSum() {
- totalSum = 0;
- // iteration over elements after index 6.
- for (int i = 6; i < disableValues.length; i++) {
- // grabs to total sum of those.
- totalSum += disableValues[i];
- }
- // grabs our node list from our sumTextField container
- ObservableList<Node> sumTextField = sumTextFields.getChildren();
- // iteration over each node and then after the first iteration, we make a sub list.
- // With the first element in our container and sets its innerText to our totalSum value.
- for (Node element : sumTextField.subList(1, sumTextField.size())) {
- ((TextField)element).setText("" + totalSum);
- }
- }
- /*
- * Method to calculate to total sum of all sums.
- * */
- public void setTotal() {
- // create an integer with the total value of all our sums
- int bonus = totalSum + upperSectionSum + bonusSum;
- // grabs our container and puts its children inside an observable list
- ObservableList<Node> bonusTextfield = totalTextFields.getChildren();
- // iteration over each element in list, and grabs our bonus field and sets its value to bonus.
- for (Node element : bonusTextfield.subList(1, bonusTextfield.size())) {
- ((TextField)element).setText("" + bonus);
- }
- }
- /*
- * Method to update our hold array values to determine how many
- * dice are being held by the player
- * */
- public void updateHold(int num) {
- // creates an array of integers from our getValues method from our yatzy class.
- int[] values = yatzy.getValues();
- // then we grab the element at our num position in array.
- int getValue = values[num];
- // then we set element at n positions, value to true.
- holdArray[num] = true;
- }
- /*
- * Method to feed our text elements with values.
- */
- public void setTextValues() {
- // test to make sure the user only is able to set one value at each round
- if (didUserSelect != true) {
- // create an node list from our text field containers children.
- ObservableList<Node> textFieldsElement = textFields.getChildren();
- // creates an array of integers from our getValues method from our yatzy class.
- int[] results = yatzy.getResults();
- // creates an integer increment variable
- int counter = 0;
- // for each loop over each node in node list
- for(Node temp : textFieldsElement) {
- // if the node is an instance of a TextField element.
- if(txf instanceof TextField) {
- // we feed the nodes with text as innerText.
- temp.setDisable(holdChosenTextValues[counter]);
- ((TextField)temp).setText("" + results[counter]);
- if (temp.isDisable()) {
- ((TextField)temp).setText("" + disableValues[counter]);
- }
- counter++;
- }
- }
- } else {
- System.out.println("You already selected something");
- }
- }
- /*
- * Method to hide the selected element, witch makes sure the user can't select the same
- * box again.
- * */
- public boolean setValueOnSelectedElement(int selectedIndex) {
- // creates an array of integers from our getValues method from our yatzy class.
- int[] results = yatzy.getResults();
- // with the value of our selectedIndex we pick the element at n position in our disableValues array
- // and sets its value equal to our results array n position value.
- disableValues[selectedIndex] = results[selectedIndex];
- // creates an boolean, to determine if we should hide our element.
- boolean hideElement = false;
- // we set the element at our holdChosenTextValues arrays position to true.
- holdChosenTextValues[selectedIndex] = true;
- // and then we feed that value to our hideElement.
- hideElement = holdChosenTextValues[selectedIndex];
- // returns our hideElement.
- return hideElement;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement