vicinity function

Difficulty: Advanced   Keywords: Strategy, Theory

Kjeld Petersen: My first version of the vicinity function didn't come out as I attended. Actually it is more or less a influence function, but it should have been a function that would show the vicinity of the last played stone.

I now came up with a more simple version, that will give the correct result.

//all positions has to be unmarked before calling this function
void vicinity(x,y,level)
{
if( !is_on_board(x,y) || is_marked(x,y) || !level )
{
return;
}
mark(x,y);
if( is_empty(x,y) )
{
level--;
}
vicinity(x+1,y,level);
vicinity(x-1,y,level);
vicinity(x,y+1,level);
vicinity(x,y-1,level);
return;
}

Kjeld Petersen: Call this function with the coordinate of the last placed stone, and the color of this stone, and the function will return a expression about how many free point this stone has some influence over.

Example:

x = 4;
y = 4;
max=5; //globle variable also used inside the recursive funtion
color = black;
points = vicinity( x,y, count=max, color, hold=false );

The count option is used inside the recursive function, but can be used to set how wide the function should search.

The hold option is used inside the recursive function, and should always be initiated to "false".

vicinity( x,y, count, color, hold )
{
if( is_not_on_board(x,y) ) return 0
if( count <= 0 ) return 0
if( count > max ) return (color_of(x,y)==opposit(color))? 1 : 0
if( mark_is(x,y) >= count ) return 0
mark(x,y) = count
number = 0
switch position(x,y)
{
equal color:
{
mark(x,y) = max
number += vicinity( x+1,y, max, color, hold )
number += vicinity( x-1,y, max, color, hold )
number += vicinity( x,y+1, max, color, hold )
number += vicinity( x,y-1, max, color, hold )
break
}
opposit color:
{
number += vicinity( x+1,y, count, color, hold )
number += vicinity( x-1,y, count, color, hold )
number += vicinity( x,y+1, count, color, hold )
number += vicinity( x,y-1, count, color, hold )
break
}
free position:
{
if( hold ) return 0
number++
around = 0
around += vicinity( x+1,y, count, color, true )
around += vicinity( x-1,y, count, color, true )
around += vicinity( x,y+1, count, color, true )
around += vicinity( x,y-1, count, color, true )
if( around )
{
number += vicinity( x+1,y, count-1, color, true )
number += vicinity( x-1,y, count-1, color, true )
number += vicinity( x,y+1, count-1, color, true )
number += vicinity( x,y-1, count-1, color, true )
}
else
{
number += vicinity( x+1,y, count, color, hold )
number += vicinity( x-1,y, count, color, hold )
number += vicinity( x,y+1, count, color, hold )
number += vicinity( x,y-1, count, color, hold )
}
break
}
return number
}

vicinity function last edited by Unkx80 on January 10, 2009 - 21:06