QuickHit项目——Java实现

package src.com.company;

public class Main {

    public static void main(String[] args) {

        Player player=new Player();
        player.play();
    }
}


package src.com.company;

/**
 * 各个级别的具体参数信息
 */
public class LevelParam {
    public final static Level levels[]=new Level[6];//可以直接通过类名访问,且无法修改其值
    //对应六个级别
    static {
        levels[0]=new Level(1,2,10,30,1);
        levels[1]=new Level(2,3,9,26,2);
        levels[2]=new Level(3,4,8,22,5);
        levels[3]=new Level(4,5,7,18,8);
        levels[4]=new Level(5,6,6,15,10);
        levels[5]=new Level(6,7,5,12,15);
    }
}


package src.com.company;

import java.util.Scanner;

/**
 * 玩家类
 */
public class Player {
    private int levelNo;//当前级别号
    private int currScore;//当前级别积分
    private long startTime=0;//当前级别开始时间
    private int elapseTime;//当前级别已用时间
    /*
    设置setter、getter()方法
     */
    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 getElapseTime(){
        return elapseTime;
    }
    public void setElapseTime(int elapseTime) {
        this.elapseTime = elapseTime;
    }
    /*
    构造方法
    */
    public Player(int levelNo,int currScore,long startTime,int elapseTime){
        super();
        this.levelNo=levelNo;
        this.currScore=currScore;
        this.startTime=startTime;
        this.elapseTime=elapseTime;
    }
    public Player(){
        super();
    }
    /*
    方法:玩家玩游戏
     */
    public void play(){
        Game game=new Game(this);
        Scanner scanner=new Scanner(System.in);
        //外层循环代表等级
        for(int i=0;i<LevelParam.levels.length;i++){
            levelNo +=1;//玩家晋级
            startTime=System.currentTimeMillis();//获得新的游戏开始时间
            currScore=0;//每次晋级玩家积分清零
            //内层循环代表每个级别所需要玩的次数
            for(int j=0;j<LevelParam.levels[i].getStrTime();j++){
                String out=game.printStr();//系统生成的字符串
                String in=scanner.next();//玩家输入的字符串
                game.printResult(out,in);//比较产生结果
            }
        }
    }
}



package src.com.company;

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

    /*
    构造方法
     */
    public Level(int levelNo,int strLength,int strTime,int timeLimit,int perScore){
        super();
        this.levelNo=levelNo;
        this.strLength=strLength;
        this.strTime=strTime;
        this.timeLimit=timeLimit;
        this.perScore=perScore;
    }
    public Level(){
        super();
    }
    /*
     设置setter、getter()方法
     */
    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 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 src.com.company;

import java.util.Random;//导入java.util包中的Random类

/**
 * 游戏类
 */

public class Game {
    private Player player;

    public Game(Player player){
        super();
        this.player=player;
    }
    /*
    输出字符串
     */
    public String printStr(){
        StringBuffer buffer=new StringBuffer();//在循环中执行字符串拼接操作
        Random random=new Random();
        for(int i=0;i<LevelParam.levels[player.getLevelNo()-1].getStrLength();i++){
            int rand=random.nextInt(6);//产生0-5之间的随机数
            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());
        return buffer.toString();
    }
    /*
    确认玩家输入是否正确和输出相应的结果信息
     */
    public void printResult(String out,String in){
        //输入和输出相同时:判断是否超时;否则闯关失败
        if(out.equals(in)){
            long currentTime=System.currentTimeMillis();//获得自1970-1-1到现在的时间距离,单位是毫秒
            if((currentTime-player.getStartTime())/1000>LevelParam.levels[player.getLevelNo()-1].getTimeLimit()){
                System.out.println("游戏超时,闯关失败!");
                System.exit(1);
            }else {
                player.setCurrScore(player.getCurrScore()+LevelParam.levels[player.getLevelNo()-1].getPerScore());
                System.out.println("输入正确,您的积分是"+player.getCurrScore()+"分," +
                        "您的级别是"+player.getLevelNo()+"级," +
                        "您所用时间"+(currentTime-player.getStartTime())/1000+"秒");
                if(player.getLevelNo()==6){
                    int score=LevelParam.levels[player.getLevelNo()-1].getPerScore()*LevelParam.levels[player.getLevelNo()-1].getStrTime();
                    if(player.getCurrScore()==score){
                        System.out.println("闯关成功!!!");
                        System.exit(0);
                    }
                }
            }
        }else {
            System.out.println("输入错误,闯关失败!!!");
            System.exit(1);
        }
    }


}


 

  • 11
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值