⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chesspad.java

📁 局域网内实现的五子棋和简单的聊天工具。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
          if ( ( (a + step) * 20 == chessWhite_x[chessCompare]) &&
              (b * 20 == chessWhite_y[chessCompare])) {
            chessLink++;
            if (chessLink == 5) {
              return (true);
            }
          }
        }
        if (chessLink == (chessLinkTest + 1)) {
          chessLinkTest++;
        }
        else {
          break;
        }
      }
      for (step = 1; step <= 4; step++) {
        for (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare++) {
          if ( ( (a - step) * 20 == chessWhite_x[chessCompare]) &&
              (b * 20 == chessWhite_y[chessCompare])) {
            chessLink++;
            if (chessLink == 5) {
              return (true);
            }
          }
        }
        if (chessLink == (chessLinkTest + 1)) {
          chessLinkTest++;
        }
        else {
          break;
        }
      }
      chessLink = 1;
      chessLinkTest = 1;
      for (step = 1; step <= 4; step++){
        for (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare++) {
          if ( (a * 20 == chessWhite_x[chessCompare]) &&
              ( (b + step) * 20 == chessWhite_y[chessCompare])) {
            chessLink++;
            if (chessLink == 5) {
              return (true);
            }
          }
        }
        if (chessLink == (chessLinkTest + 1)) {
          chessLinkTest++;
        }
        else {
          break;
        }
      }
      for (step = 1; step <= 4; step++) {
        for (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare++) {
          if ( (a * 20 == chessWhite_x[chessCompare]) &&
              ( (b - step) * 20 == chessWhite_y[chessCompare])) {
            chessLink++;
            if (chessLink == 5) {
              return (true);
            }
          }
        }
        if (chessLink == (chessLinkTest + 1)) {
          chessLinkTest++;
        }
        else {
          break;
        }
      }
      chessLink = 1;
      chessLinkTest = 1;
      for (step = 1; step <= 4; step++) {
        for (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare++) {
          if ( ( (a - step) * 20 == chessWhite_x[chessCompare]) &&
              ( (b + step) * 20 == chessWhite_y[chessCompare])) {
            chessLink++;
            if (chessLink == 5) {
              return (true);
            }
          }
        }
        if (chessLink == (chessLinkTest + 1)) {
          chessLinkTest++;
        }
        else {
          break;
        }
      }
      for (step = 1; step <= 4; step++) {
        for (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare++) {
          if ( ( (a + step) * 20 == chessWhite_x[chessCompare]) &&
              ( (b - step) * 20 == chessWhite_y[chessCompare])) {
            chessLink++;
            if (chessLink == 5) {
              return (true);
            }
          }
        }
        if (chessLink == (chessLinkTest + 1)) {
          chessLinkTest++;
        }
        else {
          break;
        }
      }
      chessLink = 1;
      chessLinkTest = 1;
      for (step = 1; step <= 4; step++) {
        for (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare++) {
          if ( ( (a + step) * 20 == chessWhite_x[chessCompare]) &&
              ( (b + step) * 20 == chessWhite_y[chessCompare])) {
            chessLink++;
            if (chessLink == 5) {
              return (true);
            }
          }
        }
        if (chessLink == (chessLinkTest + 1)) {
          chessLinkTest++;
        }
        else {
          break;
        }
      }
      for (step = 1; step <= 4; step++) {
        for (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare++) {
          if ( ( (a - step) * 20 == chessWhite_x[chessCompare]) &&
              ( (b - step) * 20 == chessWhite_y[chessCompare])) {
            chessLink++;
            if (chessLink == 5) {
              return (true);
            }
          }
        }
        if (chessLink == (chessLinkTest + 1)) {
          chessLinkTest++;
        }
        else {
          break;
        }
      }
    }
    return (false);
  }

  public void paint(Graphics g){
  //棋盘格局为19*19
  //画棋盘的Y的平行线
    for(int i = 40; i <= 380; i = i + 20){
      g.drawLine(40, i, 400, i);
    }
    //画最后一条竖线
    g.drawLine(40, 400, 400, 400);
    //画棋盘的X的平行线
    for(int j = 40; j <= 380; j = j + 20){
      g.drawLine(j, 40, j, 400);
    }
    //画最后一条X的平行线
    g.drawLine(400, 40, 400, 400);
    //画棋盘中的圆点,半径为6;
    g.fillOval(97, 97, 6, 6);
    g.fillOval(337, 97, 6, 6);
    g.fillOval(97, 337, 6, 6);
    g.fillOval(337, 337, 6, 6);
    g.fillOval(217, 217, 6, 6);
  }
  //在当前Panel上画出当前下的棋子,用Canvas实现,根据chessPoint_a,chessPoint_b功能和颜色;
  //并且把该信息发送给对手,格式为:/chesspeerName /chess chessPoint_a chessPoint_b color;
  public void chessPaint(int chessPoint_a, int chessPoint_b, int color){
    chessPoint_black chesspoint_black = new chessPoint_black();
    chessPoint_white chesspoint_white = new chessPoint_white();
    if(color == 1 && isMouseEnabled){
      //获得位置;用处为了checkwin使用;
      getLocation(chessPoint_a, chessPoint_b, color);
      //判断是否胜利;
      isWin = checkWin(chessPoint_a, chessPoint_b, color);
      if (isWin == false){
        //发送当前坐标信息给对手;
        chessthread.sendMessage("/" + chessPeerName + " /chess " + chessPoint_a +
                                " " + chessPoint_b + " " + color);
        this.add(chesspoint_black);
        chesspoint_black.setBounds(chessPoint_a * 20 - 7, chessPoint_b * 20 - 7,
                                   16, 16);
        statusText.setText("黑(第" + chessBlackCount + "步)" + chessPoint_a + " " +
                           chessPoint_b + ",请白棋下子");
        isMouseEnabled = false;
      }
      else{
       //发送当前坐标信息给对手;
       //格式为:/chessPeerName /chess chessPoint_a chessPoint_b color;
        chessthread.sendMessage("/" + chessPeerName + " /chess " + chessPoint_a +
                                " " + chessPoint_b + " " + color);
        this.add(chesspoint_black);
        chesspoint_black.setBounds(chessPoint_a * 20 - 7, chessPoint_b * 20 - 7,
                                   16, 16);
        chessVictory(1);
        isMouseEnabled = false;
      }
    }
    else if (color == -1 && isMouseEnabled){
      getLocation(chessPoint_a, chessPoint_b, color);
      isWin = checkWin(chessPoint_a, chessPoint_b, color);
      if (isWin == false) {
        chessthread.sendMessage("/" + chessPeerName + " /chess " + chessPoint_a +
                                " " + chessPoint_b + " " + color);
        this.add(chesspoint_white);
        chesspoint_white.setBounds(chessPoint_a * 20 - 7, chessPoint_b * 20 - 7,
                                   16, 16);
        statusText.setText("白(第" + chessWhiteCount + "步)" + chessPoint_a + " " +
                           chessPoint_b + ",请黑棋下子");
        isMouseEnabled = false;
      }
      else{
        chessthread.sendMessage("/" + chessPeerName + " /chess " + chessPoint_a +
                                " " + chessPoint_b + " " + color);
        this.add(chesspoint_white);
        chesspoint_white.setBounds(chessPoint_a * 20 - 7, chessPoint_b * 20 - 7,
                                   16, 16);
        chessVictory( -1);
        isMouseEnabled = false;
      }
    }
  }
  //对方的下棋步骤,根据颜色画出对方的下棋步骤;
  public void netChessPaint(int chessPoint_a, int chessPoint_b, int color){
    chessPoint_black chesspoint_black = new chessPoint_black();
    chessPoint_white chesspoint_white = new chessPoint_white();
    //获得棋盘该点的坐标,为了checkWin使用
    getLocation(chessPoint_a, chessPoint_b, color);
    if (color==1){
      isWin = checkWin(chessPoint_a, chessPoint_b, color);
      if (isWin == false){
        this.add(chesspoint_black);
        chesspoint_black.setBounds(chessPoint_a * 20 - 7, chessPoint_b * 20 - 7,
                                   16, 16);
        statusText.setText("黑(第" + chessBlackCount + "步)" + chessPoint_a + " " +
                           chessPoint_b + ",请白棋下子");
        isMouseEnabled = true;
      }
      else{
        this.add(chesspoint_black);
        chesspoint_black.setBounds(chessPoint_a * 20 - 7, chessPoint_b * 20 - 7,
                                   16, 16);
        chessVictory(1);
        isMouseEnabled = true;
      }
    }
    else if(color == -1){
      isWin = checkWin(chessPoint_a, chessPoint_b, color);
      if (isWin == false){
        this.add(chesspoint_white);
        chesspoint_white.setBounds(chessPoint_a * 20 - 7, chessPoint_b * 20 - 7,
                                   16, 16);
        statusText.setText("白(第" + chessWhiteCount + "步)" + chessPoint_a + " " +
                           chessPoint_b + ",请黑棋下子");
        isMouseEnabled = true;
      }
      else{
        chessthread.sendMessage("/" + chessPeerName + " /victory " + color);
        this.add(chesspoint_white);
        chesspoint_white.setBounds(chessPoint_a * 20 - 7, chessPoint_b * 20 - 7,
                                   16, 16);
        chessVictory( -1);
        isMouseEnabled = true;
      }
    }
  }
  public void mousePressed(MouseEvent e){
    if (e.getModifiers() == InputEvent.BUTTON1_MASK) {
      chessPoint_x = (int) e.getX();
      chessPoint_y = (int) e.getY();
      int a = (chessPoint_x + 10) / 20, b = (chessPoint_y + 10) / 20;
      if (chessPoint_x / 20 < 2 || chessPoint_y / 20 < 2 ||
          chessPoint_x / 20 > 19 || chessPoint_y / 20 > 19) {}
      else {
        chessPaint(a, b, chessColor);
      }
    }
  }

  public void mouseReleased(MouseEvent e) {}

  public void mouseEntered(MouseEvent e) {}

  public void mouseExited(MouseEvent e) {}

  public void mouseClicked(MouseEvent e) {}

  public void actionPerformed(ActionEvent e) {

  }
}
//一个黑子的画板,大小为(20,20);
class chessPoint_black  extends Canvas {
  chessPoint_black(){
    setSize(20, 20);
  }
  public void paint(Graphics g){
    g.setColor(Color.black);
    g.fillOval(0, 0, 14, 14);
  }
}
//一个白子的画板,大小为(20,20);
class chessPoint_white extends Canvas {
  chessPoint_white(){
    setSize(20, 20);
  }
  public void paint(Graphics g){
    g.setColor(Color.white);
    g.fillOval(0, 0, 14, 14);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -