Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Yahtzee game in JAVAFX with code below : -add a button to roll the dice

ID: 3913413 • Letter: C

Question

Create a Yahtzee game in JAVAFX with code below :

-add a button to roll the dice

- allow 3 rolls per round.only 10 rounds

-adding scoring to it

Should be done in Java FX

// DiceGame.java

import java.util.Random;

import javafx.application.Application;

import static javafx.application.Application.launch;

import javafx.scene.Scene;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.scene.layout.HBox;

import javafx.scene.layout.Pane;

import javafx.stage.Stage;

public class DiceGame extends Application {

    Image images[];

    ImageView imageViews[];

    /**

     * method to read dice images from files

     */

    public void setupDiceImages() {

        images = new Image[6];

        /**

         * loading each dice images into the Image object. Please replace the

         * path with your dice images path. Dont remove the prefix 'file:'

         */

        images[0] = new Image("file:D:\work\June 2018\javafx dice\dice\1.png");

        images[1] = new Image("file:D:\work\June 2018\javafx dice\dice\2.png");

        images[2] = new Image("file:D:\work\June 2018\javafx dice\dice\3.png");

        images[3] = new Image("file:D:\work\June 2018\javafx dice\dice\4.png");

        images[4] = new Image("file:D:\work\June 2018\javafx dice\dice\5.png");

        images[5] = new Image("file:D:\work\June 2018\javafx dice\dice\6.png");

    }

    /**

     * method to roll the 5 dices and display 5 random dice roll images in the

     * imageviews

     */

    public void roll() {

        Random random = new Random();

        for (int i = 0; i < imageViews.length; i++) {

            /**

             * generating a value between 0 and 5

             */

            int index = random.nextInt(6);

            /**

             * setting dice image at this random index as current imageview's

             * image

             */

            imageViews[i].setImage(images[index]);

        }

    }

    @Override

    public void start(Stage primaryStage) {

        //reading dice images

        setupDiceImages();

        //setting up 5 imageviews for displaying dices

        imageViews = new ImageView[5];

        /**

         * initializing imageviews

         */

        for (int i = 0; i < imageViews.length; i++) {

            imageViews[i] = new ImageView();

            //setting height and width

            imageViews[i].setFitHeight(150);

            imageViews[i].setFitWidth(150);

        }

        /**

         * Defining an HBox to arrange all imageviews

         */

        HBox hBox = new HBox(imageViews);

        hBox.setSpacing(20);//space between each elements

        //rolling the 5 dices

        roll();

        /**

         * creating a pane and adding this hbox to it

         */

        Pane pane = new Pane(hBox);

        /**

         * defining and displaying the scene

         */

        Scene scene = new Scene(pane);

        primaryStage.setScene(scene);

        primaryStage.show();

    }

    public static void main(String[] args) {

        launch(args);

    }

}

Explanation / Answer

YahtzeeMain.fx

/*

* YahtzeeMain.fx -

* A compiled JavaFX program that demonstrates creating custom

* components with CompositeNode and evaluates Yahtzee dice rolls

* to serve as a JavaFX Script example.

*/

import javafx.ui.*;

import javafx.ui.canvas.*;

import java.lang.System;

