迷宫游戏(图形化界面)

迷宫游戏

本程序的功能为实现迷宫游戏。打开游戏,系统弹出游戏菜单界面。玩家可以选择开始游戏,游戏设置,退出游戏。玩家选择开始游戏时,系统自动生成一个规格为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();
	}
	
}

部分运行结果:
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 19
    点赞
  • 99
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值