Java实现下五子棋

该程序使用JavaSwing的JButton组件创建了一个15x15的棋盘,每个按钮代表棋盘的一个位置。玩家点击按钮放置黑白棋子,程序会检查放置合法性,判断是否有五子连线,以及游戏是否结束。当游戏结束时,会弹出对话框提示胜利者或平局。
摘要由CSDN通过智能技术生成

利用Java.swing包

话不多说直接上代码!

该程序使用JButton数组来表示棋盘,在按钮中显示黑棋、白棋或空棋盘图标。每当玩家点击一个按钮,程序会检查该位置是否可以放置棋子,然后判断是否有五子连线或者是否平局,最后切换到下一个玩家。如果游戏结束,程序会弹出一个对话框提示。


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

public class FiveInARow extends JFrame implements ActionListener {
    private JButton[][] board;  // 棋盘按钮数组
    private ImageIcon black;    // 黑棋图标
    private ImageIcon white;    // 白棋图标
    private ImageIcon empty;    // 空棋盘图标
    private int currentPlayer;  // 当前玩家(1代表黑棋,2代表白棋)
    private boolean gameOver;   // 游戏是否结束

    public FiveInARow() {
        // 初始化棋盘按钮数组
        board = new JButton[15][15];
        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 15; j++) {
                board[i][j] = new JButton();
                board[i][j].setIcon(empty);
                board[i][j].setPreferredSize(new Dimension(40, 40));
                board[i][j].addActionListener(this);
            }
        }

        // 初始化图标和玩家
        black = new ImageIcon(getClass().getResource("black.png"));
        white = new ImageIcon(getClass().getResource("white.png"));
        empty = new ImageIcon(getClass().getResource("empty.png"));
        currentPlayer = 1;
        gameOver = false;

        // 添加到布局中
        JPanel panel = new JPanel(new GridLayout(15, 15));
        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 15; j++) {
                panel.add(board[i][j]);
            }
        }
        add(panel);

        // 设置窗口属性
        setTitle("五子棋");
        setSize(600, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    // 处理按钮点击事件
    public void actionPerformed(ActionEvent e) {
        if (gameOver) {
            return;
        }

        // 获取点击的按钮
        JButton button = (JButton)e.getSource();

        // 获取按钮在数组中的位置
        int row = -1, col = -1;
        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 15; j++) {
                if (button == board[i][j]) {
                    row = i;
                    col = j;
                    break;
                }
            }
        }

        // 如果该按钮已经有棋子或者按钮不在棋盘内,不做任何处理
        if (row == -1 || col == -1 || board[row][col].getIcon() != empty) {
            return;
        }

        // 在该位置放置当前玩家的棋子
        board[row][col].setIcon(currentPlayer == 1 ? black : white);

        // 检查是否有五子连线
        if (checkWin(row, col)) {
            JOptionPane.showMessageDialog(this, "玩家 " + currentPlayer + " 获胜!");
            gameOver = true;
            return;
        }

        // 判断是否平局
        boolean isTie = true;
        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 15; j++) {
                if (board[i][j].getIcon() == empty) {
                    isTie = false;
                    break;
                }
            }
            if (!isTie) {
                break;
            }
        }
        if (isTie) {
            JOptionPane.showMessageDialog(this, "平局!");
            gameOver = true;
            return;
        }

        // 切换到下一个玩家
        currentPlayer = currentPlayer == 1 ? 2 : 1;
    }

    // 检查是否有五子连线
    private boolean checkWin(int row, int col) {
        int count = 1;

        // 检查横向连线
        for (int i = col - 1; i >= 0; i--) {
            if (board[row][i].getIcon() == board[row][col].getIcon()) {
                count++;
            } else {
                break;
            }
        }
        for (int i = col + 1; i < 15; i++) {
            if (board[row][i].getIcon() == board[row][col].getIcon()) {
                count++;
            } else {
                break;
            }
        }
        if (count >= 5) {
            return true;
        }

        // 检查纵向连线
        count = 1;
        for (int i = row - 1; i >= 0; i--) {
            if (board[i][col].getIcon() == board[row][col].getIcon()) {
                count++;
            } else {
                break;
            }
        }
        for (int i = row + 1; i < 15; i++) {
            if (board[i][col].getIcon() == board[row][col].getIcon()) {
                count++;
            } else {
                break;
            }
        }
        if (count >= 5) {
            return true;
        }

        // 检查左上到右下连线
        count = 1;
        for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
            if (board[i][j].getIcon() == board[row][col].getIcon()) {
                count++;
            } else {
                break;
            }
        }
        for (int i = row + 1, j = col + 1; i < 15 && j < 15; i++, j++) {
            if (board[i][j].getIcon() == board[row][col].getIcon()) {
                count++;
            } else {
                break;
            }
        }
        if (count >= 5) {
            return true;
        }

        // 检查左下到右上连线
        count = 1;
        for (int i = row + 1, j = col - 1; i < 15 && j >= 0; i++, j--) {
            if (board[i][j].getIcon() == board[row][col].getIcon()) {
                count++;
            } else {
                break;
            }
        }
        for (int i = row - 1, j = col + 1; i >= 0 && j < 15; i--, j++) {
            if (board[i][j].getIcon() == board[row][col].getIcon()) {
                count++;
            } else {
                break;
            }
        }
        if (count >= 5) {
            return true;
        }

        // 无连线
        return false;
    }

    public static void main(String[] args) {
        new FiveInARow();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值