项目总体为五个类:
Test(测试类)
Level(等级类 声明变量)
LevelParam (等级参数数组类)
Player(玩家类 其中定义Play方法 )
Game(游戏类 定义Game方法 )
Level类:
public class Level {
private int levelNo; //级别
private int strLength;//字符长度
private int strTime;//字符次数
private int strLimit;//时间上限
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 getStrTime() {
return strTime;
}
public void setStrTime(int strTime) {
this.strTime = strTime;
}
public int getStrLimit() {
return strLimit;
}
public void setStrLimit(int strLimit) {
this.strLimit = strLimit;
}
public int getPerScore() {
return perScore;
}
public void setPerScore(int perScore) {
this.perScore = perScore;
}
public Level(int levelNo, int strLength, int strTime, int strLimit,
int perScore) {
super();
this.levelNo = levelNo;
this.strLength = strLength;
this.strTime = strTime;
this.strLimit = strLimit;
this.perScore = perScore;
}
public Level() {
super();
// TODO Auto-generated constructor stub
}
}
Game类:
import java.util.Random;
public class Game {
private Player player;
public Game(Player player){
this.player=player;
}
public String Getstr(){
StringBuffer buffer=new StringBuffer();
Random random=new Random();
int strLength=LevelParam.levels[player.getLevelNo()-1].getStrLength();
for(int i=0;i<strLength;i++){
int rand=random.nextInt(strLength);
switch(rand){
case 0:
buffer.append(">");
break;
case 1:
buffer.append("<");
break;
case 2:
buffer.append("*");
break;
case 3:
buffer.append("&");
break;
case 4:
buffer.append("%");
break;
case 5:
buffer.append("#");
break;
}
}
System.out.println(buffer);
return buffer.toString();
}
public void compare(String out,String in){
long currentTime=System.currentTimeMillis();
if(!out.equals(in)){
System.out.println("输入错误");
System.exit(1);
}else{
//当前关卡 目前总分数 = 原始累计分数+本次分数
//LevelParam.levels[player.getLevelNo()-1].getScore()
if((currentTime-player.getStartTime())/1000>
LevelParam.levels[player.getLevelNo()-1].getStrLimit()){
System.out.println("抱歉 你已经超时");
System.exit(1);
}else{
player.setCurScore(player.getCurScore()+
LevelParam.levels[player.getLevelNo()-1].getPerScore());
//耗时
long alreadyusetime=(int)(currentTime-player.getStartTime())/1000;
int result=(int)alreadyusetime;
player.setElapsedTime(result);
//获取当前等级
int level=player.getLevelNo();
//获取当前总分数
int scores=player.getCurScore();
//获取当前关卡已用时间
int usetime=player.getElapsedTime();
System.out.println("等级为:"+level+"积分为:"+scores+"所用时间为:"+usetime);
if(level==6){
System.out.println("恭喜你 完美通关!");
System.exit(1);
}
}
}
}
}
LavelParam类:
public class LevelParam {
public final static Level[] levels=new Level[6];
static{
levels[0]=new Level(1,2,10,30,1);
levels[1]=new Level(2,3,9,24,2);
levels[2]=new Level(3,4,8,22,5);
levels[3]=new Level(4,5,7,18,8);
levels[4]=new Level(5,6,6,15,10);
levels[5]=new Level(6,7,5,12,15);
}
}
Play类:
import java.util.Scanner;
public class Player {
private int levelNo;//玩家当前级别号
private int curScore;//玩家当前级别积分
private long startTime;//当前级别开始时间
private int elapsedTime;//当前级别已用时间
public Player() {
super();
// TODO Auto-generated constructor stub
}
public Player(int levelNo, int curScore, long startTime, int elapsedTime) {
super();
this.levelNo = levelNo;
this.curScore = curScore;
this.startTime = startTime;
this.elapsedTime = elapsedTime;
}
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 void play(){
Game game=new Game(this);
Scanner input=new Scanner(System.in);
for(int i=0;i<LevelParam.levels.length;i++){
this.levelNo+=1;
this.curScore=0;
this.startTime= System.currentTimeMillis();
for(int j=0;j<LevelParam.levels[levelNo-1].getStrTime();j++){
String outString=game.Getstr();
String inString=input.next();
game.compare(outString, inString);
}
}
}
}
Test 类:
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Player player=new Player();
player.play();
}
}