最详细QuickHit小游戏

package QuickHit;

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

public Level(){}

public Level(int levelNo, int strLength, int strTimes, int timeLimit, int perScore) {
    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;
}

}

package QuickHit;
//级别参数,给各个级别参数赋值
public class LevelParam {
//创建一个存放六组数据的Level类型的数组
public final static Level levels[] = new Level[6];//对应六个级别
static {
//编号 一次输出字符串的长度 输出字符串的次数 闯关的时间限制 正确输入一次的得分
levels[0] = new Level(1,2,6,30,1);
levels[1] = new Level(2,3,5,26,2);
levels[2] = new Level(3,4,4,22,5);
levels[3] = new Level(4,5,3,18,8);
levels[4] = new Level(5,6,2,15,10);
levels[5] = new Level(6,7,1,12,15);
}
}

package QuickHit;

import java.util.Random;

public class Game {
private Player player;

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

//生成字符串并输出字符串,用于和玩家输入进行比较
public String printStr(){
    //获取玩家当前级别的输出字符串长度
    int strLength = LevelParam.levels[player.getLevelNo()-1].getStrLength();
    //StringBuffer字符长度可修改
    StringBuffer buffer = new StringBuffer();
    Random random = new Random();

    //使用for循环根据各级别的字符串长度向StringBuffer字符串中添加字符以输出
    //append方法向字符串后面加字符
    for (int i = 0; i < strLength; i++) {
        //产生随机整数数
        int rand = random.nextInt(strLength);
        //根据随机数拼接字符串
        switch (rand){
            case 0:
                buffer.append("1");
                break;
            case 1:
                buffer.append("2");
                break;
            case 2:
                buffer.append("3");
                break;
            case 3:
                buffer.append("4");
                break;
            case 4:
                buffer.append("5");
                break;
            case 5:
                buffer.append("6");
                break;
        }
    }
    //输出字符串
    System.out.println(buffer);

    //toString方法:将StringBuffer类型转为String类型
    //返回字符串用于和玩家输入比较
    return buffer.toString();
}

//判断玩家输入是否正确,输出相应结果
//String out 游戏输出的字符串
//String in 玩家输入的字符串
public void printResult(String out,String in){

    boolean flag;//用于比较out和in是否相等,从而判断是否正确
    if (out.equals(in)){
        flag = true;
    }else {
        flag = false;
    }

    if (flag){
        //获取系统的时间
        long currentTime = System.currentTimeMillis();
        //如果超时
        if ((currentTime - player.getStartTime())/1000>
                LevelParam.levels[player.getLevelNo()-1].getTimeLimit()){
            System.out.println("你输入的太慢了,已经超时,退出!");
            //System类的一个静态方法,作用是是关掉jvm,status参数是状态参数,0是正常关闭!其他非0为非正常关闭
            System.exit(1);
        }else {
        //没有超时,计算当前积分
            player.setCurScore(player.getCurScore()+LevelParam.levels[player.getLevelNo()-1].getPerScore());
            //计算已用时间
            player.setElapsedTime((int)((currentTime - player.getStartTime())/1000));
            //输出当前积分、当前级别、已用时间
            System.out.println("输入正确,您的级别"+player.getLevelNo()+
                    ",您的积分"+player.getCurScore()+",已用时间"+player.getElapsedTime()+"秒");
            //判断用户是否已经闯过最后一关
            if (player.getLevelNo() == 6){
                //计算闯关分数 score为闯过第六关获得的积分
                int score = LevelParam.levels[player.getLevelNo()-1].getPerScore()*LevelParam.levels[player.getLevelNo()-1].getStrTimes();
                if (player.getCurScore() == score){
                    System.out.println("恭喜你闯关成功!!!");
                    System.exit(0);
                }
            }
        }
    }else {
        //输入错误
        System.out.println("输入错误,退出");
        System.exit(1);
    }
}

}

package QuickHit;

import java.util.Scanner;

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

//玩家玩游戏
public void play(){
    //this直接引用,指向当前对象本身
    Game game = new Game(this);
    Scanner input = new Scanner(System.in);

    //外层循环,循环一次级别晋级一次
    for (int i = 0; i < LevelParam.levels.length; i++) {
        //晋级
        this.levelNo++;
        //晋级计时清零
        this.startTime = System.currentTimeMillis();
        this.curScore = 0;
        //内存循环,循环一次完成一次字符串的输出、输入、比较
        for (int j = 0; j < LevelParam.levels[levelNo - 1].getStrTimes(); j++) {
            //游戏输出字符串
            String outStr = game.printStr();
            //用户输入字符串
            String inStr = input.next();
            //判断是否正确
            game.printResult(outStr,inStr);
        }
    }
}

public Player(){}

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

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 long getStartTime() {
    return startTime;
}

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

public int getElapsedTime() {
    return elapsedTime;
}

public void setElapsedTime(int elapsedTime) {
    this.elapsedTime = elapsedTime;
}

}

package QuickHit;

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

上一篇:18、递归


下一篇:ERA5气压层数据驱动WRF的一些问题