javaoop第五章

                           玩家类
package bao;
import java.util.Scanner;
	//玩家类
public class Player {
		private int levelNo;//玩家当前级别号
		private int curScore;///玩家当前级别积分
		private long stratTime;//当前级别开始时间
		private  int elapseTime;//当前级别已用时间
		//封装
		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 getStratTime() {
			return stratTime;
		}
		public void setStratTime(long stratTime) {
			this.stratTime = stratTime;
		}
		public int getElapseTime() {
			return elapseTime;
		}
		public void setElapseTime(int elapseTime) {
			this.elapseTime = elapseTime;
		}
	//玩家玩游戏的方法
		public void play()
		{
		Game game=new Game(this);
		Scanner input=new Scanner(System.in);
		//外层循坏一次,游戏晋级一级
		for (int i = 0; i < levelProgram .levels.length; i++) {
			//晋级
			this.levelNo+=1;
			//晋级后计时清零,积分清零
			this.stratTime=System.currentTimeMillis();
			this.curScore=0;
			//内层循环一次完成一次字符串输出输入比较
			for (int j = 0; j < levelProgram .levels[levelNo-1].getStrTime(); j++) {
				String outstr=game.PrintStr();
				System.out.println(outstr);
				String instr=input.next();
				game.printResult(outstr, instr);
			}
		}
		}


		

		
		

}



                                            游戏类

package bao;
//游戏类
import java.util.Random;
public class Game {
	private Player player;
	public Game(Player player){
		this.player=player;
	}
	public String PrintStr(){
		StringBuffer buffer=new StringBuffer();
		Random random=new Random();
		//通过循环生成要输出的字符串
		for (int i = 0; i <2; i++) {
			int rand=random.nextInt(2);
			//根据随机数拼接字符串
			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;
			}
			
		}
			return buffer.toString();
		}
	
	
    public void printResult(String out,String in)
    {
    	if (out.equals(in)) {
    		long time=System.currentTimeMillis();
    		if((time-player.getStratTime())/1000>levelProgram.levels[player.getLevelNo()-1].getTimeLimit()){
				System.out.println("你输入太慢,已经超时,退出");
				System.exit(1);
			}else
			{
				//当前获取分值
				player.setCurScore(player.getCurScore()+ levelProgram.levels[player.getLevelNo() - 1].getPerScore());
				player.setElapseTime((int) (time - player.getStratTime()) / 1000);
				int levelNo=player.getLevelNo();
				int score=player.getCurScore();
				int elapsedTime=player.getElapseTime();
				System.out.println("输入正确,您的级别"+levelNo+"您的积分"+score+"已用时间"+elapsedTime+"秒。");
				if(levelNo==6){
					System.out.println("游戏结束");
					System.exit(1);
				}
			}
			
		}else{
			System.out.println("输入错误,退出");
			System.exit(1);
		}
    	
    }
    
}

                                                           级别

package bao;//级别类

public class Level {

	
	private int levelNo;//级别编号
	private int strLength;//各级别一次输出字符串长度
	private int strTime;//各级别输出字符串的次数
	private int timeLimit;//各级别闯关的时间限制
	private int perScore;//各级别正确输入一次的得分
	//封装
	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 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(){}
	//有参构造
	public Level(int levelNo,int StrinLength,int strTime,int timeLimit,int perScore )
	{
		this.levelNo=levelNo;
		this.strLength=StrinLength;
		this.strTime=strTime;
		this.timeLimit=timeLimit;
		this.perScore=perScore;
	}
}
package bao;







public class levelProgram extends Level {

	
//继承父类无参构造
	public levelProgram() {
		super();
		// TODO Auto-generated constructor stub
	}
//继承父类有参构造
	public levelProgram(int levelNo, int StrinLength, int strTime,
			int timeLimit, int perScore) {
		super(levelNo, StrinLength, strTime, timeLimit, perScore);
		// TODO Auto-generated constructor stub
		
		}
	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,14,10);
		levels[5]=new Level(6,7,5,10,15);
	}
}

                                      测试类

package bao;

public class Test {

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

}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值