QuickHit源码

QuickHit这个项目是《Java面向对象程序开发及实战》 课工场出品   ISBN  978-7-115-47399-8

一书的第十章,综合实战。

整个项目分为4个类:

  1. Player                    玩家类     
  2. Level                     等级类
  3. LevelParam          等级参照类
  4. Game                   游戏类

然后自己写一个测试类,就可以实现。在这里我都附上源码,希望大家指正。

Player                    玩家类     

package com.pojo;

import java.util.Scanner;


/**
  *  玩家类
 * @author NZL
 *
 */
public class Player {
	
	private int leveINo;//当前等级
	private int curScore;//当前积分
	private long startTime;//开始时间
	private long elapsedTime;//结束时间
	
	public Player() {
		super();
	}

	public Player(int leveINo) {
		super();
		this.leveINo = leveINo;
	}
	
	/*
	  *  玩游戏的方法 
	 */
	public void play() {
		
		Game game = new Game(this);
		for(int i = LevelParam.level[this.leveINo].getLeveINo();i<=LevelParam.level.length;i++) {
			
			this.startTime = System.currentTimeMillis();//当前等级开始计时
			
			for(int j = 0;j<LevelParam.level[this.leveINo].getStrTimes();j++) {
				String str = game.printStr();//获取当前玩家要测试的字符串
				
				System.out.println("当前等级:" + this.leveINo + "\n请输入:" +str);
				long startTime = System.currentTimeMillis();//获取每次输入前的时间
				
				@SuppressWarnings("resource")
				Scanner input = new Scanner(System.in);
				String string = input.next();//接收玩家输入的字符串
				
				long lastTime = System.currentTimeMillis();//获取每次输入后的时间
				
				this.elapsedTime = lastTime;//更新当前等级的停留时间
				
				//验证玩家输入的是否正确
				if(!game.printResult(str,string,(lastTime-startTime))) {
					System.exit(1);
				}
			}
			
			this.leveINo++;//升级
			if(this.leveINo > LevelParam.level.length+1) {
				System.out.println("恭喜你,你已经通关了!!!");
				//System.exit(0);
			}
		}
		
	}

	
	public int getLeveINo() {
		return leveINo;
	}

	public void setLeveINo(int leveINo) {
		this.leveINo = leveINo;
	}

	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 long getElapsedTime() {
		return elapsedTime;
	}

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

Level                     等级类

package com.pojo;
/**
  *  等级类
 * @author NZL
 *
 */
public class Level {
	
	private int leveINo;//各级别编号
	private int strLength;//各级别一次的输出字符串的长度
	private int strTimes;//各级别输出字符串的次数
	private int timeLimit;//各级别的闯关时间限制
	private int perScore;//各级别正确输入一次的得分
		
	public Level() {
		super();
	}

	public Level(int leveINo, int strLength, int strTimes, int timeLimit, int perScore) {
		super();
		this.leveINo = leveINo;
		this.strLength = strLength;
		this.strTimes = strTimes;
		this.timeLimit = timeLimit;
		this.perScore = perScore;
	}

	public int getLeveINo() {
		return leveINo;
	}

	public void setLeveINo(int leveINo) {
		this.leveINo = leveINo;
	}

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

LevelParam          等级参照类

 

package com.pojo;
/**
  *  为等级类提供参照标准
 * @author NZL
 *
 */
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 com.pojo;

import java.util.Random;

public class Game {

	private Player player;// 玩家类做为属性

	public Game() {

	}

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

	public Player getPlayer() {
		return player;
	}

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

	public String printStr() {

		String str = "><*&%#";// 定义随机出现字符串的源

		int leveINo = this.player.getLeveINo();// 获取传递过来这个对象的等级

		int n = LevelParam.level[leveINo].getStrLength();// 通过当前对象的等级,获取该等级对应的字符串长度

		Random random = new Random();

		StringBuffer strb = new StringBuffer();// 使用可变长的的StringBuffer接收每次产生的一个字符

		/*
		 * 通过循环,随机出指定长的字符串
		 */
		for (int i = 0; i < n; i++) {
			strb.append(str.charAt(random.nextInt(6)));
		}

		str = strb.toString();

		return str;
	}

	public boolean printResult(String str, String string, long t) {

		int time = (int) (t / 1000);
		if (string.equals(str)) {
			if (time <= LevelParam.level[this.player.getLeveINo()].getTimeLimit()) {

				// 输入正确且不超时 增加对应等级应该增加的积分
				this.player.setCurScore(
						this.player.getCurScore() + LevelParam.level[this.player.getLeveINo()].getPerScore());

				System.out.println("恭喜你输入正确!!" + "当前等级:" + this.player.getLeveINo() + "级,当前积分:"
						+ this.player.getCurScore() + "分,本次用时:" + time + "秒,本等级用时:"
						+ (this.player.getElapsedTime() - this.player.getStartTime()) / 1000 + "秒。");
				return true;
			} else {
				System.out.println("输入超时!!" + "当前等级闯关时间限制为:" + this.player.getLeveINo() + "秒,本次用时:" + time + "秒,本等级用时:"
						+ (this.player.getElapsedTime() - this.player.getStartTime()) / 1000 + "秒。");
				return false;
			}
		} else {
			System.out.println("对不起,你输入有误!!!");
			return false;
		}
	}

}

 

测试类

package com.pojo;

class Test {
	@org.junit.jupiter.api.Test
	void test() {
		Player player = new Player();
		player.play();
	}
}

 

注释有的写的不到位的地方,欢迎大家给我留言。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值