예제 #1
1
  /** 实现计时功能 */
  @SuppressWarnings("deprecation")
  public void run() {
    try {
      Thread.sleep(1000); // 延迟1s
    } catch (InterruptedException e1) {
      e1.printStackTrace();
    }
    while (true) {
      if (blacktime > 0 && whitetime > 0 && blackOneTime > 0 && whiteOneTime > 0) {
        if (cb.isBlack) {
          whiteOneTime = maxOneStepTime;
          blacktime--;
          blackOneTime--;
        } else {
          blackOneTime = maxOneStepTime;
          whitetime--;
          whiteOneTime--;
        }
        blackMessage = this.initTime(blacktime);
        whiteMessage = this.initTime(whitetime);
        if (blackOneTime < 5 || whiteOneTime < 5) {
          v.play("clock");
        }
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      if (blackOneTime == 0 || blacktime == 0) {
        JOptionPane.showMessageDialog(cb, "黑方时间到,白方胜");
        cb.message = "黑方时间到,白方胜";
        v.play("end");
        cb.changeColor = true;
        cb.canPlay = false;
        cb.whiteWin++;
        rp.ready.setText("准备");
        rp.isReady = false;
        t.suspend();
      }
      if (whiteOneTime == 0 || whitetime == 0) {
        JOptionPane.showMessageDialog(cb, "白方时间到,黑方胜");
        cb.message = "白方时间到,黑方胜";
        v.play("end");
        cb.changeColor = true;
        cb.canPlay = false;
        cb.blackWin++;
        rp.ready.setText("准备");
        rp.isReady = false;
        t.suspend();
      }

      cb.repaint();
    }
  }
예제 #2
0
 /** 实现对悔棋的处理 */
 public void regretAnswer() {
   if (answer == 0) {
     for (int i = 0; i < 8; i++) {
       for (int j = 0; j < 8; j++) {
         if (cb.regChess[i][j] == 2) {
           cb.allChess[i][j] = 0;
         }
         if (cb.regChess[i][j] == 1) {
           if (cb.allChess[i][j] == BLACK) {
             cb.allChess[i][j] = WHITE;
           } else {
             cb.allChess[i][j] = BLACK;
           }
         }
       }
     }
     if (cb.isBlack == true) {
       cb.isBlack = false;
       cb.message = "轮到白方";
     } else {
       cb.isBlack = true;
       cb.message = "轮到黑方";
     }
     cb.isOpposite = false;
     cb.ch.countBlack = cb.ch.lastCountBlack;
     cb.ch.countWhite = cb.ch.lastCountWhite;
     cb.repaint();
     JOptionPane.showMessageDialog(cb, "对方同意了你的悔棋!");
   }
   if (answer != 0) {
     JOptionPane.showMessageDialog(cb, "对方不同意悔棋!");
   }
   answer = 3; // 初始化answer
 }
예제 #3
0
 /** 实现对打开文件的处理 */
 public void openAnswer() {
   if (answer == 0) {
     cb.canPlay = true;
     File file = null;
     JFileChooser fileChooser = new JFileChooser();
     fileChooser.setApproveButtonText("确定");
     fileChooser.setDialogTitle("打开文件");
     fileChooser.setCurrentDirectory(new File("SavaAndOpen"));
     int result = fileChooser.showOpenDialog(gc); // 显示选择框
     if (result == JFileChooser.APPROVE_OPTION) { // 选择的是确定按钮
       file = fileChooser.getSelectedFile(); // 得到选择的文件
       cb.repaint();
     }
     if (file != null) {
       try {
         DataInputStream dis = new DataInputStream(new FileInputStream(file));
         for (int i = 0; i < 8; i++) {
           for (int j = 0; j < 8; j++) {
             allChess[i][j] = dis.readInt();
           }
         }
         cb.message = dis.readUTF();
         cb.isBlack = dis.readBoolean();
         if (cb.isBlack == false) { // 如果是和电脑下且轮到白子下
           cb.ch.playWithCom();
         }
         cb.changeColor = dis.readBoolean();
         cb.isStop = dis.readBoolean();
         if (cb.computerPlay == false) { // 如果不和电脑下,发送信息
           cb.send(cc.dos);
         }
         dis.close();
         cb.repaint();
       } catch (Exception e1) {
       }
     }
   }
   if (answer != 0) {
     JOptionPane.showMessageDialog(cb, "对方拒绝了你的请求!");
   }
   answer = 3;
 }
예제 #4
0
 public void oneTimeAnswer() {
   if (answer == 0) {
     maxOneStepTime = Integer.parseInt(oneTime);
     blackOneTime = maxOneStepTime;
     whiteOneTime = maxOneStepTime;
     JOptionPane.showMessageDialog(cb, "对方同意了你的请求!");
     cb.repaint();
   }
   if (answer != 0) {
     JOptionPane.showMessageDialog(cb, "对方拒绝了你的请求!");
   }
   answer = 3;
 }
예제 #5
0
 boolean setChess(int x, int y, int flag) {
   if (myChessPoint[x][y].state == 0) {
     myChessPoint[x][y].state = flag;
     moveFlag = !moveFlag;
     chessBoard.repaint();
     if (flag == 1) {
       judge();
       sendData(x + "," + y);
     }
     return true;
   }
   return false;
 }
예제 #6
0
 public void timeAnswer() {
   if (answer == 0) {
     try {
       maxtime = Integer.parseInt(time) * 60;
       blacktime = maxtime;
       whitetime = maxtime;
       this.blackMessage = this.initTime(blacktime);
       this.whiteMessage = this.initTime(whitetime);
       JOptionPane.showMessageDialog(cb, "对方同意了你的请求!");
       cb.repaint();
     } catch (Exception e1) {
       e1.printStackTrace();
     }
   }
   if (answer != 0) {
     JOptionPane.showMessageDialog(cb, "对方拒绝了你的请求!");
   }
   answer = 3;
 }