Create a Yahtzee Dice Roller and Scorer in Compiled JavaFX Script
Join the DZone community and get the full member experience.
Join For Freei'm still at the server side java symposium in las vegas , and i think that walking by all these slot machines has inspired this blog post. today's example builds on the program in the roll the dice post in order to create a yahtzee dice roller and scorer. each time you click the roll button, the five dice assume random values, and the combination of values is scored according to the possible categories in the bottom portion of a yahtzee scoring sheet. here's a screenshot:
in addition to the dice.fx and pipplacement.fx files from the roll the dice post, this program consists of the following source code files.
yahtzeemain.fx
/*
* yahtzeemain.fx -
* a compiled javafx program that demonstrates creating custom
* components with compositenode and evaluates yahtzee dice rolls.
*
*
* developed 2008 by james l. weaver (jim.weaver at lat-inc.com)
* 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
*
* developed 2008 by james l. weaver (jim.weaver at lat-inc.com)
* 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);
}
}
a javafx script concept that we haven't covered yet is the set of static functions available in the new javafx.lang.sequences class. these functions enable you to perform operations (such as sorting) on a sequence. in this program i'm using the max and min functions of that class to find the largest and smallest value in a dice roll.
regards,
jim weaver
javafx script: dynamic java scripting for rich internet/client-side applications
immediate ebook (pdf) download available at
the book's apress site
Opinions expressed by DZone contributors are their own.
Trending
-
Auto-Scaling Kinesis Data Streams Applications on Kubernetes
-
Batch Request Processing With API Gateway
-
Top 10 Pillars of Zero Trust Networks
-
Hibernate Get vs. Load
Comments