任务描述
需求概述
级别越高,一次现实的字符串数越多,玩家正确输入一次的得分也越高。
规定时间内完成规定次数的输入,正确率达到规定要求,则升级。
用户错误输入一次,游戏结束。
要使用的类
发现类:
玩家类(Player)
游戏类(Game)
级别类(Level)
发现类的属性:
玩家类(Player)的属性:
玩家当前级别(levelNo)
玩家当前级别积分(currScore)
当前级别开始时间(startTime)
当前级别已用时间(elapsedTime)
游戏类(Game)
级别类(Level)的属性:
各级别号(levelNo)
各级别一次输出字符串的长度(strLength)
各级别输出字符换的次数(strTimes)
各级别闯关的时间限制(timeLimit)
各级别正确输入一次的得分(perScore)
发现类的方法:
玩家类(Player)的方法:
玩游戏play()
游戏类(Game)的方法:
String printStr()
输出字符串,返回字符串用于和玩家输入比较
void printResult(String out,String in)
比较游戏输出out和玩家输入in,根据比较结果输出相应信息
优化设计:
修改游戏类(Game),添加属性
添加类:LevelParam
public final static Level level[6]
主要功能分析:
游戏输出字符串
生成字符串、输出字符串、返回字符串
生成长度固定但内容随机的字符串
确认输入并输出结果
输入
正确、未潮池
输出
当前级别、当前积分、已用时间
计算玩家的当前级别、当前积分和已用时间
玩游戏
6个级别循环实现
每次晋级后积分清零、即使清零
Level类:
public class Level
{
private int levelNo;
private int strLength;
private int strTimes;
private int timeLimit;
private int perScore;
public Level(int levelNo, int strLength, int strTimes, int timeLimit, int perScore)
{
this.levelNo = levelNo;
this.strLength = strLength;
this.strTimes = strTimes;
this.timeLimit = timeLimit;
this.perScore = 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;
}
}
Game类:
public class Game implements LevelParam
{
Player player=new Player();
public String printStr()
{
String str="";
for (int i = 0; i < LEVELS[player.getLevelNo() - 1].getStrLength(); i++)
{
str+=(int)(Math.random()*10);
}
return str;
}
public void printResult(String out,String in)
{
if ((System.currentTimeMillis()-player.getStartTime())/1000>LEVELS[player.getLevelNo()-1].getTimeLimit())
{
System.out.println("输入超时");
System.exit(-1);
}
if (!out.equals(in))
{
System.out.println("输入错误");
System.exit(-1);
}
player.setElapsedTime((int)(System.currentTimeMillis()-player.getStartTime())/1000);
player.setCurrScore(player.getCurrScore()+LEVELS[player.getLevelNo()-1].getPerScore());
System.out.println("输入正确,您的积分"+player.getCurrScore()+
",您的级别"+player.getLevelNo()+",已用时间"+player.getElapsedTime());
}
public void run()
{
player.setStartTime(System.currentTimeMillis());
Scanner input=new Scanner(System.in);
for (int i = player.getLevelNo()-1; i < LEVELS.length; i++)
{
for (int i1 = 0; i1 < LEVELS[player.getLevelNo() - 1].getStrTimes(); i1++)
{
String out=printStr();
System.out.println(out);
String in=input.next();
printResult(out,in);
}
player.setLevelNo(player.getLevelNo()+1);
}
}
}
Player类:
public class Player
{
private int levelNo=1;
private int currScore;
private long startTime;
private int elapsedTime;
public void play()
{
Game game=new Game();
game.run();
System.out.println("通关");
}
public int getLevelNo()
{
return levelNo;
}
public void setLevelNo(int levelNo)
{
this.levelNo = levelNo;
}
public int getCurrScore()
{
return currScore;
}
public void setCurrScore(int currScore)
{
this.currScore = currScore;
}
public long getStartTime()
{
return startTime;
}
public void setStartTime(long startTime)
{
this.startTime = startTime;
}
public int getElapsedTime()
{
return elapsedTime;
}
public void setElapsedTime(int elapsedTime)
{
this.elapsedTime = elapsedTime;
}
}
LevelParam接口:
public interface LevelParam
{
public static final Level LEVELS[]={
new Level(1,2,10,30,1),
new Level(2,3,9,26,2),
new Level(3,4,8,22,5),
new Level(4,5,7,18,8),
new Level(5,6,6,15,10),
new Level(6,7,5,12,15)
};
}
Test类:
public class Test
{
public static void main(String[] args)
{
Player player=new Player();
player.play();
}
}