快速击键(MyEclipse编写的QuickHit项目)

public class Level {
    private int levelNo;// 各级别编号
    private int strLength;// 各级别一次输出字符串的长度
    private int strTimes;// 各级别输出字符串的次数
    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 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;
    }

    public  Level() {
        super();
    }

    public Level(int levelNo, int strLength, int strTimes, int timeLimit,
            int perScore) {
        super();
        this.levelNo = levelNo;
        this.strLength = strLength;
        this.strTimes = strTimes;
        this.timeLimit = timeLimit;
        this.perScore = perScore;
    }

}
import java.util.Scanner;

/**
 * 玩家类
 * @author accp
 *
 */
public class Player {
    private int levelNo;   //等级
    private int curScore;   //积分
    private int elapsedTime; //已用时间
    private long startTime;  //开始时间
    
    
    public Player() {
        
    }
    
    
    public Player(int levelNo, int curScore, int elapsedTime, long startTime) {

        this.levelNo = levelNo;
        this.curScore = curScore;
        this.elapsedTime = elapsedTime;
        this.startTime = startTime;
    }


    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 int getElapsedTime() {
        return elapsedTime;
    }
    public void setElapsedTime(int elapsedTime) {
        this.elapsedTime = elapsedTime;
    }
    public long getStartTime() {
        return startTime;
    }
    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }
    
    public void play()
    {
        Game game=new Game(this);//this代表已经创建了的对象引用player
        //外层循环,循环一次级别进一级
        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();//游戏输出字符串
                System.out.println(outstr);
                Scanner input=new Scanner(System.in);
                String instr=input.next();
                game.printResult(outstr, instr);
            }
            
        }
    }
}
import java.util.Date;
import java.util.Random;

/**
 * 游戏类
 * @author accp
 *
 */

public class Game {
    private Player player;

    public Game() {
        
    }
    
    public Game(Player player) {
        this.player = player;
    }

    public Player getPlayer() {
        return player;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }
    
    //输出字符串用于和玩家的输入进行比较
    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;
            default:
                break;
            }
            
        }
        String result=buffer.toString();
        return result;
    }
    //比较结果,输出相应信息
    public void printResult(String out,String in)
    {
        if (out.equals(in)) {
            //正确输入
            Date date=new Date();
            long currentTime=date.getTime();
            //如果超时
            long closeTime=LevelParam.levels[player.getLevelNo()-1].getTimeLimit();
            if ((currentTime-player.getStartTime())/1000>closeTime) {
                System.out.println("你输入的太慢了,已经超时,退出!");
                System.exit(1);
            }
            else
            {
                //计算当前玩家积分
                //所有的积分相加
            player.setCurScore(player.getCurScore()+LevelParam.levels[player.getLevelNo()-1].getPerScore());
            int score=player.getCurScore();//获取数组内的开始积分
            int time=(int)(currentTime-player.getStartTime())/1000;//获取时间差
            player.setElapsedTime(player.getElapsedTime()+time);
            int alltime=player.getElapsedTime();
            int no=player.getLevelNo();
            System.out.println("输入正确,您的级别"+no+",您的积分"+score+",已用时间"+alltime+"");
            }
        }else{
            System.out.println("输入错误 !!!,退出");
            System.exit(1);
            }
            
            
        }
}
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);
     }
}
public class Test {

    public static void main(String[] args) {
        
        Player player=new Player();
        player.play();
}
}

 

posted @ 2016-10-18 16:53  刘二雄  阅读(265)  评论(0编辑  收藏  举报