Game Development Reference
In-Depth Information
This function basically works the same way as the getKinds ; however, when comparing two dice for equal values
in the loop, the next die value is subtracted by one. If they match in value, then you know that they are in sequence.
This continues on and either increments matches or resets it back to 0. The button key is also used in this function as
the necessary goal for matches.
Before running this function, a function was called to extract any duplicate values in the dice values array. Since
it is quite possible for two dice to be of the same value and still achieve a small straight, this could break the match
count in the middle of a run. Consider the following dice values:
[1,2,3,3,4];
During the loop, the third and fourth dice will not pass the conditional, and this will result in a reset of the match
count. Therefore it is necessary to make this array a unique array.
[1,2,3,4];
This procedure is accomplished by adding the utility function, uniqueArray to the Scoring object (see Listing 7-36).
Listing 7-36. The uniqueArray Utility Function Removes All Duplicates in an Array
Scoring.uniqueArray = function (a) {
var temp = {};
for (var i = 0; i < a.length; i++) {
temp[a[i]] = true;
}
var r = [];
for (var k in temp) {
r.push(k / 1);
}
return r;
}
This simple utility function loops through the dice values and assigns each as a property name in a temporary,
local object. When coming across a value that has already been added to the object, it simply overrides it. You would
end up with this object, using the previous example array:
{1:true,2:true,3:true,4:true}
This object can now be used in a loop to create and return a unique array.
Scoring for a Full House
A full house is used for only one category, so the button's key is not needed. Listing 7-37 shows how to check the dice
for a full house.
Listing 7-37. Scoring for the Full House Category
Scoring.getFullHouse = function () {
var pass = false;
var score = 0;
if (this.dice[0] == this.dice[1] && this.dice[1] != this.dice[2] &&
this.dice[2] == this.dice[3] && this.dice[3] == this.dice[4]) {
pass = true;
}
 
Search WWH ::




Custom Search