So, to explain what we need to do – I've drawn an image:
Let's say that the player clicks on the Field[1,1]. Our game should detect that the [1,1] field is connected to two more blue fields (Field[2,1] and Field[2,0]). In our game, the rule will be that fields are connected horizontal and vertical, but not diagonal. How do we do this? First of all, we'll create a list of connected fields. We'll put in the field user clicked. And then, we'll check fields above it, below, to the right and to the left to see if they hold the same (color) value. If they do, we'll put them too in our list. And then we'll go trough our list, field by field, searching for connections for every field in our list. If we find any connections, we'll check if they are already in the list. If no, we'll add them. Sounds complicated. Even I can't understand what I just wrote. :o) I'll try to write this as a step by step list:
Player clicks Field[1,1]- We add Filed[1,1] to our list
- We check the field above it (Field[0,1]) to see if it's the same color. Since it's not, we ignore it.
- We check the field below if (Field[2,1]) to see if it's the same color. It is, so we add it to the list.
- We check the field on the left (Field[1,0]) – it's not the same color, so we ignore it.
- We check the field on the right (Field[1,2]) – it's not the same color, so we, once again, ignore it.
- We move to the next item (Field[2,1]) in our list, and repeat the steps for it…
- We check the field above it (Field[1,1]), but we ignore it since it's already in our list…
- Eventualy we find the Field[2,0], add it to our list, and that's it…
So, how does this look like, code wise? First we'll have to declare our list, and then create a couple of methods for checking connections: A method that goes up, down, left and right, another one that cheks if the field is already in our list, and another one that makes sure that we check connections for every field from the list. There's one new thing here, thoug – structs. Think of them as some sort of containers that can hold a lot of different variables… So, first declare our struct. Put the declaration on top of the code, where you have your other public declarations.
// Struct for our connected
// fields.struct stField
{
public int iFieldColor;
public int iXcoordinate;
public int iYcoordinate;
}
And then declare the list to hold the connected fields:
// List of our connected fields.
List<stField> ConnectedList =
new List<stField>();
Right. Now, check the code - the method & function declarations are a little different... So, be careful :)
private void CheckConnected
(int Xpos, int Ypos, bool bClick)
{
// This is the struct that will hold the
// Connected field position and value.
stField stField = new stField();
stField.iFieldColor = Field[Xpos, Ypos];
stField.iXcoordinate = Xpos;
stField.iYcoordinate = Ypos;
// bClick actualy means that the field
// was clicked on by player, so we add
// it to the list automatically.
if (bClick == true)
{
// So, we add the field to our list,
// we check if it's already in the list,
// and we add it if not. This isn't
// necesary here, since logic thinking
// tells us that the list is empty at
// this point. But, just to be safe...
if (CheckIfInList
(stField.iXcoordinate,
stField.iYcoordinate) == false)
{
ConnectedList.Add(stField);
}
}
//Checks fields up and down from
//the desired field.
if (stField.iXcoordinate > 0)
{
if (Field[stField.iXcoordinate - 1,
stField.iYcoordinate] ==
Field[stField.iXcoordinate,
stField.iYcoordinate])
{
// Field above has the same value(color).
// We add it to the list.
stField.iXcoordinate =
stField.iXcoordinate - 1;
if (CheckIfInList(stField.iXcoordinate,
stField.iYcoordinate) == false)
{
ConnectedList.Add(stField);
}
stField.iXcoordinate =
stField.iXcoordinate + 1;
}
}
if (stField.iXcoordinate < iHeight - 1)
{
if (Field[stField.iXcoordinate + 1,
stField.iYcoordinate] ==
Field[stField.iXcoordinate,
stField.iYcoordinate])
{
// Field below has the same value(color).
// We add it to the list.
stField.iXcoordinate =
stField.iXcoordinate + 1;
if (CheckIfInList(stField.iXcoordinate,
stField.iYcoordinate) == false)
{
ConnectedList.Add(stField);
}
stField.iXcoordinate =
stField.iXcoordinate - 1;
}
}
//Checks fields left and right
//from the desired field.
if (stField.iYcoordinate > 0)
{
if (Field[stField.iXcoordinate,
stField.iYcoordinate - 1] ==
Field[stField.iXcoordinate,
stField.iYcoordinate])
{
// Field to the left has the same
// value(color). We add it to the list.
stField.iYcoordinate =
stField.iYcoordinate - 1;
if (CheckIfInList(stField.iXcoordinate,
stField.iYcoordinate) == false)
{
ConnectedList.Add(stField);
}
stField.iYcoordinate =
stField.iYcoordinate + 1;
}
}
if (stField.iYcoordinate < iWidth - 1)
{
if (Field[stField.iXcoordinate,
stField.iYcoordinate + 1] ==
Field[stField.iXcoordinate,
stField.iYcoordinate])
{
// Field to the right has the same
// value(color). We add it to the list.
stField.iYcoordinate =
stField.iYcoordinate + 1;
if (CheckIfInList(stField.iXcoordinate,
stField.iYcoordinate) == false)
{
ConnectedList.Add(stField);
}
stField.iYcoordinate =
stField.iYcoordinate - 1;
}
}
}
private void CheckAll()
{
// This method goes trough the list,
// and re-checks connections for
// every field in it.
if (ConnectedList.Count > 1)
{
for (int l = 0; l <
ConnectedList.Count; l++)
{
CheckConnected
(ConnectedList[l].iXcoordinate,
ConnectedList[l].iYcoordinate,
false);
}
}
}
private bool CheckIfInList
(int PXpos, int PYpos)
{
// This function checks if the
// field is in the list or not.
// If the field is in the list,
// the function returns boolean
// 'TRUE'. If not, it returns
// boolean 'FALSE'.
for (int l = 0;
l < ConnectedList.Count; l++)
{
if (ConnectedList[l].iXcoordinate
== PXpos && ConnectedList[l].iYcoordinate
== PYpos)
{
// The field is in the list:
return true;
}
}
// The field is not in the list:
return false;
}
Right. Hope this works… So, now we know what the user clicked. And if there are more than one blocks connected, we should destroy them. That's going to be easy. After destroying the blocks, we'll have to make the gravity. So we don't wind up with floating blocks… Moving on…
0 comments:
Post a Comment