2020-12-28

居上.發表於2020-12-28

JAVA迷宮開發

一、專案概述
實驗內容
1) 迷宮遊戲是非常經典的遊戲,在該題中要求隨機生成一個迷宮,並求解迷宮;
2) 要求查詢並理解迷宮生成的演算法,並嘗試用兩種不同的演算法來生成隨機的迷宮。
3)要求遊戲支援玩家走迷宮,和系統走迷宮路徑兩種模式。玩家走迷宮,通過鍵盤方向鍵控制,並在行走路徑上留下痕跡;系統走迷宮路徑要求基於 A演算法實現,輸出走迷宮的最優路徑並顯示。設計互動友好的遊戲圖形介面。
開發平臺:eclipse
二、專案設計
關鍵演算法分析
深度優先演算法:
1.將起點作為當前迷宮單元並標記為已訪問
2.當還存在未標記的迷宮單元,進行迴圈
3.如果當前迷宮單元有未被訪問過的的相鄰的迷宮單元
4.隨機選擇一個未訪問的相鄰迷宮單元
5.將當前迷宮單元入棧
6.移除當前迷宮單元與相鄰迷宮單元的牆
7.標記相鄰迷宮單元並用它作為當前迷宮單元
8.如果當前迷宮單元不存在未訪問的相鄰迷宮單元,並且棧不空
9.棧頂的迷宮單元出棧
10.令其成為當前迷宮單元
深度優先構建迷宮的思想就是,每次把新找到的未訪問迷宮單元作為優先,尋找其相鄰的未訪問過的迷宮單元,直到所有的單元都被訪問到。通俗的說,就是從起點開始隨機走,走不通了就返回上一步,從下一個能走的地方再開始隨機走。一般來說,深度優先法生成的迷宮極度扭曲,有著一條明顯的主路。
A 搜尋演算法:是一種在圖形平面上,有多個節點的路徑,求出最低通過成本的演算法。常用於遊戲中的NPC的移動計算,或網路遊戲的BOT的移動計算上。該演算法綜合了最良優先搜尋和Dijkstra演算法的優點:在進行啟發式搜尋提高演算法效率的同時,可以保證找到一條最優路徑。
在此演算法中,如果以 g ( n ) g(n)g(n) 表示從起點到當前頂點 n nn 的實際距離,h ( n ) h(n)h(n)表示當前頂點 n nn 到目標頂點的估算距離(根據所採用的評估函式的不同而變化),那麼A*演算法的估算函式為:
f(n)=g(n)+h(n)
f(n):代表了該節點的綜合預估值,值越小,到達目標的成本就越小,所以訪問的時候儘量優先考慮最小的。
這裡的h ( n )值可以採用歐幾里得距離、曼哈頓距離、切比雪夫距離等公式計算而來。
三、程式執行結果分析
主函式:
public class Maze {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	start();
}

public static void start(){
	Figure maze=new Figure();
	maze.init();
}

}
路徑探索:
public class Path {
//呼叫建立迷宮類
CreateMaze newMaze;
//儲存迷宮路徑
boolean[] path;
//儲存合格迷宮
Place[] maze=null;
int entrance;
int exit;
private int searchPathNumber(){
maze=newMaze.getMaze();
int pathAll=0;
//儲存當前路徑
Place [][] path=new Place [maze.length][];
for(int i=1;i<path.length;i++){
path [i] = new Place [5];
}
//當前路徑陣列下標
int pathTop=0;
//當前位置的下一位置的可能數下標
int [] top=new int [maze.length];
for(int i=1;i<top.length;i++){
top[i]=-1;
}
//尋找迷宮路徑數
if(maze[entrance].getWall()==0){
pathTop++;
top[pathTop]++;
path[pathTop][top[pathTop]]=maze[entrance];
while(pathTop>0){
//判斷當前位置是否為結束位置,是,儲存迷宮路徑,退回上一位置,否,尋找下一不重複位置
if(path[pathTop][0]==maze[exit]){
pathAll++;
top[pathTop]–;
pathTop–;
}else if(!path[pathTop][top[0]].isSearch()){//尋找當前位置的下一位置的可能數
if(path[pathTop][0].getEast()!=null&&path[pathTop][0].getEast()!=path[pathTop][0].getLast()&&!path[pathTop][0].getEast().isSearch()){
path[pathTop][++top[pathTop]]=path[pathTop][0].getEast();
}
if(path[pathTop][0].getSouth()!=null&&path[pathTop][0].getSouth()!=path[pathTop][0].getLast()&&!path[pathTop][0].getSouth().isSearch()){
path[pathTop][++top[pathTop]]=path[pathTop][0].getSouth();
}
if(path[pathTop][0].getWest()!=null&&path[pathTop][0].getWest()!=path[pathTop][0].getLast()&&!path[pathTop][0].getWest().isSearch()){
path[pathTop][++top[pathTop]]=path[pathTop][0].getWest();
}
if(path[pathTop][0].getNorth()!=null&&path[pathTop][0].getNorth()!=path[pathTop][0].getLast()&&!path[pathTop][0].getNorth().isSearch()){
path[pathTop][++top[pathTop]]=path[pathTop][0].getNorth();
}
path[pathTop][0].setSearch(true);
}//當前位置的下一位置的所有可能依次查詢,無下一位置則回退到上一位置
if(top[pathTop]==0){
path[pathTop][0].setLast(null);
path[pathTop][0].setSearch(false);
top[pathTop]–;
pathTop–;
}else{
pathTop++;
top[pathTop]++;
path[pathTop][0]=path[pathTop-1][top[pathTop-1]–];
path[pathTop][0].setLast(path[pathTop-1][0]);
}
}
}
return pathAll;
}

