如何编辑简单打字游戏

先来简单看一下要求

一,我们可以先编写Player类,先把属性定义好,然后进行封装

 

public class Player {
    private String name;                    //定义姓名
    private int lvlNo=1;                 //定义级别编号为1
    private long startTime=System.currentTimeMillis();      //定义开始时间
    private long usedTime;              //定义已用时间
    private int totalScores=0;           //定义初始积分为0
    public void play(Game g){//传参进来,传入当前对象
        g.run(this);//注释1:this是本类对象
    }

    public Player(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLvlNo() {
        return lvlNo;
    }

    public void setLvlNo(int lvlNo) {
        this.lvlNo = lvlNo;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public long getUsedTime() {
        return usedTime;
    }

    public void setUsedTime(long usedTime) {
        this.usedTime = usedTime;
    }

    public int getTotalScores() {
        return totalScores;
    }

    public void setTotalScores(int totalScores) {
        this.totalScores = totalScores;
    }

}

二,咱们可以编写级别类,接下来把级别编号等定义好,仍然进行封装

public class Level {
    private int lvlNo;//级别编号
    private int charNum;//每次要匹配的字符数
    private int perScore;//每次匹配成功获得的积分
    private int limitTime;//每个级别限制的时间,单位为秒数
    private int typeCnt;//每个级别要匹配的次数

    public Level(int lvlNo, int charNum, int perScore, int limitTime, int typeCnt) {
        this.lvlNo = lvlNo;
        this.charNum = charNum;
        this.perScore = perScore;
        this.limitTime = limitTime;
        this.typeCnt = typeCnt;
    }

    public int getLvlNo() {
        return lvlNo;
    }

    public void setLvlNo(int lvlNo) {
        this.lvlNo = lvlNo;
    }

    public int getCharNum() {
        return charNum;
    }

    public void setCharNum(int charNum) {
        this.charNum = charNum;
    }

    public int getPerScore() {
        return perScore;
    }

    public void setPerScore(int perScore) {
        this.perScore = perScore;
    }

    public int getLimitTime() {
        return limitTime;
    }

    public void setLimitTime(int limitTime) {
        this.limitTime = limitTime;
    }

    public int getTypeCnt() {
        return typeCnt;
    }

    public void setTypeCnt(int typeCnt) {
        this.typeCnt = typeCnt;
    }
}

三,接着咱们要配置好接口,建立好对象数组

public interface LevelParms {
    Level[] LEVELS={
            new Level(1,2,2,20,3),
            new Level(2,3,3,35,4),
            new Level(3,4,4,50,5),
            new Level(4,5,5,60,5),
            new Level(5,5,6,60,6),
            new Level(6,6,7,50,5),
    };

}

四,设置好两个后面将要抛出的异常,分别是超时和打错字

public class InputTypeMismatchException extends RuntimeException{
    public InputTypeMismatchException(String message) {
        super(message);
    }
}
public class InputOutOfTimeException extends RuntimeException{
    public InputOutOfTimeException(String message) {
        super(message);
    }
    //    public InputOutOfTimeException(String message) {
//        super(message);
    }


五,最后开始编辑Game类
public class Game {
    private Scanner s = new Scanner(System.in);
    private Level[] levels = LevelParms.LEVELS;
    int a=0;
    long used;

    public void run(Player p) {//注释1:这里是把玩家看成属性,传参进来
        //欢迎词
        //开始游戏
        int lvlNo = p.getLvlNo();//获取当前级别
        if (lvlNo>6){//递归的退出时间
            System.out.println("恭喜"+p.getName()+"成功通关");
            return;
        }
        int cnt = levels[lvlNo - 1].getTypeCnt();//当前级别需要有多少个字符串相匹配
        int charNum = levels[lvlNo - 1].getCharNum();//当前级别每次要匹配的字符数
        //根据级别来打印当前的字符串,同时匹配
        for (int i = 1; i <= cnt; i++) {//输入
            ;
            String prinStr = randomString(charNum);//定义随机字符串
            System.out.println("要匹配的字符串为:" + prinStr);
            boolean rst=isCorrect(prinStr);
            long now=System.currentTimeMillis();//系统当前的时间赋值
            if (!rst){
//                throw new InputTypeMismatchException("输入错误!游戏结束");
                a=levels[i-1].getCharNum()+a;
        }

             used=now-p.getStartTime();//已经使用的时间
//            p.setUsedTime(used);
//            int timeLimit=levels[lvlNo-1].getLimitTime();//现在的限定时间
//            if(used>timeLimit*1000){//系统自带的时间是毫秒,我们前面定义的是秒
//                throw new InputOutOfTimeException("输入超时,游戏结束!");
//            }
            int score = p.getTotalScores() + levels[lvlNo - 1].getPerScore();//把积分塞给客户
            p.setTotalScores(score);
        }

        p.setLvlNo(lvlNo+1);//升级操作
//        p.setStartTime(System.currentTimeMillis());//把开始的时间设置为现在的时间,重置时间
        run(p);
    }

    public String randomString(int charNum) {
        String str = "";
        for (int i = 0; i < charNum; i++) {
            int num = (int) (Math.random() * 2);
//            char c=(char)(int)(Math.random()*26+96);
            char c = (char) (int) (Math.random() * 26 + (num == 0 ? 65 : 97));
            str = str + c;

        }
        return str;
    }

    //输入并且匹配
    public boolean isCorrect(String str) {
        boolean result = false;
        System.out.println("请输入:");
        String input = s.next();
        if (str.equals(input)) {
            return true;
        }
        return result;
    }
    public void text(){
        int c=a;
        int b;
        int d=0;
        int p;
        int k;
        for (int j = 0; j < levels.length; j++) {
            d=levels[j].getCharNum()*levels[j].getTypeCnt()+d;
            k=levels[j].getTypeCnt();
            if (j==levels.length-1){
                b=100-(a*100)/k;
                p=((int)(d*60*60))/((int)(used/1000));
                System.out.println("正确率为:"+b+"%"+"\t"+"打字速度为:"+p+"个字/小时");

            }
        }

    }
}