好玩的快速击键项目

      今天做了一个很有意思的项目,叫快速击键,这个项目他主要考察的就是:面向对象设计的思想, 类的封装, 构造方法的使用, this、static关键字的使用

 一.项目需求:

   根据输入的速率和正确率将玩家分为不同级别,级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高。如果玩家在规定的时间内完成规定次数的输入,正确率达到规定标准,则玩家升级(在这里规定只要错误输入一次,则游戏结束)。玩家最高级为6级,初始级别一律为一级。

运行效果图:

二.分析需求:

01.发现类:

    玩家类(Player),游戏类(Game),级别类(Level),LevelParam(保存每一个级别的参数信息)

02.发现类中的属性:

   玩家类(Player)的属性:

levelNo:玩家当前级别号,currScore:玩家当前级别积分,startTime:玩家当前级别开始时间,elapsedTime 当前级别已用时间

   级别类(Level)的属性:

 levelNo:各级别编号,strLength各级别一次输入字符串的长度,strTime各级别输出字符串的次数,timeLimit各级别闯关的时间限制,perScore各级别正确输入一次的得分。

 游戏类(Game):既然是玩游戏,肯定有玩家,所以在游戏类中要添加一个玩家的属性player;

03.发现类的方法

   玩家类(Player)的方法:play();

  游戏类(Game)的方法:

String printStr(),输出字符串,返回字符串用于和玩家的输入进行比较。

void printResult(String out,String in)比较输出out和玩家输入in

三.实现功能:

第一步我们要形成这个项目的一个整体框架。

Level类:

package cn.hyj.quickhit;

/**
 * 级别类
 * 
 * @author HYJ
 * 
 */
public class Level {
    private int levelNo;// 各级别编号
    private int strLength;// 各级别一次输出字符串的长度
    private int strTimes;// 各级别输出字符串的次数
    private int timeLimit;// 各级别闯关的时间限制
    private int perScore;// 各级别正确输入一次的得分

    public Level() {
        super();
        // TODO Auto-generated constructor stub
    }

    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;
    }

    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;
    }

}

LevelParam类:

package cn.hyj.quickhit;

//存放级别的属性
public class LevelParam {
    // 定义级别类型数组保存级别信息
    public static final Level[] levels = new Level[6];
    // 静态代码块给数组赋初值
    static {
        levels[0] = new Level(1, 2, 5, 60, 1);
        levels[1] = new Level(2, 3, 6, 60, 2);
        levels[2] = new Level(3, 4, 7, 60, 5);
        levels[3] = new Level(4, 5, 8, 60, 8);
        levels[4] = new Level(5, 6, 9, 60, 10);
        levels[5] = new Level(6, 7, 10, 60, 15);
    }

}

Game类:

package cn.hyj.quickhit;

import java.util.Random;

/**
 * 游戏类
 * 
 * @author HYJ
 * 
 */
public class Game {
    Player play;// 代表玩家
    /**
     * 无参构造
     */
    public Game() {
    }

    public Game(Player plays) {
        play = plays;
    }
    /**
     * 生成字符串
     */
    public String getStr() {
        // 获取级别对应的要输出字符串的长度
        int strLength = LevelParam.levels[play.getLevelNo() - 1].getStrLength();
        StringBuffer str = new StringBuffer();
        // 实例化生成随机数的对象
        Random rd = new Random();
        // 通过循环输出要生成的字符串
        for (int i = 0; i < strLength; i++) {
            // 产生随机数
            int random = rd.nextInt(strLength);
            switch (random) {
            case 0:
                str.append(">");
                break;
            case 1:
                str.append("<");
                break;
            case 2:
                str.append("*");
                break;
            case 3:
                str.append("&");
                break;
            case 4:
                str.append("#");
                break;
            case 5:
                str.append("@");
                break;
            }

        }
        // 输出字符串
        System.out.println(str);
        // 返回该字符串的值,用于和用户输入字符串的值作比较
        return str.toString();
    }

    /**
     * 系统给的字符串和用户输入的字符串作对比
     * 
     * @param outstr
     *            系统输出的字符串
     * @param instr
     *            用户输入的字符串
     */
    public void strThesame(String outstr, String instr) {
        Boolean flag = false;// 定义标记默认不同
        if (outstr.equals(instr)) {
            // 证明两个字符串相同
            flag = true;// 改变标记
        } else {
            System.out.println("输入错误,退出!");
            System.exit(0);
        }
        /**
         * 如果输入正确则会出现两种情况 01.如果超时,则直接输出错误信息并退出程序 02.如果没有超时: 计算玩家当前积分 计算玩家已用时间
         * 输出玩家当前级别,当前积分和已用时间 判断用户是否已经闯过最后一关
         * */
        if (flag) {
            long currentTime = System.currentTimeMillis(); // 返回以毫秒为单位的当前时间
            // 如果超时的情况
            if (LevelParam.levels[play.getLevelNo() - 1].getTimeLimit() < (currentTime - play
                    .getStartTime()) / 1000) {
                System.out.println("你输入太慢了,已经超时,退出!");
                System.exit(0);
            }
            // 如果没有超时的情况
            else {
                // 计算玩家当前积分
                play.setCurrScore(play.getCurrScore()
                        + LevelParam.levels[play.getLevelNo() - 1]
                                .getPerScore());
                // 计算玩家已用时间
                play.setEndTime((int) (currentTime - play.getStartTime()) / 1000);
                // 输出玩家当前级别,当前积分和已用时间
                System.out.println("输入正确,您的级别" + play.getLevelNo() + ",您的积分"
                        + play.getCurrScore() + ",已用时间" + play.getEndTime()
                        + "秒。");

            }
        }
    }

}

Player类:

package cn.hyj.quickhit;

import java.util.Scanner;

/**
 * 玩家类
 * 
 * @author HYJ
 * 
 */
public class Player {
    private int levelNo;// 玩家当前级别号
    private int currScore;// 玩家当前积分
    private long startTime;// 当前玩家级别的开始时间
    private int endTime;// 当前玩家级别的已用时间

    /**
     * 玩家玩游戏的方法
     */
    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.setCurrScore(0);
            this.setStartTime(System.currentTimeMillis());
            if (this.levelNo == 6) {
                System.out.println("高手,恭喜已通关!");
                break;
            }
            // 内层循环控制用户输入的字符串和系统的字符串作比较
            for (int j = 0; j < LevelParam.levels[levelNo - 1].getStrTimes(); j++) {
                // 游戏输出字符串
                String outstr = game.getStr();
                // 调用game类的方法比较两个字符串是否相同
                // 接收用户输入的字符串
                String instr = input.next();
                game.strThesame(outstr, instr);

            }
        }

    }

    public Player() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Player(int levelNo, int currScore, long startTime, int endTime) {
        super();
        this.levelNo = levelNo;
        this.currScore = currScore;
        this.startTime = startTime;
        this.endTime = endTime;
    }

    public int getLevelNo() {
        return levelNo;
    }

    public void setLevelNo(int levelNo) {
        this.levelNo = levelNo;
    }

    public int getCurrScore() {
        return currScore;
    }

    public void setCurrScore(int currScore) {
        this.currScore = currScore;
    }

    public long getStartTime() {
        return startTime;
    }

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

    public int getEndTime() {
        return endTime;
    }

    public void setEndTime(int endTime) {
        this.endTime = endTime;
    }

}

测试:

package cn.hyj.quickhit;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
//实例化玩家对象 Player player
= new Player(); //调用玩家玩游戏的方法
player.play(); } }

 

原文地址:https://www.cnblogs.com/hyjj/p/5418283.html