package com.ketang.game;
/**
* 游戏级别类
* @author
*
*/
public class Level {
private int levelNo; //各级别编号
private int strLength; //字符串长度
private int strTimes; //字符串的次数
private int timeLimit; //各级别闯关的时间限制
private int perScore; //各级别正确输入一次的得分
public Level() {
}
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;
}
}
package com.ketang.game;
/**
* 级别参数类,配置各个级别参数
* @author
*
*/
public class LevelParam {
public static final 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,15,10);
levels[5]=new Level(6,7,5,12,15);
}
}
package com.ketang.game;
import java.util.Scanner;
/**
* 玩家类
* @author
*
*/
public class Player {
private int levelNo; //玩家当前级别号
private int curScore; //玩家当前级别积分
private long startTime; //当前级别开始时间
private int elapsedTime; //当前级别已用时间
public Player() {
super();
}
public Player(int levelNo, int curScore, long startTime, int elapsedTime) {
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.startTime=System.currentTimeMillis();
this.curScore=0;
//内循环,循环一次完成一次字符串的输出、输入、比较
for(int j=0;j<LevelParam.levels[levelNo-1].getStrTimes();j++) {
//游戏输出字符串
String outStr = game.printStr();
//接收用户输入
String inStr = input.next();
//游戏判断玩家输入是否正确,并输出相应结果信息
game.printResult(outStr, inStr);
}
}
}
}
package com.ketang.game;
import java.util.Random;
/**
* 游戏类
* @author
*
*/
public class Game {
private Player player; //玩家
public Game() {
}
/**
* 构造方法,传入玩家信息
* @param player 玩家
*/
public Game(Player player) {
super();
this.player = player;
}
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
/**
* 输出指定级别规定长度的字符串
* @return 输出的字符串,用于和用户输入比较
*/
public String printStr() {
int strLength=LevelParam.levels[player.getLevelNo()-1].getStrLength();
StringBuffer buffer=new StringBuffer();
Random random=new Random();
//通过循坏生成要输出的字符串
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();
}
/**
* 判断玩家输入是否正确,并输出相应结果信息
* @param out 游戏输出的字符串
* @param in 玩家输入的字符串
*/
public void printResult(String out, String in) {
boolean flag=false;
if(out.equals(in)) {
flag=true;
}else {
flag=false;
}
//如果输入正确
if(flag) {
long currentTime=System.currentTimeMillis();
//如果超时
if((currentTime-player.getStartTime())/1000>LevelParam.levels[player.getLevelNo()-1].getTimeLimit()) {
System.out.println("你输入太慢了,已经超时,退出!");
System.exit(1);
//如果没有超时
}else {
//计算当前积分
player.setCurScore(player.getCurScore()+LevelParam.levels[player.getLevelNo()-1].getPerScore());
//计算已用时间
player.setElapsedTime((int)((currentTime-player.getStartTime())/1000));
//输出当前积分、当前级别、已用时间
System.out.println("输入正确,您的级别"+player.getLevelNo()+",您的积分"+player.getCurScore()
+",已用时间"+player.getElapsedTime()+"秒。");
//判断用户是否已经闯过最后一关
if(player.getLevelNo()==6) {
int score=LevelParam.levels[player.getLevelNo()-1].getPerScore()
*LevelParam.levels[player.getLevelNo()-1].getStrTimes();
if(player.getCurScore()==score) {
System.out.println("你已闯关成功,成为绝世高手,恭喜你!");
System.exit(0);
}
}
}
//如果输入错误
}else {
System.out.println("输入错误,退出!");
System.exit(1);
}
}
}