魔域

首先,项目从一个小小游戏,石头剪子布开始。
为玩家提供“内幕”,提供一个玩家按照一定顺序玩耍游戏,能够百分百获胜。
电脑的猜拳是由随机数生成器所控制,在0-2之间随机选择,在根据这个随机数,
在字符串数组中选择一个对应的手势猜拳。
// 给电脑随机生成一个手势
randomGenerator.setSeed(seed[index++]);
int index = randomGenerator.nextInt(0, 2); // 产生一个0-2之间的随机数
String computerShape = SHAPES[index]; // 根据此随机数从数组中选择一个手势

那么要如何给玩家提供百分百赢呢,其实随机数都有另一个相对应的数,应该叫做为随机数指定种子?
如何找到这个对应的数呢?有很多方法,我用的是最直观的,就是直接在一个较大的指定范围内,输出随机数生成器0-2相对应的数,
RandomGenerator randomGenerator=RandomGenerator.getInstance();

for(int i=-10000;i<=10000;i++) {
randomGenerator.setSeed(i);
int num=randomGenerator.nextInt(0,2);

if(num==0) {
//System.out.println("0: "+i);
}
else if(num==1) {
//System.out.println("1: "+i);
}
else if(num==2) {
System.out.println("2: "+i);
}
}

再将其对应上即可。
private final long[] seed=new long[]{2000l,0l,8500l,2000l,2000l,8500l,0l}; 将其递增就可对应表中的顺序,使玩家立于不败之地。
石头
剪刀
石头
石头 当然,如果你不按照这个表进行猜拳,那么也就无法保证利于不败之地,会进入随机。
剪刀
退出


文字游戏。
简单介绍一下这个小游戏,开局一个人,你可以去很多地方,搜索东西,还可以存储一下数据。
像我们电视剧那样,仙侠游戏里的宝物装备,往往伴随着一些敌人,需要打败敌人夺取宝物。
("东, 西, 南, 北。 ");
("查看行李, 存档, 搜索。");
("使用道具,请在道具名字前面加上使用。");
("攻击, 补血, 查看状态。");
(" 退出。");
游戏的所有命令

游戏中BUG级武器,得到便可纵横此游戏
public String FTHJ(){
if (props.size() == 0){
return (String.format("你的道具为空"));
}else{
for (int i = 0; i < props.size(); i++) {
if (props.get(i).equals("方天画戟"));
int GJL = 100;
player.maxAtt += GJL;
}
}
return (String.format("恭喜你解锁本游戏最强大的武器,攻击力增加加100"));
}

加上道具

// 读取道具
private void loadProps(Scanner scanner) {
// 先确定有多少道具
scanner.nextLine();
int props = scanner.nextInt();
scanner.next();

// 遍历表,把表里道具读出来。
for (int i = 0; i < props; i++) {
int propnumber = scanner.nextInt();
String propname = scanner.next();

// 遍历地点数组,从数组中拿出顺序循环的地点信息,存放到一个新的place中
// 判断地点编号是否与道具编号一致,一致则将道具遍历到的道具名字放进place中

for (int j = 0; j < places.size(); j++) {
Place place = places.get(j);
if (place.getNumber() == propnumber){
place.addProp(propname);
break;
}

}

}

case "查看行李":
if (props.size() == 0) {
println("你的行李箱内空无一物。");
} else {
println(String.format("你现在共有%d个道具,依次是:", props.size()));
for (int i = 0; i < props.size(); i++) {
println(String.format("%d.%s", i + 1, props.get(i)));
}
}
break;


case "搜索":
if (currPlace.getProps().size() == 0) {
println("DEBUG2 - 你什么没找到。");
} else {
for (int i = 0; i < currPlace.getProps().size(); i++) {
String prop = currPlace.getProps().get(i);
println(String.format("DEBUG2 - 你找到了一个%s!", prop));
props.add(prop);
playScene("");
}
currPlace.clearProp();
}

使用自定义道具

public String FTHJ() {
if (props.size() == 0) {
return (String.format("你的道具为空"));
} else {
for (int i = 0; i < props.size(); i++) {
if (props.get(i).equals("方天画戟")) ;
int GJL = 100;
player.maxAtt += GJL;
}
}
return (String.format("恭喜你解锁本游戏最强大的武器,攻击力增加加100"));
}


//读取表中的道路
private void loadRoutes(Scanner scanner) {
scanner.nextLine();
int nRoutes = scanner.nextInt();
scanner.next();

for (int i = 0; i < nRoutes; i++) {
int a = scanner.nextInt();
String direction = scanner.next();
int b = scanner.nextInt();


Place placeA = null;
Place placeB = null;

// 遍历数组places,找到和a编号相等的那个元素,保存进变量placeA中
for (int j = 0; j < places.size(); j++) {
if (a == places.get(j).getNumber()) {
placeA = places.get(j);
}

}
// for(Place ii : places){
// if (a == ii.getNumber()){
// placeA = ii;
// }
// }

// 遍历数组places,找到和b编号相等的那个元素,保存进变量placeB中
for (Place xx : places) { //xx会等于遍历后的places元素
if (b == xx.getNumber()) {
placeB = xx;
}
}

switch (direction) {
case "东":
placeA.setEast(placeB);
placeB.setWest(placeA);
break;
case "西":
placeA.setWest(placeB);
placeB.setEast(placeA);
break;
case "南":
placeA.setSouth(placeB);
placeB.setNorth(placeA);
break;
case "北":
placeA.setNorth(placeB);
placeB.setSouth(placeA);
break;

}
}
}

用构造方法实现,也应用了super
//名字和角色
public Enemy(int level){
super(null,null,level);
int index;
// 设置下标的随机数
index = randomGenerator.nextInt(0,ENEMY_NAMES.length-1);
name = ENEMY_NAMES[index];

index = randomGenerator.nextInt(0,ENEMY_ROLES.length-1);
role = ENEMY_ROLES[index];
}

玩家在搜索的时候会被攻击,类似于怪物守护道具,想得到就得进行抢夺。
case "搜索":
if (currPlace.getProps().size() == 0) {
println("DEBUG2 - 你什么没找到。");
} else {
for (int i = 0; i < currPlace.getProps().size(); i++) {
String prop = currPlace.getProps().get(i);
println(String.format("DEBUG2 - 你找到了一个%s!", prop));
props.add(prop);
playScene("");
}
currPlace.clearProp();
}


游戏的开始便是输入玩家的名字,进入游戏后选择各种角色,查看攻略,去到游戏中任意地点,然后进行搜索或者或者不搜索
游戏中有一个地方是一定会被攻击,其他都是只有找到道具的时候,怪物才会攻击你。
public void run() {
print("请输入你的名字:");
String playerName = readLine();
boolean exists = existPlayer(playerName);
if (exists) {
// 恢复数据
String filepath = Paths.get("res", playerName).toString();
player = SaveData.readObject(filepath);

filepath = Paths.get("res", "place").toString();
currPlace = SaveData.readObject(filepath);
isNewPlayer = false;
} else {
println();
println(play.story());
println();
String playerRole = choose("请选择角色", "莽夫", "混混", "九品芝麻官");
println("");
player = new Player(playerName, playerRole);
}

String userChoice = choose("请选择" + " " + "新手推荐查看攻略", "查看攻略", "我已知晓");
if (userChoice.equals("查看攻略")) {
println("☺☺ 暂无攻略,你被骗了!☺☺\n"
+ "请从新开始游戏");
}
println();
if (loadGame()) {
mainLoop();
}
}
posted @ 2021-02-09 12:37  Ga7l  阅读(102)  评论(0)    收藏  举报