基于java的五子棋小游戏

游戏规则

在这里插入图片描述

棋盘:游戏在 15x15 的方格棋盘上进行,每个交叉点都可以放置棋子。
棋子:游戏双方分别执黑棋和白棋,黑棋先行,双方轮流在棋盘的交叉点上放置自己的棋子。
胜利条件:游戏的目标是率先在任意方向(横、竖、斜)上将自己的五颗棋子连成一线。达到五子连珠的一方获胜。
落子规则:每个玩家在自己的回合中只能在一个未被占据的交叉点上放置一枚棋子。
在代码中,点击棋盘时,棋子会落在最近的交叉点上,并判断是否达成五子连珠以决定胜负。

代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Gomoku extends JFrame {
    private static final int BOARD_SIZE = 15;
    private static final int CELL_SIZE = 40;
    private static final int STONE_RADIUS = 15;
    private static final Color BOARD_COLOR = new Color(222, 184, 135);

    private int[][] board = new int[BOARD_SIZE][BOARD_SIZE]; // 0: empty, 1: black, 2: white
    private boolean isBlackTurn = true; // 黑棋先行

    public Gomoku() {
        setTitle("五子棋");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false); // 设置不可拉伸窗口

        GomokuPanel panel = new GomokuPanel();
        panel.setPreferredSize(new Dimension(BOARD_SIZE * CELL_SIZE, BOARD_SIZE * CELL_SIZE));
        add(panel);
        pack(); // 自动调整窗口大小以适应面板

        setLocationRelativeTo(null); // 窗口居中显示
    }

    private class GomokuPanel extends JPanel {
        public GomokuPanel() {
            addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    int mouseX = e.getX();
                    int mouseY = e.getY();

                    // 将鼠标点击位置吸附到最近的交点上
                    int x = (mouseX + CELL_SIZE / 2) / CELL_SIZE;
                    int y = (mouseY + CELL_SIZE / 2) / CELL_SIZE;

                    if (x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE && board[x][y] == 0) {
                        board[x][y] = isBlackTurn ? 1 : 2; // 1: 黑棋, 2: 白棋
                        if (checkWin(x, y)) {
                            String winner = isBlackTurn ? "黑棋" : "白棋";
                            JOptionPane.showMessageDialog(null, winner + " 胜利!");
                            resetBoard();
                        } else {
                            isBlackTurn = !isBlackTurn; // 切换玩家
                        }
                        repaint();
                    }
                }
            });
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(BOARD_COLOR);
            g.fillRect(0, 0, getWidth(), getHeight());

            // 画棋盘
            g.setColor(Color.BLACK);
            for (int i = 0; i <= BOARD_SIZE; i++) {
                g.drawLine(0, i * CELL_SIZE, BOARD_SIZE * CELL_SIZE, i * CELL_SIZE); // 绘制横线
                g.drawLine(i * CELL_SIZE, 0, i * CELL_SIZE, BOARD_SIZE * CELL_SIZE); // 绘制纵线
            }

            // 画棋子
            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (board[i][j] == 1) {
                        g.setColor(Color.BLACK);
                        g.fillOval(i * CELL_SIZE - STONE_RADIUS, j * CELL_SIZE - STONE_RADIUS, STONE_RADIUS * 2, STONE_RADIUS * 2);
                    } else if (board[i][j] == 2) {
                        g.setColor(Color.WHITE);
                        g.fillOval(i * CELL_SIZE - STONE_RADIUS, j * CELL_SIZE - STONE_RADIUS, STONE_RADIUS * 2, STONE_RADIUS * 2);
                    }
                }
            }
        }
    }

    // 重置棋盘
    private void resetBoard() {
        board = new int[BOARD_SIZE][BOARD_SIZE];
        isBlackTurn = true;
    }

    // 判断是否胜利
    private boolean checkWin(int x, int y) {
        int player = board[x][y];
        return checkDirection(x, y, 1, 0, player) || // 水平
                checkDirection(x, y, 0, 1, player) || // 垂直
                checkDirection(x, y, 1, 1, player) || // 左上到右下
                checkDirection(x, y, 1, -1, player);  // 右上到左下
    }

    // 检查某一方向是否有五子连珠
    private boolean checkDirection(int x, int y, int dx, int dy, int player) {
        int count = 1;
        // 向正方向检查
        for (int i = 1; i < 5; i++) {
            int nx = x + i * dx;
            int ny = y + i * dy;
            if (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE && board[nx][ny] == player) {
                count++;
            } else {
                break;
            }
        }
        // 向反方向检查
        for (int i = 1; i < 5; i++) {
            int nx = x - i * dx;
            int ny = y - i * dy;
            if (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE && board[nx][ny] == player) {
                count++;
            } else {
                break;
            }
        }
        return count >= 5;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            Gomoku game = new Gomoku();
            game.setVisible(true);
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值