一、项目描述:
完成规定时间与保证正确率的升级闯关项目。
二、类
1.Game类
创建系统自动输出的随机字符,将他们进行拼接输出随机的字符串,与用户输入的字符串进行比对,如果正确的话,输出当前所用时间,不正确则退出。正确的情况下,如果超时,则退出。未超时则输出当前级别,时间,积分直到最后一关停止。利用if判断是否到最后一关。
public class Game {
private Player player;
public Game(){}
public Game(Player player) {
this.player = player;
}
//1.游戏输出的字符串
public String printstr(){
//获取用户当前级别号对应的字符串长度【角标从0开始,所以要减1】
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);
//将StringBufer类型的变量转变为String类型的变量===>>toString()
String ss=buffer.toString();
return ss;
}
/* 游戏对比方法【输出和输入的进行】*/
public void printResult(String out,String in){
boolean flag;
//比对输出的字符串和输入的字符串
if(out.equals(in)){
flag=true;
}else{
flag=false;
}
//如果输入正确的话
if(flag){
//获取当前时间
long currentTime=System.currentTimeMillis();
/* currentTime-player.getLevelNo()/1000 当前时间-起始时间=共使用的时间
LevelParam.levels[player.getLevelNo()-1].getTimeLimit()当前(某个用户通过)该关卡所需要的时间
*/
if( (currentTime - player.getStartTime() )/1000 >
LevelParam.levels[player.getLevelNo()-1].getTimeLimit()){
//如果超时,则提示已超时,并退出jvm虚拟机
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].getStrTime();
//如果玩家,达到最高分
if(player.getCurScore()==score){
System.out.println("你已经闯关成功,恭喜");
System.exit(0);
}
}
}
}else{
//如果输出失败
System.out.println("输出错误,退出");
System.exit(1);
}
}
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
}
2.Level类
Level类当中封装定义的属性,对该类进行无参构造,有参构造。
public class Level {
private int levelNo;//级别号
private int strLength;//输出字符串的长度
private int strTime;//输出字符串的频率
private int timeLimit;//各级别所需要的时间变量 限制时间
private int perScore;//各级别输出字符串之后增加的分数变量
public Level(){}
public Level(int levelNo, int strLength, int strTime, int timeLimit, int perScore) {
this.levelNo = levelNo;
this.strLength = strLength;
this.strTime = strTime;
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 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;
}
}
3.LevelParam类
该类是定义常量需要的类,用static加载静态常量,对Level类中的属性就行赋值。
public class LevelParam {
//定义常量——>制定6个关卡中每个关卡的游戏规则
public static final Level levels[]
=new Level[6];
//static()静态常量,作用:类加载的时候只加载一次,独一份
static {
levels[0]=new Level(1,2,1,50,5);
levels[1]=new Level(2,3,1,50,5);
levels[2]=new Level(3,4,1,50,5);
levels[3]=new Level(4,5,1,50,5);
levels[4]=new Level(5,6,1,50,5);
levels[5]=new Level(6,7,1,50,5);
}
}
4.Player类
Player类当中封装定义的属性,并进行无参,有参构造,创建play方法,在方法中利用for循环实现晋级计时积分的条件,用Scanner系统输出字符串,由用户输入相应字符串
public class Player {
private int levelNo;//级别号
private int curScore;//当前积分
private long startTime=0;//各级别开始时间
private int elapsedTime=0;//各级别已用时间
//玩游戏
public void play(){
Game game = new Game(this);
Scanner scanner = new Scanner(System.in);
for(int i=0;i<LevelParam.levels.length;i++){
//1.晋级
this.levelNo+=1;
//2.计时清零
this.startTime=System.currentTimeMillis();
//3.积分清零
this.curScore=0;
for(int j=0;j<LevelParam.levels[levelNo-1].getStrTime();j++){
//输出字符串
String outStr=game.printstr();
//输入字符串
String inStr=scanner.next();
game.printResult(outStr,inStr);
}
}
}
public Player(){
}
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;
}
}
5.Text类
输出程序。
public class Text {
public static void main(String[] args) {
Player player = new Player();
player.play();
}
}