QuickHit 项目

package cn.javaoppday01;

import java.util.Random;



public class Game {
    public Player player;

    public Game(Player player) {
        this.player = player;
    }
    public void printResult(String out, String in) {

        long currentTime = System.currentTimeMillis();

        if ((currentTime - player.getStartTime()) / 1000 > LevelParam.levels[player
                .getLevelNo() - 1].getTimeLimit()) {
            System.out.println("你输入太慢了,已经超时,退出!");
            System.exit(1);
        } else if (!out.equals(in)) {
            System.out.println("你输入错了了退出!");
            System.exit(1);
        }

        // 计算玩家当前积分
        player.setCurScore(player.getCurScore()
                + LevelParam.levels[player.getLevelNo() - 1].getPerScore());
        System.out.println("下一关" + "您的当前积分是" + player.getCurScore());

    }

    

  
  // 输出字符串,返回字符串用于和玩家输入比较。
     public String printStr() {
         StringBuffer buffer = new StringBuffer();
         Random random = new Random();
         // 通过循环生成要输出的字符串
         for (int i = 0; i < 5; i++) {
             int rand = random.nextInt(5); // 产生随机数
             // 根据随机数拼接字符串
             switch (rand) {
             case 0:
                 buffer.append(">");
                 break;
             case 1:
                 buffer.append("<");
                 break;
             case 2:
                 buffer.append("a");
                 break;
             case 3:
                 buffer.append("b");
                 break;
             case 4:
                 buffer.append("c");
                 break;
             case 5:
                 buffer.append("d");
                 break;
             case 6:
                 buffer.append("e");
                 break;
             }
         }

         return buffer.toString();
     }

   
}
游戏类
package cn.javaoppday01;

public class Level {

    private int levelNo;//级别号
    private int strLength;//各级别一次输出字符串的长度
    private int stTtime;//各级别输出字符串次数
    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 getStTtime() {
        return stTtime;
    }
    public void setStTtime(int stTtime) {
        this.stTtime = stTtime;
    }
    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(int levelNo,int strLength,int stTtime ,int timeLimit,int perScore){
        this.levelNo=levelNo;
        this.strLength=strLength;
        this.stTtime=stTtime;
        this.timeLimit=timeLimit;
        this.perScore=perScore;
        
        
    }
}
等级类
package cn.javaoppday01;

import java.util.Scanner;

public class Player {

    private int levelNo;//级别号
    private int curScore;//当前积分
    private long startTime;//各级别开始时间
    private int 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].getStTtime();j++){
                String outStr =game.printStr();
                System.out.println(outStr);
                String inStr =input.next();
                game.printResult(outStr,inStr);
                
                
            }
            
            
        }
    }
    
    
    

}
玩家类
package cn.javaoppday01;

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,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 cn.javaoppday01;

public class MyMain {
public static void main(String[] args) {
    Player play=new Player();
    play.play();
}
}
Main方法测试类

 

posted @ 2018-03-05 15:34  秋风伊人  阅读(286)  评论(0编辑  收藏  举报