用Java实现简单的五子棋

该文章展示了一个使用JavaSwing库编写的五子棋游戏GUI。程序创建了一个15x15的棋盘,使用JButton数组来表示棋盘的每个位置,并实现了按钮点击事件监听器以处理玩家的移动。游戏还包括了检查获胜条件、切换玩家以及重置游戏的功能。
摘要由CSDN通过智能技术生成
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();
                }
            }
        }
    }
}

 

java五子棋 五子棋 java 五子棋代码直接和电脑下对战 Public class FiveChessAI { public int data_a[][] = new int[5][3];// 用于储存进攻值 public int data_d[][] = new int[5][3];// 用于储存防守值 FiveChessAI() { // 进攻值的初始化 data_a[1][1] = 2; data_a[1][2] = 3; data_a[2][1] = 10; data_a[2][2] = 110; data_a[3][1] = 2500; data_a[3][2] = 3000; data_a[4][1] = 99999; data_a[4][2] = 99999; // 防守值的初始化 data_d[1][1] = 1; data_d[1][2] = 2; data_d[2][1] = 1; data_d[2][2] = 100; data_d[3][1] = 100; data_d[3][2] = 500; data_d[4][1] = 20000; data_d[4][2] = 50000; } public FiveChessMap g1 = new FiveChessMap(); public int x, y; void find()// 查找最大值 { int max = 0; for (int i = 0; i < 15; ++i) { for (int j = 0; j < 15; ++j) { if (max < g1.data[i][j]) { max = g1.data[i][j]; } } } for (int i = 0; i < 15; ++i) { for (int j = 0; j < 15; ++j) { if (max == g1.data[i][j]) { x = i; y = j; return; } } } } int getx()// 返回x坐标值 { return x; } int gety()// 返回y坐标值 { return y; } boolean judge_result(int x, int y, int who, FiveChessMap gm)// 判断胜负 { int m, n, i, lianzi = 0; for (m = -1; m <= 1; m++) for (n = -1; n <= 1; n++) { if (m != 0 || n != 0) { for (i = 1; i <= 4; i++) { if (x + i * m >= 0 && x + i * m < 15 && y + i * n >= 0 && y + i * n < 15 && gm.data[x + i * m][y + i * n] == who) { lianzi++; } else { break; } } for (i = -1; i >= -4; i--) { if (x + i * m >= 0 && x + i * m < 15 && y + i * n >= 0 && y + i * n < 15 && gm.data[x + i * m][y + i * n] == who) { lianzi++; } else { break; } } if (lianzi >= 4) { return true; } else { lianzi = 0; } } } return false; }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值