/*
* 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;
}
}
迷宫小游戏
最新推荐文章于 2024-01-15 10:38:09 发布