package cn.happy.Play;
import java.util.Random;
import cn.happy.Play.LevelParam;
/*
* 游戏类
*/
public class Game {
public Player player;
public Game(Player player) {
super();
this.player = player;
}
public String printStr() {
StringBuffer buffer=new StringBuffer();
Random random=new Random();
int strLength=LevelParam.level[player.getLevelNo()-1].getStrLength();
for (int i = 0; i < strLength; i++) {
//产生随机数
int ran=random.nextInt(strLength);
switch (ran) {
case 0:
buffer.append("1");
break;
case 1:
buffer.append("1");
break;
case 2:
buffer.append("1");
break;
case 3:
buffer.append("1");
break;
case 4:
buffer.append("1");
break;
case 5:
buffer.append("1");
break;
case 6:
buffer.append("1");
break;
}
}
System.out.println(buffer);
return buffer.toString();
}
public void printResult(String out,String in) {
long currentTime=System.currentTimeMillis();
if (out.equals(in)) {
if ((currentTime-player.getStartTime())/1000>LevelParam.level[player.getLevelNo()-1].getTimeLinit()) {
System.out.println("你的速度太慢了 ");
System.exit(1);
}else {
//当前级别
int level=player.getLevelNo();
//当前级别所用时间
int time=(int)(currentTime-player.getStartTime())/1000;
//当前的积分情况
player.setCurScore((player.getCurScore()+LevelParam.level[player.getLevelNo()-1].getPerScore()));
int scores=player.getCurScore();
System.out.println("输入正确,您的级别为"+level+",获得积分为"+scores+",所用时间为"+time);
if (level==6) {
System.out.println("恭喜通关");
System.exit(1);
}
}
}else {
System.out.println("输入错误,正确的是"+out);
System.exit(1);
}
}
}
package cn.happy.Play;
/*
* 级别类
*/
public class Level {
private int levelNo;//各级别编号
private int strLength;//各级别一次输出字符串的长度
private int strTimes;//各级别输出字符串的次数
private int timeLinit;//各级别闯关的时间限制
private int perScore;//各级别正确输入一次的得分
public Level() {
}
public Level(int levelNo, int strLength, int strTimes, int timeLinit,
int perScore) {
super();
this.levelNo = levelNo;
this.strLength = strLength;
this.strTimes = strTimes;
this.timeLinit = timeLinit;
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 getTimeLinit() {
return timeLinit;
}
public void setTimeLinit(int timeLinit) {
this.timeLinit = timeLinit;
}
public int getPerScore() {
return perScore;
}
public void setPerScore(int perScore) {
this.perScore = perScore;
}
}
package cn.happy.Play;
public class LevelParam {
public static final Level[] level=new Level[6];
static{
level[0]=new Level(1,2,10,30,1);
level[1]=new Level(2,3,9,26,2);
level[2]=new Level(3,4,8,22,5);
level[3]=new Level(4,5,7,18,8);
level[4]=new Level(5,6,6,15,10);
level[5]=new Level(6,7,5,12,15);
}
}
package cn.happy.Play;
import java.util.Scanner;
/*
* 玩家类
*/
public class Player {
Scanner input=new Scanner(System.in);
private int levelNo;//玩家当前级别号
private int curScore;//玩家当前级别积分
private long startTime;//当前级别开始时间
private int elapsedTime;//当前级别已用时间
private int perScore; // 各级别成功输入一次字符串后增加的分值
public void Play() {
Game game=new Game(this);
for (int i = 0; i <LevelParam.level.length ; i++) {
levelNo+=1;
startTime=System.currentTimeMillis();
curScore=0;
elapsedTime=0;
for (int j = 0; j < LevelParam.level[i].getStrTimes(); j++) {
//游戏输出字符串
String outstr=game.printStr();
//接受用户输入
String instr=input.next();
//游戏判断玩家输入是否正确,并输出相应结果信息
game.printResult(outstr, instr);
}
}
}
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 getElapsedTime() {
return elapsedTime;
}
public void setElapsedTime(int elapsedTime) {
this.elapsedTime = elapsedTime;
}
public Player(int levelNo, int curScore, long startTime, int elapsedTime) {
this.levelNo = levelNo;
this.curScore = curScore;
this.startTime = startTime;
this.elapsedTime = elapsedTime;
}
public Player() {
}
}
package cn.happy.Play;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Player player=new Player();
player.Play();
}
}