第五章 快速击键游戏

package back;

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, 22, 15);
		
	}
}


package back;

import java.util.Scanner;
   
public class Player {
	//玩家类
	
private  int leveINo;  //玩家当前级别号
private  int  currScore;  //玩家当前级别积分
private  long  startTime; //玩家级别开始时间
private  int  elapsedTime; //当前级别已用时间



public  int getLeveINo() {
	return leveINo;
}

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

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  pay(){
	Game  game=new Game(this);
	Scanner  input=new  Scanner(System.in);
	for (int i = 0; i < LevelParam.levels.length; i++) {
		//晋级
		this.leveINo+=1;
		//晋级后清零,积分清零
		this.startTime=System.currentTimeMillis();
		this.currScore=0;
		//内层循环,循环一次完成一次字符串的输入,输出
		for (int j = 0; j < LevelParam.levels[leveINo-1].getStrTimes(); j++) {
			//输出游戏字符串
			String  outt=game.printStr();
			System.out.println(outt);
			
			//接受用户输入
			String  inr=input.next();
			//游戏判断玩家是否输入正确,并输入相应结果信息
			game.printResult(outt, inr);
			
		}
	}
	
}

public static long getStrTimes() {
	// TODO Auto-generated method stub
	return 0;
}
	
	
}


//级别类
package back;

public class Level {
  private int leveINo;
  private int strLength;
  private int strTimes;
  private int timeLimit;
  private int perScore;
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 Level() {
	super();
	// TODO Auto-generated constructor stub
}
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;
}
  

  
	
}


//游戏类
package back;

import java.sql.Time;

import java.util.Random;

public class Game{
      public Player player;
      public Game(Player player)
      {
    	  this.player=player;
      }
   
      
      
	public String printStr(){
		int  strlth=LevelParam.levels[player.getLeveINo()-1].getStrLength();
		
		 StringBuffer buff=new StringBuffer();
		 Random re=new Random();
		 //通过循环生成要输出的字符串
		 for (int i = 0; i < strlth;i++) {
			 //产生随机数
			 int  rand=re.nextInt(strlth);
		      //根据随机数拼接字符串
			 switch(rand){
			 case 0:
				 buff.append(">");
				 break;
			 case 1:
				 buff.append("<");
				 break;
			 case 2:
				 buff.append("*");
				 break;
			 case 3:
				 buff.append("&");
				 break;
			 case 4:
				 buff.append("%");
				 break;
			 case 5:
				 buff.append("#");
				 break;
			 
			 }
			 
		 }
		
		 return  buff.toString();
	}
	
	
	public  void  printResult(String out,String in){
		   if(out.equals(in)){
		   long  time=System.currentTimeMillis();
		   //如果超时
		   if((time-player.getStartTime())/1000>LevelParam.levels[player.getLeveINo()-1].getTimeLimit()){
				System.out.println("你输入太慢,已经超时,退出");
				System.exit(1);
			}else{

		   //计算玩家当前积分
		   player.setCurrScore(player.getCurrScore()+LevelParam.levels[player.getLeveINo()-1].getPerScore());
		
		     player.setElapsedTime((int)(time-player.getStartTime())/1000);
		     int  leveno=player.getLeveINo();
		     int  score=player.getCurrScore();
		     int  Time=player.getElapsedTime();
		     System.out.println("输入正确,您的级别"+leveno+",您的积分"+score+",已用时间"+Time+"秒");
		     if(leveno==6){
		    	 System.out.println("游戏结束!");
		    	 System.exit(1);
		     }
		     
			}
		   
		   }else{
		   
		   System.out.println("输入错误,退出!");
		   System.exit(1);
		   }
	}
	
}

package back;

public class Text {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	Player  pp=new Player();
	pp.pay();

	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值