QuickHit

QuickHit 遊戲考驗你鍵盤輸入的速度和準確性。
根據輸入速度和正確率將玩家分爲不同級別,級別越高,一次顯示的字符數就越多,玩家
正確輸入一次的得分也越高。如果玩家在規定時間內完成規定次數的輸入,正確率達到
規定要求,則玩家升級(爲了簡單起見,規定用戶錯誤輸入一次,遊戲結束)。玩家最
高級別爲六級,初始級別一律爲一級。
/*
總結

如果自己分析的話,估計要弄很久纔會理清思路,感覺代碼的運行順序不是很理解,一種到達瓶頸的感覺,方法的調用不是很理解,關鍵是做的題以及看的題不夠多。
*/
import java.util.*;
class Player {
	private int levelNo;//級別
	private long startTime;//開始時間
	private int score;//積分
	private long elapseTime;//持續時間
	
	public void setLevelNo(int levelNo){		
		this.levelNo = levelNo;	
	}	
	public void setScore(int score){		
		this.score = score;
	}	
    public void setStartTime(long time){		
		this.startTime = time;
	}
    public void setElapseTime(long elapse){	
	    this.elapseTime = elapse;
    }
    public int getLevelNo(){   	
    	return this.levelNo;
    }   
    public int getScore(){    	
    	return this.score;
    }
    public long getStartTime(){ 	
 	     return this.startTime;
    } 
    public long getElapseTime(){	
 	     return this.elapseTime;
    }   
    public void play(){
    	Game game = new Game(this);
    	Scanner input = new Scanner(System.in);
		//開始遊戲
    	for(int i = 0; i < LevelInitial.levelsArr.length; i++){
			//晉級
    		levelNo += 1;
			//開始時間
    		startTime = System.currentTimeMillis();
    		for(int j = 0; j < LevelInitial.levelsArr[levelNo -1].getStrTime();j++){
				//生成字符串
    			String out = game.printStr();
				//輸入字符串
    			String in = input.next();
				//比較
    			game.result(out, in);    			
    		}   		
    	}    	
    }
}

class Game{
	private Player player;
	public Game(Player player){
		
		this.player = player;
	}
	public String printStr(){
		int levelNo = player.getLevelNo() - 1;
		int strlen = LevelInitial.levelsArr[levelNo].getStrlength();
		Random random = new Random();
		StringBuffer buffer = new StringBuffer();
		//通過循環生成要輸出的額字符串
		for(int i = 0; i < strlen; i++){
			//產生隨機數
			int rand = random.nextInt(strlen);
			//根據隨機數拼接字符串
			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;
			case 6:
				buffer.append("@");
				break;			
			}			
		}	
		//輸出字符串
		System.out.println(buffer);
		//返回字符串
		return buffer.toString();
	}
	public void result(String out, String in){	
		//對比
		if(out.equals(in)){ 
			long currentTime = System.currentTimeMillis();
			if((currentTime - player.getStartTime())/ 1000 <= LevelInitial.levelsArr[player.getLevelNo() -1].getTimeLimit()){
				//計算分數
				int newScore = player.getScore()+ LevelInitial.levelsArr[player.getLevelNo() -1].getPerScore();
				player.setScore(newScore);
				//計算已用時間
				long time = (currentTime - player.getStartTime())/ 1000;
				//給玩家的用時
				player.setElapseTime(time);
				//輸出 時間,分數,等級
				System.out.println("輸入正確,您的等級是:" + player.getLevelNo() + ", 您的積分是:" + player.getScore() + ", 您的用時爲:" + player.getElapseTime() + "秒");
				//判斷 是否全部通關
				if(player.getLevelNo() == 6){
					int sum = 0;					
					for(int j = 0; j < LevelInitial.levelsArr.length; j++ ){
						//每個級別的總分
				    	int currentScore = LevelInitial.levelsArr[j].getPerScore() * LevelInitial.levelsArr[j].getStrTime();
						//所有總分
				    	sum += currentScore;				    	
				    }	
					//總分一樣,通關
					if(player.getScore() == sum){
						System.out.println("恭喜您全部通關!!真棒!!");
						System.exit(0);
					}			
				}				
				}else{//超時
				System.out.println("輸入正確,但是時間超時,遊戲結束!!遊戲退出!!");
				System.exit(1);
			}
		}else{
			System.out.println("輸入錯誤,遊戲結束!!遊戲退出!!");
			System.exit(1);			
		}		
	}
}

class Level {
	private int levelNo;//級別編號
	private int strlength;//當前級別對應的 字符串長度
	private int timeLimit;//當前級別對應的輸入時間限制要求
	private int strTime;//當前級別限制的 次數
	private int perScore;//當前級別對應的 分數
	public Level(int levelNo, int strlength, int timeLimit, int strTime, int perScore){		
		this.levelNo = levelNo;
		this.strlength = strlength;
		this.timeLimit = timeLimit;
		this.perScore = perScore;
		this.strTime = strTime;
	}
	
	//每個級別的對象 代表一個級別 需要獲取這個值
	public int getLevelNum(){
		return this.levelNo;
	}	
    public int getTimeLimit(){		
		return this.timeLimit;
	}   
     public int getStrTime(){		
		return this.strTime;
	}	
     public int getStrlength(){		
 		return this.strlength;
 	}    
     public int getPerScore(){ 		
		return this.perScore;
 	}
}

class LevelInitial {
	public static Level[] levelsArr = new Level[6];
	static {		
		levelsArr[0] = new Level(1, 1, 30, 5, 1);
		levelsArr[1] = new Level(2, 2, 25, 4, 2);
		levelsArr[2] = new Level(3, 3, 22, 3, 5);
		levelsArr[3] = new Level(4, 4, 20, 3, 8);
		levelsArr[4] = new Level(5, 4, 18, 3, 10);
		levelsArr[5] = new Level(6, 5, 15, 3, 15);		
	}
}

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章