I recommend learning about Graphs at some point, because they are useful and interesting, but I guess you can use an easier method for now.
1. Make each space on your board an object, called something like
Space. (If you are using
Sprite objects to represent your spaces, then consider creating a subclass of
Sprite and adding these variables to it.) Each
Space object should have a
bool called something like
checked (set to
false by default) and an integer to indicate which player controls the space, called something like
player (set to
-1 by default.) You will need to either store your
Space objects in a way where you know the spaces around it, or you need to store references to the spaces around it in the
Space object itself.
2. Create a function called something like
searchForUnoccupied. This function will take a
Space object that belongs to the group you want to check if surrounded, and an integer to indicate which player controls the piece you are checking. The function returns
false if the spaces are not surrounded and
true if they are.
searchForUnoccupied do this, in this order:
- If the
checked value of the
checked for the
player for the
Space set to
player for the
Space is different from the
player parameter, return
searchForUnoccupied for each
Space connected to the current
Space and using the
player parameter you already have for the call. If any of these calls return
false, then return
false. Otherwise, return
4. Whenever whoever controls a
Space changes, remember to change its
player variable to its new controller.
When you want to check if a group is surrounded:
1. Choose a
Space in the group and call
searchForUnoccupied with that
Space and the integer of the player who controls it. If the function returns
true, the group is surrounded. Otherwise, if the the function returns
false, the group is not.
2. After the search is complete, reset the
checked value of each
I'm doing this off the top of my head, so hopefully it should work. Feel free to ask questions if this doesn't make sense.