第五章项目:QuickHit

需求概述:

      根据输入速率和正确率将玩家分为不同级别,级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高。如果玩家在规定时间内完成规定次数的输入,正确率达到规定要求,则玩家升级(为了简单起见,规定用户只要错误一次,则游戏结束)。最高为6级,刚开始一律1级。

案例覆盖的技能点:

     面向对象设计的思想

     使用类图理解类的关系

     类的封装

     构造方法的使用

     this和static关键字的使用

需要用到的类:

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

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

     LevelParam类:定义一个长度为6的Level数组,用来存放各级别的具体参数信息

     游戏(Game)类:Player对象的属性,两个方法,printStr(用来随机输出字符串),printResult(用来比较输出参数和输入参数和计算)

Player类

package quickhit;

import java.util.Scanner;

public class Player {
    //属性
    public int levelNo;
    public int curScore;
    public long startTime;
    public int elapsedTime;
    
    //有参构造
    public Player(int levelNo, int curScore, long startTime, int elapsedTime) {
        this.levelNo = levelNo;
        this.curScore = curScore;
        this.startTime = startTime;
        this.elapsedTime = elapsedTime;
    }
 
    //无参构造
    public Player() {
        
    }



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

    //玩游戏的方法
    public void play(){
        //调用game的带参.this代表Player属性
        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.curScore=0;
            //满级通关
            if(levelNo==6){
                System.out.println("通关..........");
                break;
            }
            //内层循环,输出输入比较
            for (int j = 0; j < LevelParam.level[levelNo-1].getStrTimes(); j++) {
                //输出字符串
                String outStr=game.printStr();
                //接收用户输入字符串
                String inStr=input.next();
                //调用game的printResult方法,对比
                game.printResult(outStr, inStr);
            }           
            
        }
} }

Level类

package quickhit;

public class Level {

    public int levelNo;
    public int strLength;
    public int strTimes;
    public int timeLimit;
    public int 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;
    }

        //无参
    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;
    }
    
    
}

LevelParam类

package quickhit;

public class LevelParam {

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

Game类

package quickhit;

import java.util.Random;


public class Game {
    
    public Player player;
    

    public Player getPlayer() {
        return player;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }

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

    public String printStr(){
        //定义一个int类型的对应各级编号应输出字符串的长度
        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;
            }
        }
        //输出
        String str=buffer.toString();
        System.out.println(str);
        return str;
    }
    
    public void printResult(String out,String in){
        
        long currentTime=System.currentTimeMillis();
        //判断是否一致
        if(out.equals(in)){
        //判断是否超时
        if((currentTime-player.getStartTime())/1000>LevelParam.level[player.getLevelNo()-1].getTimeLimit()){
            System.out.println("太慢了吧!~");
            System.exit(1);
        }else{
        
        //计算当前积分
        player.setCurScore(player.getCurScore()+LevelParam.level[player.getLevelNo()-1].getPerScore());
        //计算时间
        player.setElapsedTime((int)(currentTime-player.getStartTime())/1000);
        //输出级别,积分和时间
        System.out.println("输入正确,您的级别"+player.getLevelNo()+"积分"+player.curScore+"已用时间"+player.getElapsedTime());
        }
        }else{
            System.out.println("输入错误........");
            System.exit(1);
        }
    }
}

测试类

package quickhit;

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

}

游戏效果

原文地址:https://www.cnblogs.com/hr1997/p/5539469.html