
public class leve { private int leveNo; private int strLength; private int strTimes; private int timeLimit; private int perScore; public leve(int leveNo, int strLength, int strTimes, int timeLimit, int perScore) { super(); this.leveNo = leveNo; this.strLength = strLength; this.strTimes = strTimes; this.timeLimit = timeLimit; this.perScore = perScore; } public leve() { super(); } public int getLeveNo() { return leveNo; } public void setLeveNo(int leveNo) { this.leveNo = leveNo; } 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; } }
首先編寫類,游戲類,玩家類,級別類
玩家類的屬性:levelNo玩家編號類,currScore玩家當前積分,stratTime當前級別開始時間,elapsedTime
當前級別已用時間
級別類的屬性:levelNo各級別編號,strLengh一次輸入的字符串長度,strTime各級別輸出字符串的次數,timeLimit各級闖關的時間限制
perScore各級別輸入一次正確的得分!
游戲類:player玩家屬性(玩家來玩游戲)
因為級別類不包括各個級別的具體參數信息,所以增加一個levelParam類,創建一個長度為6的數組,存放各個級別的參數信息
游戲類Game

public class Game { public Player player; public Game(Player player) { super(); this.player = player; } public Game() { } public Player getPlayer() { return player; } public void setPlayer(Player player) { this.player = player; } Random random = new Random(); public String printstr(){ StringBuffer buffer = new StringBuffer(); int str =Levelparam.leves[player.getLevelNo()-1].getStrLength(); for (int i = 0; i < str; i++) { int rand = random.nextInt(str); switch(rand){ case 0: buffer.append("w"); break; case 1: buffer.append("s"); break; case 2: buffer.append("a"); break; case 3: buffer.append("d"); break; case 4: buffer.append("h"); break; case 5: buffer.append("j"); break; } } System.out.println(buffer.toString()); return buffer.toString(); } public void printReslut(String out, String jieshou) { if(out.equals(jieshou)) { long time = System.currentTimeMillis(); //如果超時 if((time-player.getStartTime())/1000>Levelparam.leves[player.getLevelNo()-1].getTimeLimit()) { System.out.println("已超時,您的速度有待提高"); System.exit(1); } else { //當前說獲得分值 player.setCurScore(player.getCurScore()+Levelparam.leves[player.getLevelNo()-1].getPerScore()); //游戲所需時間 player.setElapsedTime((int)(time-player.getStartTime())/1000); int Level = player.getLevelNo(); int score = player.getCurScore(); int usetime = player.getElapsedTime(); System.out.println("輸入正確,您的級別是"+Level+"您的積分是"+score+"已用時間"+usetime); if(Level==6) { System.exit(1); System.out.println("游戲結束"); } } } else { System.out.println("游戲失敗"); System.exit(1); } } }
Leve

public class leve { private int leveNo; private int strLength; private int strTimes; private int timeLimit; private int perScore; public leve(int leveNo, int strLength, int strTimes, int timeLimit, int perScore) { super(); this.leveNo = leveNo; this.strLength = strLength; this.strTimes = strTimes; this.timeLimit = timeLimit; this.perScore = perScore; } public leve() { super(); } public int getLeveNo() { return leveNo; } public void setLeveNo(int leveNo) { this.leveNo = leveNo; } 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; } }
levelParam類

public class Levelparam { public final static leve[] leves = new leve[6]; static{ leves[0]=new leve(1,2,10,30,1); leves[1]=new leve(2,3,9,26,2); leves[2]=new leve(3,4,8,22,5); leves[3]=new leve(4,5,7,18,8); leves[4]=new leve(5,6,6,15,10); leves[5]=new leve(6,7,5,12,15); } }
Player

public class Player { private int levelNo; private int curScore; private long startTime; private int elapsedTime; 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 print(){ Scanner input = new Scanner(System.in); Game game= new Game(this);//創建游戲實例 game.getPlayer().setLevelNo(1); for (int i = 0; i < Levelparam.leves.length; i++) { setLevelNo(getLevelNo()+1); this.curScore=0; System.out.println("恭喜你進入下一級別"); game.getPlayer().setStartTime(System.currentTimeMillis());//游戲開始,給Player的開始時間賦值 //內層循環 for(int j=0;j<Levelparam.leves[game.getPlayer().getLevelNo()-1].getStrTimes();j++) { String out=game.printstr();//接收游戲輸出的字符串 String in=input.next();//接受玩家輸入的字符串 game.printReslut(out,in);//判斷 } } } }
Text

public class Test { public static void main(String[] args) { Player py = new Player(); py.print(); } }
--首先game類(先有游戲才能玩):
方法有二:printStr()
printResult()
1:printStr()方法:生成隨機的字符串,使用switch選擇結構以生成的隨機數進行判斷生成的字符串。字符串的長度不得大於各級別輸出字符串的長度。
int strLength=LevelParam.levels[player.getLevelNo()-1].getStrLength();由於數組下標是從0開始的,獲取該級別輸入
的字符串的長度定位到數組中的一項要使用(級別號-1 )定位數組下標。創建一個0到5的隨機數,創建StringBuffer對象來拼接字符串。該方法返回
一個拼接好了的字符串。
2:long time=System.currentTimeMillis();獲取系統當前時間的毫秒數(嚴謹到毫秒)
(time-player.getStartTime())/1000>LevelParam.levels[player.getLevelNo()-1].getTimeLimit()如果游戲所用時間大於
游戲規定的時間,判斷出局!
player.setCurScore(player.getCurScore()+LevelParam.levels[player.getLevelNo()-1].getPerScore());加上當前獲得的分數
player.setElapsedTime((int)(time-player.getStartTime())/1000);計算玩游戲所需的時間
輸出當前這一關的信息,當進行到第六個級別(最高級別)時,將級別清空,還原為1.
--Player類:創建游戲類(game對象),記錄循環的次數,將級別初始為1.
game.getPlayer().setStartTime(System.currentTimeMillis())記錄下游戲開始時間!
循環條件,小於輸入次數,接收隨機生成的字符串,如果用戶輸入的字符串與該字符串相等,繼續游戲,否則,gameOver!
--Text類:直接調用player類的print()方法!