Game Development Reference
In-Depth Information
if (match >= matchesNeeded) {
pass = true;
}
}
}
else {
match = 0;
}
}
score = pass ? score : 0;
return score;
}
Scoring for kinds is one of the functions that take advantage of the dice being sorted. Looping through the dice,
each die is compared to the die in front of it. When matches are found, the local variable
match
is incremented.
Along with
match
being declared as 0, a few more variables are set. The
score
variable starts at 0 and is
incremented with each iteration of the loop by the value of each die. At the end of the loop, the
score
variable will
contain the sum of all dice. The
pass
value is set to true as soon as the appropriate consecutive matches are found.
This
pass
variable is determined by comparing matches against the button key and will be used to determine if the
player is awarded the score or not.
Scoring for Straights
Two categories use this next function. Thirty points are awarded for four dice that form a sequence. Forty points are
given if all dice form a sequence (see Listing 7-35).
Listing 7-35.
Small and Large Straight Scores
Scoring.getStraights = function () {
var i;
var match = 0;
var score = this.btnValue == 4 ? 30 : 40;
var matchesNeeded = this.btnValue;
var pass = false;
var len = this.dice.length - 1;
for (i = 0; i < len ; i++) {
if (this.dice[i] == (this.dice[i + 1] - 1)) {
match++;
if (match >= matchesNeeded) {
pass = true;
break;
}
}
else {
match = 0;
}
}
score = pass ? score : 0;
return score;
}