Java实现五子棋游戏的步骤与要点
项目概述与需求分析
五子棋是一种两人对弈的棋类游戏,目标是在15×15的棋盘上率先形成横、竖、斜连续五子的一方获胜,使用Java开发五子棋游戏,需实现以下核心功能:

- 棋盘绘制:通过图形界面展示15×15的网格棋盘。
- 落子逻辑:玩家轮流点击棋盘交叉点落子,黑子先行。
- 胜负判定:实时检测是否有五子连珠。
- 游戏控制:支持重新开始、悔棋等功能。
开发工具可选择JDK + Swing(Java GUI库)或JavaFX,本文以Swing为例,因其轻量且适合简单游戏开发。
项目结构设计
为提升代码可读性和可维护性,建议将功能拆分为以下类:
- ChessBoard:棋盘类,负责绘制棋盘、管理棋子状态。
- ChessPiece:棋子类,记录棋子的位置(坐标)和颜色(黑/白)。
- GameController:游戏控制器,处理落子逻辑、胜负判定和用户交互。
- MainFrame:主窗口类,整合棋盘和游戏控制功能。
核心功能实现
棋盘绘制与初始化
使用Swing的JPanel作为棋盘容器,通过重写paintComponent方法绘制网格线和棋子。

public class ChessBoard extends JPanel {
private static final int GRID_SIZE = 40; // 网格大小
private static final int BOARD_SIZE = 15; // 棋盘尺寸
private ChessPiece[][] pieces = new ChessPiece[BOARD_SIZE][BOARD_SIZE]; // 棋子数组
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawGrid(g); // 绘制网格
drawPieces(g); // 绘制棋子
}
private void drawGrid(Graphics g) {
g.setColor(Color.BLACK);
for (int i = 0; i < BOARD_SIZE; i++) {
// 横线
g.drawLine(GRID_SIZE, (i + 1) * GRID_SIZE,
BOARD_SIZE * GRID_SIZE, (i + 1) * GRID_SIZE);
// 竖线
g.drawLine((i + 1) * GRID_SIZE, GRID_SIZE,
(i + 1) * GRID_SIZE, BOARD_SIZE * GRID_SIZE);
}
}
private void drawPieces(Graphics g) {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (pieces[i][j] != null) {
ChessPiece piece = pieces[i][j];
g.setColor(piece.getColor());
g.fillOval(j * GRID_SIZE + 5, i * GRID_SIZE + 5,
GRID_SIZE - 10, GRID_SIZE - 10);
}
}
}
}
}
落子逻辑与事件监听
在ChessBoard中添加鼠标点击事件,计算点击位置对应的网格坐标,并更新棋子数组。
public ChessBoard() {
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int x = (e.getX() - GRID_SIZE / 2) / GRID_SIZE;
int y = (e.getY() - GRID_SIZE / 2) / GRID_SIZE;
if (x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE) {
placePiece(y, x); // 注意坐标转换
}
}
});
}
public void placePiece(int row, int col) {
if (pieces[row][col] == null) {
pieces[row][col] = new ChessPiece(row, col, currentPlayer);
repaint(); // 重绘棋盘
if (checkWin(row, col)) {
JOptionPane.showMessageDialog(this, currentPlayer + " 获胜!");
}
currentPlayer = currentPlayer == Color.BLACK ? Color.WHITE : Color.BLACK;
}
}
胜负判定算法
从当前落子点向8个方向检测连续同色棋子数量,若达到5个则判定胜利。
private boolean checkWin(int row, int col) {
int[][] directions = {{0, 1}, {1, 0}, {1, 1}, {1, -1}}; // 横、竖、斜、反斜
Color color = pieces[row][col].getColor();
for (int[] dir : directions) {
int count = 1; // 当前落子点已有一个棋子
// 正向检测
for (int i = 1; i < 5; i++) {
int newRow = row + dir[0] * i;
int newCol = col + dir[1] * i;
if (newRow >= 0 && newRow < BOARD_SIZE && newCol >= 0 && newCol < BOARD_SIZE
&& pieces[newRow][newCol] != null && pieces[newRow][newCol].getColor() == color) {
count++;
} else {
break;
}
}
// 反向检测
for (int i = 1; i < 5; i++) {
int newRow = row - dir[0] * i;
int newCol = col - dir[1] * i;
if (newRow >= 0 && newRow < BOARD_SIZE && newCol >= 0 && newCol < BOARD_SIZE
&& pieces[newRow][newCol] != null && pieces[newRow][newCol].getColor() == color) {
count++;
} else {
break;
}
}
if (count >= 5) return true;
}
return false;
}
游戏控制功能
- 重新开始:清空棋子数组,重置当前玩家。
- 悔棋:移除最后一步棋子,切换回上一位玩家。
public void restart() { pieces = new ChessPiece[BOARD_SIZE][BOARD_SIZE]; currentPlayer = Color.BLACK; repaint(); }
public void undo() {
// 需额外记录每一步的落子位置,可通过栈(Stack)实现
}

#### 四、界面优化与用户体验
1. **添加控制按钮**:在主窗口中添加“重新开始”“悔棋”按钮,绑定对应方法。
2. **显示当前玩家**:通过`JLabel`实时提示当前落子方。
3. **棋盘样式调整**:设置网格线颜色、棋子阴影等细节,提升视觉效果。
#### 五、扩展功能
1. **人机对战**:实现简单AI算法(如极小化极大算法),增加电脑对手。
2. **存档与读档**:通过序列化将棋局状态保存至文件,支持后续加载。
3. **网络对战**:使用Socket编程实现双机对战功能。
#### 六、
通过Java Swing实现五子棋游戏,需合理设计类结构,重点处理棋盘绘制、落子逻辑和胜负判定等核心功能,代码实现过程中需注意坐标转换、事件监听和界面刷新的细节,进一步扩展功能可提升游戏的交互性和趣味性,适合作为Java GUI开发的练手项目。



















