返回

五子棋java源代码:五子棋Java源代码实现,从规则到完整游戏程序

来源:网络   作者:   日期:2025-10-27 03:47:22  

五子棋是一种古老而经典的棋类游戏,起源于中国古代,因其简单易懂的规则和策略性深受玩家喜爱,在现代编程学习中,五子棋常被用作练习面向对象编程、游戏逻辑和图形界面开发的入门项目,本文将通过一个完整的Java源代码示例,展示如何实现一个可运行的五子棋游戏,并解析其中的关键代码逻辑。


五子棋规则简述

五子棋的目标是通过交替在棋盘上放置棋子,先在横、竖、斜方向上连成五个或以上同色棋子的一方获胜,棋盘通常为15×15或19×19的网格,玩家轮流落子,直到一方获胜或棋盘填满。


Java源代码实现

下面是一个基于Java Swing的五子棋游戏实现,代码分为以下几个部分:

  1. 棋盘表示
    使用二维数组 char[][] board 来表示棋盘,0 表示空位,1 表示黑棋,2 表示白棋。

    五子棋java源代码:五子棋Java源代码实现,从规则到完整游戏程序

  2. 游戏逻辑

    • 轮流落子
    • 判断胜负
    • 检查落子是否有效
  3. 图形界面
    使用Swing组件绘制棋盘和棋子,并处理鼠标点击事件。


代码示例:

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
public class GomokuGame extends JFrame {
    private static final int BOARD_SIZE = 15;
    private static final int CELL_SIZE = 40;
    private static final int MARGIN = 50;
    private char[][] board = new char[BOARD_SIZE][BOARD_SIZE];
    private boolean isBlackTurn = true; // true为黑棋,false为白棋
    public GomokuGame() {
        setTitle("五子棋");
        setSize(BOARD_SIZE * CELL_SIZE + 2 * MARGIN, BOARD_SIZE * CELL_SIZE + 2 * MARGIN);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initBoard();
        paintBoard();
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int x = e.getX() - MARGIN;
                int y = e.getY() - MARGIN;
                int col = x / CELL_SIZE;
                int row = y / CELL_SIZE;
                if (isValidMove(row, col)) {
                    board[row][col] = isBlackTurn ? '1' : '2';
                    isBlackTurn = !isBlackTurn;
                    repaint();
                    if (checkWin(row, col)) {
                        JOptionPane.showMessageDialog(GomokuGame.this, isBlackTurn ? "白棋获胜!" : "黑棋获胜!");
                        resetGame();
                    }
                }
            }
        });
    }
    private void initBoard() {
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                board[i][j] = '0';
            }
        }
    }
    private void paintBoard() {
        Graphics g = getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, getWidth(), getHeight());
        // 绘制棋盘网格
        g.setColor(Color.BLACK);
        for (int i = 0; i < BOARD_SIZE; i++) {
            // 横线
            g.drawLine(MARGIN, MARGIN + i * CELL_SIZE, MARGIN + (BOARD_SIZE - 1) * CELL_SIZE, MARGIN + i * CELL_SIZE);
            // 竖线
            g.drawLine(MARGIN + i * CELL_SIZE, MARGIN, MARGIN + i * CELL_SIZE, MARGIN + (BOARD_SIZE - 1) * CELL_SIZE);
        }
        // 绘制棋子
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                if (board[i][j] != '0') {
                    Ellipse2D circle;
                    if (board[i][j] == '1') {
                        circle = new Ellipse2D.Double(
                                MARGIN + j * CELL_SIZE - CELL_SIZE / 2,
                                MARGIN + i * CELL_SIZE - CELL_SIZE / 2,
                                CELL_SIZE,
                                CELL_SIZE
                        );
                        g.setColor(Color.BLACK);
                    } else {
                        circle = new Ellipse2D.Double(
                                MARGIN + j * CELL_SIZE - CELL_SIZE / 2,
                                MARGIN + i * CELL_SIZE - CELL_SIZE / 2,
                                CELL_SIZE,
                                CELL_SIZE
                        );
                        g.setColor(Color.WHITE);
                    }
                    g.fill(circle);
                    g.setColor(Color.BLACK);
                    g.draw(circle);
                }
            }
        }
    }
    private boolean isValidMove(int row, int col) {
        if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {
            return false;
        }
        return board[row][col] == '0';
    }
    private boolean checkWin(int row, int col) {
        char current = board[row][col];
        // 检查方向:横、竖、左下-右上、右下-左上
        int[] directions = {0, 1, 1, 1, -1, 1, -1, 0};
        for (int d = 0; d < 8; d++) {
            int count = 1;
            for (int i = 1; i <= 4; i++) {
                int r = row + i * directions[d];
                int c = col + i * directions[d];
                if (r < 0 || r >= BOARD_SIZE || c < 0 || c >= BOARD_SIZE || board[r][c] != current) {
                    break;
                }
                count++;
            }
            for (int i = 1; i <= 4; i++) {
                int r = row - i * directions[d];
                int c = col - i * directions[d];
                if (r < 0 || r >= BOARD_SIZE || c < 0 || c >= BOARD_SIZE || board[r][c] != current) {
                    break;
                }
                count++;
            }
            if (count >= 5) {
                return true;
            }
        }
        return false;
    }
    private void resetGame() {
        initBoard();
        repaint();
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            GomokuGame game = new GomokuGame();
            game.setVisible(true);
        });
    }
}

代码解析

  1. 棋盘初始化
    initBoard() 方法初始化棋盘数组,所有位置设为 '0',表示空位。

    五子棋java源代码:五子棋Java源代码实现,从规则到完整游戏程序

  2. 落子逻辑
    isValidMove() 方法检查落子位置是否有效(在棋盘范围内且为空)。

  3. 胜负判断
    checkWin() 方法通过检查八个方向(横、竖、斜)的连续棋子数量,判断是否获胜。

  4. 图形界面
    使用Swing绘制棋盘和棋子,鼠标点击事件触发落子。


扩展建议

  1. 添加悔棋功能
  2. 实现人机对战(AI)
  3. 增加计时器和音效
  4. 保存和加载游戏记录

分类:编程
责任编辑:今题网
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。

相关文章:

文章已关闭评论!