软件实习2020-12-11

今天下午是软件实习,今天的目标是完成一个非常简单的迷宫小游戏,结果也是有些成功的😊
此程序绝大部分(占到了90%)为原创,像第二个实验相似,利用java的分类的优点,把总程序分为两个类,分别是Map类和Person类,分别表示迷宫的地图和游戏角色如何走出迷宫。
很遗憾,迷宫地图并不是随机生成的:在这里插入图片描述
源代码:Map类
package maze;

public class Map {

public static char[][] array = { { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
		{ '#', '@', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
		{ '#', ' ', '#', '#', '#', '#', '#', ' ', '#', ' ', '#' },
		{ '#', ' ', ' ', ' ', '#', '#', ' ', ' ', '#', ' ', '#' },
		{ '#', '#', '#', ' ', '#', ' ', ' ', '#', '#', '#', '#' },
		{ '#', '#', ' ', ' ', '#', '#', '#', '#', ' ', ' ', ' ' },
		{ '#', '#', ' ', '#', '#', '#', '#', '#', ' ', '#', '#' },
		{ '#', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', '#' },
		{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }

};

Map() {
	array = array;
}

Map(char[][] array) {
	this.array = array;
}

public static void print() {
	for (int i = 0; i < array.length; i++) {
		for (int j = 0; j < array[i].length; j++) {
			System.out.print(array[i][j]);
		} // 输出数组(迷宫)的第一行
		System.out.println();// 第一行输出完毕后换行
	}
}

}

创建一个数组array,利用“#”当作墙,空白处即可行走。
输出数组时利用两个for循环完成输出,需要注意的一点是完成第一行的后要换行!
第二个类即为Person类,包括了键盘输入内容的获取、人物角色的走向控制和构造方法及方法的运用等内容。
源代码:Person类
package maze;

import java.util.Scanner;

public class Person extends Map {
private int x;
private int y;
private int ex;
private int ey;

Person() {
	x = 1;
	y = 1;// array[1][1]代表人物的初始位置
	ex = 5;
	ey = 10;// array[5][10]代表出口位置
}

Person(int x, int y, int ex, int ey) {
	this.x = x;
	this.y = y;
	this.ex = ex;
	this.ey = ey;
}

public int getX() {
	return x;
}

public int getY() {
	return y;
}

public int getEX() {
	return ex;
}

public int getEY() {
	return ey;
}

public static void main(String[] args) {
	Person person1 = new Person();
	while (true) {
		person1.print();
		Scanner input = new Scanner(System.in);// 创建一个键盘接收器
		String accept = input.nextLine();// 接受键盘输入的内容

		switch (accept) {
		case "w":
			if (array[person1.getX() - 1][person1.getY()] != '#') {
				array[person1.getX()][person1.getY()] = ' ';
				array[person1.getX() - 1][person1.getY()] = '@';
				person1.x = person1.getX() - 1;
			}
			break;
		case "s":
			if (array[person1.getX() + 1][person1.getY()] != '#') {
				array[person1.getX()][person1.getY()] = ' ';
				array[person1.getX() + 1][person1.getY()] = '@';
				person1.x = person1.getX() + 1;
			}
			break;
		case "a":
			if (array[person1.getX()][person1.getY() - 1] != '#') {
				array[person1.getX()][person1.getY()] = ' ';
				array[person1.getX()][person1.getY() - 1] = '@';
				person1.y = person1.getY() - 1;
			}
			break;
		case "d":
			if (array[person1.getX()][person1.getY() + 1] != '#') {
				array[person1.getX()][person1.getY()] = ' ';
				array[person1.getX()][person1.getY() + 1] = '@';
				person1.y = person1.getY() + 1;
			}
			break;
		default:
			System.out.println();
			break;
		}
		if (person1.getX() == person1.getEX() && person1.getY() == person1.getEY()) {
			System.out.println("恭喜你赢了!!!");
		}

	}
}

}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
由于并没有使用窗口使程序可视化,所以程序采用的是走一步显示一次运行结果。
下一步准备对程序加以改善,争取实现程序的可视化,使其能够像第二个实验一样有一个窗口,通过键盘控制人物角色的走向!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值