board game AI

Hi I am creating a board game for 2 players.
Each player takes turn to place a piece on the grid.
The objective is to circle-up the opponent's pieces with your own.
The problem is creating a function that knows when a circle is complete so that the program knows when and where to connect the pieces on the board.
What methods can I use to create this function?
I have created a function called Adjacent, it will scan similar pieces that are adjacent to a particular coordinate. But I find it hard to implement this to achieve the required task.

Any suggestions?

Since I cannot place a snapshot of my program here, I will type it out.
Assume below as a grid. Star (*) player has circled up the opponent - hash (#).
So I need the function to determine if a "circle" is completed.
This is only a simple example. "Circling" can be more complex consisting more than one pieces.
______________________
________*#____#_______
_______*#*____________
___#____*_____________
______________________
So you do have a grid right?

I don't see your problem scanning for the adjacent.

1
2
3
000000100000
000001210000
000000100000


That would mean 2 is circled by 1?

In case of a 2D array:

Take the coordinates (x,y) and check whether you have a 1 in array[x-1][y], in array[x+1][y], in array[x][y+1], and array[x][y-1]

Same with a 1D array or vector (with given width and height)

And of course check for boundaries! Don't run out of your array index...

Hope that helps
__*___
_*#*__
_*##*_
__**__

Is this a valid way to circle something?
If yes, then to find similar 'circles' I suggest you make a recursive function
bool IsCircled(int x, int y){
    for each adjacent tile
        if it is * it's ok
        if it is # then
            if IsCircled(position of the adjacent tile) it's ok
            else return 0
        if it is _ return 0
}

You'll also have to prevent it from going forever ( if you have two adjacent # then first one will call a check on the second one, and the second one will call a check on the first one). You have to have a way of marking tiles that you've already checked.
Good luck
Last edited on
Topic archived. No new replies allowed.