java悔棋代码 java实现悔棋思路( 三 )


break;
case oblique_2: displace_x=displace_y=1;
break;
case horizontal: displace_x=1;
displace_y=0;
break;
case vertical: displace_x=0;
displace_y=1;
break;
}
x=x+displace_x;
y=y+displace_y;
if(x=0xlengthy=0ylengthgrid[x][y]==check)
{
count=count+1;
if(count==step)
{
if(x+displace_x=0x+displace_xlengthy+displace_y=0y+displace_ylengthgrid[x+displace_x][y+displace_y]==0)
{
if(x-step*displace_x0 || x-step*displace_x=length || y-step*displace_y0 || y-step*displace_y=length || !gobang_SpaceAI(x+displace_x, y+displace_y) || (grid[x-step*displace_x][y-step*displace_y]!=0step3) ) /* 癸翴?猵 */
gobangAI3_2();
else
setMark2(x+displace_x, y+displace_y);
}
else
gobangAI3_2();
}
else
gobangAI3(x, y);
}
else
gobangAI3_2();
}
private void gobangAI3_2()
{
if(direction!=vertical)
{
count=1;
direction=direction+1;
gobangAI3(locX, locY);
}
}
private void gobangAI4(boolean player)
{
check = player? Player: AI;
for(int i=0; ilength; i++)
for(int j=0; jlength; j++)
{
if(turn)
break;
if(grid[i][j]==0)
{
direction=oblique_1;
locX=i;
locY=j;
gobangAI4();
}
}
}
private void gobangAI4()
{
count=0;
switch(direction)
{
case oblique_1: displace_x=1;
displace_y=-1;
direction=oblique_2;
break;
case oblique_2: displace_x=displace_y=1;
direction=horizontal;
break;
case horizontal: displace_x=1;
displace_y=0;
direction=vertical;
break;
case vertical: displace_x=0;
displace_y=1;
direction=nil;
break;
}
x=locX+displace_x;
y=locY+displace_y;
while(x=0xlengthy=0ylengthgrid[x][y]==check)
{
count=count+1;
x=x+displace_x;
y=y+displace_y;
}
x=locX-displace_x;
y=locY-displace_y;
while(x=0xlengthy=0ylengthgrid[x][y]==check)
{
count=count+1;
x=x-displace_x;
y=y-displace_y;
}
if(count=4)
setMark(locX, locY);
else if(direction!=nil)
gobangAI4();
}
private void gobangAI5()
{
for(int i=0; ilength; i++)
for(int j=0; jlength; j++)
{
if(turn)
break;
if(grid[i][j]==-1)
{
direction=oblique_1;
locX=i;
locY=j;
gobangAI5_2();
}
}
}
/* 璸衡硈絬?フ耞糷 */
private void gobangAI5_2()
{
count=0;
switch(direction)
{
case oblique_1: displace_x=1;
displace_y=-1;
direction=oblique_2;
break;
case oblique_2: displace_x=displace_y=1;
direction=horizontal;
break;
case horizontal: displace_x=1;
displace_y=0;
direction=vertical;
break;
case vertical: displace_x=0;
displace_y=1;
direction=nil;
break;
}
x=locX+displace_x;
y=locY+displace_y;
while(x=0xlengthy=0ylengthgrid[x][y]==0count4)
{
count=count+1;
x=x+displace_x;
y=y+displace_y;
}
x=locX-displace_x;
y=locY-displace_y;
if(count==4x=0xlengthy=0ylengthgrid[x][y]==0)
setMark(locX+displace_x, locY+displace_y);
else if(count1)
{
count = count==4? 3: count;
while(x=0xlengthy=0ylengthgrid[x][y]==0count4)
{
count=count+1;
x=x-displace_x;
y=y-displace_y;
}
if(count==4)
setMark(locX+displace_x, locY+displace_y);
else if(direction!=nil)
gobangAI5_2();
}
else if(direction!=nil)
gobangAI5_2();
}
private boolean gobang_SpaceAI(int x, int y) /* 璸衡逞缁?フ?竚 */
{
int space=0;

推荐阅读