Frame {

var model = YahtzeeModel {}

width: 510

height: 400

title: "Roll Dice and Evaluate Yahtzee Combinations"

background: Color.WHITE

content:

BorderPanel {

center:

Box {

var evalFont =

Font {

size: 20

}

orientation: Orientation.VERTICAL

content: [

Canvas {

content:

for (diceNum in [0 .. model.numDice - 1]) {

model.newDice =

Dice {

x: diceNum * 100 + 10

y: 10

width: 80

height: 80

faceColor: Color.RED

pipColor: Color.WHITE

}

}

},

GroupPanel {

var fiveOfKindRow = Row { alignment: Alignment.BASELINE }

var largeStraightRow = Row { alignment: Alignment.BASELINE }

var smallStraightRow = Row { alignment: Alignment.BASELINE }

var fullHouseRow = Row { alignment: Alignment.BASELINE }

var fourOfKindRow = Row { alignment: Alignment.BASELINE }

var threeOfKindRow = Row { alignment: Alignment.BASELINE }

var chanceRow = Row { alignment: Alignment.BASELINE }

var labelsColumn = Column {

alignment: Alignment.TRAILING

}

var fieldsColumn = Column {

alignment: Alignment.LEADING

}

rows: [

fiveOfKindRow,

largeStraightRow,

smallStraightRow,

fullHouseRow,

fourOfKindRow,

threeOfKindRow,

chanceRow

]

columns: [

labelsColumn,

fieldsColumn

]

content: [

SimpleLabel {

font: evalFont

row: fiveOfKindRow

column: labelsColumn

text: "Five of a Kind (Yahtzee):"

},

SimpleLabel {

font: evalFont

row: fiveOfKindRow

column: fieldsColumn

text: bind

if (model.fiveOfKind)

"{model.fiveOfKindScore}"

else "N/A"

},

  

SimpleLabel {

font: evalFont

row: largeStraightRow

column: labelsColumn

text: "Large Straight:"

},

SimpleLabel {

font: evalFont

row: largeStraightRow

column: fieldsColumn

text: bind

if (model.largeStraight)

"{model.largeStraightScore}"

else "N/A"

},

  

SimpleLabel {

font: evalFont

row: smallStraightRow

column: labelsColumn

text: "Small Straight:"

},

SimpleLabel {

font: evalFont

row: smallStraightRow

column: fieldsColumn

text: bind

if (model.smallStraight)

"{model.smallStraightScore}"

else "N/A"

},

  

SimpleLabel {

font: evalFont

row: fullHouseRow

column: labelsColumn

text: "Full House:"

},

SimpleLabel {

font: evalFont

row: fullHouseRow

column: fieldsColumn

text: bind

if (model.fullHouse)

"{model.fullHouseScore}"

else "N/A"

},

  

SimpleLabel {

font: evalFont

row: fourOfKindRow

column: labelsColumn

text: "Four of a Kind:"

},

SimpleLabel {

font: evalFont

row: fourOfKindRow

column: fieldsColumn

text: bind

if (model.fourOfKind)

"{model.sumOfDiceValues}"

else "N/A"

},

  

SimpleLabel {

font: evalFont

row: threeOfKindRow

column: labelsColumn

text: "Three of a Kind:"

},

SimpleLabel {

font: evalFont

row: threeOfKindRow

column: fieldsColumn

text: bind

if (model.threeOfKind)

"{model.sumOfDiceValues}"

else "N/A"

},

  

SimpleLabel {

font: evalFont

row: chanceRow

column: labelsColumn

text: "Chance:"

},

SimpleLabel {

font: evalFont

row: chanceRow

column: fieldsColumn

text: bind "{model.sumOfDiceValues}"

},

]

},

]

}  

bottom:

FlowPanel {

content:

Button {

text: "Roll"

defaultButton: true

action:

function():Void {

model.roll();

}

}

}

}

visible: true

onClose:

function():Void {

System.exit(0);

}

}

YahtzeeModel.fx

/*

* YahtzeeModel.fx -

* The model behind the Yahtzee dice roll and combination evaluation

* to serve as a JavaFX Script example.

*/

import javafx.lang.Sequences;

import java.lang.System;

class YahtzeeModel {

attribute numDice:Integer = 5;

attribute diceDistribution:Integer[];

attribute newDice:Dice on replace {

insert newDice into dice;

}

attribute dice:Dice[];

  

attribute fiveOfKind:Boolean;

attribute largeStraight:Boolean;

attribute smallStraight:Boolean;

attribute fullHouse:Boolean;

attribute fourOfKind:Boolean;

attribute threeOfKind:Boolean;

attribute fiveOfKindScore:Integer = 50;

attribute largeStraightScore:Integer = 40;

attribute smallStraightScore:Integer = 30;

attribute fullHouseScore:Integer = 25;

attribute sumOfDiceValues:Integer = 0;

  

function roll():Void {

for (die in dice) {

die.roll();

}

evalYahtzeeCombos();

}

  

function evalYahtzeeCombos() {

var values =

for (val in dice) {

val.value;

}

var maxVal:Integer = Sequences.max(values, null) as Integer;

var minVal:Integer = Sequences.min(values, null) as Integer;

// Create a sequence that contains the distribution of values

// and Calclulate the sum of the dice values

diceDistribution =

for (i in [1 .. 6]) 0;

sumOfDiceValues = 0;

  

for (val in values) {

diceDistribution[val - 1]++;

sumOfDiceValues += val;

}

  

// Determine if five-of-a-kind

fiveOfKind =

((for (occurance in diceDistribution

where occurance >= 5) occurance) <> []);   

  

// Determine if four-of-a-kind

fourOfKind =

((for (occurance in diceDistribution

where occurance >= 4) occurance) <> []);

  

// Determine if three-of-a-kind

threeOfKind =

sizeof (for (occurance in diceDistribution

where occurance >= 3) occurance) > 0;

// Determine if full house

fullHouse =

sizeof (for (occurance in diceDistribution

where occurance == 3) occurance) > 0 and

sizeof (for (occurance in diceDistribution

where occurance == 2) occurance) > 0;   

// Determine if large straight

largeStraight =

sizeof (for (occurance in diceDistribution

where occurance > 1) occurance) == 0 and

(maxVal - minVal == 4);

  

// Determine if small straight

smallStraight =

sizeof (for (occurance in diceDistribution

where occurance == 2) occurance) == 1 and

(maxVal - minVal == 3);

}

}