具体代码:
Player.java
package 字符串游戏;
import java.util.Scanner;
public class Player {
private int levelNo = 0;// 玩家当前级别号
private int curScore = 0;// 玩家当前级别积分
private long startTime = 0;// 当前级别开始时间
private int clapsedTime = 0;// 当前级别已用时间
public int getLevelNo() {
return levelNo;
}
public void setLevelNo(int levelNo) {
this.levelNo = levelNo;
}
public int getCurScore() {
return curScore;
}
public void setCurScore(int curScore) {
this.curScore = curScore;
}
public long getStartTime() {
return startTime;
}
public void setStartTime(long startTime) {
this.startTime = startTime;
}
public int getClapsedTime() {
return clapsedTime;
}
public void setClapsedTime(int clapsedTime) {
this.clapsedTime = clapsedTime;
}
// 玩游戏
public void play() {
Game game = new Game(this);// 玩家玩游戏,this: 本类对象的
for (int i = 0; i < LevelParam.level.length; i++) {// 外层循环一次,内层循环一周,外层:关数=级别,外层:关数=级别,循环一次晋级一次,一次通关成功
this.levelNo += 1;// 玩家记录游戏级别,晋级
this.startTime = System.currentTimeMillis(); // 开始时间=当前系统时间
this.curScore = 0;// 晋级后计时清零,积分清零
for (int j = 0; j < LevelParam.level[levelNo - 1].getStrTimes(); j++) {// 内层循环,循环一次完成一次字符串的输出、输入,比较
String outStr = game.printStr();// 游戏输出字符串
Scanner input = new Scanner(System.in);// 接受用户控制台输入
String inStr = input.next();
game.printResult(outStr, inStr);// 游戏判断玩家输入是否正确,并输出相应结果
}
}
}
}
Game.java
package 字符串游戏;
import java.util.Random;
public class Game {
private Player player;// 引入玩家类
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
public Game(Player player) {// 游戏被玩家玩
this.player = player;
}
// 输出字符串,返回字符串用于和玩家输入比较
// String printStr(int m) {
// if (m == 1) {
// return "<<";
// }
// if (m == 2) {
// return "<>";
// }
// return "><";
// }
public String printStr() {
int strLength = LevelParam.level[player.getLevelNo() - 1].getStrLength();// 获取一次输出的字符串的长度
StringBuffer reboot = new StringBuffer();// 定义动态拼接字符串的对象
Random random = new Random();// 创建生成随机数的对象
for (int i = 0; i < strLength; i++) {// 循环拼接生成要输出的字符串
int randnum = random.nextInt(strLength);// 产生随机数
switch (randnum) {// 根据随机数拼接字符串
case 0:
reboot.append(">");
break;
case 1:
reboot.append("<");
break;
case 2:
reboot.append("$");
break;
case 3:
reboot.append("%");
break;
case 4:
reboot.append("&");
break;
case 5:
reboot.append("@");
break;
}
}
System.out.println(reboot.toString());// 输出字符串
return reboot.toString();// 返回字符串用于和玩家输入做对比
}
// 比较游戏输出out和玩家输入in,根据比较结果输出相应信息
public void printResult(String out, String in) {
// player = new Player();
//
// if (out.equals(in)) {
// System.out.println(
// "输入正确,你的积分" + player.curScore + ",你的级别" + player.levelNo + ",已用时间" + player.clapsedTime + "秒");
// } else
// System.out.println("输入错误,退出!");
if (out.equals(in)) {// 输入正确
long currentTime = System.currentTimeMillis();// 获取当前系统时间(毫秒为单位)
long playtime = (currentTime - player.getStartTime()) / 1000;// 游戏时间=当前时间-开始时间(秒)
long limitTime = LevelParam.level[player.getLevelNo() - 1].getTimeLimit();// 游戏规定的时间限制
if (playtime > limitTime) {// 判断游戏是否超时
System.out.println("已经超时,退出!");// 超时
System.exit(1);
} else {// 没有超时
int currentScore = player.getCurScore();// 原来积分
int playPerScore = LevelParam.level[player.getLevelNo() - 1].getPerScore();// 当前等级成功输入一次的游戏积分
player.setCurScore(currentScore + playPerScore);// 玩完当局之后设置当前的积分
player.setClapsedTime((int) playtime);// 计算已用时间
System.out.println("输入正确,您的级别" + player.getLevelNo() + ",您的积分" + player.getCurScore() + ",已用时间"
+ player.getClapsedTime());// 输出当前积分,当前级别,已用时间
if (player.getLevelNo() == LevelParam.level.length) {// 判断用户是否已经闯过最后一关
int perScore = LevelParam.level[player.getLevelNo() - 1].getPerScore();// 当前局成功一次的积分
int perNum = LevelParam.level[player.getLevelNo() - 1].getStrTimes();// 获取当前关的局数
int score = perScore * perNum;// 计算每关的总积分
if (player.getCurScore() == score) { // 判断是否最后通关
System.out.println("恭喜你已经通完所有关卡!");
System.exit(0);
}
}
}
} else {// 输入不正确
System.out.println("输入错误,退出!");
System.exit(0);
}
}
}
Level.java
package 字符串游戏;
public class Level {
private int levelNo;// 各级别号
private int strLength;// 各级别一次输出字符串的长度
private int strTimes;// 各级别输出字符串的次数
private int timeLimit;// 各级别闯关的时间限制
private int perScore;// 各级别正确输入一次的得分
public int getLevelNo() {
return levelNo;
}
public void setLevelNo(int levelNo) {
this.levelNo = levelNo;
}
public int getStrLength() {
return strLength;
}
public void setStrLength(int strLength) {
this.strLength = strLength;
}
public int getStrTimes() {
return strTimes;
}
public void setStrTimes(int strTimes) {
this.strTimes = strTimes;
}
public int getTimeLimit() {
return timeLimit;
}
public void setTimeLimit(int timeLimit) {
this.timeLimit = timeLimit;
}
public int getPerScore() {
return perScore;
}
public void setPerScore(int perScore) {
this.perScore = perScore;
}
public Level() {
}
public Level(int levelNo, int strLength, int strTimes, int timeLimit, int perScore) {
// TODO Auto-generated constructor stub
this.levelNo = levelNo;
this.strLength = strLength;
this.strTimes = strTimes;
this.timeLimit = timeLimit;
this.perScore = perScore;
}
}
LevelParam.java
package 字符串游戏;
public class LevelParam {
public final static Level[] level=new Level[6];//规定游戏级别6个级
static {//各级别号、各级别一次输出字符串的长度、各级别输出字符串的次数、各级别闯关的时间限制、各级别正确输入一次的得分
level[0]=new Level(1,2,6,30,10);
level[1]=new Level(2,3,5,25,20);
level[2]=new Level(3,4,4,20,30);
level[3]=new Level(4,5,3,15,40);
level[4]=new Level(5,6,2,10,50);
level[5]=new Level(6,7,1,5,60);
}
}
Run.java
package 字符串游戏;
public class Run {
public static void main(String[] args) {
// TODO Auto-generated method stub
Player player = new Player();
player.play();
}
}
运行结果: