public void init() throws Exception {
   table = ImageIO.read(new File("image/board.jpg"));
   black = ImageIO.read(new File("image/black.gif"));
   white = ImageIO.read(new File("image/white.gif"));
   selected = ImageIO.read(new File("image/selected.gif"));
   // 把每个元素赋为"╋",用于在控制台画出棋盘
   for (int i = 0; i < BOARD_SIZE; i++) {
     for (int j = 0; j < BOARD_SIZE; j++) {
       board[i][j] = "╋";
     }
   }
   chessBoard.setPreferredSize(new Dimension(TABLE_WIDTH, TABLE_HETGHT));
   chessBoard.addMouseListener(
       new MouseAdapter() {
         public void mouseClicked(MouseEvent e) {
           // 将用户鼠标事件的座标转换成棋子数组的座标。
           int xPos = (int) ((e.getX() - X_OFFSET) / RATE);
           int yPos = (int) ((e.getY() - Y_OFFSET) / RATE);
           board[xPos][yPos] = "●";
           /*
           电脑随机生成2个整数,作为电脑下棋的座标,赋给board数组。
           还涉及:
           1.如果下棋的点已经棋子,不能重复下棋。
           2.每次下棋后,需要扫描谁赢了
           */
           chessBoard.repaint();
         }
         // 当鼠标退出棋盘区后,复位选中点座标
         public void mouseExited(MouseEvent e) {
           selectedX = -1;
           selectedY = -1;
           chessBoard.repaint();
         }
       });
   chessBoard.addMouseMotionListener(
       new MouseMotionAdapter() {
         // 当鼠标移动时,改变选中点的座标
         public void mouseMoved(MouseEvent e) {
           selectedX = (e.getX() - X_OFFSET) / RATE;
           selectedY = (e.getY() - Y_OFFSET) / RATE;
           chessBoard.repaint();
         }
       });
   f.add(chessBoard);
   f.pack();
   f.setVisible(true);
 }
Beispiel #2
0
 public void CheckRight(ChessBoard cel) {
   int r = cel.getRow();
   int c = cel.getColumn();
   if (c + 1 < 8 && cell[r][c + 1].state != ' ' && cell[r][c + 1].state != turn) {
     for (int i = c + 2; i < 8; i++) {
       if (cell[r][i].state == turn) {
         can = true;
         for (int j = c; j < i; j++) {
           cell[r][j].state = cel.state = turn;
           cell[r][j].repaint();
         }
         return;
       } else {
         if (cell[r][i].state == ' ') break;
         else continue;
       }
     }
   }
 }
Beispiel #3
0
 public void CheckLeft(ChessBoard cel) {
   int r = cel.getRow();
   int c = cel.getColumn();
   if (c - 1 >= 0 && cell[r][c - 1].state != ' ' && cell[r][c - 1].state != turn) {
     for (int i = c - 2; i >= 0; i--) {
       if (cell[r][i].state == turn) {
         can = true;
         for (int j = i + 1; j <= c; j++) {
           cell[r][j].state = cel.state = turn;
           cell[r][j].repaint();
         }
         return;
       } else {
         if (cell[r][i].state == ' ') break;
         else continue;
       }
     }
   }
 }
Beispiel #4
0
 public void CheckDown(ChessBoard cel) {
   int r = cel.getRow();
   int c = cel.getColumn();
   if (r + 1 < 8 && cell[r + 1][c].state != ' ' && cell[r + 1][c].state != turn) {
     for (int i = r + 2; i < 8; i++) {
       if (cell[i][c].state == turn) {
         can = true;
         for (int j = r; j < i; j++) {
           cell[j][c].state = cel.state = turn;
           cell[j][c].repaint();
         }
         return;
       } else {
         if (cell[i][c].state == ' ') break;
         else continue;
       }
     }
   }
 }