	private void setPath(){
		//儲存當前路徑
		Place [][] path=new Place [maze.length][];
		for(int i=1;i<path.length;i++){
			path [i] = new Place [5];
		}
		//當前路徑陣列下標
		int pathTop=0;
		//當前位置的下一位置的可能數下標
		int [] top=new int [maze.length];
		for(int i=1;i<top.length;i++){
			top[i]=-1;
		}
		//尋找迷宮路徑數
		if(maze[entrance].getWall()==0){
			pathTop++;
			top[pathTop]++;
			path[pathTop][top[pathTop]]=maze[entrance];
			while(pathTop>0){
				//判斷當前位置是否為結束位置,是,儲存迷宮路徑,退回上一位置,否,尋找下一不重複位置
				if(path[pathTop][0]==maze[exit]){
					for(int i=1;i<=pathTop;i++){
						this.path[path[i][0].getIndex()]=true;
					}
					top[pathTop]--;
					pathTop--;
					break;
				}else if(!path[pathTop][top[0]].isSearch()){//尋找當前位置的下一位置的可能數
					if(path[pathTop][0].getEast()!=null&&path[pathTop][0].getEast()!=path[pathTop][0].getLast()&&!path[pathTop][0].getEast().isSearch()){
						path[pathTop][++top[pathTop]]=path[pathTop][0].getEast();
					}
					if(path[pathTop][0].getSouth()!=null&&path[pathTop][0].getSouth()!=path[pathTop][0].getLast()&&!path[pathTop][0].getSouth().isSearch()){
						path[pathTop][++top[pathTop]]=path[pathTop][0].getSouth();
					}
					if(path[pathTop][0].getWest()!=null&&path[pathTop][0].getWest()!=path[pathTop][0].getLast()&&!path[pathTop][0].getWest().isSearch()){
						path[pathTop][++top[pathTop]]=path[pathTop][0].getWest();
					}					
					if(path[pathTop][0].getNorth()!=null&&path[pathTop][0].getNorth()!=path[pathTop][0].getLast()&&!path[pathTop][0].getNorth().isSearch()){
						path[pathTop][++top[pathTop]]=path[pathTop][0].getNorth();
					}
					path[pathTop][0].setSearch(true);
				}//當前位置的下一位置的所有可能依次查詢,無下一位置則回退到上一位置
				if(top[pathTop]==0){
					path[pathTop][0].setLast(null);
					path[pathTop][0].setSearch(false);
					top[pathTop]--;
					pathTop--;
				}else{
					pathTop++;
					top[pathTop]++;
					path[pathTop][0]=path[pathTop-1][top[pathTop-1]--];
					path[pathTop][0].setLast(path[pathTop-1][0]);
				}
			}
		}
	}
	
	private void searchPath(){
		while(true){
			if(searchPathNumber()==1){
				setPath();
				break;
			}
		}
	}
	
	public Path(){
		newMaze=new CreateMaze();
		path=new boolean [newMaze.getSize()*newMaze.getSize()+1];
		this.entrance=newMaze.getEntrance();
		this.exit=newMaze.getExit();
	}
	
