迷宫(骑士救公主)

环境: Notepad++

分析:使用cmd命令窗口模拟出一个迷宫,效果图如下:

        1,A代表影响,可上下左右移动

        2,星星代表墙壁,英雄无法穿越

        3,WSAD,分别代表上下左右

        4,英雄移动到右下角,就代表移动成功

        5,步数记录走了多少步

代码:

/**
	迷宫
*/
import java.util.Scanner;
import java.io.IOException;

class Maze4{
	static int tempi = 0;
	static int tempj = 0;
	static int[][] m = {
		{2,1,1,1,1,1},
		{0,0,0,0,0,1},
		{1,1,0,1,1,1},
		{1,1,0,0,0,1},
		{1,0,0,1,0,3},
		{1,1,1,1,1,1}
	};
	public static void cls() throws IOException, InterruptedException{
		new ProcessBuilder("cmd", "/c", "cls")
		.inheritIO()
		.start()
		.waitFor();
	}
	
	public static void getMap(){
		//输出迷宫
		for(int i = 0; i < m.length ; i++){
			for(int j = 0; j < m[i].length; j++){
				if(m[i][j]==1){
					System.out.print(" *");
				}else if(m[i][j]==0){
					System.out.print("  ");
				}else if(m[i][j]==3){
					System.out.print(" O");
				}else{
					System.out.print(" A");
					tempi = i;
					tempj = j;
				}
			}
			System.out.println();
		}
	}

	public static void main(String[] args) throws IOException,InterruptedException{
		int step = 0;
		System.out.println("\n请通过输入“w”“s”“a”“d”,控制A走出迷宫(走到O)");
		getMap();
		while(true){
			//行走
			Scanner sc = new Scanner(System.in);
			String walk = sc.nextLine();
			step = step+1;
			if (walk.equals("w")) {
				cls();
				System.out.println("请通过输入“w”“s”“a”“d”,控制A走出迷宫(走到O)");
				if(m[tempi-1][tempj] == 1){
					System.out.println("这是墙,不能通过,请重新输入");
				}else{
					System.out.println();
					m[tempi][tempj] = 0;
					m[tempi-1][tempj] = 2;
				}
				getMap();
			}else if (walk.equals("s")) {
				cls();
				System.out.println("请通过输入“w”“s”“a”“d”,控制A走出迷宫(走到O)");
				if(m[tempi+1][tempj] == 1){
					System.out.println("这是墙,不能通过,请重新输入");
				}else{
					System.out.println();
					m[tempi][tempj] = 0;
					m[tempi+1][tempj] = 2;
				}
				getMap();
			}else if (walk.equals("a")) {
				cls();
				System.out.println("请通过输入“w”“s”“a”“d”,控制A走出迷宫(走到O)");
				if(m[tempi][tempj-1] == 1){
					System.out.println("这是墙,不能通过,请重新输入");
				}else{
					System.out.println();
					m[tempi][tempj] = 0;
					m[tempi][tempj-1] = 2;
				}
				getMap();
			}else if (walk.equals("d")) {
				cls();
				System.out.println("请通过输入“w”“s”“a”“d”,控制A走出迷宫(走到O)");
				if(m[tempi][tempj+1] == 1){
					System.out.println("这是墙,不能通过,请重新输入");
				}else if(m[tempi][tempj+1] == 3){
					System.out.println("恭喜您走出迷宫,您走出迷宫用了"+step+"步");
					m[tempi][tempj] = 0;
					m[tempi][tempj+1] = 2;
				}else{
					System.out.println();
					m[tempi][tempj] = 0;
					m[tempi][tempj+1] = 2;
				}
				getMap();
			}else{
				System.out.println("输入错误,请输入“w”“s”“a”“d”,控制A走出迷宫(走到O)");
			}	
		}
	}
}

代码运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个生成迷宫的 Python 代码示例: ```python import random def generate_maze(width, height): # 创建一个空白迷宫 maze = [['#' for x in range(width)] for y in range(height)] # 设置起始点和终点 start_x, start_y = random.randint(0, width - 1), random.randint(0, height - 1) maze[start_y][start_x] = 'S' end_x, end_y = random.randint(0, width - 1), random.randint(0, height - 1) maze[end_y][end_x] = 'E' # 随机生成迷宫路径 x, y = start_x, start_y while x != end_x or y != end_y: directions = ['N', 'S', 'W', 'E'] random.shuffle(directions) for direction in directions: if direction == 'N': if y > 0 and maze[y - 1][x] == '#': maze[y - 1][x] = ' ' y -= 1 break elif direction == 'S': if y < height - 1 and maze[y + 1][x] == '#': maze[y + 1][x] = ' ' y += 1 break elif direction == 'W': if x > 0 and maze[y][x - 1] == '#': maze[y][x - 1] = ' ' x -= 1 break elif direction == 'E': if x < width - 1 and maze[y][x + 1] == '#': maze[y][x + 1] = ' ' x += 1 break # 返回生成的迷宫 return maze # 示例:生成一个 10 行 10 列的迷宫 maze = generate_maze(10, 10) for row in maze: print(' '.join(row)) ``` 输出结果: ``` # # # # # # # # # # # S # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # E ``` 这是一个简单的随机生成迷宫的方法,代码中使用 `#` 表示障碍物,使用空格表示可行走的路径。你可以根据需要修改代码来自定义生成迷宫的形状和大小。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值