Game Development Reference
In-Depth Information
First, we retrieve the value that represents which jewel we are dealing with, using
the
Variation
property. Then we define a divider number that is equal to 9 (we are
first going to divide by 9). We then define a
for
-instruction that runs three times. In
the body of the
for
-instruction, we place a condition that the sum of the three vari-
ation indices divided by
divider
should be divisible by 3. If this is not the case, we
return
false
since the combination condition does not hold for one of the properties.
We then assign the rest of the division by the divider to each of the variables con-
taining the current variation index. We then divide the divider by 3. If we exit the
for
-instruction, it means that in all cases, the condition in the
if
-instruction was
true
,
meaning that we found a valid combination. Since we found a valid combination,
we return the value
true
.
16.4.3 Removing Jewels from the Grid
Inside the
HandleInput
method, we can now use the
IsValidCombination
method to de-
termine if a valid combination exists. For this, we use a
while
-instruction that evalu-
ates all sequences of three symbols in the middle column:
int
middleCol =
this
.Columns / 2;
int
i=0;
while
(i <
this
.Rows
−
2)
{
if
(IsValidCombination((Jewel)grid[middleCol, i],
(Jewel)grid[middleCol, i + 1],
(Jewel)grid[middleCol, i + 2]))
{
dosomething...
}
else
i++;
}
When we find a valid combination, we need to remove these jewels from the grid,
and insert new jewels. For this, we define a method called
ReplaceJewel
, which re-
moves a jewel from the grid, and inserts a new one. Because we want to create a nice
'falling down' motion, we place these jewels in different positions above the grid.
We pass the desired
y
-location as a parameter to the
ReplaceJewel
method so that it
knows where the new jewel should be located. The complete method then becomes
public void
ReplaceJewel(
int
x,
int
y,
int
newYPosition)
{
this
.Clear(x, y);
Jewel s =
new
Jewel();
this
.Add(s);
s.Position =
new
Vector2(x
∗
cellWidth, newYPosition);
}