用Java实现简单的五子棋

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GomokuGUI extends JFrame {
    private static final int BOARD_SIZE = 15;
    private static final int CELL_SIZE = 50;
    private static final int BOARD_PADDING = 50;
    private static final Color BOARD_COLOR = new Color(204, 153, 102);
    private static final Color LINE_COLOR = Color.BLACK;

    private JButton[][] buttons; // 存储按钮的二维数组
    private char[][] board; // 存储棋盘的二维数组
    private char currentPlayerSymbol; // 当前玩家的符号('X'或'O')

    public GomokuGUI() {
        setTitle("五子棋");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

        board = new char[BOARD_SIZE][BOARD_SIZE];
        currentPlayerSymbol = 'X';
        initializeBoard(); // 初始化棋盘
        initializeButtons(); // 初始化按钮

        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void initializeBoard() {
        JPanel boardPanel = new JPanel(new GridLayout(BOARD_SIZE, BOARD_SIZE));
        boardPanel.setBackground(BOARD_COLOR);
        boardPanel.setPreferredSize(new Dimension(BOARD_SIZE * CELL_SIZE, BOARD_SIZE * CELL_SIZE));

        buttons = new JButton[BOARD_SIZE][BOARD_SIZE]; // 初始化按钮数组

        for (int row = 0; row < BOARD_SIZE; row++) {
            for (int col = 0; col < BOARD_SIZE; col++) {
                board[row][col] = '-'; // 初始化棋盘的每个位置为'-'

                JButton button = new JButton();
                button.setPreferredSize(new Dimension(CELL_SIZE, CELL_SIZE));
                button.setBackground(BOARD_COLOR);
                button.addActionListener(new ButtonClickListener(row, col)); // 每个按钮添加点击事件监听器

                buttons[row][col] = button; // 将按钮添加到按钮数组

                boardPanel.add(button); // 添加按钮到面板
            }
        }

        add(boardPanel, BorderLayout.CENTER);
    }

    private void initializeButtons() {
        JPanel buttonPanel = new JPanel();

        JButton resetButton = new JButton("重新开始");
        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                resetGame(); // 点击重新开始按钮时重置游戏
            }
        });
        buttonPanel.add(resetButton);

        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void makeMove(int row, int col) {
        buttons[row][col].setText(String.valueOf(currentPlayerSymbol)); // 在按钮上显示当前玩家的符号
        buttons[row][col].setEnabled(false); // 禁用已经下过的按钮
        board[row][col] = currentPlayerSymbol; // 更新棋盘数组中的位置为当前玩家的符号
    }

    private void switchPlayer() {
        currentPlayerSymbol = (currentPlayerSymbol == 'X') ? 'O' : 'X'; // 切换当前玩家的符号
    }

    private boolean checkWin(int row, int col) {
        char symbol = currentPlayerSymbol;

        // 检查行
        int count = 0;
        for (int c = 0; c < BOARD_SIZE; c++) {
            if (board[row][c] == symbol) {
                count++;
                if (count == 5) {
                    return true; // 玩家获胜
                }
            } else {
                count = 0;
            }
        }

        // 检查列
        count = 0;
        for (int r = 0; r < BOARD_SIZE; r++) {
            if (board[r][col] == symbol) {
                count++;
                if (count == 5) {
                    return true; // 玩家获胜
                }
            } else {
                count = 0;
            }
        }

        // 检查主对角线
        count = 0;
        int r = row;
        int c = col;
        while (r > 0 && c > 0) {
            r--;
            c--;
        }
        while (r < BOARD_SIZE && c < BOARD_SIZE) {
            if (board[r][c] == symbol) {
                count++;
                if (count == 5) {
                    return true; // 玩家获胜
                }
            } else {
                count = 0;
            }
            r++;
            c++;
        }

        // 检查副对角线
        count = 0;
        r = row;
        c = col;
        while (r < BOARD_SIZE - 1 && c > 0) {
            r++;
            c--;
        }
        while (r >= 0 && c < BOARD_SIZE) {
            if (board[r][c] == symbol) {
                count++;
                if (count == 5) {
                    return true; // 玩家获胜
                }
            } else {
                count = 0;
            }
            r--;
            c++;
        }

        return false; // 未获胜
    }

    private boolean isBoardFull() {
        for (int row = 0; row < BOARD_SIZE; row++) {
            for (int col = 0; col < BOARD_SIZE; col++) {
                if (board[row][col] == '-') {
                    return false; // 只要有一个位置为'-',即表示棋盘未满
                }
            }
        }
        return true; // 所有位置都不为'-',表示棋盘已满
    }

    private void resetGame() {
        for (int row = 0; row < BOARD_SIZE; row++) {
            for (int col = 0; col < BOARD_SIZE; col++) {
                buttons[row][col].setEnabled(true); // 启用所有按钮
                buttons[row][col].setText(""); // 清空按钮上的文本
                board[row][col] = '-'; // 将棋盘数组中的位置重置为'-'
            }
        }
        currentPlayerSymbol = 'X'; // 将当前玩家设置为'X'
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GomokuGUI(); // 在事件分发线程中创建并显示GUI
            }
        });
    }

    private class ButtonClickListener implements ActionListener {
        private int row;
        private int col;

        public ButtonClickListener(int row, int col) {
            this.row = row;
            this.col = col;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (board[row][col] == '-') { // 只有未下棋的位置才能点击
                makeMove(row, col);
                if (checkWin(row, col)) {
                    JOptionPane.showMessageDialog(GomokuGUI.this, "玩家 " + currentPlayerSymbol + " 胜利!", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
                    resetGame();
                } else if (isBoardFull()) {
                    JOptionPane.showMessageDialog(GomokuGUI.this, "平局!", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
                    resetGame();
                } else {
                    switchPlayer();
                }
            }
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值