项目实战——QuickHit

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

游戏类

 public class Game {
public Player player;
public Player getPlayer() {
	return player;
}
public void setPlayer(Player player) {
	this.player = player;
}
/**
 * 带参方法
 * @param 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);表示异常退出, System.exit(0);表示正常退出
			 */
			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.getCurScore()+"已用时间"+player.getElapsedTime()+"秒<");
		}
	}else{
		System.out.println("菜鸟输入错误了");
		/**
		 * System.exit(1);表示异常退出, System.exit(0);表示正常退出
		 */
		System.exit(1);

	}
}

级别类

public class Level {
public int levelNo; //各级编号
public int strLength;//各级别一次输入字符串的长度
public int strTimes;//各级别输入字符串的次数
public int timeLimit;//各级别闯关的时间限制
public int perScore;//各级别正确输入一次的得分
/**
 * 各个属性的封装
 * @return
 */
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(){

}
/**
 * 带参方法
 * @param levelNo
 * @param strLength
 * @param strTimes
 * @param timeLimit
 * @param perScore
 */
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;
}

}
玩家类

public class Player {
private int levelNo;//玩家当前级别号
private int curScore;//玩家当前积分
private long startTime;//当前级别开始时间
private 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);
		}

	}

}

玩家数组的建立

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

测试类

public class Test {
public static void main(String[] args) {
	// TODO Auto-generated method stub
	Player player=new Player();
	player.play();
	
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值