@Override
public void run() {
Game game = new Game(this);
for(int i=0 ;i<6;i++){
Level level = Level.leve[getLeveINO()];
int strTimes = level.strTimes;
for(int j=0 ; j<strTimes;j++){
String out = game.out();
System.out.println(out);
long StartTime = System.currentTimeMillis();
setStartTime(StartTime);
String in = scan.next();
game.Result(out, in);
}
System.out.println("恭喜你升级,已经为你清理了积分和时间");
this.setLeveINO(getLeveINO()+1);
this.setCurrScore(0);
this.setElapsedTime(0);
}
}
级别类
public final static Level[] leve = new Level[6];
/**
* 将6个不同的等级抽象为6个等级对象,不同等级的对象属性相同,属性值不相同,
* static 在创建该类时,就会初始化这些静态代码块中的属性
*/
static{
leve[0] = new Level(1,2,6,8,1);
leve[1] = new Level(2,3,5,8,2);
leve[2] = new Level(3,4,5,7,3);
leve[3] = new Level(4,5,5,7,4);
leve[4] = new Level(5,6,5,7,5);
leve[5] = new Level(6,7,5,7,6);
}
public Level(int leveIno, int strLength, int strTimes, int timeLimit, int perSocre) {
this.LeveIno = leveIno;
this.strLength = strLength;
this.strTimes = strTimes;
this.timeLimit = timeLimit;
this.perSocre = perSocre;
}
游戏方法类
public class Game {
private Player play;
public Game(Player play){
this.play = play;
}
public String out(){
int strLength = Level.leve[play.getLeveINO()].strLength;
StringBuilder sb = new StringBuilder();
Random random = new Random();
for(int i=0 ;i<strLength;i++){
int ran = random.nextInt(6);
switch(ran){
case 0:
sb.append("<");
break;
case 1:
sb.append(">");
break;
case 2:
sb.append("&");
break;
case 3:
sb.append("@");
break;
case 4:
sb.append("#");
break;
default :
sb.append("*");
break;
}
}
return sb.toString();
}
public void Result(String out,String in){
Level leve = Level.leve[play.getLeveINO()];
long time = System.currentTimeMillis();
if(in.equals(out)){
int TimeLimit = leve.getTimeLimit();
long totalTime = (time-play.getStartTime())/1000;
if(totalTime>TimeLimit){
System.out.println("你输入太慢了,已经超时,退出!");
// 终止当前java虚拟机
System.exit(1);
}
int currScore = leve.perSocre;
play.setCurrScore(play.getCurrScore()+currScore);
play.setElapsedTime(totalTime+play.getElapsedTime());
System.out.println("输入正确,您的级别:"+(play.getLeveINO()+1)+",时间"+play.getElapsedTime()+"秒,积分"+play.getCurrScore());
} else {
System.out.println("输入错误");
System.exit(1);
}
}