/** 实现对悔棋的处理 */ 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 }
/** 实现对打开文件的处理 */ 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; }