	public Path(int size,int entrance,int exit){
		newMaze=new CreateMaze(size,entrance,exit);
		path=new boolean [newMaze.getSize()*newMaze.getSize()+1];
		this.entrance=newMaze.getEntrance();
		this.exit=newMaze.getExit();
	}
	
	public Place[] getMaze() {
		searchPath();
		return maze;
	}

	public int getSize(){
		return newMaze.getSize();
	}

	public int getEntrance() {
		return entrance;
	}

	public int getExit() {
		return exit;
	}

	public boolean[] getPath() {
		return path;
	}

	public CreateMaze getNewMaze() {
		return newMaze;
	}

}
格子資訊:
public class Place {
//定義當前格子是否可走,若wall為0,則表示可走;若wall為1,則表示不可走。
private int wall;
//表示當前格子是否被搜尋過。
private boolean search=false;
//表示當前格子的四個方向分別是哪些格子,搜尋時的上一個格子。
private Place east=null,south=null,west=null,north=null,last=null;
//儲存迷宮格子位置
private int index=0;
public Place(int wall){
this.wall=wall;
}
public int getWall() {
return wall;
}
public void setWall(int wall) {
this.wall = wall;
}
public boolean isSearch() {
return search;
}
public void setSearch(boolean search) {
this.search = search;
}
public Place getEast() {
return east;
}
public void setEast(Place east) {
this.east = east;
}
public Place getSouth() {
return south;
}
public void setSouth(Place south) {
this.south = south;
}
public Place getWest() {
return west;
}
public void setWest(Place west) {
this.west = west;
}
public Place getNorth() {
return north;
}
public void setNorth(Place north) {
this.north = north;
}
public Place getLast() {
return last;
}
public void setLast(Place last) {
this.last = last;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}

}
建立迷宮:
public class CreateMaze {
//定義迷宮規模
private int size;
//定義迷宮的入口和出口
private int entrance,exit;
//用一維陣列表示迷宮,0號下標位置空出
private Place[] maze=null;
//設定迷宮中每一個格子的方向
private void setDirections(Place[] maze){
for(int i=1;i<=sizesize;i++){
if(i%size!=0&&maze[i+1].getWall()==0&&maze[i+1]!=null){
maze[i].setEast(maze[i+1]);
}
if(i<=size
(size-1)&&maze[i+size].getWall()==0&&maze[i+size]!=null){
maze[i].setSouth(maze[i+size]);
}
if(i%size!=1&&maze[i-1].getWall()==0&&maze[i-1]!=null){
maze[i].setWest(maze[i-1]);
}
if(i>size&&maze[i-size].getWall()==0&&maze[i-size]!=null){
maze[i].setNorth(maze[i-size]);
}
}
}

	public CreateMaze(){
		this.size=10;
		this.entrance=1;
		this.exit=this.size*this.size;
	}
	
	public CreateMaze(int size,int entrance,int exit){
		this.size=size;
		this.entrance=entrance;
		this.exit=exit;
	}
	
	public Place[] getMaze() {
		maze=new Place[size*size+1];
		for(int i=1;i<=size*size;i++){
			maze[i]=new Place((int)(Math.random()*2));
			maze[i].setIndex(i);
		}
		setDirections(maze);
		return maze;
	}
	public int getEntrance() {
		return entrance;
	}
	public void setEntrance(int entrance) {
		this.entrance = entrance;
	}
	public int getExit() {
		return exit;
	}
	public void setExit(int exit) {
		this.exit = exit;
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}

}
繪畫介面:
public class Figure {
// 定義路徑
Path path;
// 定義當前迷宮周圍狀態
Place[] maze = null;
// 用按鈕實現迷宮格
JButton[] button = null;
boolean[] isPath = null;

// A* 尋路介面  比較遺憾的是隻實現了圖形化介面,A* 自動尋路並未實現,有機會有能力再更吧
class FindMaze extends JFrame implements ActionListener {
    public FindMaze() {
        super("A* Maze");
        // 介面大小
        this.setSize(500, 500);
        // 返回預設工具箱
        Toolkit kit = Toolkit.getDefaultToolkit();
        // 獲取螢幕尺寸
        Dimension screenSize = kit.getScreenSize();
        // 獲取螢幕寬度
        int screenWidth = screenSize.width;
        // 獲取螢幕高度
        int screenHeight = screenSize.height;
        // 獲取介面視窗寬度
        int windowWidth = this.getWidth();
        // 獲取介面視窗高度
        int windowHeight = this.getHeight();
        // 介面居中
        this.setLocation((screenWidth - windowWidth) / 2, (screenHeight - windowHeight) / 2);
        // 四行一列布局
        this.setLayout(new GridLayout(path.getSize(), path.getSize()));
        maze = path.getMaze();
        int entrance = path.getEntrance();
        int exit = path.getExit();
        button = new JButton[maze.length];
        for (int i = 1; i < maze.length; ++i) {
            // 當前格子是路則設定活動指令為 1,背景顏色為綠色
            if (maze[i].getWall() == 0) {
                button[i] = new JButton();
                button[i].setActionCommand("1");
                button[i].setBackground(Color.GREEN);
            }
            // 當前格子為牆則設定活動指令為 0,標記為灰色
            if (maze[i].getWall() == 1) {
                button[i] = new JButton();
                button[i].setActionCommand("0");
                button[i].setBackground(Color.LIGHT_GRAY);
            }
        }
        for (int i = 1; i < button.length; i++) {
            button[i].addActionListener(this);
            add(button[i]);
        }
        button[path.getSize() * path.getSize()].setActionCommand("退出");
        button[path.getSize() * path.getSize()].setText("退出");
        button[path.getSize() * path.getSize()].setFont(new Font("宋體", 0, 7));
        addWindowListener(new closeWin());
        this.setVisible(true);
    }
	// 自動尋路
    public void innit() {
        isPath = path.getPath();
        for (int i = 1; i < isPath.length; i++) {
            if (isPath[i]) {
                button[i].setBackground(Color.YELLOW);
            }
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("退出")) {
            dispose();
            Figure figure = new Figure();
            figure.init();
        }
    }
}

// 啟動遊戲介面
class MazeGameFigure extends JFrame implements ActionListener {
    public MazeGameFigure() {
        super("迷宮遊戲");
    }

    public void init() {
        // 介面大小
        this.setSize(500, 500);
        // 返回預設工具箱
        Toolkit kit = Toolkit.getDefaultToolkit();
        // 獲取螢幕尺寸
        Dimension screenSize = kit.getScreenSize();
        // 獲取螢幕寬度
        int screenWidth = screenSize.width;
        // 獲取螢幕高度
        int screenHeight = screenSize.height;
        // 獲取介面視窗寬度
        int windowWidth = this.getWidth();
        // 獲取介面視窗高度
        int windowHeight = this.getHeight();
        // 介面居中
        this.setLocation((screenWidth - windowWidth) / 2, (screenHeight - windowHeight) / 2);
        // 四行一列布局
        this.setLayout(new GridLayout(5, 1));
        JLabel welcom = new JLabel("歡迎進入迷宮遊戲!");
        welcom.setBackground(Color.CYAN);
        welcom.setFont(new Font("宋體", 1, 25));
        JButton find = new JButton("A* 尋路");
        find.setFont(new Font("宋體", 1, 25));
        JButton start = new JButton("開始遊戲");
        start.setFont(new Font("宋體", 1, 25));
        JButton set = new JButton("遊戲設定");
        set.setFont(new Font("宋體", 1, 25));
        JButton end = new JButton("退出遊戲");
        end.setFont(new Font("宋體", 1, 25));
        find.setBackground(Color.PINK);
        start.setBackground(Color.PINK);
        set.setBackground(Color.PINK);
        end.setBackground(Color.PINK);
        add(welcom);
        add(find);
        add(start);
        add(set);
        add(end);
        find.addActionListener(this);
        start.addActionListener(this);
        set.addActionListener(this);
        end.addActionListener(this);
        addWindowListener(new closeWin());
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("A* 尋路")) {
            dispose();
            new FindMaze().innit();
        }
        // 點選開始遊戲生成迷宮
        if (e.getActionCommand().equals("開始遊戲")) {
            MazeFigure mazeFigure = new MazeFigure();
            mazeFigure.init();
            dispose();
        }
        // 點選遊戲設定進入設定模式
        if (e.getActionCommand().equals("遊戲設定")) {
            MazeSetFigure mazeSetFigure = new MazeSetFigure();
            mazeSetFigure.init();
            dispose();
        }
        if (e.getActionCommand().equals("退出遊戲")) {
            dispose();
        }
    }
}

// 開始遊戲介面
class MazeFigure extends JFrame implements ActionListener {
    public MazeFigure() {
        super("Maze");
    }

    public void init() {
        this.setSize(500, 500);
        this.setBackground(Color.BLACK);
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenWidth = screenSize.width;
        int screenHeight = screenSize.height;
        int windowWidth = this.getWidth();
        int windowHeight = this.getHeight();
        this.setLocation((screenWidth - windowWidth) / 2, (screenHeight - windowHeight) / 2);
        // 獲取迷宮尺寸設計按鈕佈局
        this.setLayout(new GridLayout(path.getSize(), path.getSize()));
        maze = path.getMaze();
        int entrance = path.getEntrance();
        int exit = path.getExit();
        button = new JButton[maze.length];
        for (int i = 1; i < maze.length; i++) {
            // 當前格子是路則設定活動指令為 1,背景顏色為綠色
            if (maze[i].getWall() == 0) {
                button[i] = new JButton();
                button[i].setActionCommand("1");
                button[i].setBackground(Color.GREEN);
            }
            // 當前格子為牆則設定活動指令為 0,標記為灰色
            if (maze[i].getWall() == 1) {
                button[i] = new JButton();
                button[i].setActionCommand("0");
                button[i].setBackground(Color.LIGHT_GRAY);
            }
        }
        button[entrance].setText("入口");
        button[entrance].setFont(new Font("宋體", 1, 7));
        button[exit].setText("出口");
        button[exit].setFont(new Font("宋體", 1, 7));
        // 為每個按鈕新增監聽器
        for (int i = 1; i < button.length; i++) {
            button[i].addActionListener(this);
            add(button[i]);
        }
        addWindowListener(new closeWin());
        this.setVisible(true);
    }

    // 判斷是否完成通路
    private boolean isComplete() {
        isPath = path.getPath();
        for (int i = 1; i < isPath.length; i++) {
            if (isPath[i] && button[i].getBackground() != Color.YELLOW) {
                return false;
            }
        }
        return true;
    }

    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton) e.getSource();
        if (button.getActionCommand().equals("1")) {
            if (button.getBackground() == Color.GREEN) {
                button.setBackground(Color.YELLOW);
            } else if (button.getBackground() == Color.YELLOW) {
                button.setBackground(Color.GREEN);
            }
        }
        if (isComplete()) {
            CongratulationFigure congratulationFigure = new CongratulationFigure();
            congratulationFigure.init();
            this.dispose();
        }
    }
}

// 迷宮設定介面
class MazeSetFigure extends Frame implements ActionListener, TextListener {
    String newSize, newEntrance, newExit;
    JTextField setMaze, setEntrance, setExit;
    int size, entrance, exit;

    public MazeSetFigure() {
        super("迷宮設定");
    }

