迷宮遊戲(圖形化介面)
阿新 • • 發佈:2018-12-17
迷宮遊戲
本程式的功能為實現迷宮遊戲。開啟遊戲,系統彈出遊戲選單介面。玩家可以選擇開始遊戲,遊戲設定,退出遊戲。玩家選擇開始遊戲時,系統自動生成一個規格為10*10,入口為左上角,出口為右下角且從入口到出口僅有一條有效路徑的迷宮,當玩家找到路徑後,系統會自動提示玩家已經成功走出迷宮,並重新開啟遊戲選單介面。玩家選擇遊戲設定時,可從鍵盤輸入迷宮規格,迷宮入口,迷宮出口,若玩家放棄設定,可點選返回選單按鈕返回選單,若玩家要儲存設定,則可點選設定完成按鈕,系統會自動檢查玩家輸入資料是否合理,若合理,則設定成功,返回遊戲選單,若不合理,則系統會提示設定錯誤,設定失敗。 需要注意的是,玩家設定只有設定完後第一次遊戲是有效的,第二次開始則恢復系統預設設定。
/**
*
* @author DELL
* 迷宮遊戲
* 本類提供開啟迷宮遊戲的方法。
*
*/
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();
}
}
import java.awt.*; import java.awt.event.*; /** * * @author DELL * 迷宮遊戲 * 本類儲存迷宮中每一個格子的資訊。 * */ 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; } }
/** * * @author DELL * 迷宮遊戲 * 本類中儲存迷宮的相關引數,並提供方法建立迷宮。 * */ 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<=size*size;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; } }
import java.awt.Color;
/**
*
* @author DELL
* 迷宮遊戲
* 本類中對迷宮進行路徑搜尋,儲存合格迷宮的相關資訊(合格迷宮只有1條路徑)。
*
*/
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;
}
}
import java.awt.*;
import java.awt.event.*;
/**
*
* @author DELL
* 迷宮遊戲
* 本類為迷宮遊戲提供圖形化介面。
*
*/
public class Figure {
Path path;
Place[] maze=null;
Button[] button=null;
boolean[] isPath=null;
class MazeGameFigure extends Frame implements ActionListener{
public MazeGameFigure(){
super("迷宮遊戲");
}
public void init(){
this.setSize(250, 250);
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(4,1));
Label welcom=new Label("歡迎進入迷宮遊戲!");
Button start=new Button("開始遊戲");
Button set=new Button("遊戲設定");
Button end=new Button("退出遊戲");
start.setBackground(Color.WHITE);
set.setBackground(Color.WHITE);
end.setBackground(Color.WHITE);
add(welcom);
add(start);
add(set);
add(end);
start.addActionListener(this);
set.addActionListener(this);
end.addActionListener(this);
addWindowListener(new closeWin());
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
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 Frame implements ActionListener{
public MazeFigure(){
super("迷宮");
}
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 Button[maze.length];
for(int i=1;i<maze.length;i++){
if(maze[i].getWall()==0){
button[i]=new Button("");
button[i].setActionCommand("路");
button[i].setBackground(Color.WHITE);
}
if(maze[i].getWall()==1){
button[i]=new Button("牆");
button[i].setActionCommand("牆");
button[i].setBackground(Color.LIGHT_GRAY);
}
}
button[entrance].setLabel("入口");
button[exit].setLabel("出口");
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.RED){
return false;
}
}
return true;
}
public void actionPerformed(ActionEvent e){
Button button=(Button)e.getSource();
if(button.getActionCommand().equals("路")){
if(button.getBackground()==Color.WHITE){
button.setBackground(Color.RED);
}else if(button.getBackground()==Color.RED){
button.setBackground(Color.WHITE);
}
}
if(isComplete()){
CongratulationFigure congratulationFigure=new CongratulationFigure();
congratulationFigure.init();
this.dispose();
}
}
}
class MazeSetFigure extends Frame implements ActionListener ,TextListener{
String newSize,newEntrance,newExit;
TextField setMaze,setEntrance,setExit;
int size,entrance,exit;
public MazeSetFigure(){
super("迷宮設定");
}
public void init(){
this.setSize(250, 150);
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(4,2);
this.setLayout(layout);
Label size=new Label("迷宮規模");
Label entrance=new Label("迷宮入口");
Label exit=new Label("迷宮出口");
Button menu=new Button("返回選單");
Button set=new Button("設定完成");
setMaze= new TextField("10");
setEntrance= new TextField("左上角");
setExit= new TextField("右下角");
add(size);
add(setMaze);
add(entrance);
add(setEntrance);
add(exit);
add(setExit);
add(menu);
add(set);
menu.addActionListener(this);
set.addActionListener(this);
setMaze.addTextListener(this);
setEntrance.addTextListener(this);
setExit.addTextListener(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, 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));
Label text=new Label("恭喜您成功走出迷宮!");
Button button=new Button("確認");
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));
Label text=new Label("您輸入的資料不合理,設定失敗!");
Button button=new Button("確認");
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();
}
}
部分執行結果: