Java面向对象–QuickHit项目

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

二、项目所覆盖的知识点:
①面向对象设计的思想.
②使用类图理解类的关系
③类的封装
④构造方法的使用
⑤this和static关键字的使用


ecilpse运行效果如下:

 


Player类 

注意:

//玩的方法
    /**
     * 1.创建Game对象并传入player属性
     * 2.外层循环(循环次数是6,每循环一次,玩家提升一级)
     *     (1)晋级
     *     (2)积分清零,计时清零。
     *     (3)内层循环(循环次数是该级别的strTime,每循环一次完成一次人机交互)。
     *         [1].游戏输出字符串
     *         [2].玩家输入字符串
     *          [3].判断玩家输入的字符并输出相应的结果
     */

package QuickHit;

import java.util.Scanner;

/**
 * 
 * @author LiQingLin
 *
 */

public class Player {
	private int levelNo;//当前级别号
	private int curScore;//当前级别积分
	private long startTime;//当前级别开始时间
	private int elapsedTime;//当前级别已用时间
	
	//玩的方法
	/**
	 * 1.创建Game对象并传入player属性
	 * 2.外层循环(循环次数是6,每循环一次,玩家提升一级)
	 * 	(1)晋级
	 * 	(2)积分清零,计时清零。
	 * 	(3)内层循环(循环次数是该级别的strTime,每循环一次完成一次人机交互)。
	 *		 [1].游戏输出字符串
	 *		 [2].玩家输入字符串
	 * 		 [3].判断玩家输入的字符并输出相应的结果
	 */
	public void play() {
		Scanner input=new Scanner(System.in);
		//Game的构造方法需要传入一个Player对象,但现在就是在Player类下,Player对象还未创建
		Game game =new Game(this);
		for (int i = 0; i <=5; i++) {
			//等级提升1级(晋级)
			this.levelNo+=1;
			//积分清零
			this.curScore=0;
			//计时清零
//			this.elapsedTime=0;
			this.startTime=System.currentTimeMillis();
			for (int j = 0; j <=LevelParam.levels[this.getLevelNo()-1].getStrTimes(); j++) {
				//游戏输出字符串
				String out =game.printStr();
				//玩家输入字符串
				String in=input.next();
				//判断玩家输入的字符并输出相应的结果
				game.printResult(out, in);
			}
			
		}
		
		
	}
	//无参构造
	public Player() {
		super();
		// TODO Auto-generated constructor stub
	}
	//有参构造
	public Player(int levelNo, int curScore, long startTime, int elapsedTime) {
		super();
		this.levelNo = levelNo;
		this.curScore = curScore;
		this.startTime = startTime;
		this.elapsedTime = elapsedTime;
	}

	/**
	 * @return the levelNo
	 */
	public int getLevelNo() {
		return levelNo;
	}

	/**
	 * @param levelNo the levelNo to set
	 */
	public void setLevelNo(int levelNo) {
		this.levelNo = levelNo;
	}

	/**
	 * @return the curScore
	 */
	public int getCurScore() {
		return curScore;
	}

	/**
	 * @param curScore the curScore to set
	 */
	public void setCurScore(int curScore) {
		this.curScore = curScore;
	}

	/**
	 * @return the startTime
	 */
	public long getStartTime() {
		return startTime;
	}

	/**
	 * @param startTime the startTime to set
	 */
	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}

	/**
	 * @return the elapsedTime
	 */
	public int getElapsedTime() {
		return elapsedTime;
	}

	/**
	 * @param elapsedTime the elapsedTime to set
	 */
	public void setElapsedTime(int elapsedTime) {
		

		this.elapsedTime = elapsedTime;
	}
	
}

 Game类

/**
     * 游戏输出字符串  String printStr()
     * 
     * 要点:1.生成字符串2.输出字符串3.返回字符串
     * 思路:查询player的级别号levelNo,然后根据级别号到LevelParam类中获取该级别的字符串长度
     * @return
     */

/**
     * 确认输入的字符并输出结果  printResult(String out,String in)
     * @param out
     * @param in
     *         补充:
     * currentTimeMillis()函数表示:   自1970年1月1日开始计算到先现在的总毫秒
     * 
     * System.exit(int status)  当status为0时表示正常退出程序;为非0的其他整数时(包括负数,一般是1或-1)表示非正常退出当前程序
     * 
     *
     */

package QuickHit;

import java.util.Random;

import javax.swing.plaf.synth.SynthScrollBarUI;
/**
 * 
 * @author LiQingLin
 *要点:Game类中的player属性代表玩家,查询player的级别号levelNo,
 *然后根据级别号到LevelParam类中获取该级别的字符串长度
 *
 */
public class Game {

    private Player player;
    
    public Game() {
        
    }
    public Game(Player player) {
        super();
        this.player = player;
    }    
    /**
     * @return the player
     */
    public Player getPlayer() {
        return player;
    }
    /**
     * @param player the player to set
     */
    public void setPlayer(Player levelNo) {
        this.player = levelNo;
    }


    
    /**
     * 游戏输出字符串
     * 
     * 要点:1.生成字符串2.输出字符串3.返回字符串
     * 思路:查询player的级别号levelNo,然后根据级别号到LevelParam类中获取该级别的字符串长度
     * @return
     */
    public String printStr(){
    
    StringBuffer buffer=new StringBuffer();    
    Random random=new Random();
    
        //查询player的级别号levelNo,然后根据级别号到LevelParam类中获取该级别的字符串长度
        int strLength=LevelParam.levels[player.getLevelNo()-1].getStrLength();
        //通过循环生成要输出的字符串
        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() ;
    }
    
    
    /**
     * 确认输入的字符并输出结果
     * @param out
     * @param in
     *         补充:
     * currentTimeMillis()函数表示:   自1970年1月1日开始计算到先现在的总毫秒
     * 
     * System.exit(int status)  当status为0时表示正常退出程序;为非0的其他整数时(包括负数,一般是1或-1)表示非正常退出当前程序
     * 
     *
     */
    public void printResult(String out,String in) {
        //目前时间 (currentTimeMillis()函数表示自1970年1月1日开始计算到先现在的总毫秒)
        long currentTime=System.currentTimeMillis();
        
        //确认玩家输入是否正确
        if(out.equals(in)) {
            //如果超时   判断方法:(目前时间-开始时间)/1000与限制时间比较
            if((currentTime-player.getStartTime())/1000>LevelParam.levels[player.getLevelNo()-1].getTimeLimit()) {
                System.out.println("你输入太慢了,已经超时,退出!");
                System.exit(1);
            }else {
                
                //不超时
                /**
                 * 第一步:计算玩家当前积分
                 * 第二步:计算玩家已用时间
                 * 第三步:输出玩家当前级别、当前积分和已用时间
                 */
                //计算玩家当前积分
                player.setCurScore(player.getCurScore()
                        +LevelParam.levels[player.getLevelNo()-1].getPerScore());
                //计算玩家已用时间
                
//                int time=(int)(currentTime-player.getStartTime())/1000;
                player.setElapsedTime((int)(currentTime-player.getStartTime())/1000);
                //输出玩家当前级别、当前积分和已用时间
                System.out.println("输入正确,您的级别"+player.getLevelNo()+",您的积分"+player.getCurScore()+",已用时间"+player.getElapsedTime()+"秒");
            }
        }else {
            System.out.println("输入错误,退出!");
            System.exit(1);

        }
        
        
    }
}


 

Level类

package QuickHit;

public class Level {
	private int levelNo;//各级别编号
	private int strLength;//各级别一次输出各字符串的长度
	private int strTimes;//各级别输出字符串的次数
	private int timeLimit;//各级别闯关的时间限制
	private int perScore;//各级别正确输入一次的得分
	
	public Level() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Level(int levelNo, int strLength, int strTimes, int timeLimit, int perScore) {
		super();
		this.levelNo = levelNo;
		this.strLength = strLength;
		this.strTimes = strTimes;
		this.timeLimit = timeLimit;
		this.perScore = perScore;
	}
	/**
	 * @return the levelNo
	 */
	public int getLevelNo() {
		return levelNo;
	}
	/**
	 * @param levelNo the levelNo to set
	 */
	public void setLevelNo(int levelNo) {
		this.levelNo = levelNo;
	}
	/**
	 * @return the strLength
	 */
	public int getStrLength() {
		return strLength;
	}
	/**
	 * @param strLength the strLength to set
	 */
	public void setStrLength(int strLength) {
		this.strLength = strLength;
	}
	/**
	 * @return the strTimes
	 */
	public int getStrTimes() {
		return strTimes;
	}
	/**
	 * @param strTimes the strTimes to set
	 */
	public void setStrTimes(int strTimes) {
		this.strTimes = strTimes;
	}
	/**
	 * @return the timeLimit
	 */
	public int getTimeLimit() {
		return timeLimit;
	}
	/**
	 * @param timeLimit the timeLimit to set
	 */
	public void setTimeLimit(int timeLimit) {
		this.timeLimit = timeLimit;
	}
	/**
	 * @return the perScore
	 */
	public int getPerScore() {
		return perScore;
	}
	/**
	 * @param perScore the perScore to set
	 */
	public void setPerScore(int perScore) {
		this.perScore = perScore;
	}
	
	
}

LevelParam类

package QuickHit;

public class LevelParam {
	public final static Level levels[]=new Level[6];//对应六个级别
	static {
		levels[0]=new Level(1,1,3,30,1);
		levels[1]=new Level(2,2,3,30,2);
		levels[2]=new Level(3,3,3,30,5);
		levels[3]=new Level(4,4,3,30,8);
		levels[4]=new Level(5,5,3,30,10);
		levels[5]=new Level(6,6,3,30,15);

	}
}

Test类 

package QuickHit;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		
		Player player=new Player();
		player.setStartTime(System.currentTimeMillis());
		player.play();
		
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值