📄 othello.java
字号:
}
if (x<=6)
{
if (chess[x+1][y]==-side)
{
for(i=x+2;i<=8;i++)
if (chess[i][y]==side)
{
for(ii=x+1;ii<=i-1;ii++)
chess[ii][y]=side;
break;
}
else if(chess[i][y]==0) break;
}
}
if (x>=2)
{
if (chess[x-1][y]==-side)
{
for(i=x-2;i>=1;i--)
if (chess[i][y]==side)
{
for(ii=x-1;ii>=i+1;ii--)
chess[ii][y]=side;
break;
}
else if(chess[i][y]==0) break;
}
}
}
boolean Judge( int chess[][],int x,int y,int side)
{
//判断是否可以放置
int i,j;
if (!((x<=8) && (x>=1) && (y<=8) && (y>=1)))
{
Integer _px,_py;
_px = new Integer(x);
_py = new Integer (y);
String s = "判断出错:"+_px.toString () + '.' +_py.toString ();
MessageBox.createMessageBox (s);
return false;
}
if (chess[x][y]==0)
{
if (y<=6)
{
if(chess[x][y+1]==-side)
{
for (j=y+2;j<=8;j++)
if(chess[x][j]==side){return true;}
else if (chess[x][j]==0) break;
}
if(chess[x-1][y+1]==-side)
{
j = y+2;
for (i=x-2;(j<=8)&&(i>=1);i--)
{
if(chess[i][j]==side) {return true;}
else if (chess[i][j]==0) break;
j++;
}
}
if(chess[x+1][y+1]==-side)
{
j = y+2;
for (i=x+2;(j<=8)&&(i<=8);i++)
{
if(chess[i][j]==side) {return true;}
else if (chess[i][j]==0) break;
j++;
}
}
}
if (y>=2)
{
if(chess[x][y-1]==-side)
{
for (j=y-2;j>=1;j--)
if(chess[x ][j]==side) {return true;}
else if (chess[x ][j]==0) break;
}
if(chess[x-1 ][y-1]==-side)
{
j = y-2;
for (i=x-2;(j>=1)&&(i>=1);i--)
{
if(chess[i ][j]==side) {return true; }
else if (chess[i ][j]==0) break;
j--;
}
}
if(chess[x+1 ][y-1]==-side)
{
j = y-2;
for (i=x+2;(j>=1)&&(i<=8);i++)
{
if(chess[i ][j]==side) {return true; }
else if (chess[i ][j]==0) break;
j--;
}
}
}
if (x>=2)
if(chess[x-1][y]==-side)
{
for (i=x-2;i>=1;i--)
if(chess[i ][y]==side) {return true; }
else if (chess[i ][y]==0) break;
}
if (x<=6)
if(chess[x+1][y]==-side)
{
for (i=x+2;i<=8;i++)
if(chess[i ][y]==side) {return true; }
else if (chess[i][y]==0) break;
}
}
return false;
}
//搜索的递归算法
void Search( int chess[][] ,int l,int side)
{
int i,j;
// 查找最佳走法
// _chess[][] 保存棋盘布局
// l 当前查找深度
int _chess[][],x,y;
int z;
z=0;
_chess = new int[10][10];
if (_chess == null ) TRACE("_chess[][] 返回空指针");
for (i=1;i<=8;i++)
for (j=1;j<=8;j++)
_chess[i][j]=chess[i][j];
if (l<= SEARCH_DEEPNESS)
{
//查找每一个位置
for (x=1;x<=8;x++)
for (y=1;y<=8;y++)
if (Judge(chess,x,y,side))
{
//可以放棋
Put(chess,x,y,side);
Search(chess,l+1,-side);
if (m_exit)
{
return; //放弃此分支
}
}
}
else
{
//已到最深层
for (i=1;i<=8;i++)
for (j=1;j<=8;j++) z+=chess[i][j]*m_value[i][j];
z*=m_side;
if (z<=m_trunkmin)
{
m_exit=true;
return ;//放弃此支
}
else m_branchmin=((z<=m_branchmin)?z:m_branchmin);//置分支最小值
}
//恢复棋盘
for (i=1;i<=8;i++)
for (j=1;j<=8;j++)
chess[i][j]=_chess[i][j];
}
void ComputerSide(int chess[][],int side)
{
//由电脑走棋
TRACE("由电脑走棋");
int _chess[][];
int x_ai,y_ai,i,j,x,y,l;
boolean Com_Passable;
DrawSide(m_side);
_chess = new int[10][10];
Com_Passable = true;
m_trunkmin=-300;
x_ai=0;y_ai=0;
for(i=1;i<=8;i++)
for(j=1;j<=8;j++)
_chess[i][j]=chess[i][j];
for (x=1;x<=8;x++)for (y=1;y<=8;y++)
{
if (Judge(_chess,x,y,side)) //电脑判断分析下棋位置
{
l=1;
m_branchmin=300;
Com_Passable = false; //可以走棋
m_exit = false;
Put(_chess,x,y,side);
Search(_chess,l,-side);
if (! m_exit )
{
m_trunkmin=m_branchmin;
x_ai=x;
y_ai=y;
}
for (i=1;i<=8;i++)
for (j=1;j<=8;j++)
_chess[i][j]=chess[i][j];
}
}
if (!Com_Passable)
{
//可以走棋
if (!((x_ai<=8) && (x_ai>=1) && (y_ai<=8) && (y_ai>=1)))
{
TRACE("放棋子出错");
return;
}
if (!Judge(chess,x_ai,y_ai,side))
{
TRACE("判断出错");
return ;
}
Put(chess,x_ai,y_ai,side); //电脑下棋
m_backup_x[m_step]=x_ai;
m_backup_y[m_step]=y_ai;
m_currentx=x_ai;
m_currenty=y_ai;
m_step++;
DrawChessBoard();
}
else
{
//无棋可走
TRACE_2("无棋可走");
DrawChessBoard();
m_backup_x[m_step]=PASS;
m_backup_y[m_step]=PASS;
m_step++;
}
TRACE("电脑走棋完毕");
if(JudgeEnd())
{
return;
}
m_side = -m_side;
m_meput = true;
DrawSide(m_side);
}
void OtherSide()
{
if(JudgeEnd())
{
m_meput = false;
return;
}
if(state == WITH_NET)
{
m_meput = false;
String buf = new String();
Integer bufi = new Integer(m_tempx);
buf += bufi.toString();
buf += '\\';
bufi = new Integer(m_tempy);
buf += bufi.toString();
buf += '\\';
m_socket.Send(21,rival,buf);
this.DrawSide( - m_side);
m_side = - m_side;
}
else
if(state == WITH_COM)
{
m_meput = false;
m_side = - m_side;
ComputerSide(m_chess,m_side);
}
else
if(state == WITH_LOC)
{
m_side=-m_side;
DrawSide(m_side);
}
}
public boolean JudgeEnd()
{
int i ,j;
int countb=0 ;
int countw=0 ;
for(i=1;i<=8;i++)
for(j=1;j<=8;j++)
if ( (m_chess[i][j] == NONE) )
return false;
for(i=1;i<=8;i++)
for(j=1;j<=8;j++)
if ( m_chess[i][j] == WHITE )
countw++;
else
countb++;
if (countw<countb)
MessageBox.createMessageBox(" 黑方胜 !! "+countb+" vs "+countw,"胜利");
else
if (countb<countw)
MessageBox.createMessageBox("白方胜 !! "+countw+" vs "+countb,"胜利");
else
MessageBox.createMessageBox("平局 !!","平局");
return true;
}
/* **************
绘图函数
*****************/
// 画代表当前方的标志
void DrawSide(int side)
{
if(side == BLACK )
{
g.drawImage(m_swordGif,
SWORD_B_OFF_X , SWORD_B_OFF_Y ,
SWORD_B_OFF_X+SWORD_WIDTH ,
SWORD_B_OFF_Y+SWORD_HEIGHT,
0,0,SWORD_WIDTH , SWORD_HEIGHT,this);
g.drawImage(m_backgdGif,
SWORD_W_OFF_X , SWORD_W_OFF_Y ,
SWORD_W_OFF_X+SWORD_WIDTH ,
SWORD_W_OFF_Y+SWORD_HEIGHT,
SWORD_W_OFF_X , SWORD_W_OFF_Y ,
SWORD_W_OFF_X+SWORD_WIDTH ,
SWORD_W_OFF_Y+SWORD_HEIGHT,
this);
}
else
if(side == WHITE )
{
g.drawImage(m_swordGif,
SWORD_W_OFF_X , SWORD_W_OFF_Y ,
SWORD_W_OFF_X+SWORD_WIDTH ,
SWORD_W_OFF_Y+SWORD_HEIGHT,
0,0,SWORD_WIDTH , SWORD_HEIGHT,this);
g.drawImage(m_backgdGif,
SWORD_B_OFF_X , SWORD_B_OFF_Y ,
SWORD_B_OFF_X+SWORD_WIDTH ,
SWORD_B_OFF_Y+SWORD_HEIGHT,
SWORD_B_OFF_X , SWORD_B_OFF_Y ,
SWORD_B_OFF_X+SWORD_WIDTH ,
SWORD_B_OFF_Y+SWORD_HEIGHT,
this);
}
else
{
g.drawImage(m_backgdGif,
SWORD_B_OFF_X , SWORD_B_OFF_Y ,
SWORD_B_OFF_X+SWORD_WIDTH ,
SWORD_B_OFF_Y+SWORD_HEIGHT,
SWORD_B_OFF_X , SWORD_B_OFF_Y ,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -