迷宫小游戏

本文介绍了一个使用Java实现的迷宫寻路程序。该程序利用递归回溯算法在一个10x10的迷宫中寻找从起点到终点的路径,并提供用户界面进行交互操作,包括设置障碍物、寻找路径及清除路径等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javalearning4;

/**
 *
 * @author WEN
 */
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class JavaLearning4 extends JFrame {

    private final int MAX_SIZE = 10;
    private Cell[][] board = new Cell[MAX_SIZE][MAX_SIZE];
    private JButton jbFindPath = new JButton("寻路");
    private JButton jbClearPant = new JButton("清除");
    private JPanel jpUp, jpBut;

    JavaLearning4() {
        this.setTitle("迷宫模拟小程序");
        this.setSize(400, 320);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.jpUp = new JPanel();
        this.jpBut = new JPanel();
        this.jpUp.setLayout(new GridLayout(MAX_SIZE, MAX_SIZE, 2, 2));
        for (int i = 0; i < MAX_SIZE; i++) {
            for (int j = 0; j < MAX_SIZE; j++) {
                this.board[i][j] = new Cell();
                this.jpUp.add(board[i][j]);
            }
        }
        this.add(jpUp, BorderLayout.CENTER);
        this.jpBut.add(jbFindPath);
        this.jpBut.add(jbClearPant);
        this.add(jpBut, BorderLayout.SOUTH);
        this.jbFindPath.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JavaLearning4.this.findPath();
            }
        });
        jbClearPant.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JavaLearning4.this.clearPath();
            }
        });
        this.setVisible(true);
    }

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

    public void findPath() {
        if (this.findPath(0, 0)) {
            JOptionPane.showMessageDialog(this, "我找到了~");
        } else {
            JOptionPane.showMessageDialog(this, "我没找到!");
        }
    }

    public boolean findPath(int row, int col) {
        board[row][col].visit();
        if (row == (MAX_SIZE - 1) && col == (MAX_SIZE - 1)) {
            board[row][col].selectedCell();
            return true;
        }
        if ((row > 0) && !board[row - 1][col].blocked()
                && !board[row - 1][col].marked() && !board[row - 1][col].visited()) {
            this.block(row, col);
            if (this.findPath(row - 1, col)) {
                board[row][col].selectedCell();
                return true;
            }
            this.unblock(row, col);
        }
        if ((row < (MAX_SIZE - 1)) && !board[row + 1][col].blocked()
                && !board[row + 1][col].marked() && !board[row + 1][col].visited()) {
            this.block(row, col);
            if (this.findPath(row + 1, col)) {
                board[row][col].selectedCell();
                return true;
            }
            this.unblock(row, col);
        }
        if ((col > 0) && !board[row][col - 1].blocked()
                && !board[row][col - 1].marked() && !board[row][col - 1].visited()) {
            this.block(row, col);
            if (this.findPath(row, col - 1)) {
                board[row][col].selectedCell();
                return true;
            }
            this.unblock(row, col);
        }
        if ((col < (MAX_SIZE - 1)) && !board[row][col + 1].blocked()
                && !board[row][col + 1].marked() && !board[row][col + 1].visited()) {
            this.block(row, col);
            if (this.findPath(row, col + 1)) {
                board[row][col].selectedCell();
                return true;
            }
            this.unblock(row, col);
        }
        return false;
    }

    public void clearPath() {
        for (int i = 0; i < MAX_SIZE; i++) {
            for (int j = 0; j < MAX_SIZE; j++) {
                this.board[i][j].desSelectedCell();
            }
        }
    }

    public void block(int row, int col) {
        if (row > 0) {
            board[row - 1][col].block();
        }
        if (row < MAX_SIZE - 1) {
            board[row + 1][col].block();
        }
        if (col > 0) {
            board[row][col - 1].block();
        }
        if (col < MAX_SIZE - 1) {
            board[row][col + 1].block();
        }
    }

    public void unblock(int row, int col) {
        if (row > 0) {
            board[row - 1][col].unblock();
        }
        if (row < MAX_SIZE - 1) {
            board[row + 1][col].unblock();
        }
        if (col > 0) {
            board[row][col - 1].unblock();
        }
        if (col < MAX_SIZE - 1) {
            board[row][col + 1].unblock();
        }
    }
}

class Cell extends JPanel {

    private boolean visited;
    private boolean marked;
    private boolean blocked;

    Cell() {
        this.visited = false;
        this.marked = false;
        this.blocked = false;
        this.setBackground(Color.red);
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                marked = !marked;
                if (marked) {
                    Cell.this.setBackground(Color.black);
                } else {
                    Cell.this.setBackground(Color.red);
                }
                Cell.this.repaint();
            }
        });
    }

    public boolean visited() {
        return this.visited;
    }

    public void visit() {
        this.visited = true;
    }

    public boolean marked() {
        return this.marked;
    }

    public void block() {
        this.blocked = true;
    }

    public void unblock() {
        this.blocked = false;
    }

    public boolean blocked() {
        return this.blocked;
    }

    public void selectedCell() {
        this.setBackground(Color.green);
        this.repaint();
    }

    public void desSelectedCell() {
        this.setBackground(Color.red);
        this.repaint();
        this.marked = false;
        this.visited = false;
        this.blocked = false;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值