Game Development Reference
In-Depth Information
a bit easier, let's encode each jewel by using three integers from 0 to 2. Let's say the first integer
represents the color (yellow, blue, or red), the second integer the shape (diamond, oval, or round),
and the last integer the number of jewels (one, two, or three). Using this encoding scheme, you can,
for example, encode the blue oval-shaped single jewel as (1,0,0). The yellow round single jewel is
defined as (0,2,0), and the red oval-shaped triple jewel is defined as (2,1,2).
Note Humans would probably try to find valid combinations of jewels in a different way, by simply looking
at the row that was just shifted and the rows above and below. A computer can run periodic checks in the
main loop to check all combinations of three in the center column, even if the player hasn't touched it, as is
done here. Computers can do that, so programmers write it that way; but if you're new to programming, it
may take you some time to get used to thinking this way.
Now let's see if you can use this encoding scheme to find valid combinations of three jewels (let's
call them jewels A, B, and C). For each jewel, you have to compare the color, the shape, and the
number. Each of these properties has to be either the same for all jewels, or different for all jewels.
For example, if A has encoding value 0 for the color, B has value 0, and C also has value 0, then
the condition holds for the color, because all three jewels have the same color (yellow). The same is
true if the jewels all are blue (A-color = 1, B-color = 1, C-color = 1) or red (A-color = 2, B-color = 2,
C-color = 2). Finally, the condition holds if all their colors are different: there is an ordering of A-color,
B-color, and C-color that yields 0, 1, and 2. If you look at the sum of these different combinations,
you see an interesting property: 0 + 0 + 0 = 0, 1 + 1 + 1 = 3, 2 + 2 + 2 = 6, and 0 + 1 + 2 = 3. In other
words: the sum is divisible by three . Also, it happens that any of the other possible combinations
of values is not divisible by three. Therefore, you can say that for each property (color, shape, and
number), the sum of the encoding values of each jewel must be divisible by three. If this sum is
represented by a variable sum , then in JavaScript code the condition sum % 3 === 0 must hold! So,
if you calculate this sum for each property and determine that it's divisible by three, you have found
a valid combination of three jewels. As you can see, sometimes basic mathematics can be very
useful for writing efficient code. The alternative in this case would be to write a large number of if
instructions that handle the different cases, which would most certainly result in slower code.
The only thing left to do is to retrieve the encoding from each jewel. As of now, you have a single
number: the offset of the jewel in the sprite. This is a number from 0 to 26. If you look again at
Figure 15-1 , you see that the first nine jewels are yellow, the following nine are blue, and the last nine
are red. Therefore, if you divide the variation variable by 9, you get a value between 0 and 2 that
represents the color! The rest of that division is a number from 0 to 8. If you divide that number by 3,
you again get a number from 0 to 2 that represents the shape. The rest of that division is a number
from 0 to 2, and it represents the number of jewels. By using this concept, you can construct an
algorithm that calculates these values for each property and that checks whether the sum of the
properties is divisible by 3. Look at the following algorithm:
var curra = a.variation;
var currb = b.variation;
var currc = c.variation;
var divider = 9;
Search WWH ::




Custom Search