    public void init() {
        this.setSize(500, 400);
        this.setBackground(Color.WHITE);
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenWidth = screenSize.width;
        int screenHeight = screenSize.height;
        int windowWidth = this.getWidth();
        int windowHeight = this.getHeight();
        this.setLocation((screenWidth - windowWidth) / 2, (screenHeight - windowHeight) / 2);
        GridLayout layout = new GridLayout(5, 2);
        this.setLayout(layout);
        JLabel size = new JLabel("迷宮規模");
        size.setFont(new Font("宋體", 1, 20));
        JLabel entrance = new JLabel("迷宮入口");
        entrance.setFont(new Font("宋體", 1, 20));
        JLabel exit = new JLabel("迷宮出口");
        exit.setFont(new Font("宋體", 1, 20));
        JButton menu = new JButton("返回選單");
        menu.setFont(new Font("宋體", 1, 20));
        JButton set = new JButton("設定完成");
        set.setFont(new Font("宋體", 1, 20));
        setMaze = new JTextField("10");
        setEntrance = new JTextField("左上角");
        setEntrance.setFont(new Font("宋體", 1, 18));
        setExit = new JTextField("右下角");
        setExit.setFont(new Font("宋體", 1, 18));
        JLabel tip = new JLabel("tips:出入口只能設定為四個角");
        JLabel tips = new JLabel("即左上角,右上角,左下角,右下角");
        add(tip);
        add(tips);
        add(size);
        add(setMaze);
        add(entrance);
        add(setEntrance);
        add(exit);
        add(setExit);
        add(menu);
        add(set);
        menu.addActionListener(this);
        set.addActionListener(this);
        setMaze.addActionListener(this);
        setEntrance.addActionListener(this);
        setExit.addActionListener(this);
        addWindowListener(new closeWin());
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("返回選單")) {
            dispose();
            Figure figure = new Figure();
            figure.init();
        }
        if (e.getActionCommand().equals("設定完成")) {
            boolean isSizeReasonable = true;
            boolean isEntranceReasonable = true;
            boolean isExitReasonable = true;
            newSize = setMaze.getText();
            newEntrance = setEntrance.getText();
            newExit = setExit.getText();
            try {
                size = Integer.parseInt(newSize);
            } catch (Exception ex) {
                isSizeReasonable = false;
            }
            if (isSizeReasonable == true) {
                if (newEntrance.equals("左上角")) {
                    entrance = 1;
                } else if (newEntrance.equals("右上角")) {
                    entrance = size;
                } else if (newEntrance.equals("左下角")) {
                    entrance = size * (size - 1) + 1;
                } else if (newEntrance.equals("右下角")) {
                    entrance = size * size;
                } else {
                    isEntranceReasonable = false;
                }

                if (newExit.equals("左上角")) {
                    exit = 1;
                } else if (newExit.equals("右上角")) {
                    exit = size;
                } else if (newExit.equals("左下角")) {
                    exit = size * (size - 1) + 1;
                } else if (newExit.equals("右下角")) {
                    exit = size * size;
                } else {
                    isExitReasonable = false;
                }

                if (isEntranceReasonable == true && isExitReasonable == true) {
                    if (entrance == exit) {
                        isEntranceReasonable = false;
                        isExitReasonable = false;
                    }
                }
            }
            if (isSizeReasonable == true && isEntranceReasonable == true && isExitReasonable == true) {
                dispose();
                Figure figure = new Figure(size, entrance, exit);
                figure.init();
            } else {
                SetErrorFigure setErrorFigure = new SetErrorFigure();
                setErrorFigure.init();
                dispose();
            }
        }
    }

    public void textValueChanged(TextEvent e) {

    }
}

// 通過迷宮遊戲介面
class CongratulationFigure extends Frame implements ActionListener {
    public CongratulationFigure() {
        super("恭喜");
    }

    public void init() {
        this.setSize(220, 200);
        this.setBackground(Color.WHITE);
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenWidth = screenSize.width;
        int screenHeight = screenSize.height;
        int windowWidth = this.getWidth();
        int windowHeight = this.getHeight();
        this.setLocation((screenWidth - windowWidth) / 2, (screenHeight - windowHeight) / 2);
        this.setLayout(new GridLayout(2, 1));
        JLabel text = new JLabel("恭喜您成功走出迷宮!");
        JButton button = new JButton("確認");
        button.setBackground(Color.WHITE);
        add(text);
        add(button);
        button.addActionListener(this);
        addWindowListener(new closeWin());
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("確認")) {
            dispose();
            Figure figure = new Figure();
            figure.init();
        }
    }
}

// 遊戲設定資料錯誤介面
class SetErrorFigure extends Frame implements ActionListener {
    public SetErrorFigure() {
        super("錯誤");
    }

    public void init() {
        this.setSize(230, 100);
        this.setBackground(Color.WHITE);
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenWidth = screenSize.width;
        int screenHeight = screenSize.height;
        int windowWidth = this.getWidth();
        int windowHeight = this.getHeight();
        this.setLocation((screenWidth - windowWidth) / 2, (screenHeight - windowHeight) / 2);
        this.setLayout(new GridLayout(2, 1));
        JLabel text = new JLabel("您輸入的資料不合理,設定失敗!");
        JButton button = new JButton("確認");
        button.setBackground(Color.WHITE);
        add(text);
        add(button);
        button.addActionListener(this);
        addWindowListener(new closeWin());
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("確認")) {
            dispose();
            Figure figure = new Figure();
            figure.init();
        }
    }
}

class closeWin extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
        Window w = e.getWindow();
        w.dispose();
    }
}

public Figure() {
    path = new Path();
}

public Figure(int size, int entrance, int exit) {
    path = new Path(size, entrance, exit);
}

public void init() {
    MazeGameFigure mazeGameFigure = new MazeGameFigure();
    mazeGameFigure.init();
}

}
四、結果圖在這裡插入圖片描述