java五子棋代码可悔棋 java五子棋源码( 三 )


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;
do
{
space=space+1;
x=x+displace_x;
y=y+displace_y;
}while(x=0xlengthy=0ylengthgrid[x][y]==0space4);
if(space+count=5) /* 逞缁?フ场だΤ硈絬ぇ?? */
return true;
else
{
space=0;
x=locX-displace_x;
y=locY-displace_y;
while(x=0xlengthy=0ylengthgrid[x][y]==0space4)
{
space=space+1;
x=x-displace_x;
y=y-displace_y;
}
if(space+count=5) /* 逞缁?フ场だΤ硈絬ぇ?? */
return true;
else
return false;
}
}
public void ThreadStart()
{
new Thread(this).start();
}
private void setMark(int x, int y)
{
steps.add(grids[x][y]);

推荐阅读