玩家类
package bao;
import java.util.Scanner;
//玩家类
public class Player {
private int levelNo;//玩家当前级别号
private int curScore;///玩家当前级别积分
private long stratTime;//当前级别开始时间
private int elapseTime;//当前级别已用时间
//封装
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 getStratTime() {
return stratTime;
}
public void setStratTime(long stratTime) {
this.stratTime = stratTime;
}
public int getElapseTime() {
return elapseTime;
}
public void setElapseTime(int elapseTime) {
this.elapseTime = elapseTime;
}
//玩家玩游戏的方法
public void play()
{
Game game=new Game(this);
Scanner input=new Scanner(System.in);
//外层循坏一次,游戏晋级一级
for (int i = 0; i < levelProgram .levels.length; i++) {
//晋级
this.levelNo+=1;
//晋级后计时清零,积分清零
this.stratTime=System.currentTimeMillis();
this.curScore=0;
//内层循环一次完成一次字符串输出输入比较
for (int j = 0; j < levelProgram .levels[levelNo-1].getStrTime(); j++) {
String outstr=game.PrintStr();
System.out.println(outstr);
String instr=input.next();
game.printResult(outstr, instr);
}
}
}
}
游戏类
package bao;
//游戏类
import java.util.Random;
public class Game {
private Player player;
public Game(Player player){
this.player=player;
}
public String PrintStr(){
StringBuffer buffer=new StringBuffer();
Random random=new Random();
//通过循环生成要输出的字符串
for (int i = 0; i <2; i++) {
int rand=random.nextInt(2);
//根据随机数拼接字符串
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;
}
}
return buffer.toString();
}
public void printResult(String out,String in)
{
if (out.equals(in)) {
long time=System.currentTimeMillis();
if((time-player.getStratTime())/1000>levelProgram.levels[player.getLevelNo()-1].getTimeLimit()){
System.out.println("你输入太慢,已经超时,退出");
System.exit(1);
}else
{
//当前获取分值
player.setCurScore(player.getCurScore()+ levelProgram.levels[player.getLevelNo() - 1].getPerScore());
player.setElapseTime((int) (time - player.getStratTime()) / 1000);
int levelNo=player.getLevelNo();
int score=player.getCurScore();
int elapsedTime=player.getElapseTime();
System.out.println("输入正确,您的级别"+levelNo+"您的积分"+score+"已用时间"+elapsedTime+"秒。");
if(levelNo==6){
System.out.println("游戏结束");
System.exit(1);
}
}
}else{
System.out.println("输入错误,退出");
System.exit(1);
}
}
}
级别
package bao;//级别类
public class Level {
private int levelNo;//级别编号
private int strLength;//各级别一次输出字符串长度
private int strTime;//各级别输出字符串的次数
private int timeLimit;//各级别闯关的时间限制
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 getTimeLimit() {
return timeLimit;
}
public void setTimeLimit(int timeLimit) {
this.timeLimit = timeLimit;
}
public int getPerScore() {
return perScore;
}
public void setPerScore(int perScore) {
this.perScore = perScore;
}
//无参构造
public Level(){}
//有参构造
public Level(int levelNo,int StrinLength,int strTime,int timeLimit,int perScore )
{
this.levelNo=levelNo;
this.strLength=StrinLength;
this.strTime=strTime;
this.timeLimit=timeLimit;
this.perScore=perScore;
}
}
package bao;
public class levelProgram extends Level {
//继承父类无参构造
public levelProgram() {
super();
// TODO Auto-generated constructor stub
}
//继承父类有参构造
public levelProgram(int levelNo, int StrinLength, int strTime,
int timeLimit, int perScore) {
super(levelNo, StrinLength, strTime, timeLimit, perScore);
// TODO Auto-generated constructor stub
}
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,26,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,14,10);
levels[5]=new Level(6,7,5,10,15);
}
}
测试类
package bao;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Player player=new Player();
player.play();
}
}