- import java.awt.*;
- import java.awt.event.*;
- import java.util.Scanner;
- import javax.swing.*;
- public class Maze extends JFrame {
- private final int n = 10;
- JButton[] btMaze = new JButton[n * n];
- int[] fx = {1,-1,0,0};
- int[] fy = {0,0,-1,1};
- int nowx = 0;
- int nowy = 0;
- int[][] maze = {
- { 0, 0, 0, 0, 0, 0, 1, 1, 1, 0 },
- { 0, 1, 1, 0, 1, 1, 1, 0, 0, 0 },
- { 0, 1, 0, 0, 0, 0, 0, 0, 1, 1 },
- { 0, 1, 1, 1, 1, 1, 1, 0, 0, 0 },
- { 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
- { 1, 1, 0, 1, 0, 1, 1, 1, 0, 1 },
- { 0, 0, 0, 0, 0, 1, 0, 1, 0, 1 },
- { 0, 1, 1, 1, 1, 1, 0, 1, 0, 0 },
- { 0, 1, 1, 0, 0, 0, 0, 1, 1, 0 },
- { 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 } };
- public static void main(String[] args) {
- new Maze();
- }
- public Maze() {
- System.out.println("请选择演示方式:1.手动操作 2.自动演示");
- Scanner sc = new Scanner(System.in);
- int temp = sc.nextInt();
- if(temp == 1){
- frame(maze);
- }
- else if(temp == 2){
- frame(maze);
- search(0,0);
- }
- else{
- System.out.println("输入错误");
- }
- }
- private void frame(int[][] maze) {
- JFrame frame = new JFrame("迷宫");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(60 * n, 60 * n);
- frame.setLocation(200, 100);
- Container ct = frame.getContentPane();
- JPanel panel = new JPanel();
- panel.setLayout(new GridLayout(n, n, 0, 0));
- for (int i = 0; i < n * n; i++) {
- btMaze[i] = new JButton();
- btMaze[i].setBackground(Color.gray);
- panel.add(btMaze[i]);
- }
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- if (maze[i][j] == 1) {
- btMaze[i * n + j].setBackground(Color.darkGray);
- }
- }
- }
- btMaze[nowx*n + nowy].setBackground(Color.blue);
- btMaze[n*n-1].setBackground(Color.red);
- frame.add(panel,new BorderLayout().CENTER);
- JPanel panel2 = new JPanel();
- JButton jrb1 = new JButton("手动操作");
- //jrb1.addActionListener(new MyListener(frame));
- jrb1.setSelected(true);
- JButton jrb2 = new JButton("自动完成");
- //jrb2.addActionListener(new MyListener(frame));
- panel2.add(jrb1);
- panel2.add(jrb2);
- panel2.setBackground(Color.gray);
- frame.add(panel2,new BorderLayout().SOUTH);
- frame.addKeyListener(new MyListener(frame));
- frame.setFocusable(true);// 不加没反应
- frame.setVisible(true);
- }
- public void search(int i, int j)
- {
- int k;
- int newi;
- int newj;
- btMaze[i*n + j].setBackground(Color.gray);
- for (k = 0; k < 4; k++)
- {
- if (check(i, j, k) == 1)
- {
- newi = i + fx[k];
- newj = j + fy[k];
- maze[newi][newj] = 3;
- btMaze[newi*n + newj].setBackground(Color.blue);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- if (newi == n-1 && newj == n-1)
- {
- JOptionPane.showMessageDialog(null, "恭喜,胜利了");
- }
- else
- {
- search(newi, newj);
- break;
- }
- }
- }
- maze[i][j] = 2;
- }
- public int check(int i, int j, int k)
- {
- int flag = 1;
- i = i + fx[k];
- j = j + fy[k];
- if (i < 0 || i > n-1 || j < 0 || j > n-1)
- {
- flag = 0;
- return flag;
- }
- if (maze[i][j] != 0)
- flag = 0;
- return flag;
- }
- class MyListener extends KeyAdapter implements ActionListener{
- JFrame frame;
- public MyListener(JFrame frame) {
- this.frame = frame;
- }
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_UP) {
- if(nowx > 0){
- if(maze[nowx-1][nowy] == 0){
- btMaze[nowx*n + nowy].setBackground(Color.gray);
- nowx--;
- btMaze[nowx*n + nowy].setBackground(Color.blue);
- }
- }
- }
- if (e.getKeyCode() == KeyEvent.VK_DOWN) {
- if(nowx < n-1){
- if(maze[nowx+1][nowy] == 0){
- btMaze[nowx*n + nowy].setBackground(Color.gray);
- nowx++;
- btMaze[nowx*n + nowy].setBackground(Color.blue);
- }
- }
- if(nowx == n-1 && nowy == n-1){
- JOptionPane.showMessageDialog(null, "恭喜,胜利了!");
- }
- }
- if (e.getKeyCode() == KeyEvent.VK_LEFT) {
- if(nowy > 0){
- if(maze[nowx][nowy-1] == 0){
- btMaze[nowx*n + nowy].setBackground(Color.gray);
- nowy--;
- btMaze[nowx*n + nowy].setBackground(Color.blue);
- }
- }
- }
- if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
- if(nowy < n-1){
- if(maze[nowx][nowy+1] == 0){
- btMaze[nowx*n + nowy].setBackground(Color.gray);
- nowy++;
- btMaze[nowx*n + nowy].setBackground(Color.blue);
- }
- }
- if(nowx == n-1 && nowy == n-1){
- JOptionPane.showMessageDialog(null, "恭喜,胜利了!");
- }
- }
- }
- public void actionPerformed(ActionEvent e) {
- System.out.println(e.getActionCommand());
- if(e.getActionCommand().equals("手动操作")){
- frame.addKeyListener(new MyListener(frame));
- }
- if(e.getActionCommand().equals("自动完成")){
- search(0,0);
- }
- }
- }
- }