我目前正在尝试编写一个代码,其中包含1s,0s,2和3的文本文档作为迷宫,2是开始,3是结束,并使用计时器和搜索方法来解决迷宫说。目前我的程序将正确启动并显示迷宫,但它无法解决迷宫问题。我运行了一个调试,它将进入类TimerListener,但不是方法actionPerformed,我无法弄清楚原因。这是我的代码的副本:
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.Timer;
public class homework11and12 extends JFrame {
private final int width = 1000;
private final int height = 700;
private JFrame frame;
private DrawComponent component;
private JPanel panel;
private final int exitBlock = 3;
private final int charBlock = 2;
private final int emptyBlock = 0;
private final int wallBlock = 1;
private final int visitedBlock = 4;
private final int moveRight = 5;
private final int moveDown = 6;
private final int moveUp = 7;
private final int moveLeft = 8;
private int[][] maze;
private static int startX = 0;
private static int startY = 0;
private ActionListener listener;
public homework11and12() {
this.setSize(width, height + 100);
maze = new int[7][10];
loadMaze("test", maze);
this.component = new DrawComponent(maze);
this.add(this.component);
listener = new TimerListener();
final int DELAY = 100;
Timer t = new Timer(DELAY, listener);
t.start();
}
public JPanel addPanel() {
panel = new JPanel();
component = new DrawComponent(maze);
panel.add(component);
panel.setVisible(true);
return panel;
}
public static void loadMaze(String filename, int[][] maze) {
try {
File file = new File(filename);
Scanner in = new Scanner(file);
int y = 0;
while(y < 7) {
for (int x = 0; x < 10; x++) {
try {
maze[y][x] = in.nextInt();
if( maze[y][x] == 2) {
startX = x;
startY = y;
}
}
catch(Exception e) {
e.printStackTrace();
}
}
y++;
}
}
catch (FileNotFoundException e) {
System.out.println("Error in finding file.");
}
}
private ArrayList<Integer> findSolution(int[][] board,int posx, int posy) {
ArrayList<Integer> moveList = null;
if(posx+1<width) {
if(board[posy][posx] == exitBlock) {
moveList = new ArrayList<Integer>();;
moveList.add(moveRight);
repaint();
return moveList;
}
else if (board[posy][posx] == emptyBlock) {
moveList = new ArrayList<Integer>();
moveList.add(moveRight);
repaint();
int[][] newBoard = arrayCopy(board);
newBoard[posy][posx+1] = visitedBlock;
ArrayList<Integer> result = findSolution(newBoard, posx+1, posy);
if (result == null) {
moveList = null;
}
else {
moveList.addAll(result);
return moveList;
}
}
}
else if (posy + 1< height) {
if(board[posy][posx] == exitBlock) {
moveList = new ArrayList<Integer>();
moveList.add(moveDown);
repaint();
return moveList;
}
else if (board[posy+1][posx] == emptyBlock) {
moveList = new ArrayList<Integer>();
moveList.add(moveDown);
repaint();
int[][] newBoard = arrayCopy(board);
newBoard[posy+1][posx] = visitedBlock;
ArrayList<Integer> result = findSolution(newBoard, posx, posy + 1);
if (result == null) {
return null;
}
else {
moveList.addAll(result);
return moveList;
}
}
}
else if(posy - 1 >= 0) {
if(board[posy-1][posx] == exitBlock) {
moveList = new ArrayList<Integer>();
moveList.add(moveDown);
repaint();
return moveList;
}
else if (board[posy-1][posx] == emptyBlock) {
moveList = new ArrayList<Integer>();
moveList.add(moveDown);
repaint();
int[][] newBoard = arrayCopy(board);
newBoard[posy-1][posx] = visitedBlock;
ArrayList<Integer> result = findSolution(newBoard, posx, posy-1);
if (result == null) {
return result;
}
else {
moveList.addAll(result);
return moveList;
}
}
}
else if(posx - 1 >= 0) {
if(board[posy][posx-1] == exitBlock) {
moveList = new ArrayList<Integer>();
moveList.add(moveLeft);
repaint();
return moveList;
}
else if(board[posy][posx-1] == emptyBlock) {
moveList = new ArrayList<Integer>();
moveList.add(moveLeft);
repaint();
int[][] newBoard = arrayCopy(board);
newBoard[posy][posx - 1] = visitedBlock;
ArrayList<Integer> result = findSolution(newBoard, posx-1, posy);
if (result == null) {
moveList = null;
}
else {
moveList.addAll(result);
return moveList;
}
}
}
return moveList;
}
private int[][] arrayCopy(int[][] array) {
if(array == null) {
return null;
}
int[][] newArray = new int[array.length][];
for(int r = 0; r <array.length; r++) {
newArray[r] = array[r].clone();
}
return newArray;
}
class DrawComponent extends JComponent {
private int[][] maze;
private DrawComponent(int[][] maze) {
this.maze = maze;
}
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics g) {
int y = 0;
while (y < 7) {
for (int x = 0; x < 10; x++) {
if (maze[y][x] == 1) {
g.setColor(Color.BLACK);
g.fillRect(x * 100, y* 100, 100, 100);
}
else if(maze[y][x] == 2) {
g.setColor(Color.ORANGE);
g.fillRect(x * 100, y * 100, 100, 100);
}
else if(maze[y][x] == 3) {
g.setColor(Color.RED);
g.fillRect(x * 100, y * 100, 100, 100);
}
}
y++;
}
}
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
findSolution(maze, startX, startY);
}
}
}
类TimerListener位于代码的完整底部。这是我启动计时器的问题吗?已经回答的几个问题是相似的,并说问题是计时器永远不会启动,但我几乎肯定我的计时器正在启动。
编辑:这是我的main()方法
import java.awt.BorderLayout;
import java.io.FileNotFoundException;
import java.util.Arrays;
import javax.swing.JFrame;
public class homeworkTest extends homework11and12 {
public static void main(String[] args) throws FileNotFoundException {
homework11and12 test = new homework11and12();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
test.setTitle("Maze");
}
}
由于