📄 othellopanel.java
字号:
}
else
setCursor( new Cursor(Cursor.DEFAULT_CURSOR) );
}
public void mouseDown(int px, int py)
{
//得到在棋盘中的坐标
Graphics g= this.getGraphics();
int x= (px-CHESS_OFF_X)/ CHESS_GRILLE_X;
int y= (py-CHESS_OFF_Y)/ CHESS_GRILLE_Y;
Integer _px,_py;
_px = new Integer(x);
_py = new Integer (y);
String s = "mouse Down : "+_px.toString () + '.' +_py.toString ();
if((x<=8)&&(x>=1)&&(y<=8)&&(y>=1))//鼠标是否在棋盘内
{
//在棋盘上
if(m_MePut)
if (Judge(m_chess,x,y,m_side))
{
//放置棋子
Put(m_chess,x,y,m_side); //放置棋子
DrawChessBoard(g); //重画棋盘
m_TempX=x;
m_TempY=y;
m_TempPut=false;
m_CurrentX=x;
m_CurrentY=y;
m_BackupX[m_step]=x;
m_BackupY[m_step]=y;
m_step++; //存储
OtherSide();
}
}
}
void OnButtonNew()
{
// 新局
//重新初始化数据
Graphics g= this.getGraphics();
RefreshData();
// 重画棋盘
for (int i=0 ; i <= 8 ;i++)
for (int j=0 ; j <= 8 ;j++)
// if (m_chess[i][j] != NONE )
DrawChess(i,j,m_chess[i][j],g);
this.DrawSide(BLACK,g);
}
void OnButtonBack()
{
Graphics g= this.getGraphics();
//悔棋
if (m_step<=1 || m_State == WITH_NET)
{
//不能悔棋
MessageBox.createMessageBox("Cannot back ! ","ERROR");
return ;
}
else
{
int _step;
int _x, _y, side;
// !@#$#@!%#$%
_x=0;
_y=0;
// 棋盘恢复初始状态 :
for (_x=1;_x<=8;_x++)
for (_y=1;_y<=8;_y++)
m_chess[_x][_y]=0;
m_chess[4][4]=WHITE;
m_chess[5][5]=WHITE;
m_chess[4][5]=BLACK;
m_chess[5][4]=BLACK;
m_TempX=0;
m_TempY=0;
m_TempPut=false;
side=BLACK;
for (_step=1;_step<=m_step-3;_step++)
{
_x=m_BackupX[_step];
_y=m_BackupY[_step];
if (! ( ((_x<=8) && (_x>=1) && (_y<=8) && (_y>=1))||((_x==PASS)&&(_y==PASS)) ) )
{
MessageBox.createMessageBox(" ERROR in backup","ERROR");
return;
}
else
{
if(!(_x==PASS))
{
if( Judge( m_chess,_x , _y ,side)) Put(m_chess,_x,_y,side);
else
{
MessageBox.createMessageBox(" ERROR in backup","ERROR");
return;
}
}
side=-side;
}
}
m_step-=2;
m_CurrentX=_x;
m_CurrentY=_y;
for (int i=0 ; i <= 8 ;i++)
for (int j=0 ; j <= 8 ;j++)
DrawChess(i,j,m_chess[i][j],g);
}
}
void OnButtonPass()
{
//无法走棋,略过
Graphics g= this.getGraphics();
char i,j;
boolean Passable=true;
for (i=1;i<=8;i++)
for (j=1;j<=8;j++)
if (Judge(m_chess,i,j,m_side)) { Passable=false; break;}
if (Passable)
{
m_BackupY[m_step]=PASS;
m_BackupY[m_step]=PASS;
m_step++;
DrawSide(-m_side,g);
// ComputerSide(chess,side);
OtherSide();
}
else { MessageBox.createMessageBox(" Cannot Pass !","ERRO"); return ; }
}
void OnButtonNet()
{
m_ChatFrm.show();
}
void OnButtonLoc()
{
// TODO: Add your control notification handler code here
//交换先后手
switch ( m_State)
{
case WITH_COM:
{
OnButtonNew();
this.m_bLoc.setImage( with_loc[0],with_loc[1],with_loc[2],with_loc[0]);
this.m_bLoc.repaint();
m_State = this.WITH_LOC;
m_MePut = true;
break;
}
case WITH_LOC :
{
OnButtonNew();
this.m_bLoc.setImage( with_com[0],with_com[1],with_com[2],with_com[0]);
this.m_bLoc.repaint();
m_State = this.WITH_COM;
m_MePut = true;
break;
}
case WITH_NET :
{
OnButtonNew();
this.m_State = this.WITH_LOC;
this.m_bLoc.setImage( with_loc[0],with_loc[1],with_loc[2],with_loc[0]);
this.m_bLoc.repaint();
m_MePut = true;
break;
}
}
}
//对网络发来的消息的处理
public void DealInput(int type,String txt)
{
Graphics g= this.getGraphics();
String msg = new String();
msg = "受到消息, type : " +type + " ,正文 : " + txt;
String buf = new String();
int i;
int len = txt.length();
switch (type)
{
case 20:
{
// chat 消息
txt+='\n';
m_ChatFrm.m_text.append(txt);
break;
}
case 22:
{
// chat 消息
txt+='\n';
m_ChatFrm.m_text.append(txt);
break;
}
case 4:
{
//新登陆者的名字
int id ;
i=0;
buf = "";
while(txt.charAt(i) != '\\' )
{
buf+= txt.charAt(i);
i++;
}
id = Integer.decode(buf).intValue();
i++;
buf = "";
while(i<len)
{
buf+=txt.charAt(i);
i++;
}
m_socket.m_client[id] = buf;
m_ChatFrm.m_obj.insert(m_socket.m_client[id],0);
break;
}
case 6:
{
//某客户断开连接,删除
// MessageBox("有客户断开连接" + txt);
i=0;
while(i<len)
{
buf+=txt.charAt(i);
i++;
}
int id = Integer.decode(buf).intValue();
if(m_State == WITH_NET )
if ( m_Rival.equals(m_socket.m_client[id]) )
{
MessageBox.createMessageBox("你与 " + m_socket.m_client[id] +" 的连接已中断,对战中断。","对战中断");
m_bLoc.setImage(this.with_loc[0],this.with_loc[1],this.with_loc[2],with_loc[0]);
OnButtonNew();
m_bLoc.repaint();
m_State = WITH_LOC;
m_MePut = true;
m_side = BLACK;
}
m_ChatFrm.m_obj.remove(m_socket.m_client[id]);
m_ChatFrm.m_text.append(" " + m_socket.m_client[id] + " 已经离开主机\n");
m_socket.m_client[id] = null ;
break;
}
case 7:
{
//受到挑战请求 (对方执黑)
if ( m_State != WITH_NET )
{
if( MessageBox.createMessageBox("" + txt + " 向你发出挑战要求 !! (他执黑先行) ","挑战 !!",1))
{
//接收
this.OnButtonNew();
m_socket.Send(9,txt,m_ChatFrm.m_name);
m_State = WITH_NET;
m_Rival = txt;
m_MePut = false;
m_side = BLACK;
DrawSide(BLACK,g);
m_bLoc.setImage(this.with_net[0],this.with_net[1],this.with_net[2],with_net[0]);
m_bLoc.repaint();
}
else
{
m_socket.Send(10,txt,m_ChatFrm.m_name+" 拒绝与你对战.");
}
}
else
{
m_socket.Send(10,txt,m_ChatFrm.m_name+ " 正在对战中. ");
}
break;
}
case 8:
{
//收到挑战请求 (对方执白)
if ( m_State != WITH_NET )
{
//接收
if( MessageBox.createMessageBox( " " + txt + " 向你发出挑战要求 !! (你执黑先行) ","挑战 !!",1))
{
this.OnButtonNew();
m_State = WITH_NET;
m_Rival = txt;
m_socket.Send(9,txt,m_ChatFrm.m_name);
m_side = BLACK;
DrawSide(BLACK,g);
m_MePut = true;
m_bLoc.setImage(this.with_net[0],this.with_net[1],this.with_net[2],with_net[0]);
m_bLoc.repaint();
}
else
{
m_socket.Send(10,txt,m_ChatFrm.m_name +" 拒绝与你对战.");
}
}
else
{
m_socket.Send(10,txt,m_ChatFrm.m_name+ " 正在对战中. ");
}
break;
}
case 9:
{
// 对方接收挑战
if ( txt.equals(m_Rival))
{
MessageBox.createMessageBox( " " + m_Rival + " 接受你的挑战 !! 对战开始 !!","对战开始");
if ( m_side == WHITE )
{
this.OnButtonNew();
m_State = WITH_NET;
m_side = - m_RivalSide;
m_MePut = false;
DrawSide(BLACK,g);
}
else
{
this.OnButtonNew();
m_State = WITH_NET;
m_side = - m_RivalSide;
m_MePut = true;
DrawSide(BLACK,g);
}
m_bLoc.setImage(this.with_net[0],this.with_net[1],this.with_net[2],with_net[0]);
m_bLoc.repaint();
}
else
{
m_socket.Send(10,txt,m_ChatFrm.m_name+ " 正在对战中. ");
}
break;
}
case 10 :
{
//对方拒绝应战
MessageBox.createMessageBox(" "+ m_Rival + " 拒绝接受挑战。","拒绝挑战");
break;
}
case 21 :
{
//对方走棋消息
if(m_State == WITH_NET)
{
if ( !m_MePut )
{
buf = "";
i =0;
while(txt.charAt(i) != '\\')
{
buf+= txt.charAt(i);
i++;
}
i++;
int x = Integer.decode(buf).intValue();
buf = "";
while(txt.charAt(i) != '\\')
{
buf+= txt.charAt(i);
i++;
}
int y = Integer.decode(buf).intValue();
Put(m_chess,x,y,m_side);
DrawChessBoard(g);
m_MePut = true;
m_side = - m_side;
if( this.JudgeEnd() )
{
// 网络对弈结束
m_bLoc.setImage(this.with_loc[0],this.with_loc[1],this.with_loc[2],with_loc[0]);
m_bLoc.repaint();
}
}
}
break;
}
case 11 :
{
//服务器已满
MessageBox.createMessageBox("连接被拒绝,服务器已满","连接失败");
m_ChatFrm.OnDisconnect();
return;
}
case 12 :
{
//名字冲突
MessageBox.createMessageBox("已经有人用这个名字登陆到服务器,换个名字吧","登陆失败");
m_ChatFrm.OnDisconnect();
return;
}
case 2:
{
// 得到主机发来的名字列表
i=0;
int list = 0;
while(i<len)
{
// get name
buf = "";
while(txt.charAt(i) != '\\')
{
buf += txt.charAt(i);
i++;
}
list = Integer.decode(buf).intValue();
i++;
m_socket.m_client[list] = new String();
while (txt.charAt(i)!='\\')
{
m_socket.m_client[list]+=txt.charAt(i);
i++;
}
i++; // charAt(i) ==0
this.m_ChatFrm.m_obj.insert(m_socket.m_client[list],list); // insert to choice
System.out.print("insert client name count ");
System.out.println(list);
}
this.m_ChatFrm.m_text.append(" 成功连接到主机 " + m_socket.m_socket.getInetAddress() + "\n");
System.out.println("End deal with name ");
break;
}
default :
break;
}
}
public void destroy()
{
this.m_ChatFrm.dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -