今天做了一個很有意思的項目,叫快速擊鍵,這個項目他主要考察的就是:面向對象設計的思想, 類的封裝, 構造方法的使用, this、static關鍵字的使用
一.項目需求:
根據輸入的速率和正確率將玩家分為不同級別,級別越高,一次顯示的字符數越多,玩家正確輸入一次的得分也越高。如果玩家在規定的時間內完成規定次數的輸入,正確率達到規定標准,則玩家升級(在這裡規定只要錯誤輸入一次,則游戲結束)。玩家最高級為6級,初始級別一律為一級。
運行效果圖:

二.分析需求:
01.發現類:
玩家類(Player),游戲類(Game),級別類(Level),LevelParam(保存每一個級別的參數信息)
02.發現類中的屬性:
玩家類(Player)的屬性:
levelNo:玩家當前級別號,currScore:玩家當前級別積分,startTime:玩家當前級別開始時間,elapsedTime 當前級別已用時間
級別類(Level)的屬性:
levelNo:各級別編號,strLength各級別一次輸入字符串的長度,strTime各級別輸出字符串的次數,timeLimit各級別闖關的時間限制,perScore各級別正確輸入一次的得分。
游戲類(Game):既然是玩游戲,肯定有玩家,所以在游戲類中要添加一個玩家的屬性player;
03.發現類的方法
玩家類(Player)的方法:play();
游戲類(Game)的方法:
String printStr(),輸出字符串,返回字符串用於和玩家的輸入進行比較。
void printResult(String out,String in)比較輸出out和玩家輸入in
三.實現功能:
第一步我們要形成這個項目的一個整體框架。

Level類:
package cn.hyj.quickhit;
/**
* 級別類
*
* @author HYJ
*
*/
public class Level {
private int levelNo;// 各級別編號
private int strLength;// 各級別一次輸出字符串的長度
private int strTimes;// 各級別輸出字符串的次數
private int timeLimit;// 各級別闖關的時間限制
private int perScore;// 各級別正確輸入一次的得分
public Level() {
super();
// TODO Auto-generated constructor stub
}
public Level(int levelNo, int strLength, int strTimes, int timeLimit,
int perScore) {
super();
this.levelNo = levelNo;
this.strLength = strLength;
this.strTimes = strTimes;
this.timeLimit = timeLimit;
this.perScore = 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 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類:
package cn.hyj.quickhit;
//存放級別的屬性
public class LevelParam {
// 定義級別類型數組保存級別信息
public static final Level[] levels = new Level[6];
// 靜態代碼塊給數組賦初值
static {
levels[0] = new Level(1, 2, 5, 60, 1);
levels[1] = new Level(2, 3, 6, 60, 2);
levels[2] = new Level(3, 4, 7, 60, 5);
levels[3] = new Level(4, 5, 8, 60, 8);
levels[4] = new Level(5, 6, 9, 60, 10);
levels[5] = new Level(6, 7, 10, 60, 15);
}
}
Game類:
package cn.hyj.quickhit;
import java.util.Random;
/**
* 游戲類
*
* @author HYJ
*
*/
public class Game {
Player play;// 代表玩家
/**
* 無參構造
*/
public Game() {
}
public Game(Player plays) {
play = plays;
}
/**
* 生成字符串
*/
public String getStr() {
// 獲取級別對應的要輸出字符串的長度
int strLength = LevelParam.levels[play.getLevelNo() - 1].getStrLength();
StringBuffer str = new StringBuffer();
// 實例化生成隨機數的對象
Random rd = new Random();
// 通過循環輸出要生成的字符串
for (int i = 0; i < strLength; i++) {
// 產生隨機數
int random = rd.nextInt(strLength);
switch (random) {
case 0:
str.append(">");
break;
case 1:
str.append("<");
break;
case 2:
str.append("*");
break;
case 3:
str.append("&");
break;
case 4:
str.append("#");
break;
case 5:
str.append("@");
break;
}
}
// 輸出字符串
System.out.println(str);
// 返回該字符串的值,用於和用戶輸入字符串的值作比較
return str.toString();
}
/**
* 系統給的字符串和用戶輸入的字符串作對比
*
* @param outstr
* 系統輸出的字符串
* @param instr
* 用戶輸入的字符串
*/
public void strThesame(String outstr, String instr) {
Boolean flag = false;// 定義標記默認不同
if (outstr.equals(instr)) {
// 證明兩個字符串相同
flag = true;// 改變標記
} else {
System.out.println("輸入錯誤,退出!");
System.exit(0);
}
/**
* 如果輸入正確則會出現兩種情況 01.如果超時,則直接輸出錯誤信息並退出程序 02.如果沒有超時: 計算玩家當前積分 計算玩家已用時間
* 輸出玩家當前級別,當前積分和已用時間 判斷用戶是否已經闖過最後一關
* */
if (flag) {
long currentTime = System.currentTimeMillis(); // 返回以毫秒為單位的當前時間
// 如果超時的情況
if (LevelParam.levels[play.getLevelNo() - 1].getTimeLimit() < (currentTime - play
.getStartTime()) / 1000) {
System.out.println("你輸入太慢了,已經超時,退出!");
System.exit(0);
}
// 如果沒有超時的情況
else {
// 計算玩家當前積分
play.setCurrScore(play.getCurrScore()
+ LevelParam.levels[play.getLevelNo() - 1]
.getPerScore());
// 計算玩家已用時間
play.setEndTime((int) (currentTime - play.getStartTime()) / 1000);
// 輸出玩家當前級別,當前積分和已用時間
System.out.println("輸入正確,您的級別" + play.getLevelNo() + ",您的積分"
+ play.getCurrScore() + ",已用時間" + play.getEndTime()
+ "秒。");
}
}
}
}
Player類:
package cn.hyj.quickhit;
import java.util.Scanner;
/**
* 玩家類
*
* @author HYJ
*
*/
public class Player {
private int levelNo;// 玩家當前級別號
private int currScore;// 玩家當前積分
private long startTime;// 當前玩家級別的開始時間
private int endTime;// 當前玩家級別的已用時間
/**
* 玩家玩游戲的方法
*/
public void play() {
// 調用游戲類的帶參構造傳入玩家對象
Game game = new Game(this);
Scanner input = new Scanner(System.in);
// 外層循環每循環一次讓等級加一
for (int i = 0; i < LevelParam.levels.length; i++) {
// 讓玩家級別加一
this.levelNo += 1;
// 升級後積分清零,計時清零
this.setCurrScore(0);
this.setStartTime(System.currentTimeMillis());
if (this.levelNo == 6) {
System.out.println("高手,恭喜已通關!");
break;
}
// 內層循環控制用戶輸入的字符串和系統的字符串作比較
for (int j = 0; j < LevelParam.levels[levelNo - 1].getStrTimes(); j++) {
// 游戲輸出字符串
String outstr = game.getStr();
// 調用game類的方法比較兩個字符串是否相同
// 接收用戶輸入的字符串
String instr = input.next();
game.strThesame(outstr, instr);
}
}
}
public Player() {
super();
// TODO Auto-generated constructor stub
}
public Player(int levelNo, int currScore, long startTime, int endTime) {
super();
this.levelNo = levelNo;
this.currScore = currScore;
this.startTime = startTime;
this.endTime = endTime;
}
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 getEndTime() {
return endTime;
}
public void setEndTime(int endTime) {
this.endTime = endTime;
}
}
測試:
package cn.hyj.quickhit;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
//實例化玩家對象
Player player = new Player();
//調用玩家玩游戲的方法
player.play();
}
}