OOP小游戏QuickHit

package com.itek;

import java.util.Random;

/**
 * @Author Liu Lei
 * @Date 2020/8/30 1:39
 * @Version 1.0
 * @description 在游戏类中要添加一个玩家的属性player
 * String printStr(),输出字符串,返回字符串用于和玩家的输入进行比较。
 * void printResult(String out,String in)比较输出out和玩家输入in
 */
public class Game {

    public Player player;

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

    /**
     * 输出字符串,返回字符串用于和玩家的输入进行比较
     *
     * @return
     */

    public String printStr() {
        //获取级别对应的要输出字符串的长度
        int strLength = LevelParam.level[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;
            }
        }
        //输出字符串
        System.out.println(buffer);
        // 返回该字符串的值,用于和用户输入字符串的值作比较
        return buffer.toString();
    }

    //系统给的字符串和用户输入的字符串对比
    //out  系统输出的字符串
    //in 用户输入的字符串
    public void printResult(String out, String in) {
        boolean flag = false;  //定义标记默认不同
        if (out.equals(in)) {
            //证明两个字符串相同
            flag = true;  //改变标记
        } else {
            System.out.println("输出错误,退出");
            System.exit(0);
        }

        /**
         * 如果输入正确则会出现两种情况 01.如果超时,则直接输出错误信息并退出程序 02.如果没有超时: 计算玩家当前积分 计算玩家已用时间
         * 输出玩家当前级别,当前积分和已用时间 判断用户是否已经闯过最后一关
         * */
        if (flag) {
            long currentTime = System.currentTimeMillis();
            //如果超时
            if ((currentTime - player.getStartTime()) / 1000 > LevelParam.level[player.getLevelNo() - 1].getTimeLimit()) {
                System.out.println("您输入太慢了,已经超时,退出");
                System.exit(1);
            }
            //如果没有超时
            else {
                //计算玩家当前积分
                player.setCurrScore(player.getCurrScore() + LevelParam.level[player.getLevelNo() - 1].getPerScore());
                //计算玩家已用时间
                player.setElapsedTime((int) (currentTime - player.getStartTime()) / 1000);
                //输出玩家当前级别,当前积分和已用时间
                System.out.println("输入正确,您的级别:" + player.getLevelNo() + "您的积分:" + player.getCurrScore() + "已用时间:" + player.getElapsedTime() + "秒");
            }


        }

    }

}
package com.itek;

/**
 * @Author Liu Lei
 * @Date 2020/8/30 1:39
 * @Version 1.0
 * @description 游戏级别类
 */


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

    public Level() {
    }

    public Level(int levelNo, int strLength, int strTime, long timeLimit, int perScore) {
        this.levelNo = levelNo;
        this.strLength = strLength;
        this.strTime = strTime;
        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 getStrTime() {
        return strTime;
    }

    public void setStrTime(int strTime) {
        this.strTime = strTime;
    }

    public long getTimeLimit() {
        return timeLimit;
    }

    public void setTimeLimit(long timeLimit) {
        this.timeLimit = timeLimit;
    }

    public int getPerScore() {
        return perScore;
    }

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

/**
 * @Author Liu Lei
 * @Date 2020/8/30 1:44
 * @Version 1.0
 * @description 添加类
 */
public class LevelParam {

    //定义6个游戏级别
    public final static Level level[] = new Level[6];

    //各级别数据初始化
    static {
        level[0] = new Level(1, 2, 10, 30, 1);
        level[1] = new Level(2, 3, 9, 26, 2);
        level[2] = new Level(3, 4, 8, 22, 5);
        level[3] = new Level(4, 5, 7, 18, 8);
        level[4] = new Level(5, 6, 6, 15, 10);
        level[5] = new Level(6, 7, 2, 12, 15);
    }


}
package com.itek;

import java.util.Scanner;

/**
 * @Author Liu Lei
 * @Date 2020/8/30 1:39
 * @Version 1.0
 * @description 玩家类
 */

public class Player {
    private int levelNo; //玩家当前级别
    private int currScore; //玩家当前级别积分
    private long startTime; //当前级别开始时间
    private int elapsedTime; //当前级别已用时间

    public Player() {
    }

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

    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) {
        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.level.length; i++) {
            //晋级
            this.levelNo += 1;
            //晋级后计时清零,积分清零
            this.startTime = System.currentTimeMillis();
            this.currScore = 0;
            if (this.levelNo == 6) {
                System.out.println("恭喜通关");
                break;
            }
            //内层循环,循环一次完成一次字符串的输出,输入,比较
            for (int j = 0; j < LevelParam.level[levelNo - 1].getStrTime(); j++) {
                //游戏输出字符串
                String outstr = game.printStr();
                //接受用户输入
                String instr = input.next();
                //游戏判断玩家输入是否正确,并输出相应结果信息
                game.printResult(outstr, instr);
            }
        }


    }

}
package com.itek;

/**
 * @Author Liu Lei
 * @Date 2020/8/30 2:31
 * @Version 1.0
 * @description
 */
public class Test {
    public static void main(String[] args) {
        Player player = new Player();
        player.play();
    }
}
原文地址:https://www.cnblogs.com/shanghuliu/p/13593766.html