基于java的围棋小游戏(半成品)

游戏规则

棋盘:游戏使用一个9x9的棋盘,棋盘由横线和竖线组成的网格表示。
棋子:围棋分为黑棋和白棋,两名玩家轮流下子。黑棋先行。
落子:玩家点击棋盘上的某个点,棋子会被放置在最近的格子上。棋盘的格子大小为40像素,每个棋子的半径为15像素。
提子规则:每个棋子有“气”,即它上下左右相邻的空位。如果一组同色棋子被对方的棋子围住,所有的“气”都被封死,这组棋子会被提走,也就是从棋盘上移除。
胜负判定:程序目前未实现围棋的胜负判定,主要展示基本的围棋下子和提子的逻辑。
游戏的核心是通过点击棋盘进行下子,同时遵循围棋的基本规则,如提子。
在这里插入图片描述

代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashSet;
import java.util.Set;

public class GoGame extends JFrame {
    private static final int BOARD_SIZE = 9; // 棋盘尺寸 9x9
    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 static final Color BLACK_STONE_COLOR = Color.BLACK; // 黑棋颜色
    private static final Color WHITE_STONE_COLOR = Color.WHITE; // 白棋颜色

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

    public GoGame() {
        setTitle("围棋游戏");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false); // 设置窗口不可拉伸

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

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

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

                    // 将鼠标点击位置吸附到最近的网格点上
                    int x = mouseX / CELL_SIZE;
                    int y = mouseY / CELL_SIZE;

                    if (x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE && board[x][y] == 0) {
                        // 进行落子
                        board[x][y] = isBlackTurn ? 1 : 2;

                        // 检查并提子
                        captureStones(x, y, isBlackTurn ? 2 : 1);

                        // 切换玩家
                        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(BLACK_STONE_COLOR);
                        g.fillOval(i * CELL_SIZE + (CELL_SIZE - STONE_RADIUS * 2) / 2,
                                j * CELL_SIZE + (CELL_SIZE - STONE_RADIUS * 2) / 2,
                                STONE_RADIUS * 2, STONE_RADIUS * 2);
                    } else if (board[i][j] == 2) {
                        g.setColor(WHITE_STONE_COLOR);
                        g.fillOval(i * CELL_SIZE + (CELL_SIZE - STONE_RADIUS * 2) / 2,
                                j * CELL_SIZE + (CELL_SIZE - STONE_RADIUS * 2) / 2,
                                STONE_RADIUS * 2, STONE_RADIUS * 2);
                    }
                }
            }
        }
    }

    private void captureStones(int x, int y, int opponent) {
        // 查找被提子的棋子
        boolean[][] visited = new boolean[BOARD_SIZE][BOARD_SIZE];
        Set<Point> stonesToRemove = new HashSet<>();

        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                if (board[i][j] == opponent && !visited[i][j] && !hasLiberties(i, j, visited)) {
                    stonesToRemove.add(new Point(i, j));
                }
            }
        }

        // 移除被提子的棋子
        for (Point p : stonesToRemove) {
            board[p.x][p.y] = 0;
        }
    }

    private boolean hasLiberties(int x, int y, boolean[][] visited) {
        if (visited[x][y]) return false;
        visited[x][y] = true;

        int color = board[x][y];
        boolean hasLiberty = false;

        // 四个方向的相邻位置
        int[] dx = {-1, 1, 0, 0};
        int[] dy = {0, 0, -1, 1};

        for (int d = 0; d < 4; d++) {
            int nx = x + dx[d];
            int ny = y + dy[d];
            if (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE) {
                if (board[nx][ny] == 0) {
                    hasLiberty = true;
                } else if (board[nx][ny] == color && !visited[nx][ny]) {
                    hasLiberty = hasLiberties(nx, ny, visited);
                }
            }
        }
        return hasLiberty;
    }

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

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值