任务描述
1.根据输入速率和正确率将玩家分为不同级别
2.级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高
3.规定时间内完成规定次数的输入,正确率达到规定要求,则升级
玩家最高级别为6级、初始级别一律为1级
4.用户错误输入一次,游戏结束
分析
1.玩家类:级别、当前级别时间、姓名、得分,玩游戏()
2.级别:字符数、得分、次数、规定时间、级别号
3.6个级别的常量:
4.游戏类:玩家,生成字符()、玩游戏()
流程控制:
欢迎
循环(根据当前级别的显示次数)
如果循环正常走完,升级
级别编号、时间初始化为当前时间,已用时间初始化为0
输入错误,退出
超时,退出
//玩家类public class Player {private String name;private int curLevel=1; //当前级别编号private int curScore=0; //当前得分private long startTime=System.currentTimeMillis();private long usedTime;public Player(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getCurLevel() {return curLevel;}public void setCurLevel(int curLevel) {this.curLevel = curLevel;}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 long getUsedTime() {return usedTime;}public void setUsedTime(long usedTime) {this.usedTime = usedTime;}public void play(){Game g=new Game();g.player=this;g.run();}}
//游戏类public class Game implements LevelParams{Player player=new Player(\"张浩\");//生成一串字符public String getStrings(){int curLevel=player.getCurLevel();Level level=LEVELS[curLevel-1];String str=\"\";for (int i = 0; i < level.getCharCount(); i++) {int ranNum=(int)(Math.random()*10);str+=ranNum;}return str;}//流程控制public void run(){Scanner in=new Scanner(System.in);System.out.println(\"欢迎\"+player.getName()+\"来到quickHit峡谷!\");while (player.getCurLevel()<=6){int lvlNum=player.getCurLevel();Level lvl=LEVELS[lvlNum-1];for (int i = 0; i <=lvl.getInputNum(); i++) {System.out.println(\"当前级别为\"+player.getCurLevel()+\",积分为\"+player.getCurScore()+\",已用时间\"+player.getUsedTime()/1000+\"秒\");String out=getStrings();System.out.println(out);System.out.println(\"请输入对应的字符串:\");String input=in.next();long now=System.currentTimeMillis();player.setUsedTime(now-player.getStartTime());if (!input.equals(out)){ //输入与输出不一致,错误,退出程序System.out.println(\"输入错误\");System.exit(1);}else if (player.getUsedTime()/1000>lvl.getLimitTime()){System.out.println(\"输入超时,游戏结束\");System.exit(1);}//输入正确且未超时player.setCurScore(player.getCurScore()+lvl.getPreScore());}player.setCurLevel(player.getCurLevel()+1);player.setUsedTime(0);player.setStartTime(System.currentTimeMillis());}}}
//等级类public class Level {private int levelNum; //级别号private int charCount; //当前级别每次输出的字符数private int preScore; //每次输入正确后的得分private long limitTime; //当前级别规定时间private int inputNum; //输入次数public Level(int levelNum, int charCount, int preScore, long limitTime, int inputNum) {this.levelNum = levelNum;this.charCount = charCount;this.preScore = preScore;this.limitTime = limitTime;this.inputNum = inputNum;}public int getLevelNum() {return levelNum;}public int getCharCount() {return charCount;}public int getPreScore() {return preScore;}public long getLimitTime() {return limitTime;}public int getInputNum() {return inputNum;}}
//设置等级常量接口public interface LevelParams {Level[] LEVELS={new Level(1,2,3,20,5),new Level(2,3,5,40,8),new Level(3,4,7,60,10),new Level(4,5,8,55,8),new Level(5,6,9,50,6),new Level(6,7,10,40,4),};}