Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here below is my code for Yathzee. It is working well, can roll three times, check the dices I want to keep (but not allowed

Here below is my code for Yathzee. It is working well, can roll three times, check the dices I want to keep (but not allowed until I have rolled once). I am having trouble with figuring out how to calculate the results, especially the Full house, the last method in my code. Can you help me with the Full house?

package assignment_2; import javafx.application.Application; import javafx.stage.Stage; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Random; import java.util.concurrent.atomic.AtomicInteger; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx.scene.text.Text; public class Yahtzee extends Application { Image images[]; ImageView imageViews[]; int count = 0; public static void main(String[] args) { launch(args); } public void diceImages() throws FileNotFoundException { images = new Image[6]; images[0] = new Image(new FileInputStream("src/d1.png")); images[1] = new Image(new FileInputStream("src/d2.png")); images[2] = new Image(new FileInputStream("src/d3.png")); images[3] = new Image(new FileInputStream("src/d4.png")); images[4] = new Image(new FileInputStream("src/d5.png")); images[5] = new Image(new FileInputStream("src/d6.png")); } public void roll() { Random random = new Random(); for (int i = 0; i < imageViews.length; i++) { int index = random.nextInt(6); imageViews[i].setImage(images[index]); } } @Override public void start(Stage primaryStage) throws FileNotFoundException { diceImages();//read images imageViews = new ImageView[5]; for (int i = 0; i < imageViews.length; i++) { imageViews[i] = new ImageView(); imageViews[i].setFitHeight(70); imageViews[i].setFitWidth(70); } VBox root = new VBox(); GridPane layout = new GridPane(); layout.setAlignment(Pos.CENTER); layout.setHgap(1); layout.setVgap(5); VBox titleText = new VBox (0); Text title = new Text("Yahtzee"); title.setFont(Font.font ("Verdana",30)); titleText.getChildren().add(title); titleText.setAlignment(Pos.CENTER_LEFT); titleText.setPadding(new Insets(10,0,0,0)); HBox dices1 = new HBox(imageViews); dices1.setAlignment(Pos.CENTER); dices1.setSpacing(10); roll(); VBox button = new VBox (0); HBox checkboxes = new HBox(); CheckBox d1 = new CheckBox(); CheckBox d2 = new CheckBox(); CheckBox d3 = new CheckBox(); CheckBox d4 = new CheckBox(); CheckBox d5 = new CheckBox(); checkboxes.setSpacing(60); checkboxes.setAlignment(Pos.CENTER); checkboxes.setPadding(new Insets(5,5,10,5)); checkboxes.getChildren().addAll(d1,d2,d3,d4,d5); button.setAlignment(Pos.CENTER); Button theButton = new Button("Roll the dice!"); final Label result1 = new Label(); final StringBuilder result = new StringBuilder(); result1.setPadding(new Insets(5,5,5,5)); button.getChildren().addAll(checkboxes, theButton, result1); if(count ==0 ){ String roll2 = "You have 3 rolls left"; result1.setText(roll2);} theButton.setOnAction(e -> { if(count==0){roll();} Random random = new Random(); if (d1.isSelected() == false) { imageViews[0].setImage(images[random.nextInt(6)]); } if (d2.isSelected() == false) { imageViews[1].setImage(images[random.nextInt(6)]); } if (d3.isSelected() == false) { imageViews[2].setImage(images[random.nextInt(6)]); } if (d4.isSelected() == false) { imageViews[3].setImage(images[random.nextInt(6)]); } if (d5.isSelected() == false) { imageViews[4].setImage(images[random.nextInt(6)]); } if(count ==0 ){ String roll2 = "You have 2 rolls left"; result1.setText(roll2); } if(count ==1 ){ String roll2 = "You have 1 rolls left"; result1.setText(roll2); } if(count ==2 ){ theButton.setDisable(true); if(CalculateFourOfAKind(imageViews)==true){ String roll2 = "Four of a kind"; result1.setText(roll2);} else if (CalculateThreeOfAKind(imageViews)==true){ String roll2 = "Three of a kind"; result1.setText(roll2);} else if (CalculatePair(imageViews)==true){ String roll2 = "Two of a kind"; result1.setText(roll2);} } count++; }); layout.add(titleText, 10, 5); layout.add(dices1, 10, 8); layout.add(button, 10,10); root.getChildren().addAll(layout); Scene scene= new Scene(root,450,250); primaryStage.setTitle("Yahtzee"); primaryStage.setScene(scene); primaryStage.show(); } public boolean CalculateThreeOfAKind( ImageView[] myDice ) { boolean threeOfAKind = false; for( int i = 0; i  2 ) threeOfAKind = true; } } return threeOfAKind; } public boolean CalculateFourOfAKind( ImageView[] myDice ) { boolean fourOfAKind = false; for( int i = 0; i  3 ) fourOfAKind = true; } } return fourOfAKind; } public boolean CalculatePair( ImageView[] myDice ) { boolean twoOfAKind = false; for( int i = 0; i  1 ) twoOfAKind = true; } } return twoOfAKind; } public boolean fullHouse( ImageView[] myDice){ boolean fullHouse = false; if(CalculateThreeOfAKind(myDice)==true && CalculatePair(myDice) == true){ boolean nextTo = false; //double for loop } return fullHouse; } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Why is it important to have a good understanding of statistics.

Answered: 1 week ago