From 3d4e1fd524af7a6060978914fd6ce5223e53e93e Mon Sep 17 00:00:00 2001 From: hudedong Date: Fri, 19 Jul 2019 15:09:52 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99=E8=AE=BE=E7=BD=AE=E5=B7=A5?= =?UTF-8?q?=E7=A8=8B=E7=BC=96=E7=A0=81=E4=B8=BAutf8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + bin/.gitignore | 2 - src/mychess/client/ChessBoard.java | 236 +++++++++---------- src/mychess/client/ChessClient.java | 46 ++-- src/mychess/entity/Code.java | 8 +- src/mychess/entity/DataMessage.java | 20 +- src/mychess/entity/Message.java | 16 +- src/mychess/entity/MyObjectOutputStream.java | 1 - src/mychess/entity/NormalMessage.java | 8 +- src/mychess/entity/Server.java | 80 +++---- src/mychess/util/CanMove.java | 124 +++++----- src/mychess/util/Common.java | 22 +- src/mychess/util/HasFinished.java | 104 ++++---- src/mychess/util/Internet.java | 22 +- src/mychess/util/JudgeMove.java | 130 +++++----- src/mychess/util/Withdraw.java | 8 +- 16 files changed, 412 insertions(+), 416 deletions(-) diff --git a/README.md b/README.md index ed731c7..b63bb75 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ mychess.client.ChessClient为客户端,打开两个此界面对弈 ------一些问题------ + 1.判断是否游戏结束偶尔出现判断错误 2.时间长会出现游戏崩溃 3.在游戏结束的时候悔棋,会出现异常情况 diff --git a/bin/.gitignore b/bin/.gitignore index fcc094c..54686ad 100644 --- a/bin/.gitignore +++ b/bin/.gitignore @@ -1,3 +1 @@ /mychess/ -/chess.properties -/conf/ diff --git a/src/mychess/client/ChessBoard.java b/src/mychess/client/ChessBoard.java index e41a417..fde3d35 100644 --- a/src/mychess/client/ChessBoard.java +++ b/src/mychess/client/ChessBoard.java @@ -21,12 +21,12 @@ public class ChessBoard extends JPanel implements MouseListener,Runnable{ /** serialVersionUID*/ private static final long serialVersionUID = 1L; - protected DataMessage message;//ÿ׼һϢģ׼Ϣͨ - protected boolean tip;//ǷػʱʾҲʱбǣ - protected boolean isSelected;//ǷѡУһƶӻѡ - protected int[][] data;//ǰֵ״̬,ػ - protected Image[] pics;//ͼƬ - private Internet internet;//ݷ + protected DataMessage message;//每次准备一个消息报文,准备消息通信 + protected boolean tip;//是否重绘的时候加上提示(也就是走棋的时候有标记) + protected boolean isSelected;//棋子是否被选中,决定下一步点击是移动棋子还是在选择棋子 + protected int[][] data;//当前棋局的状态数组,用于重绘 + protected Image[] pics;//加载象棋的图片 + private Internet internet;//数据服务器对象 private String result=""; public ChessBoard() { // TODO Auto-generated constructor stub @@ -36,25 +36,25 @@ public ChessBoard(Image[] pictures) { // TODO Auto-generated constructor stub pics=pictures; init(); - addMouseListener(this);// - Thread t=new Thread(this);//ݷ񽻻 + addMouseListener(this);//监听鼠标操作 + Thread t=new Thread(this);//数据服务交互 t.start(); } public void init() { - internet=new Internet();//ݷϢҪֶǰ + internet=new Internet();//开启数据服务器,至于消息服务器需要手动提前开启 message= (DataMessage) internet.readMessage(); data=Common.String_to_Array(message.getData()); } - //ͼ + //绘图 @Override protected void paintComponent(Graphics g) { // TODO Auto-generated method stub super.paintComponent(g); int height=getHeight(); int width=getWidth(); - //Ȼ910 + //先画数线9个,横线10 g.drawLine(width/11, height/12, width/11, height*10/12); g.drawLine(width*9/11, height/12, width*9/11, height*10/12); for(int i=0;i<7;i++){ @@ -65,14 +65,14 @@ protected void paintComponent(Graphics g) { g.drawLine(width/11, height*(i+1)/12, width*9/11, height*(i+1)/12); } - //Ӻ - g.setFont(new Font("п", Font.BOLD, 25)); - g.drawString(" ", (int)(width*1.5/11), (int)(height*(4.6+1)/12)); - g.drawString(" ", (int)(width*7.1/11), (int)(height*(4.6+1)/12)); + //画楚河汉界 + g.setFont(new Font("华文行楷", Font.BOLD, 25)); + g.drawString("楚 河", (int)(width*1.5/11), (int)(height*(4.6+1)/12)); + g.drawString("汉 界", (int)(width*7.1/11), (int)(height*(4.6+1)/12)); g.setColor(Color.RED); g.drawString(result,(int)(width*4/11), (int)(height*(4.6+1)/12)); g.setColor(Color.BLACK); - //ʿ + //画士的线 g.drawLine(width*4/11, height/12, width*6/11, height/4); g.drawLine(width*6/11, height/12, width*4/11, height/4); g.drawLine(width*4/11, height*10/12, width*6/11, height*8/12); @@ -81,7 +81,7 @@ protected void paintComponent(Graphics g) { drawLines(3, 2, width, height, g); drawLines(3, 8, width, height, g); drawLines(8, 2, width, height, g); - drawLines(8, 8, width, height, g);// + drawLines(8, 8, width, height, g);//炮 drawLines(4, 1, width, height, g); drawLines(4, 3, width, height, g); @@ -92,9 +92,9 @@ protected void paintComponent(Graphics g) { drawLines(7, 3, width, height, g); drawLines(7, 5, width, height, g); drawLines(7, 7, width, height, g); - drawLines(7, 9, width, height, g);// + drawLines(7, 9, width, height, g);//兵 - //ͼ + //画图像 for(int i=1;i<=10;i++){ for(int j=1;j<=9;j++){ if(data[i-1][j-1]==0) continue; @@ -112,8 +112,8 @@ protected void paintComponent(Graphics g) { int prerow=message.getPrerow(); int col=message.getCol(); int row=message.getRow(); - if((message.isYourTurn() || message.getRole()>2) && message.getCode()==Code.Run && row!=0){//Թ - //Էʾ + if((message.isYourTurn() || message.getRole()>2) && message.getCode()==Code.Run && row!=0){//或者是旁观者 + //画对方的提示 g.setColor(Color.GREEN); g.drawLine(precol*width/11-width/22, prerow*height/12-height/24, precol*width/11-width/44, prerow*height/12-height/24); g.drawLine(precol*width/11-width/22, prerow*height/12-height/24, precol*width/11-width/22, prerow*height/12-height/48); @@ -134,7 +134,7 @@ protected void paintComponent(Graphics g) { g.drawLine(col*width/11-width/22, row*height/12+height/24, col*width/11-width/22, row*height/12+height/48); g.setColor(Color.black); } - //һʾ + //画一个提示 if(tip){ g.setColor(Color.green); g.drawLine(col*width/11-width/22, row*height/12-height/24, col*width/11-width/44, row*height/12-height/24); @@ -149,15 +149,15 @@ protected void paintComponent(Graphics g) { } } - //¼ + //鼠标事件 @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub - if(!message.isYourTurn() || message.getRole()>2 || message.code.getDes()=="") return;//Լغϻ߽ɫΪԹ߻ߵǰϷ״̬Ѿ - //޷ - //ȷλãȡӵк - //isSelectedΪ٣ôcolrow´isSelectedΪʱ֮ǰpreColpreRow - //isSelectedΪ棬ôcolrowڵcolrow + if(!message.isYourTurn() || message.getRole()>2 || message.code.getDes()=="结束") return;//如果不是自己回合或者角色为旁观者或者当前游戏状态已经结束 + //则无法点击棋盘 + //确定位置,获取现在棋子的列和行 + //如果isSelected为假,那么这个列col和行row将是下次isSelected为真的时候之前的preCol和preRow + //如果isSelected为真,那么这个列col和行row将是现在的col和row int width=getWidth(); int height=getHeight(); int x=e.getX(); @@ -177,11 +177,11 @@ public void mouseClicked(MouseEvent e) { } } - if(isSelected){//ѾѡӣƶӲʱĸѾȷˣ - //жܲܵ - //жǷ񱻽 + if(isSelected){//如果已经选中了棋子,接下来就是移动棋子操作(此时四个坐标已经确定了) + //判断能不能到 + //先判断是否被将军 int[] aixs=new int[4]; - if(message.getRole()==1){//췽 + if(message.getRole()==1){//红方 aixs=new int[]{message.getPrerow(),message.getPrecol(),message.getRow(),message.getCol()}; }else{ aixs=new int[]{11-message.getPrerow(),message.getPrecol(),11-message.getRow(),message.getCol()}; @@ -191,28 +191,28 @@ public void mouseClicked(MouseEvent e) { message.setChess(data[message.getPrerow()-1][message.getPrecol()-1]); - JudgeMove jm=new JudgeMove(this,data,message.getRole()==1);//ƶж϶ + JudgeMove jm=new JudgeMove(this,data,message.getRole()==1);//移动规则判断对象 switch (message.getChess()) { case 1: case 8: - //dz - if(!jm.move_che(aixs)){//ƶĹ򲻺 - common_op(aixs);//ָ + //是车 + if(!jm.move_che(aixs)){//移动车的规则不合理 + common_op(aixs);//撤销、恢复坐标 return; } break; case 2: case 9: - // - if(!jm.move_ma(aixs)){//ƶĹ򲻺 + //是马 + if(!jm.move_ma(aixs)){//移动马的规则不合理 common_op(aixs); return; } break; case 3: case 10: - // + //是相 if(!jm.move_xiang(aixs)){ common_op(aixs); return; @@ -220,7 +220,7 @@ public void mouseClicked(MouseEvent e) { break; case 4: case 11: - //ʿ + //士 if(!jm.move_shi(aixs)){ common_op(aixs); return; @@ -228,7 +228,7 @@ public void mouseClicked(MouseEvent e) { break; case 5: case 12: - //ǽ + //是将 if(!jm.move_jiang(aixs)){ common_op(aixs); return; @@ -236,7 +236,7 @@ public void mouseClicked(MouseEvent e) { break; case 6: case 13: - // + //是炮 if(!jm.move_pao(aixs)){ common_op(aixs); return; @@ -244,7 +244,7 @@ public void mouseClicked(MouseEvent e) { break; case 7: case 14: - //DZ + //是兵 if(!jm.move_bing(aixs)){ common_op(aixs); return; @@ -254,19 +254,19 @@ public void mouseClicked(MouseEvent e) { break; } - //Խƶ - int[][] datasub=Common.Backup(data);//ȱ֣̽ - message.setEatedChess(datasub[message.getRow()-1][message.getCol()-1]);//ñԵ + //可以进行移动 + int[][] datasub=Common.Backup(data);//先备份棋局,下面进行试探性走棋 + message.setEatedChess(datasub[message.getRow()-1][message.getCol()-1]);//设置被吃掉的子 datasub[message.getRow()-1][message.getCol()-1]=datasub[message.getPrerow()-1][message.getPrecol()-1]; datasub[message.getPrerow()-1][message.getPrecol()-1]=0; - if(!HasFinished.jiangTip(datasub, message.getRole()==1)){//ǰˣͽ + if(!HasFinished.jiangTip(datasub, message.getRole()==1)){//当前被将军了,不能送将 if(message.getRole()==2) message.setPrerow(11-message.getPrerow()); isSelected=true; return; - }//ǰ + }//当前步不能走 - //ˣƶIJ򡢶ûͽԼϷûн + //到此,表明移动的步符合走棋规则、而且没有送将、以及游戏没有结束 data=datasub; try { message.setStep(message.getStep()+1); @@ -274,21 +274,21 @@ public void mouseClicked(MouseEvent e) { } catch (CloneNotSupportedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); - }//͹Ϣ + }//发送构造的消息 - //ж̽Ƿ񵽴Ϸ״̬ - if(new HasFinished(data).isFinished(message.getRole()==1)){//Ϸ - NormalMessage myNormalMessage=new NormalMessage();//һϢ + //判断试探性走棋是否到达游戏结束状态 + if(new HasFinished(data).isFinished(message.getRole()==1)){//游戏结束 + NormalMessage myNormalMessage=new NormalMessage();//创建一般消息 myNormalMessage.setRole(message.getRole()); - myNormalMessage.setAttach("Ϸ"); + myNormalMessage.setAttach("游戏结束"); internet.writeMessage(myNormalMessage); message.setCode(Code.Over); } - isSelected=false;//ѡΪfalse½׶ѡӣƶ - message.setYourTurn(!message.isYourTurn());//غл + isSelected=false;//被选中为false,这样下阶段是选择棋子,而不是移动棋子 + message.setYourTurn(!message.isYourTurn());//回合切换 tip=false; - }else{//ѡӽ׶ΣisSelectedΪfalse + }else{//选子阶段,这是isSelected为false的情况 message.setPrecol(message.getCol()); message.setPrerow(message.getRow()); if(message.getRole()==2){ @@ -305,9 +305,9 @@ public void mouseClicked(MouseEvent e) { return; } - //˺ѡ - tip=true;//ʾ - isSelected=true;//isSelectedΪ棬һ׶ƶӶѡ + //到此合理选择了棋子 + tip=true;//开启提示功能 + isSelected=true;//设置isSelected为真,下一阶段是移动棋子而不是选择棋子 if(message.getRole()==2){ message.setPrerow(11-message.getPrerow()); message.setRow(11-message.getRow()); @@ -326,7 +326,7 @@ public void setSelected(boolean isSelected) { } /** - * ݷϢн + * 与数据服务器和消息服务器进行交互 */ @Override public void run() { @@ -335,75 +335,75 @@ public void run() { Message myMessage=internet.readMessage(); message.setCode(Code.Run); if(myMessage instanceof NormalMessage){ - //Ϣ - if(((NormalMessage) myMessage).getAttach().equals("")){ - int showConfirmDialog = JOptionPane.showConfirmDialog(null, "Է壬Ƿͬ"); - if(message.getRole()>2) continue;//Թ + //如果是消息 + if(((NormalMessage) myMessage).getAttach().equals("悔棋")){ + int showConfirmDialog = JOptionPane.showConfirmDialog(null, "对方请求悔棋,是否同意"); + if(message.getRole()>2) continue;//旁观者清 if(showConfirmDialog==JOptionPane.YES_OPTION){ - //ͬϢ + //发个同意的消息 NormalMessage temp=new NormalMessage(); - temp.setAttach("ͬ"); + temp.setAttach("同意悔棋"); internet.writeMessage(temp); }else if(showConfirmDialog==JOptionPane.NO_OPTION){ - //ͬ + //不同意悔棋 NormalMessage temp=new NormalMessage(); - temp.setAttach("Էͬ"); + temp.setAttach("对方不同意悔棋"); internet.writeMessage(temp); } - }else if(((NormalMessage) myMessage).getAttach().equals("Ϸ")){ - String role=myMessage.getRole()==1?"췽":"ڷ"; - result=role+"ʤ"; + }else if(((NormalMessage) myMessage).getAttach().equals("游戏结束")){ + String role=myMessage.getRole()==1?"红方":"黑方"; + result=role+"胜利"; message.setCode(Code.Over); repaint(); - }else if(((NormalMessage)myMessage).getAttach().equals("¿")){ - int showConfirmDialog = JOptionPane.showConfirmDialog(null, "Էһ֣Ƿͬ"); - if(message.getRole()>2) continue;//ͬ¿ʼ + }else if(((NormalMessage)myMessage).getAttach().equals("重新开局")){ + int showConfirmDialog = JOptionPane.showConfirmDialog(null, "对方请求再来一局,是否同意"); + if(message.getRole()>2) continue;//同意重新开始 if(showConfirmDialog==JOptionPane.YES_OPTION){ - //ͬϢ + //发个同意的消息 NormalMessage temp=new NormalMessage(); - temp.setAttach("ͬ⿪"); + temp.setAttach("同意开局"); internet.writeMessage(temp); }else if(showConfirmDialog==JOptionPane.NO_OPTION){ - //ͬ + //不同意悔棋 NormalMessage temp=new NormalMessage(); - temp.setAttach("Էͬ⿪"); + temp.setAttach("对方不同意开局"); internet.writeMessage(temp); } - }else if(((NormalMessage)myMessage).getAttach().equals("ͬ⿪")){ - //ԼݰԷ + }else if(((NormalMessage)myMessage).getAttach().equals("同意开局")){ + //将自己的最初数据包发给对方 init(); result=""; repaint(); - }else if(((NormalMessage)myMessage).getAttach().contains("")){ - result=message.getRole()==1?"췽ʤ":"ڷʤ"; + }else if(((NormalMessage)myMessage).getAttach().contains("认输")){ + result=message.getRole()==1?"红方获胜":"黑方获胜"; message.setCode(Code.Over); repaint(); - }else if(((NormalMessage)myMessage).getAttach().equals("")){ - int showConfirmDialog = JOptionPane.showConfirmDialog(null, "ԷͣǷͬ"); + }else if(((NormalMessage)myMessage).getAttach().equals("求和")){ + int showConfirmDialog = JOptionPane.showConfirmDialog(null, "对方请求求和,是否同意"); if(showConfirmDialog==JOptionPane.YES_OPTION){ - //ͬϢ + //发个同意的消息 NormalMessage temp=new NormalMessage(); - temp.setAttach("ͬ"); + temp.setAttach("同意求和"); internet.writeMessage(temp); }else if(showConfirmDialog==JOptionPane.NO_OPTION){ - //ͬ + //不同意悔棋 NormalMessage temp=new NormalMessage(); - temp.setAttach("Էͬ"); + temp.setAttach("对方不同意求和"); internet.writeMessage(temp); } - }else if(((NormalMessage)myMessage).getAttach().equals("ͬ")){ - result="˫"; + }else if(((NormalMessage)myMessage).getAttach().equals("同意求和")){ + result="双方议和"; message.setCode(Code.Over); repaint(); - }else if(((NormalMessage)myMessage).getAttach().equals("뿪")){ - JOptionPane.showMessageDialog(null, "ԷѾ뿪,˳"); + }else if(((NormalMessage)myMessage).getAttach().equals("离开")){ + JOptionPane.showMessageDialog(null, "对方已经离开,即将退出"); System.exit(1); } if(filterMessage(myMessage)) JOptionPane.showMessageDialog(null, ((NormalMessage) myMessage).getAttach()); continue; } - // + //如果是数据 message.setPrerow(((DataMessage) myMessage).getPrerow()); message.setPrecol(((DataMessage) myMessage).getPrecol()); message.setRow(((DataMessage) myMessage).getRow()); @@ -423,48 +423,48 @@ public void run() { } /** - * 幦ܵʵ + * 悔棋功能的实现 */ public void Redo() { - //ִл - //д + //执行悔棋 + //向服务器写悔棋请求数据 if(message.isYourTurn()){ - JOptionPane.showMessageDialog(null, ",ܻ"); + JOptionPane.showMessageDialog(null, "到你走棋了,不能悔棋"); return; } if(message.getStep()==1){ - JOptionPane.showMessageDialog(null, "ǰǵһܻ"); + JOptionPane.showMessageDialog(null, "当前是第一步,不能悔棋"); return; } NormalMessage message=new NormalMessage(); - message.setAttach(""); + message.setAttach("悔棋"); internet.writeMessage(message); } /** - * ¿ʼʵ + * 重新开始的实现 */ public void restart() { - if(message.getCode().getDes().equals("")){ - //ϷѾ¿ʼ + if(message.getCode().getDes().equals("结束")){ + //游戏已经结束,可以重新开始 NormalMessage message=new NormalMessage(); - message.setAttach("¿"); + message.setAttach("重新开局"); message.setRole(message.getRole()); internet.writeMessage(message); }else{ - JOptionPane.showMessageDialog(null, "Ϸûн,"); + JOptionPane.showMessageDialog(null, "游戏没有结束,请继续走棋"); return; } } public void lose() { if(message.getStep()<20){ - JOptionPane.showMessageDialog(null, "ʮ֮ڲ"); + JOptionPane.showMessageDialog(null, "二十步之内不能认输"); return; } - result=message.getRole()==1?"ڷʤ":"췽ʤ"; + result=message.getRole()==1?"黑方胜利":"红方胜利"; NormalMessage myMessage=new NormalMessage(); - myMessage.setAttach((message.getRole()==1?"췽":"ڷ")+""); + myMessage.setAttach((message.getRole()==1?"红方":"黑方")+"认输"); internet.writeMessage(myMessage); message.setCode(Code.Over); repaint(); @@ -472,21 +472,21 @@ public void lose() { public void peace() { if(message.getStep()<50){ - JOptionPane.showMessageDialog(null, "ʮ֮ڲ"); + JOptionPane.showMessageDialog(null, "五十步之内不能求和"); return; } NormalMessage message=new NormalMessage(); - message.setAttach(""); + message.setAttach("求和"); internet.writeMessage(message); } public void leave() { NormalMessage message=new NormalMessage(); - message.setAttach("뿪"); + message.setAttach("离开"); internet.writeMessage(message); } - //ͼĹ,е + //绘图的公共操作,绘制棋盘中的米字 private void drawLines(int row,int col,int width,int height,Graphics g) { //1/2 2/3 1/3 int baseX=width*col/11; @@ -513,7 +513,7 @@ private void drawLines(int row,int col,int width,int height,Graphics g) { } } - //ڲƶʱָܻƶǰ״̬ + //用于在不合理的移动棋子时候能恢复到移动前的坐标状态 protected void common_op(int[] aixs) { if(message.getRole()==1){ message.setPrerow(aixs[0]); @@ -527,12 +527,12 @@ protected void common_op(int[] aixs) { repaint(); } - //һЩϢʹ䲻ʾ + //过滤一些消息,使其不显示 private boolean filterMessage(Message message) { - //Ϣ - if(((NormalMessage)message).getAttach().equals("") || - ((NormalMessage)message).getAttach().equals("Ϸ") || ((NormalMessage)message).getAttach().equals("ͬ⿪") - || ((NormalMessage)message).getAttach().equals("") || ((NormalMessage)message).getAttach().equals("ͬ")) + //过滤消息 + if(((NormalMessage)message).getAttach().equals("悔棋") || + ((NormalMessage)message).getAttach().equals("游戏结束") || ((NormalMessage)message).getAttach().equals("同意开局") + || ((NormalMessage)message).getAttach().equals("求和") || ((NormalMessage)message).getAttach().equals("同意求和")) return false; return true; } diff --git a/src/mychess/client/ChessClient.java b/src/mychess/client/ChessClient.java index 62fc575..0122040 100644 --- a/src/mychess/client/ChessClient.java +++ b/src/mychess/client/ChessClient.java @@ -15,32 +15,32 @@ import mychess.util.ReadProperties; -//ʵͼν,ͨ +//该类实现象棋的图形界面,并负责通信 public class ChessClient extends JFrame{ private static final long serialVersionUID = 1L; - private JButton restart=new JButton("ؿ"); - private JButton redo=new JButton(""); - private JButton lose=new JButton(""); - private JButton peace=new JButton(""); - private Image[] pics =new Image[15];//ͼƬ + private JButton restart=new JButton("重开"); + private JButton redo=new JButton("悔棋"); + private JButton lose=new JButton("认输"); + private JButton peace=new JButton("求和"); + private Image[] pics =new Image[15];//加载象棋图片 private ChessBoard panel; public ChessClient() { -// pics[1]=Toolkit.getDefaultToolkit().getImage("images/1.png"); -// pics[2]=Toolkit.getDefaultToolkit().getImage("images/1.png"); -// pics[3]=Toolkit.getDefaultToolkit().getImage("images/1.png"); -// pics[4]=Toolkit.getDefaultToolkit().getImage("images/ʿ1.png"); -// pics[5]=Toolkit.getDefaultToolkit().getImage("images/˧.png"); -// pics[6]=Toolkit.getDefaultToolkit().getImage("images/1.png"); -// pics[7]=Toolkit.getDefaultToolkit().getImage("images/.png"); -// pics[8]=Toolkit.getDefaultToolkit().getImage("images/2.png"); -// pics[9]=Toolkit.getDefaultToolkit().getImage("images/2.png"); -// pics[10]=Toolkit.getDefaultToolkit().getImage("images/2.png"); -// pics[11]=Toolkit.getDefaultToolkit().getImage("images/ʿ2.png"); -// pics[12]=Toolkit.getDefaultToolkit().getImage("images/.png"); -// pics[13]=Toolkit.getDefaultToolkit().getImage("images/2.png"); -// pics[14]=Toolkit.getDefaultToolkit().getImage("images/.png"); +// pics[1]=Toolkit.getDefaultToolkit().getImage("images/车1.png"); +// pics[2]=Toolkit.getDefaultToolkit().getImage("images/马1.png"); +// pics[3]=Toolkit.getDefaultToolkit().getImage("images/相1.png"); +// pics[4]=Toolkit.getDefaultToolkit().getImage("images/士1.png"); +// pics[5]=Toolkit.getDefaultToolkit().getImage("images/帅.png"); +// pics[6]=Toolkit.getDefaultToolkit().getImage("images/炮1.png"); +// pics[7]=Toolkit.getDefaultToolkit().getImage("images/兵.png"); +// pics[8]=Toolkit.getDefaultToolkit().getImage("images/车2.png"); +// pics[9]=Toolkit.getDefaultToolkit().getImage("images/马2.png"); +// pics[10]=Toolkit.getDefaultToolkit().getImage("images/象2.png"); +// pics[11]=Toolkit.getDefaultToolkit().getImage("images/士2.png"); +// pics[12]=Toolkit.getDefaultToolkit().getImage("images/将.png"); +// pics[13]=Toolkit.getDefaultToolkit().getImage("images/炮2.png"); +// pics[14]=Toolkit.getDefaultToolkit().getImage("images/卒.png"); pics[1]=Toolkit.getDefaultToolkit().getImage("images/chess11.png"); pics[2]=Toolkit.getDefaultToolkit().getImage("images/chess10.png"); @@ -118,13 +118,13 @@ protected void processWindowEvent(WindowEvent e) { } public static void main(String[] args) { - ReadProperties.read();//ȡļ + ReadProperties.read();//读取配置文件 JFrame frame=new ChessClient(); - frame.setTitle(""); + frame.setTitle("象棋"); frame.setSize(900,700); // frame.setExtendedState(JFrame.MAXIMIZED_BOTH); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); } -} +} \ No newline at end of file diff --git a/src/mychess/entity/Code.java b/src/mychess/entity/Code.java index 935914a..219b092 100644 --- a/src/mychess/entity/Code.java +++ b/src/mychess/entity/Code.java @@ -1,9 +1,9 @@ package mychess.entity; public enum Code { - //Ϣ״̬ - Normal(200,""),Redo(201,""),Aggree(500,"ͬ"),Refuse(501,"ͬ"), - Free(100,""),Over(104,""),Run(103,""),Prepare(102,"׼"); + //消息状态代号 + Normal(200,"正常"),Redo(201,"撤销"),Aggree(500,"同意悔棋"),Refuse(501,"不同意悔棋"), + Free(100,"空闲"),Over(104,"结束"),Run(103,"进行"),Prepare(102,"准备"); private int id; private String des; @@ -28,4 +28,4 @@ public String getDes() { public void setDes(String des) { this.des = des; } -} +} \ No newline at end of file diff --git a/src/mychess/entity/DataMessage.java b/src/mychess/entity/DataMessage.java index 1a75107..17743e4 100644 --- a/src/mychess/entity/DataMessage.java +++ b/src/mychess/entity/DataMessage.java @@ -2,25 +2,25 @@ import java.io.Serializable; /** - * ͻ˴͵˵Ϣ - * 201913 10:07:45 + * 客户端传送到服务端的数据消息 + * 2019年1月3日 下午10:07:45 */ public class DataMessage extends Message implements Serializable{ private static final long serialVersionUID = 1L; - public int prerow;//ƶǰ + public int prerow;//移动前棋子所在行 - public int precol;//ƶǰ + public int precol;//移动前棋子所在列 - public int row;// + public int row;//现在棋子所在行 - public int col;// + public int col;//现在棋子所在列 - public int chess;//ƶӴ + public int chess;//移动的棋子代号 - public int eatedChess;//Ե + public int eatedChess;//被吃掉的棋子 - public String data;// + public String data;//数据 public int getPrerow() { return prerow; @@ -77,4 +77,4 @@ public void setData(String data) { public String getData() { return data; } -} +} \ No newline at end of file diff --git a/src/mychess/entity/Message.java b/src/mychess/entity/Message.java index 0f2bbce..53a505a 100644 --- a/src/mychess/entity/Message.java +++ b/src/mychess/entity/Message.java @@ -3,21 +3,21 @@ import java.io.Serializable; /** - * ͳһĸʽڿͻ˺ͷ˽ - * 201913 9:55:29 + * 定义统一报文格式,用于客户端和服务器端交互 + * 2019年1月3日 下午9:55:29 */ public class Message implements Serializable,Cloneable{ private static final long serialVersionUID = 1L; - public Code code;//Ϣ״̬ţϵͳϢţûϢԼϷ̴ŵ + public Code code;//消息状态代号,包括系统消息代号,用户消息代号以及游戏过程代号等 - public boolean isValid=true;//ñǷԵǰsocketЧ + public boolean isValid=true;//该报文是否对当前socket无效 - public byte role;//ǰɫ + public byte role;//当前角色 - public boolean yourTurn;//ǷĻغ + public boolean yourTurn;//是否是你的回合 - public int step;//ǰ + public int step;//当前步数 public boolean isValid() { return isValid; @@ -64,4 +64,4 @@ public Message clone() throws CloneNotSupportedException { // TODO Auto-generated method stub return (Message) super.clone(); } -} +} \ No newline at end of file diff --git a/src/mychess/entity/MyObjectOutputStream.java b/src/mychess/entity/MyObjectOutputStream.java index ddd6a91..695afd8 100644 --- a/src/mychess/entity/MyObjectOutputStream.java +++ b/src/mychess/entity/MyObjectOutputStream.java @@ -15,7 +15,6 @@ public MyObjectOutputStream(OutputStream out) throws IOException { super(out); } - //дObjectOutputStreamķ,invalid AC protected void writeStreamHeader() throws IOException { return; } diff --git a/src/mychess/entity/NormalMessage.java b/src/mychess/entity/NormalMessage.java index dfeda6c..fc99c52 100644 --- a/src/mychess/entity/NormalMessage.java +++ b/src/mychess/entity/NormalMessage.java @@ -3,12 +3,12 @@ import java.io.Serializable; /** - * һϢ - * 201913 10:10:34 + * 一般消息 + * 2019年1月3日 下午10:10:34 */ public class NormalMessage extends Message implements Serializable{ private static final long serialVersionUID = 1L; - public String attach;//Ϣ + public String attach;//附加消息 public String getAttach() { return attach; @@ -17,4 +17,4 @@ public String getAttach() { public void setAttach(String attach) { this.attach = attach; } -} +} \ No newline at end of file diff --git a/src/mychess/entity/Server.java b/src/mychess/entity/Server.java index 04b4593..4811f7d 100644 --- a/src/mychess/entity/Server.java +++ b/src/mychess/entity/Server.java @@ -19,36 +19,36 @@ import java.io.ObjectOutputStream; /** - * ݷ + * 数据服务器类 */ public class Server { /** - * ѾӵĿͻ׽֣Ϊһб + * 所有已经连接到本服务器的客户端套接字,保存为一个列表 */ private List clients; /** - * ݷ״̬
- * staticακӵĿͻܹ
- * ΪԹṩʱ׼ȷ
+ * 数据服务器的棋局状态
+ * static修饰表明任何连接到本服务器的客户端能共享该数据
+ * 这可以为旁观者提供及时准确的数据
*/ private static int[][] data; - private Withdraw withdraw;// + private Withdraw withdraw;//撤销功能 - private static int time=1;//ս + private static int time=1;//对战次数 /** - * ɳʼ
- * ʼӿͻ˵б,ֵijʼ״̬ãбԼΪÿӵĿͻԵ֮߳ + * 完成初始化功能
+ * 初始化连接客户端的列表,棋局的初始状态设置,悔棋列表,以及为每个连接的客户端以单独线程与之交互 * @param */ public Server() { // TODO Auto-generated constructor stub ServerSocket socket; - clients=new ArrayList();//ѾӵĿͻ + clients=new ArrayList();//已经连接的客户端 init(); try{ - //Socket + //创建Socket socket=new ServerSocket(Integer.parseInt(ReadProperties.PORT)); while(true){ Socket socket_current=socket.accept(); @@ -61,10 +61,10 @@ public Server() { } } - // + //加载数据 private void init() { // TODO Auto-generated method stub - ReadProperties.read();//ȡļ + ReadProperties.read();//读取配置文件 data=new int[][]{{8,9,10,11,12,11,10,9,8}, {0,0,0,0,0,0,0,0,0}, @@ -82,17 +82,17 @@ private void init() { } /** - * ߳࣬ÿӵĿͻ + * 服务器线程类,处理每个连接的客户端 */ class server_thread implements Runnable{ /** - * ǰӿͻ˵׽ + * 当前连接客户端的套接字 */ Socket current_socket; /** - * ʼ׽ - * @param socketǵǰӵ׽ + * 初始化套接字 + * @param socket是当前连接的套接字 */ public server_thread(Socket socket) { // TODO Auto-generated constructor stub @@ -100,7 +100,7 @@ public server_thread(Socket socket) { } /** - * ڵǰӵ׽֣֮н + * 于当前连接的套接字,服务器与之进行交互 */ @Override public void run() { @@ -112,7 +112,7 @@ public void run() { inputStreamFromClient=new ObjectInputStream(current_socket.getInputStream()); DataMessage message=new DataMessage(); if(clients.size()==1){ - //ֻһû + //只有一个用户 if((time&1)==1){ message.setRole((byte)1); message.setYourTurn(true); @@ -125,7 +125,7 @@ public void run() { outputStreamToOtherClients.writeObject(message); outputStreamToOtherClients.flush(); }else if(clients.size()==2){ - //ڶûִڣ + //第二个用户执黑,后手 if((time&1)==0){ message.setRole((byte)1); message.setYourTurn(true); @@ -139,38 +139,38 @@ public void run() { outputStreamToOtherClients.flush(); } while(true){ - // - //쳣 - //յʲôϢֱת - Message myMessage=(Message) inputStreamFromClient.readObject();//յ췽Ϣ + //交互 + //这会引发异常 + //不管收到什么消息,负责直接转发 + Message myMessage=(Message) inputStreamFromClient.readObject();//接收到红方的消息 boolean restart=false; if(myMessage instanceof DataMessage){ data[((DataMessage) myMessage).getRow()-1][((DataMessage) myMessage).getCol()-1]=data[((DataMessage) myMessage).getPrerow()-1][((DataMessage) myMessage).getPrecol()-1]; data[((DataMessage) myMessage).getPrerow()-1][((DataMessage) myMessage).getPrecol()-1]=0; ((DataMessage) myMessage).setData(Common.Array_to_String(data)); - withdraw.add(myMessage);//յľͼ뵽б + withdraw.add(myMessage);//收到报文就加入到列表中 }else if(myMessage instanceof NormalMessage){ - //һϢ - if(((NormalMessage) myMessage).getAttach().equals("ͬ")){ + //一般消息 + if(((NormalMessage) myMessage).getAttach().equals("同意悔棋")){ withdraw.remove(); myMessage=(DataMessage) withdraw.getLast(); - myMessage.setValid(false);// - data=Common.String_to_Array(((DataMessage)myMessage).getData());// - }else if(((NormalMessage) myMessage).getAttach().equals("Ϸ")){ - //յsocket֪ͨ,ôsocketʤ - myMessage.setValid(false);// - }else if(((NormalMessage)myMessage).getAttach().equals("ͬ⿪")){ - myMessage.setValid(false);//͸ + myMessage.setValid(false);//发给所有人 + data=Common.String_to_Array(((DataMessage)myMessage).getData());//撤销后的棋局 + }else if(((NormalMessage) myMessage).getAttach().equals("游戏结束")){ + //收到该socket的通知,那么该socket是胜利方 + myMessage.setValid(false);//发给所有人 + }else if(((NormalMessage)myMessage).getAttach().equals("同意开局")){ + myMessage.setValid(false);//发送给所有人 init(); restart=true; time++; - }else if(((NormalMessage)myMessage).getAttach().equals("ͬ")){ + }else if(((NormalMessage)myMessage).getAttach().equals("同意求和")){ myMessage.setValid(false); } } - myMessage.setStep(withdraw.allSteps());//ϲ + myMessage.setStep(withdraw.allSteps());//加上步数 for(Socket s:clients){ - if(s==current_socket && myMessage.isValid()) continue;//validΪfalseκ + if(s==current_socket && myMessage.isValid()) continue;//如果valid为false表明发给任何人 outputStreamToOtherClients=new MyObjectOutputStream(s.getOutputStream()); outputStreamToOtherClients.writeObject(myMessage); outputStreamToOtherClients.flush(); @@ -181,7 +181,7 @@ public void run() { // TODO Auto-generated catch block clients.clear(); init(); - }finally {// + }finally {//后处理 try { inputStreamFromClient.close(); outputStreamToOtherClients.close(); @@ -194,11 +194,11 @@ public void run() { } /** - * mainִз + * main方法,执行服务器运行 * @param */ public static void main(String[] args) { System.out.println("Server run..."); new Server(); } -} +} \ No newline at end of file diff --git a/src/mychess/util/CanMove.java b/src/mychess/util/CanMove.java index 5d33f48..a6fd854 100644 --- a/src/mychess/util/CanMove.java +++ b/src/mychess/util/CanMove.java @@ -4,35 +4,35 @@ import java.util.List; /** - * ڸУڸһ̶λϵӣߵλõ + * 这是用在给定的棋局中,对于给的一个固定位置上的棋子,该棋子所有能走的位置的类 */ public class CanMove { /** - * ȡ(i,j)λϵij,ڵǰdataпƶλü - * @param dataǵǰ飬iе0ʼ,jе0ʼ, - * labelǵǰǩ
- * صһббеĿλüϡλɣֱƶǰ - * ƶ + * 获取在(i,j)位置上的车,能在当前棋局data中所有可能移动的位置集合 + * @param data是当前棋局数组,i是棋局中的行坐标从0开始,j是棋局中的列坐标从0开始, + * label是当前标签(车)
+ * 返回的是一个列表,这个列表包含所有的可能位置集合。可能位置由两对坐标组成,分别是移动前的坐标和 + * 可能移动后的坐标 */ public List che_move(int[][] data,int i,int j,int label) { List retList=new ArrayList(); - //dz - //۲߿ƶԶ + //是车 + //观察横行左边可以移动最远处 if(j-1>=0){ for(int h=j-1;h>=0;h--){ if(data[i][h]==0){ retList.add(new int[]{i,j,i,h}); }else if((label<=7 && data[i][h]>=8) || (label>=8 && data[i][h]<=7)){ retList.add(new int[]{i,j,i,h}); - break;// + break;//吃子 }else{ - //Լ + //自己子 break; } } } - //۲ұ߿ƶԶ + //观察横行右边可以移动的最远处 if(j+1<9){ for(int h=j+1;h<9;h++){ if(data[i][h]==0){ @@ -46,7 +46,7 @@ public List che_move(int[][] data,int i,int j,int label) { } } - //۲ + //观察纵行上面最大 if(i-1>=0){ for(int k=i-1;k>=0;k--){ if(data[k][j]==0){ @@ -60,7 +60,7 @@ public List che_move(int[][] data,int i,int j,int label) { } } - //۲ + //观察纵行下面最大 if(i+1<10){ for(int k=i+1;k<10;k++){ if(data[k][j]==0){ @@ -78,7 +78,7 @@ public List che_move(int[][] data,int i,int j,int label) { public List ma_move(int[][] data,int i,int j,int label) { List retList=new ArrayList<>(); - // + //东北方向 if(i-2>=0 && j+1<9){ if(data[i-1][j]==0){ if(data[i-2][j+1]==0 || (label<=7 && data[i-2][j+1]>=8) || (label>=8 && data[i-2][j+1]<=7)){ @@ -86,7 +86,7 @@ public List ma_move(int[][] data,int i,int j,int label) { } } } - // + //东北躺 if(i-1>=0 && j+2<9){ if(data[i][j+1]==0){ if(data[i-1][j+2]==0 || (label<=7 && data[i-1][j+2]>=8) || (label>=8 && data[i-1][j+2]<=7)){ @@ -94,7 +94,7 @@ public List ma_move(int[][] data,int i,int j,int label) { } } } - // + //东南躺 if(i+1<10 && j+2<9){ if(data[i][j+1]==0){ if(data[i+1][j+2]==0 || (label<=7 && data[i+1][j+2]>=8) || (label>=8 && data[i+1][j+2]<=7)){ @@ -102,7 +102,7 @@ public List ma_move(int[][] data,int i,int j,int label) { } } } - // + //东南跳 if(i+2<10 && j+1<9){ if(data[i+1][j]==0){ if(data[i+2][j+1]==0 || (label<=7 && data[i+2][j+1]>=8) || (label>=8 && data[i+2][j+1]<=7)){ @@ -110,7 +110,7 @@ public List ma_move(int[][] data,int i,int j,int label) { } } } - // + //西南躺 if(i+1<9 && j-2>=0){ if(data[i][j-1]==0){ if(data[i+1][j-2]==0 || (label<=7 && data[i+1][j-2]>=8) || (label>=8 && data[i+1][j-2]<=7)){ @@ -118,7 +118,7 @@ public List ma_move(int[][] data,int i,int j,int label) { } } } - // + //西南跳 if(i+2<10 && j-1>=0){ if(data[i+1][j]==0){ if(data[i+2][j-1]==0 || (label<=7 && data[i+2][j-1]>=8) || (label>=8 && data[i+2][j-1]<=7)){ @@ -126,7 +126,7 @@ public List ma_move(int[][] data,int i,int j,int label) { } } } - // + //西北躺 if(i-1>=0 && j-2>=0){ if(data[i][j-1]==0){ if(data[i-1][j-2]==0 || (label<=7 && data[i-1][j-2]>=8) || (label>=8 && data[i-1][j-2]<=7)){ @@ -134,7 +134,7 @@ public List ma_move(int[][] data,int i,int j,int label) { } } } - // + //西北跳 if(i-2>=0 && j-1>=0){ if(data[i-1][j]==0){ if(data[i-2][j-1]==0 || (label<=7 && data[i-2][j-1]>=8) || (label>=8 && data[i-2][j-1]<=7)){ @@ -146,7 +146,7 @@ public List ma_move(int[][] data,int i,int j,int label) { } public List pao_move(int[][] data,int i,int j,int label) { - // + //是炮 List retList=new ArrayList<>(); if(j-1>=0){ for(int h=j-1;h>=0;h--){ @@ -241,8 +241,8 @@ else if((label<=7 && data[k][j]>=8)||(label>=8 && data[k][j]<=7)){ public List xiang_1_move(int[][] data,int i,int j) { List retList=new ArrayList<>(); - // - // + //相 + //东北 if(i-2>=10/2 && j+2<9){ if(data[i-1][j+1]==0){ if(data[i-2][j+2]==0 || data[i-2][j+2]>=8){ @@ -250,7 +250,7 @@ public List xiang_1_move(int[][] data,int i,int j) { } } } - // + //东南 if(i+2<10 && j+2<9){ if(data[i+1][j+1]==0){ if(data[i+2][j+2]==0 || data[i+2][j+2]>=8){ @@ -258,7 +258,7 @@ public List xiang_1_move(int[][] data,int i,int j) { } } } - // + //西南 if(i+2<10 && j-2>=0){ if(data[i+1][j-1]==0){ if(data[i+2][j-2]==0 || data[i+2][j-2]>=8){ @@ -266,7 +266,7 @@ public List xiang_1_move(int[][] data,int i,int j) { } } } - // + //西北 if(i-2>=10/2 && j-2>=0){ if(data[i-1][j-1]==0){ if(data[i-2][j-2]==0 || data[i-2][j-2]>=8){ @@ -278,9 +278,9 @@ public List xiang_1_move(int[][] data,int i,int j) { } public List xiang_2_move(int[][] data,int i,int j) { - // + //像蓝 List retList=new ArrayList<>(); - // + //东北 if(i-2>=0 && j+2<9){ if(data[i-1][j+1]==0){ if(data[i-2][j+2]<=7){ @@ -288,7 +288,7 @@ public List xiang_2_move(int[][] data,int i,int j) { } } } - // + //东南 if(i+2<10/2 && j+2<9){ if(data[i+1][j+1]==0){ if(data[i+2][j+2]<=7){ @@ -296,7 +296,7 @@ public List xiang_2_move(int[][] data,int i,int j) { } } } - // + //西南 if(i+2<10/2 && j-2>=0){ if(data[i+1][j-1]==0){ if(data[i+2][j-2]<=7){ @@ -304,7 +304,7 @@ public List xiang_2_move(int[][] data,int i,int j) { } } } - // + //西北 if(i-2>=0 && j-2>=0){ if(data[i-1][j-1]==0){ if(data[i-2][j-2]<=7){ @@ -316,27 +316,27 @@ public List xiang_2_move(int[][] data,int i,int j) { } public List shi_1_move(int[][] data,int i,int j) { - //ʿ + //士 List retList=new ArrayList<>(); - // + //东北 if(i-1>=7 && j+1<=5){ if(data[i-1][j+1]==0 || data[i-1][j+1]>=8){ retList.add(new int[]{i,j,i-1,j+1}); } } - // + //东南 if(i+1<10 && j+1<=5){ if(data[i-1][j+1]==0 || data[i-1][j+1]>=8){ retList.add(new int[]{i,j,i+1,j+1}); } } - // + //西南 if(i+1<10 && j-1>=3){ if(data[i+1][j-1]==0 || data[i+1][j-1]>=8){ retList.add(new int[]{i,j,i+1,j-1}); } } - // + //西北 if(i-1>=7 && j-1>=3){ if(data[i-1][j-1]==0 || data[i-1][j-1]>=8){ retList.add(new int[]{i,j,i-1,j-1}); @@ -346,27 +346,27 @@ public List shi_1_move(int[][] data,int i,int j) { } public List shi_2_move(int[][] data,int i,int j) { - //ɫʿ + //蓝色士 List retList=new ArrayList<>(); - // + //东北 if(i-1>=0 && j+1<=5){ if(data[i-1][j+1]<=7){ retList.add(new int[]{i,j,i-1,j+1}); } } - // + //东南 if(i+1<=2 && j+1<=5){ if(data[i+1][j+1]<=7){ retList.add(new int[]{i,j,i+1,j+1}); } } - // + //西南 if(i+1<=2 && j-1>=3){ if(data[i+1][j-1]<=7){ retList.add(new int[]{i,j,i+1,j-1}); } } - // + //西北 if(i-1>=0 && j-1>=3){ if(data[i-1][j-1]<=7){ retList.add(new int[]{i,j,i-1,j-1}); @@ -376,27 +376,27 @@ public List shi_2_move(int[][] data,int i,int j) { } public List jiang_2_move(int[][] data,int i,int j) { - // + //将 List retList=new ArrayList<>(); - // + //上 if(i-1>=0){ if(data[i-1][j]<=7){ retList.add(new int[]{i,j,i-1,j}); } } - // + //下 if(i+1<=2){ if(data[i+1][j]<=7){ retList.add(new int[]{i,j,i+1,j}); } } - // + //左 if(j-1>=3){ if(data[i][j-1]<=7){ retList.add(new int[]{i,j,i,j-1}); } } - // + //右 if(j+1<=5){ if(data[i][j+1]<=7){ retList.add(new int[]{i,j,i,j+1}); @@ -406,27 +406,27 @@ public List jiang_2_move(int[][] data,int i,int j) { } public List jiang_1_move(int[][] data,int i,int j) { - //˧ + //帅 List retList=new ArrayList<>(); - // + //上 if(i-1<=7){ if(data[i-1][j]==0 || data[i-1][j]>=8){ retList.add(new int[]{i,j,i-1,j}); } } - // + //下 if(i+1<10){ if(data[i+1][j]==0 || data[i+1][j]>=8){ retList.add(new int[]{i,j,i+1,j}); } } - // + //左 if(j-1>=3){ if(data[i][j-1]==0 || data[i][j-1]>=8){ retList.add(new int[]{i,j,i,j-1}); } } - // + //右 if(j+1<=5){ if(data[i][j+1]==0 || data[i][j+1]>=8){ retList.add(new int[]{i,j,i,j+1}); @@ -437,26 +437,26 @@ public List jiang_1_move(int[][] data,int i,int j) { public List bing_1_move(int[][] data,int i,int j) { List retList=new ArrayList<>(); - // + //兵 if(i>4){ - //ûй + //没有过河 if(data[i-1][j]==0 || data[i-1][j]>=8){ retList.add(new int[]{i,j,i-1,j}); } }else{ - // + //上 if(i-1>=0){ if(data[i-1][j]==0 || data[i-1][j]>=8){ retList.add(new int[]{i,j,i-1,j}); } } - // + //左 if(j-1>=0){ if(data[i][j-1]==0 || data[i][j-1]>=8){ retList.add(new int[]{i,j,i,j-1}); } } - // + //右 if(j+1<9){ if(data[i][j+1]==0 || data[i][j+1]>=8){ retList.add(new int[]{i,j,i,j+1}); @@ -468,26 +468,26 @@ public List bing_1_move(int[][] data,int i,int j) { public List bing_2_move(int[][] data,int i,int j) { List retList=new ArrayList<>(); - // + //卒 if(i<5){ - //ûй + //没有过河 if(data[i+1][j]<=7){ retList.add(new int[]{i,j,i+1,j}); } }else{ - // + //上 if(i+1<10){ if(data[i+1][j]<=7){ retList.add(new int[]{i,j,i+1,j}); } } - // + //左 if(j-1>=0){ if(data[i][j-1]<=7){ retList.add(new int[]{i,j,i,j-1}); } } - // + //右 if(j+1<9){ if(data[i][j+1]<=7){ retList.add(new int[]{i,j,i,j+1}); diff --git a/src/mychess/util/Common.java b/src/mychess/util/Common.java index 136ede6..dc4c08a 100644 --- a/src/mychess/util/Common.java +++ b/src/mychess/util/Common.java @@ -1,29 +1,29 @@ package mychess.util; /** - * ǹй + * 这是工具类中公共部分 */ public class Common { - public static int MODE;//Ϸģʽ,1ʾģʽ,2ʾģʽ,3ʾ˻ģʽ,4ʾģʽ + public static int MODE;//游戏模式,1表示单机模式,2表示联网模式,3表示人机模式,4表示机机模式 - public static boolean isRed;//ǷΪ췽 + public static boolean isRed;//单机是否为红方 /** - * ݵǰ飬ʹclone - * @param dataǵǰ + * 备份当前棋局数组,可以使用clone代替 + * @param data是当前棋局 */ public static int[][] Backup(int[][] data) { int[][] sub=new int[data.length][data[0].length]; for(int i=0;i> all=all_move(Common.Backup(data),isRed); for(int i=0;i> all_move(int[][] datasub,boolean isRed) {// 췽ʱܶ + public static List> all_move(int[][] datasub,boolean isRed) {// 红方或者蓝方此时能动的所有子 List> all = new ArrayList<>(); CanMove cm = new CanMove(); //int[][] datasub = Common.Backup(data); @@ -60,42 +60,42 @@ public boolean isFinished(boolean isRed) { for (int j = 0; j < datasub[i].length; j++) { if ((!isRed && datasub[i][j] > 7) || (isRed && datasub[i][j]<=7)) - continue;// ӲԼ,Ϊǰִ굽һֱӿǵһ + continue;// 该子不是自己的,因为当前方执行完到另一方,这里直接考虑到另一方了 switch (datasub[i][j]) { - case 1:// + case 1:// 车 case 8: all.add(cm.che_move(datasub, i, j, datasub[i][j])); break; - case 2:// + case 2:// 马 case 9: all.add(cm.ma_move(datasub, i, j, datasub[i][j])); break; - case 3:// + case 3:// 象 all.add(cm.xiang_1_move(datasub, i, j)); break; - case 4:// ʿ + case 4:// 士 all.add(cm.shi_1_move(datasub, i, j)); break; - case 5:// ˧ + case 5:// 帅 all.add(cm.jiang_1_move(datasub, i, j)); break; - case 6:// + case 6:// 炮 case 13: all.add(cm.pao_move(datasub, i, j, datasub[i][j])); break; - case 7:// + case 7:// 兵 all.add(cm.bing_1_move(datasub, i, j)); break; - case 10:// + case 10:// 相 all.add(cm.xiang_2_move(datasub, i, j)); break; - case 11:// ʿ + case 11:// 士 all.add(cm.shi_2_move(datasub, i, j)); break; - case 12:// + case 12:// 将 all.add(cm.jiang_2_move(datasub, i, j)); break; - case 14:// + case 14:// 卒 all.add(cm.bing_2_move(datasub, i, j)); break; default: @@ -107,8 +107,8 @@ public boolean isFinished(boolean isRed) { } /** - * Ƿ񱻽 - * @param subucǵǰı + * 是否被将军 + * @param subuc是当前棋局数组的备份数组 */ public static boolean isJiang(int[][] subuc) { int jx = -1; @@ -123,8 +123,8 @@ public static boolean isJiang(int[][] subuc) { } if (jx == -1) return true; - // ҵλ - // ſǷжԷij + // 找到将军的位置 + // 横着看是否有对方的车 for (int h = jy - 1; h >= 0; h--) { if (subuc[jx][h] == 0) continue; @@ -143,7 +143,7 @@ else if (subuc[jx][h] == 1) break; } } - // ſ + // 竖着看 if (jx - 1 >= 0) { for (int h = jx - 1; h >= 0; h--) { if (subuc[h][jy] == 0) @@ -165,12 +165,12 @@ else if (subuc[h][jy] == 1) } } - // Ƿڽ + // 看是否被炮将军 for (int h = jy - 1; h >= 0; h--) { if (subuc[jx][h] == 0) continue; else { - // һ + // 隔一个子了 if (h - 1 >= 0) { for (int k = h - 1; k >= 0; k--) { if (subuc[jx][k] == 0) @@ -242,7 +242,7 @@ else if (subuc[k][jy] == 6) } } - // Ƿ + // 看是否被马将军 if (jx - 1 >= 0 && subuc[jx - 1][jy + 1] == 0) { if ((jx - 2 >= 0 && subuc[jx - 2][jy + 1] == 2) || subuc[jx - 1][jy + 2] == 2) { return true; @@ -267,7 +267,7 @@ else if (subuc[k][jy] == 6) } } - // Ƿ񱻶˧ + // 看是否被对面的帅将军 for (int h = jx + 1; h < 10; h++) { if (subuc[h][jy] == 0) continue; @@ -278,7 +278,7 @@ else if (subuc[h][jy] == 5) } } - // Ƿ񱻶ı + // 看是否被对面的兵将军 if (subuc[jx + 1][jy] == 7 || subuc[jx][jy - 1] == 7 || subuc[jx][jy + 1] == 7) return true; @@ -286,11 +286,11 @@ else if (subuc[h][jy] == 5) } /** - * ˧Ƿ񱻽 - * @param subucֱ + * 帅是否被将军 + * @param subuc是棋局备份数组 */ public static boolean isShuai(int[][] subuc) { - // Ƿ񱻳 + // 看是否被车将军 int jx = -1; int jy = -1; for (int i = 0; i < 10; i++) { @@ -303,8 +303,8 @@ public static boolean isShuai(int[][] subuc) { } if (jx == -1) return true; - // ҵλ - // ſǷжԷij + // 找到将军的位置 + // 横着看是否有对方的车 for (int h = jy - 1; h >= 0; h--) { if (subuc[jx][h] == 0) continue; @@ -323,7 +323,7 @@ else if (subuc[jx][h] == 8) break; } } - // ſ + // 竖着看 for (int h = jx - 1; h >= 0; h--) { if (subuc[h][jy] == 0) continue; @@ -346,12 +346,12 @@ else if (subuc[h][jy] == 8) } } - // Ƿڽ + // 看是否被炮将军 for (int h = jy - 1; h >= 0; h--) { if (subuc[jx][h] == 0) continue; else { - // һ + // 隔一个子了 if (h - 1 >= 0) { for (int k = h - 1; k >= 0; k--) { if (subuc[jx][k] == 0) @@ -425,7 +425,7 @@ else if (subuc[k][jy] == 13) } } - // Ƿ + // 看是否被马将军 if (subuc[jx - 1][jy + 1] == 0) { if (subuc[jx - 2][jy + 1] == 9 || subuc[jx - 1][jy + 2] == 9) { return true; @@ -448,9 +448,9 @@ else if (subuc[k][jy] == 13) if ((jx + 2 < 10 && subuc[jx + 2][jy - 1] == 9) || subuc[jx + 1][jy - 2] == 9) { return true; } - } // Էﷸ9д2 + } // 对方的马,这里犯错把9写成了2 - // Ƿ񱻶˧ + // 看是否被对面的帅将军 for (int h = jx - 1; h >= 0; h--) { if (subuc[h][jy] == 0) continue; @@ -461,7 +461,7 @@ else if (subuc[h][jy] == 12) } } - // Ƿ񱻶ı + // 看是否被对面的兵将军 if (subuc[jx - 1][jy] == 14 || subuc[jx][jy - 1] == 14 || subuc[jx][jy + 1] == 14) return true; @@ -469,21 +469,21 @@ else if (subuc[h][jy] == 12) } /** - * жϵǰǷͽִϲһźţtrue or false) - * @param datasubֱ,yourTurnǷԼĻغ + * 判断当前走是否在送将,并回执给上层一个信号(true or false) + * @param datasub是棋局备份数组,yourTurn是否到自己的回合 */ public static boolean jiangTip(int[][] datasub,boolean yourTurn) { if((yourTurn && isShuai(datasub)) || (!yourTurn && isJiang(datasub))){ - JOptionPane.showMessageDialog(null, ""); + JOptionPane.showMessageDialog(null, "将军"); return false; } return true; } /** - * ж˧ǷھŹ - * @param data + * 判断帅是否在九宫中 + * @param data是棋局数组 */ private boolean Shuai_exists(int[][] data) { for(int i=7;i<=9;i++) @@ -494,8 +494,8 @@ private boolean Shuai_exists(int[][] data) { } /** - * жϽǷھŹ - * @param data + * 判断将是否在九宫中 + * @param data是棋局数组 */ private boolean Jiang_exists(int[][] data) { for(int i=0;i<=2;i++) diff --git a/src/mychess/util/Internet.java b/src/mychess/util/Internet.java index 2238040..8ae9a0c 100644 --- a/src/mychess/util/Internet.java +++ b/src/mychess/util/Internet.java @@ -5,12 +5,10 @@ import java.io.ObjectOutputStream; import java.net.Socket; -import javax.swing.JOptionPane; - import mychess.entity.Message; /** - * ͨŵ + * 用于网络通信的类 */ public class Internet{ private Socket socket; @@ -18,7 +16,7 @@ public class Internet{ private ObjectOutputStream outputStreamToServer; /** - * ݷ + * 对数据服务器进行连接 * @param */ public Internet() { @@ -26,7 +24,7 @@ public Internet() { try { socket=new Socket(ReadProperties.IP, Integer.parseInt(ReadProperties.PORT)); outputStreamToServer=new ObjectOutputStream(socket.getOutputStream()); - inputStreamFromServer=new ObjectInputStream(socket.getInputStream());//ֱoutputstream + inputStreamFromServer=new ObjectInputStream(socket.getInputStream());//阻塞直到对面的outputstream开启 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -34,16 +32,16 @@ public Internet() { } /** - * ݷϢ - * @param messageҪ͸ݷıϢ + * 给数据服务器发送消息 + * @param message是要发送给数据服务器的本地消息 */ public void writeMessage(Message message) { try { outputStreamToServer.writeObject(message); - outputStreamToServer.flush();//ջ + outputStreamToServer.flush();//清空缓存区 } catch (IOException e) { // TODO Auto-generated catch block - //ﴦ쳣رյʱ򴥷쳣 + //这里处理异常,当服务器的流关闭的时候触发该异常 try { socket.close(); inputStreamFromServer.close(); @@ -57,7 +55,7 @@ public void writeMessage(Message message) { } /** - * ݷϢ + * 从数据服务器读消息 * @param */ public Message readMessage() { @@ -66,7 +64,7 @@ public Message readMessage() { message=(Message) inputStreamFromServer.readObject(); } catch (Exception e) { // TODO Auto-generated catch block - //ﴦ쳣رյʱ򴥷쳣 + //这里处理异常,当服务器的流关闭的时候触发该异常 try { socket.close(); inputStreamFromServer.close(); @@ -75,7 +73,7 @@ public Message readMessage() { // TODO Auto-generated catch block e1.printStackTrace(); } - System.exit(1);//쳣˳ + System.exit(1);//发生异常退出 } return message; } diff --git a/src/mychess/util/JudgeMove.java b/src/mychess/util/JudgeMove.java index aed8442..27b94ff 100644 --- a/src/mychess/util/JudgeMove.java +++ b/src/mychess/util/JudgeMove.java @@ -3,15 +3,15 @@ import mychess.client.ChessBoard; /** - * ƶ + * 移动规则类 */ public class JudgeMove { - ChessBoard cb;//̶ + ChessBoard cb;//依赖棋盘对象 private int[][] data=new int[10][9]; private boolean isRed; /** - * ʼ̶顢ִɫ + * 初始化棋盘对象、棋局数组、执棋角色 * @param */ public JudgeMove(ChessBoard cb,int[][] data,boolean isRed) { @@ -22,8 +22,8 @@ public JudgeMove(ChessBoard cb,int[][] data,boolean isRed) { } /** - * жϳƶǷ - * @param aixsƶ֮ǰ֮ + * 判断车的移动是否合理 + * @param aixs是移动之前和之后的两对坐标 */ public boolean move_che(int[] aixs) { if((isRed && data[aixs[2]-1][aixs[3]-1]<8 && data[aixs[2]-1][aixs[3]-1]>0) @@ -31,26 +31,26 @@ public boolean move_che(int[] aixs) { aixs[1]=aixs[3]; aixs[0]=aixs[2]; cb.setSelected(true); - return false;//ͬɫ + return false;//同颜色 } if(aixs[2]aixs[0] && aixs[3]==aixs[1]){ - // + //想往下走 for(int i=aixs[0]+1;iaixs[1] && aixs[2]==aixs[0]){ - // + //想往右走 for(int i=aixs[1]+1;i=8 && !isRed)){ - //ѡ + //换子选择 aixs[1]=aixs[3]; aixs[0]=aixs[2]; cb.setSelected(true); @@ -69,27 +69,27 @@ else if((data[aixs[2]-1][aixs[3]-1]<8 && isRed) || (data[aixs[2]-1][aixs[3]-1]>= } public boolean move_pao(int[] aixs) { - // + //是炮 if(data[aixs[2]-1][aixs[3]-1]==0){ - //ǿոһ + //如果是空格,像车一样 if(aixs[2]aixs[0] && aixs[3]==aixs[1]){ - // + //想往下走 for(int i=aixs[0]+1;iaixs[1] && aixs[2]==aixs[0]){ - // + //想往右走 for(int i=aixs[1]+1;iaixs[0] && aixs[3]==aixs[1]){ - // + //下 for(int i=aixs[0]+1;iaixs[1]) { - // + //右 for(int i=aixs[1]+1;i0) || (!isRed && data[aixs[2]-1][aixs[3]-1]>=8)){ aixs[1]=aixs[3]; @@ -143,27 +143,27 @@ public boolean move_ma(int[] aixs) { return false; } if(aixs[3]-aixs[1]==2 && aixs[0]-aixs[2]==1){ - //ն + //躺日东北方向 if(data[aixs[0]-1][aixs[1]]!=0) return false; }else if(aixs[3]-aixs[1]==2 && aixs[2]-aixs[0]==1){ - //նϷ + //躺日东南方向 if(data[aixs[0]-1][aixs[1]]!=0) return false; }else if(aixs[1]-aixs[3]==2 && aixs[2]-aixs[0]==1){ - //Ϸ + //躺日西南方向 if(data[aixs[0]-1][aixs[1]-2]!=0) return false; }else if(aixs[1]-aixs[3]==2 && aixs[0]-aixs[2]==1){ - // + //躺日西北方向 if(data[aixs[0]-1][aixs[1]-2]!=0) return false; }else if(aixs[3]-aixs[1]==1 && aixs[0]-aixs[2]==2){ - //ն + //跳日东北 if(data[aixs[0]-2][aixs[1]-1]!=0) return false; }else if(aixs[3]-aixs[1]==1 && aixs[2]-aixs[0]==2){ - //ն + //跳日东南 if(data[aixs[0]][aixs[1]-1]!=0) return false; }else if(aixs[1]-aixs[3]==1 && aixs[2]-aixs[0]==2){ @@ -186,7 +186,7 @@ else if((data[aixs[2]-1][aixs[3]-1]<8 && isRed) || (data[aixs[2]-1][aixs[3]-1]>= } public boolean move_xiang(int[] aixs) { - // + //是相 if((isRed && data[aixs[2]-1][aixs[3]-1]<8 && data[aixs[2]-1][aixs[3]-1]>0) || (!isRed && data[aixs[2]-1][aixs[3]-1]>=8)){ aixs[1]=aixs[3]; aixs[0]=aixs[2]; @@ -196,32 +196,32 @@ public boolean move_xiang(int[] aixs) { } if(isRed){ if(aixs[2]<=5) - return false;//ܳȥ + return false;//不能出去 }else{ if(aixs[2]>5) return false; } if(aixs[3]-aixs[1]==2 && aixs[0]-aixs[2]==2){ - // + //往东北方向 if(data[aixs[0]-2][aixs[1]]!=0) return false; }else if(aixs[3]-aixs[1]==2 && aixs[2]-aixs[0]==2){ - //Ϸ + //往东南方向 if(data[aixs[0]][aixs[1]]!=0) return false; }else if(aixs[1]-aixs[3]==2 && aixs[2]-aixs[0]==2){ - //Ϸ + //往西南方向 if(data[aixs[0]][aixs[1]-2]!=0) return false; }else if(aixs[1]-aixs[3]==2 && aixs[0]-aixs[2]==2){ - // + //往西北方向 if(data[aixs[0]-2][aixs[1]-2]!=0) return false; }else { if(data[aixs[2]-1][aixs[3]-1]==0) return false; else if((data[aixs[2]-1][aixs[3]-1]<8 && isRed) || (data[aixs[2]-1][aixs[3]-1]>=8 && !isRed)){ - //ѡ + //换子选择 aixs[1]=aixs[3]; aixs[0]=aixs[2]; cb.setSelected(true); @@ -232,7 +232,7 @@ else if((data[aixs[2]-1][aixs[3]-1]<8 && isRed) || (data[aixs[2]-1][aixs[3]-1]>= } public boolean move_shi(int[] aixs) { - //ʿ + //士 if((isRed && data[aixs[2]-1][aixs[3]-1]<8 && data[aixs[2]-1][aixs[3]-1]>0) || (!isRed && data[aixs[2]-1][aixs[3]-1]>=8)){ aixs[1]=aixs[3]; @@ -250,18 +250,18 @@ public boolean move_shi(int[] aixs) { return false; } if(aixs[3]-aixs[1]==1 && aixs[2]-aixs[0]==1){ - //Ϸ + //往东南方向 }else if(aixs[3]-aixs[1]==1 && aixs[0]-aixs[2]==1){ - // + //往东北方向 }else if(aixs[1]-aixs[3]==1 && aixs[2]-aixs[0]==1){ - //Ϸ + //往西南方向 }else if(aixs[1]-aixs[3]==1 && aixs[0]-aixs[2]==1){ - // + //往西北方向 }else { if(data[aixs[2]-1][aixs[3]-1]==0) return false; else if((data[aixs[2]-1][aixs[3]-1]<8 && isRed) || (data[aixs[2]-1][aixs[3]-1]>=8 && !isRed)){ - //ѡ + //换子选择 aixs[1]=aixs[3]; aixs[0]=aixs[2]; cb.setSelected(true); @@ -272,7 +272,7 @@ else if((data[aixs[2]-1][aixs[3]-1]<8 && isRed) || (data[aixs[2]-1][aixs[3]-1]>= } public boolean move_jiang(int[] aixs) { - //ǽ + //是将 if((isRed && data[aixs[2]-1][aixs[3]-1]<8 && data[aixs[2]-1][aixs[3]-1]>0) || (!isRed && data[aixs[2]-1][aixs[3]-1]>=8)){ aixs[1]=aixs[3]; @@ -290,20 +290,20 @@ public boolean move_jiang(int[] aixs) { return false; } if(aixs[3]-aixs[1]==1 && aixs[2]==aixs[0]){ - //ұ + //右边 } else if(aixs[3]==aixs[1] && aixs[0]-aixs[2]==1){ - // + //上 } else if(aixs[1]-aixs[3]==1 && aixs[0]==aixs[2]){ - // + //左 }else if(aixs[1]==aixs[3] && aixs[2]-aixs[0]==1){ - // + //下 }else { if(data[aixs[2]-1][aixs[3]-1]==0) return false; else if((data[aixs[2]-1][aixs[3]-1]<8 && isRed) || (data[aixs[2]-1][aixs[3]-1]>=8 && !isRed)){ - //ѡ + //换子选择 aixs[1]=aixs[3]; aixs[0]=aixs[2]; cb.setSelected(true); @@ -314,7 +314,7 @@ else if((data[aixs[2]-1][aixs[3]-1]<8 && isRed) || (data[aixs[2]-1][aixs[3]-1]>= } public boolean move_bing(int[] aixs) { - //DZ + //是兵 if((isRed && data[aixs[2]-1][aixs[3]-1]<8 && data[aixs[2]-1][aixs[3]-1]>0) || (!isRed && data[aixs[2]-1][aixs[3]-1]>=8)){ aixs[1]=aixs[3]; @@ -325,9 +325,9 @@ public boolean move_bing(int[] aixs) { } if(isRed){ if(aixs[0]>5){ - //Լ ƶ + //在自己的阵地上 不能左右移动 if(aixs[0]-aixs[2]==1 && aixs[1]==aixs[3]){ - //ǰ + //前进 }else if(data[aixs[2]-1][aixs[3]-1]<8){ aixs[1]=aixs[3]; aixs[0]=aixs[2]; @@ -337,13 +337,13 @@ public boolean move_bing(int[] aixs) { return false; } }else{ - //ڶԷ + //在对方阵地上 if(aixs[0]-aixs[2]==1 && aixs[1]==aixs[3]){ - // + //进 }else if(aixs[0]==aixs[2] && aixs[1]-aixs[3]==1){ - // + //左 }else if(aixs[0]==aixs[2] && aixs[3]-aixs[1]==1){ - // + //右 }else { aixs[1]=aixs[3]; aixs[0]=aixs[2]; @@ -353,9 +353,9 @@ public boolean move_bing(int[] aixs) { } }else{ if(aixs[0]<=5){ - //Լ ƶ + //在自己的阵地上 不能左右移动 if(aixs[0]-aixs[2]==-1 && aixs[1]==aixs[3]){ - //ǰ + //前进 }else if(data[aixs[2]-1][aixs[3]-1]>=8){ aixs[1]=aixs[3]; aixs[0]=aixs[2]; @@ -365,13 +365,13 @@ public boolean move_bing(int[] aixs) { return false; } }else{ - //ڶԷ + //在对方阵地上 if(aixs[0]-aixs[2]==-1 && aixs[1]==aixs[3]){ - // + //进 }else if(aixs[0]==aixs[2] && aixs[1]-aixs[3]==1){ - // + //左 }else if(aixs[0]==aixs[2] && aixs[3]-aixs[1]==1){ - // + //右 }else { aixs[1]=aixs[3]; aixs[0]=aixs[2]; @@ -382,4 +382,4 @@ public boolean move_bing(int[] aixs) { } return true; } -} +} \ No newline at end of file diff --git a/src/mychess/util/Withdraw.java b/src/mychess/util/Withdraw.java index 81dbbf2..f6698ab 100644 --- a/src/mychess/util/Withdraw.java +++ b/src/mychess/util/Withdraw.java @@ -6,11 +6,11 @@ import mychess.entity.Message; /** - * 幦 - * 201916 7:52:08 + * 悔棋功能 + * 2019年1月6日 下午7:52:08 */ public class Withdraw { - //ռݱ + //收集数据报 private List messages; public Withdraw() { @@ -33,4 +33,4 @@ public Message getLast() { public int allSteps() { return messages.size(); } -} +} \ No newline at end of file