Beispiel #5
0
 public void CheckUp(ChessBoard cel) {
   int r = cel.getRow();
   int c = cel.getColumn();
   if (r - 1 >= 0 && cell[r - 1][c].state != ' ' && cell[r - 1][c].state != turn) {
     for (int i = r - 2; i >= 0; i--) {
       if (cell[i][c].state == turn) {
         can = true;
         for (int j = r; j > i; j--) {
           cell[j][c].state = cel.state = turn;
           cell[j][c].repaint();
         }
         return;
       } else {
         if (cell[i][c].state == ' ') break;
         else continue;
       }
     }
   }
 }
Beispiel #6
0
 public void CheckRightDown(ChessBoard cel) {
   int r = cel.getRow();
   int c = cel.getColumn();
   if (r + 1 < 8
       && c + 1 < 8
       && cell[r + 1][c + 1].state != ' '
       && cell[r + 1][c + 1].state != turn) {
     for (int i = 2; (i + r) < 8 && (i + c) < 8; i++) {
       if (r + i < 8 && c + i < 8 && cell[i + r][c + i].state == turn) {
         can = true;
         for (int j = 0; j < i; j++) {
           cell[r + j][c + j].state = cel.state = turn;
           cell[r + j][c + j].repaint();
         }
         return;
       } else {
         if (cell[r + i][c + i].state == ' ') break;
         else continue;
       }
     }
   }
 }
Beispiel #7
0
 public void CheckRightUp(ChessBoard cel) {
   int r = cel.getRow();
   int c = cel.getColumn();
   if (r - 1 >= 0
       && c + 1 < 8
       && cell[r - 1][c + 1].state != ' '
       && cell[r - 1][c + 1].state != turn) {
     for (int i = 2; (r - 2) >= 0 && (c + i) < 8; i++) {
       if (r - i >= 0 && c + i < 8 && cell[r - i][c + i].state == turn) {
         can = true;
         for (int j = 0; j < i; j++) {
           cell[r - j][c + j].state = cel.state = turn;
           cell[r - j][c + j].repaint();
         }
         return;
       } else {
         if (r - i >= 0 && c + i < 8 && cell[r - i][c + i].state == ' ') break;
         else continue;
       }
     }
   }
 }
Beispiel #8
0
 public void CheckLeftDown(ChessBoard cel) {
   int r = cel.getRow();
   int c = cel.getColumn();
   if (r + 1 < 8
       && c - 1 >= 0
       && cell[r + 1][c - 1].state != ' '
       && cell[r + 1][c - 1].state != turn) {
     for (int i = 2; (r + i) < 8 && (c - i) >= 0; i++) {
       if (r + i < 8 && c - i >= 0 && cell[r + i][c - i].state == turn) {
         can = true;
         for (int j = 0; j < i; j++) {
           cell[r + j][c - j].state = cel.state = turn;
           cell[r + j][c - j].repaint();
         }
         return;
       } else {
         if (cell[r + i][c - i].state == ' ') break;
         else continue;
       }
     }
   }
 }
Beispiel #9
0
  public int Clicked(ChessBoard cel) {
    int judge = 0;
    int r = cel.getRow();
    int c = cel.getColumn();
    System.out.println("落子前:" + cell[3][5].taken);

    if (gameStart) {
      if (cel.taken == false) {
        Check(cel);
      }
      System.out.println(cell[3][5].taken);
      System.out.println(can);
      if (can && cel.taken == false) {
        /*for (int i = 0; i < 8; i++)
        for (int j = 0; j < 8; j++)
        if (cell[i][j].changed == true)
        {
        	cell[i][j].ChangeBack();
        }*/
        RememberState();
        ShowChessNumber();
        list.add(cel);
        JudgeWhoIsWinner();
        turn = TakeTurn();
        cel.taken = true;
        System.out.println("落子后:" + cell[3][5].taken);
        can = false;
        judge = 1;
        for (int i = 0; i < 8; i++)
          for (int j = 0; j < 8; j++)
            if (cell[i][j].changed) {
              cell[i][j].ChangeBack();
            }

        boolean flag = CheckAll();
        if (!flag && white + black < 64) CheckAtTheEnd();
        /*else
        {
        	for (int i = 0; i < 8; i++)
        	for (int j = 0; j < 8; j++)
        	if (cell[i][j].taken == false)
        	{
        		CheckPlace(cell[i][j]);
        		if (canPut)
        		{
        			cell[i][j].ChangeBackground();
        			canPut = false;
        		}
        	}
        }*/
      } else {
        JOptionPane.showMessageDialog(null, "无法在该位置落子");
        judge = 0;
        System.out.println(cell[3][5].taken);
      }
      return judge;
    } else {
      JOptionPane.showMessageDialog(null, "游戏还未开始或已结束");
      return 0;
    }
  }
