Game Development Reference
In-Depth Information
The
getScore
method is the only method called from within the game. It needs three values, the first being the
type of category, such as
ones
or
fullHouse.
This determines what scoring function to call. The second is an array of the
dice values, and lastly is the button key, which is a number used in certain types of calculations.
To make things easier in certain scoring functions, the dice array is sorted so the dice are in order sequentially.
Next, the
dice
and
btnKey
properties are set so you can access them in each function.
■
Note
Since you are in the scope of
Scoring
,
this
can be used to access all properties and methods that belong to it.
Next, the
type
parameter is used to find the appropriate function to execute. A
switch
statement is added to the
end the
getScore
function (see Listing 7-32).
Listing 7-32.
A Switch Statement Is Added to Determine the Scoring Function to Run
Scoring.getScore = function (type, dice, key) {
dice.sort();
this.dice = dice;
this. btnKey = key;
switch (type) {
case 'ones':
case 'twos':
case 'threes':
case 'fours':
case 'fives':
case 'sixes':
return this.getNumberScore();
break;
case 'threeKind':
case 'fourKind':
return this.getKinds();
break;
case 'small':
case 'large':
this.dice = this.uniqueArray(this.dice);
return this.getStraights();
break;
case 'fullHouse':
return this.getFullHouse();
break;
case 'chance':
return this.getChance();
break;
case 'fakezee':
return this.getFakezee();
break;
case 'bonus':
return this.getFakezee() * 2;
break;
}
}