欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java之实战项目--QuickHit

程序员文章站 2022-03-26 21:18:52
任务描述需求概述根据输入速率和正确率将玩家分为不同级别级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高规定时间内完成规定次数的输入,正确率达到规定要求,则升级玩家*别为6级、初始级别一律为1级用户错误输入一次,游戏结束实战代码玩家类package QuickHit;/** * @Author lichangxin * @date 2020-07-27 * @Des */public class Player { private String name...

任务描述
需求概述
根据输入速率和正确率将玩家分为不同级别
级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高
规定时间内完成规定次数的输入,正确率达到规定要求,则升级
玩家*别为6级、初始级别一律为1级
用户错误输入一次,游戏结束

实战代码

玩家类

package QuickHit;

/**
 * @Author lichangxin
 * @date 2020-07-27
 * @Des
 */

public class Player {
    private String name;
    private int curLevel=1;//当前级别
    private int curScore;
    private long startTime=System.currentTimeMillis();
    private long usedTime;

    public String getName() {
        return name;
    }

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

    public int getCurLevel() {
        return curLevel;
    }

    public void setCurLevel(int curLevel) {
        this.curLevel = curLevel;
    }

    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 long getUsedTime() {
        return usedTime;
    }

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

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

    public void play(){
        Game g=new Game();
        g.player=this;
        g.run();
    }
}

关卡类

package QuickHit;

/**
 * @Author lichangxin
 * @date 2020-07-27
 * @Des
 */

public class Level {
    private int lvlNum;
    private int charCnt;//当前级别每次输出的字符数
    private int perScore;
    private long limitTime;//当前级别规定时间
    private int inputNum;//输入次数

    public Level(int lvlNum, int charCnt, int perScore, long limitTime, int inputNum) {
        this.lvlNum = lvlNum;
        this.charCnt = charCnt;
        this.perScore = perScore;
        this.limitTime = limitTime;
        this.inputNum = inputNum;
    }

    public int getLvlNum() {
        return lvlNum;
    }

    public int getCharCnt() {
        return charCnt;
    }

    public int getPerScore() {
        return perScore;
    }

    public long getLimitTime() {
        return limitTime;
    }

    public int getInputNum() {
        return inputNum;
    }
}

游戏类

package QuickHit;

import java.util.Scanner;

/**
 * @Author lichangxin
 * @date 2020-07-27
 * @Des
 */

public class Game implements LevelParams{
    Player player=new Player("sam");
    //生成一串字符
    public String getStrings(){
        int curLvl=player.getCurLevel();
        Level lvl= LEVELS[curLvl-1];
        String str="";
        for (int i = 0; i < lvl.getCharCnt(); i++) {
            int ranNum= (int) (Math.random()*10);
            str+=ranNum;
        }
        return str;
    }

    //流程控制
    public void run(){
        Scanner sc=new Scanner(System.in);
        System.out.println("欢迎"+player.getName()+"来到quickhit峡谷");
        while (player.getCurLevel()<=6) {
            int lvlNum = player.getCurLevel();
            Level lvl = LEVELS[lvlNum - 1];
            for (int i = 1; i <= lvl.getInputNum(); i++) {
                System.out.println("当前级别为" + i + ",积分为:" + player.getCurScore() + ",已用时间" + player.getUsedTime()/1000);
                String out = getStrings();
                System.out.println(out);
                System.out.println("请输入对应的字符串:");
                String input = sc.next();
                long now = System.currentTimeMillis();
                player.setUsedTime(now - player.getStartTime());
                if (!input.equals(out)) {
                    System.out.println("输入错误");
                    System.exit(-1);
                } else if (player.getUsedTime()/1000> lvl.getLimitTime()) {
                    System.out.println("输入超时");
                    System.exit(-1);
                }
                player.setCurScore(player.getCurScore() + lvl.getPerScore());

            }
            player.setCurLevel(player.getCurLevel()+1);
            player.setUsedTime(0);
            player.setStartTime(System.currentTimeMillis());
        }
    }

}

集合类

package QuickHit;

/**
 * @Author lichangxin
 * @date 2020-07-27
 * @Des
 */

public interface LevelParams {
    Level[] LEVELS={
            new Level(1,2,3,20,5),
            new Level(2,3,5,40,8),
            new Level(3,4,7,60,10),
            new Level(4,5,8,55,9),
            new Level(5,6,9,40,6),
            new Level(6,7,10,40,4),
    };
}

测试类

package QuickHit;

/**
 * @Author lichangxin
 * @date 2020-07-27
 * @Des
 */

public class test {
    public static void main(String[] args) {
        Player p=new Player("sam");
        p.play();
    }
}

本文地址:https://blog.csdn.net/zmzdmx/article/details/107617895

相关标签: java面向对象