Beispiel #10
0
  public void CheckPlace(ChessBoard cel) {
    int r = cel.getRow();
    int c = cel.getColumn();
    if (r - 1 >= 0 && cell[r - 1][c].state != ' ' && cell[r - 1][c].state != turn) {
      for (int i = r - 2; i >= 0; i--) {
        if (cell[i][c].state == turn) {
          canPut = true;
          return;
        } else {
          if (cell[i][c].state == ' ') break;
          else continue;
        }
      }
    }

    if (r + 1 < 8 && cell[r + 1][c].state != ' ' && cell[r + 1][c].state != turn) {
      for (int i = r + 2; i < 8; i++) {
        if (cell[i][c].state == turn) {
          canPut = true;
          return;
        } else {
          if (cell[i][c].state == ' ') break;
          else continue;
        }
      }
    }

    if (c - 1 >= 0 && cell[r][c - 1].state != ' ' && cell[r][c - 1].state != turn) {
      for (int i = c - 2; i >= 0; i--) {
        if (cell[r][i].state == turn) {
          canPut = true;
          return;
        } else {
          if (cell[r][i].state == ' ') break;
          else continue;
        }
      }
    }

    if (c + 1 < 8 && cell[r][c + 1].state != ' ' && cell[r][c + 1].state != turn) {
      for (int i = c + 2; i < 8; i++) {
        if (cell[r][i].state == turn) {
          canPut = true;
          return;
        } else {
          if (cell[r][i].state == ' ') break;
          else continue;
        }
      }
    }

    if (c - 1 >= 0
        && r - 1 >= 0
        && cell[r - 1][c - 1].state != ' '
        && cell[r - 1][c - 1].state != turn) {
      for (int i = 2; (r - i) >= 0 && (c - i) >= 0; i++) {
        if (cell[r - i][c - i].state == turn) {
          canPut = true;
          return;
        } else {
          if (cell[r - i][c - i].state == ' ') break;
          else continue;
        }
      }
    }

    if (r + 1 < 8
        && c - 1 >= 0
        && cell[r + 1][c - 1].state != ' '
        && cell[r + 1][c - 1].state != turn) {
      for (int i = 2; (r + i) < 8 && (c - i) >= 0; i++) {
        if (r + i < 8 && c - i >= 0 && cell[r + i][c - i].state == turn) {
          canPut = true;
          return;
        } else {
          if (cell[r + i][c - i].state == ' ') break;
          else continue;
        }
      }
    }

    if (r - 1 >= 0
        && c + 1 < 8
        && cell[r - 1][c + 1].state != ' '
        && cell[r - 1][c + 1].state != turn) {
      for (int i = 2; (r - 2) >= 0 && (c + i) < 8; i++) {
        if (r - i >= 0 && c + i < 8 && cell[r - i][c + i].state == turn) {
          canPut = true;
          return;
        } else {
          if (r - i >= 0 && c + i < 8 && cell[r - i][c + i].state == ' ') break;
          else continue;
        }
      }
    }

    if (r + 1 < 8
        && c + 1 < 8
        && cell[r + 1][c + 1].state != ' '
        && cell[r + 1][c + 1].state != turn) {
      for (int i = 2; (i + r) < 8 && (i + c) < 8; i++) {
        if (r + i < 8 && c + i < 8 && cell[i + r][c + i].state == turn) {
          canPut = true;
          return;
        } else {
          if (cell[r + i][c + i].state == ' ') break;
          else continue;
        }
      }
    }
  }