Java Quick Hit项目

需求概述
1.根据输入速率和正确率将玩家分为不同级别
2.级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高
3.规定时间内完成规定次数的输入,正确率达到规定要求,则升级(玩家最高级别为6级,初始级别一律为1级)
4.用户错误输入一次,游戏结束

问题分析一:需要使用的类

发现类
1.玩家(Player)类
2.游戏(Game)类
3.级别(Level)类

发现类的属性
1.玩家类(Player)的属性
a.玩家当前级别号(levelNo)
b.玩家当前级别积分(currScore)
c.当前级别开始时间(startTime)
d,当前级别已用时间(elapsdTime)
2.游戏类(Game)
3.级别类(Level)
a.各级别号(levelNo)
b.各级别一次输出的字符串的长度(strLength)
c.各级别输出字符串的次数(strTimes)
d.各级别闯关的时间限制(timeLimit)
e.各级别正确输入一次的得分(perScore)

发现类的方法
1.玩家类(Player)的方法
玩游戏play()
2.游戏类(Game)方法
a.String printStr()
输出字符串,返回字符串用于和玩家输入比较。
b.void printResult(String out,String in)
比较游戏输出out和in,根据比较结果输出相应信息

优化设计
1.修改游戏类(Game),添加属性
玩家:Player
2.添加类:LevelParam
pubulic final static Level level[6]

问题分析二:主要功能分析

游戏输出字符串
1.生成字符串、输出字符串、返回字符串
2.生成固定长度但内容随机的字符串

确定输入并输出结果
1.输入
正确,未超时
2.输出
当前级别、当前积分、已用时间
3.计算玩家的当前级别、当前积分和已用时间

玩游戏
1.6个级别循环实现
2.每次晋级后积分清零、计时清零

问题分析三:界面交互设计

玩家输入正确和输入超时界面

玩家输入错误界面

重点来了
主函数(估计是这个吧,学得不怎么扎实,可纠正)

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

玩家(Player)类

import java.util.Scanner;
public class Player {
    private int levelNo;
    private int currScore;
    private long startTime=0;
    private int 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) {
        this.startTime = startTime;
    }
 
    public int getElapseTime(){
        return elapsedTime;
    }
    public void setElapseTime(int elapseTime) {
        this.elapsedTime = elapseTime;
    }
    public Player(int levelNo,int currScore,long startTime,int elapseTime){
        super();
        this.levelNo=levelNo;
        this.currScore=currScore;
        this.startTime=startTime;
        this.elapsedTime=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);
            }
        }
    }
}

游戏类(Game)

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);
            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();
        if((currentTimeplayer.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);
        }
    }
 
 
}

级别(Level)类

public class Level {
    private int levelNo;
    private int strLength;
    private int strTimes;
    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.strTimes=strTime;
        this.timeLimit=timeLimit;
        this.perScore=perScore;
    }
    public Level(){
        super();
    }
  
    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 strTimes;
    }
    public void setStrTime(int strTime) {
        this.strTimes = 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;
    }
}

LevelParam类

/**
 * 各个级别的具体参数信息
 */
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);
    }
}

好了,我先把基本结构和代码放这儿了,明天再看老师的具体要求,再来修改一次,不过大体上应该不需要太多修改了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想去见见你

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值