Game Development Reference
In-Depth Information
Each scoring calculation function returns the score, which is then immediately returned back to the game, where
the method was called.
// from game with static values
var score = Scoring.getScore('sixes',[2,4,6,6,1],6); // returns 12
You can see how only one line of code is needed within your game logic to retrieve each score. You'll see next, in
the “Scoring for Number Categories” section, where the button key 6 came into play in the previous example.
Scoring for Number Categories
All six categories in section one use this next function. The scoring for this category works by adding the total of all
dice with the number that the category represents. For instance, if the category type is threes, the sum of all dice with
the value of 3 is returned (see Listing 7-33).
Listing 7-33. The Ones Through Sixes Categories Used the getNumberScore
Scoring.getNumberScore = function () {
var i, value;
var score = 0;
var len = this.dice.length;
for (i = 0; i < len; i++) {
if (this.dice[i] == this.btnKey) {
score += this.dice[i];
}
}
return score;
}
In this function, you loop through each die value and append its value to the score variable, but only if its value is
equal to btnKey . In this case, this is how the button key is used. Finally, the score is returned.
Scoring for Kinds
Two categories use this next function. For a three of a kind, a score is rewarded if three of the dice have equal values.
The awarded score is the sum all dice. The same scoring is used for four-of-a-kinds as well (see Listing 7-34).
Listing 7-34. Scoring for Three- and Four-of-a-kinds
Scoring.getKinds = function () {
var i;
var match = 0;
var score = 0;
var pass = false;
var matchesNeeded = this.btnKey;
var len = this.dice.length;
for (i = 0; i < len; i++) {
score += this.dice[i];
if (this.dice[i] == this.dice[i + 1]) {
if (i != this.dice.length) {
match++;
 
Search WWH ::




Custom Search