面向对象项目实战-----QuickHit

1 篇文章 0 订阅

项目需求:

根据输入速率和正确率将玩家分为不同级别,级别越高,一次显示的字符数越多。如果 玩家在规定时间内完成规定次数的输入,正确率达到要求,则玩家升级。最高级别为6级,初始级别为1.项目实现代码如下:

实现项目的代码:

//游戏类
public class Game {
	/***
	 * 玩家属性
	 */
	public Player play;		

	public Player getPlay() {
		return play;
	}

	public void setPlay(Player play) {
		this.play = play;
	}
	
	public Game(Player play) {		
		this.play = play;
	}

	/***
	 * 输出字符串,返回字符串用于和玩家的输入进行比较
	 * @return
	 */
	public String printStr() {
		StringBuffer sbu=new StringBuffer();
		Random rand=new Random(); 	//产生随机数类
		//根据相应的等级,来获得相应的输入次数
		int strLength=LevelParam.level[play.getLevelNo()-1].getStrLength();

		for(int i=0;i<strLength;i++) {
			int randNum=rand.nextInt(strLength);	//产生随机数
			switch(randNum) {
			case 0:
				sbu.append(">");
				break;
			case 1:
				sbu.append("<");
				break;
			case 2:
				sbu.append("!");
				break;
			case 3:
				sbu.append("#");
				break;
			case 4:
				sbu.append("@");
				break;
			case 5:
				sbu.append("%");
				break;
			case 6:
				sbu.append("&");
				break;
			case 7:
				sbu.append("*");
				break;
			case 8:
				sbu.append("~");
				break;
			case 9:
				sbu.append("?");
				break;
			}
		}
		System.out.println(sbu);  	//输出字符串
		return sbu.toString();		//返回字符串
	}
	
	
	/**
	 * 比较游戏输出out,和玩家输入in,根据结果输出信息
	 */
	public void prinrResult(String out,String in) {
		//输入正确
		if(out.equals(in)) {
			long currentTime=System.currentTimeMillis();
			//如果超时
			if((currentTime-play.getStartTime())/1000>LevelParam.level[play.getLevelNo()-1].getTimeLimit()) {
				System.out.println("你输入的太慢了,已经超时,退出!");
				System.exit(1);
			}
				//如果没有超时
				//计算玩家当前的积分
				play.setCurrScore(play.getCurrScore()+LevelParam.level[play.getLevelNo()-1].getPerScore());
				//计算已用时间
				play.setElapsedTime((int)(currentTime-play.getStartTime())/1000);
				//输出玩家当前级别,当前积分,已用时间
				System.out.println("输入正确,你的级别是:"+play.getLevelNo()+",你的积分是:"+play.getCurrScore()+",已用时间:"+play.getElapsedTime()+"秒!");
				
				//判断用户是否已经闯过最后一关:等级为6,玩家获得的积分==通过所需要的分数(次数*每次的分数)
				if(play.getLevelNo()==6) {
					//获得通关所需要的分数
					int score=LevelParam.level[play.getLevelNo()-1].getPerScore()*LevelParam.level[play.getLevelNo()-1].getStrTime();
					if(score==play.getCurrScore()) {
						System.out.println("恭喜你,通关了~");
						System.exit(0);
					}
				}
		}else {
			//输入错误
			System.out.println("输入错误,退出!");
			System.exit(1);
		}
	}
	
	
}


//玩家类
public class Player {
	private int levelNo;		//级别
	private int currScore;		//积分
	private long startTime;		//开始时间
	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 getElapsedTime() {
		return elapsedTime;
	}
	public void setElapsedTime(int elapsedTime) {
		this.elapsedTime = elapsedTime;
	}
	
	/**
	 * 玩家玩游戏
	 */
	public void play() {
		Scanner input=new Scanner(System.in);
		
		Game game=new Game(this);	//this--->指当前对象的引用,传入的值是当前Player类(现在没创建对象)以后创建对象的引用。
		//外层循环一次,等级加1
		for(int i=0;i<LevelParam.level.length;i++) {
			//等级加1
			this.levelNo+=1;
			//积分清0
			this.currScore=0;
			//计时清0
			this.startTime=System.currentTimeMillis();
			
			//获得每个级别对应的字符串输入字数
			int strLength=LevelParam.level[levelNo-1].getStrTime();
			
			//内层循环一次,判断玩家输入的字符串
			for(int k=0;k<strLength;k++) {
				//游戏输出字符串
				String out=game.printStr();
				//玩家输入
				String in=input.next();
				//判断玩家输入的字符串,并输出相应结果
				game.prinrResult(out, in);
			}
		}
		
	}
	
	
}


//级别类
public class Level {
	private int levleNo;		//级别编号
	private int strLength;		//输出的字符串长度
	private int strTime;		//输出字符串的次数
	private int timeLimit;		//闯关时间
	private int perScore;		//输入正确的得分
	
	public int getLevleNo() {
		return levleNo;
	}
	public void setLevleNo(int levleNo) {
		this.levleNo = levleNo;
	}
	public int getStrLength() {
		return strLength;
	}
	public void setStrLength(int strLength) {
		this.strLength = strLength;
	}
	public int getStrTime() {
		return strTime;
	}
	public void setStrTime(int strTime) {
		this.strTime = 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;
	}
	public Level(int levleNo, int strLength, int strTime, int timeLimit, int perScore) {
		super();
		this.levleNo = levleNo;
		this.strLength = strLength;
		this.strTime = strTime;
		this.timeLimit = timeLimit;
		this.perScore = perScore;
	}
	
}


//级别参数类
public class LevelParam {
	//对应玩游戏的6个级别
	public static final 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,5,12,15);
	}
}


//测试类
public class Test {
	public static void main(String[] args) {
		Player p = new Player();
		p.play();
	}
}

效果图:

  • 9
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值