Game Development Reference
In-Depth Information
jewel as ( 1 , 0 , 0 ) . The yellow round single jewel is then defined as ( 0 , 2 , 0 ) , and the
red oval-shaped triple jewel is defined as ( 2 , 1 , 2 ) .
Now let us see if we can use this encoding scheme to find valid combinations
of three jewels (let us call them jewel A, B, and C). For each jewel, we have to
compare their color, the shape, and the number. Each of these properties has to be
either the same for all jewels, or all different. Using the encoding, this means that
if A has encoding value 0 for the color, B has encoding value 0, and C also has
encoding value 0, then for the color the condition holds, since all symbols have the
same color (yellow). The same goes if the symbols all have the blue color (A-color
=
=
=
=
=
1, B-color
1, C-color
1) or the red color (A-color
2, B-color
2, C-
=
color
2). Finally, the condition holds if all their colors are different, or: there is
an ordering of A-color, B-color and C-color that yields 0, 1, and 2. If we look at the
sum of these different combinations, we see an interesting property: 0
+
0
+
0
=
0,
1
3, in other words: the sum is divisible by
three . Also, it happens to be the case that any of the other possible combinations of
values is not divisible by three. Therefore, we 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 C# code the condition
sum % 3 == 0 must hold! So, if we calculate this sum for each property and check
that it is divisible by three, we have found a valid combination of three jewels.
The only thing left to do now is to retrieve the encoding from each jewel. As of
now, we only have a single number: the offset of the jewel in the sprite. This is a
number between 0-26. If you take a look again at Fig. 15.1 , you will see that the
first 9 symbols are yellow, the following 9 are blue and the last 9 are red. Therefore,
if we divide the variation number by 9, we will get a value between 0 and 2 that
represents the color! The rest of that division will be a number between 0-8. If we
divide that number by 3, we will again get a number between 0 and 2 that represents
the shape. The rest of that division is a number between 0 and 2, and it represents
the number of jewels. By using this concept, we can construct an algorithm that
calculates these values for each property and that checks if the sum of the properties
is divisible by 3. Have a look at the following algorithm:
+
1
+
1
=
3, 2
+
2
+
2
=
6, and 0
+
1
+
2
=
int curra = a.Variation;
int currb = b.Variation;
int currc = c.Variation;
int divider = 9;
for ( int i=0;i<3;i++)
{
if ((curra / divider + currb / divider + currc / divider) % 3 != 0)
return false ;
curra %= divider;
currb %= divider;
currc %= divider;
divider /= 3;
}
return true ;
Search WWH ::




Custom Search