關於java的QuickHit打字游戲小項目


public class Player {
Scanner input = new Scanner(System.in);
//玩家屬性
private int level = 0;//等級
private int curScore = 0;//積分
private long stratTime = 0;//開始時間
private int elapsdTime = 0;//所用時間

//封裝
public int getLevel() {
return level;
}

public void setLevel(int level) {
this.level = level;
}

public int getCurScore() {
return curScore;
}

public void setCurScore(int curScore) {
this.curScore = curScore;
}

public long getStartTime() {
return stratTime;
}

public void setStartTime(int strTime) {
this.stratTime = strTime;
}

public int getElapsdTime() {
return elapsdTime;
}

public void setElapsdTime(int elapsdTime) {
this.elapsdTime = elapsdTime;
}

//封裝
Game game = new Game(this);//創建游戲對象,並裝入玩家
public void play ()throws Exception//玩游戲
{
for (int i = 0; i <LevelParam.levels.length; i++) {//游玩次數
//一次通關
Level level = LevelParam.levels[this.level];
this.stratTime = System.currentTimeMillis();
for (int j = 1; j <= level.getStrTime(); j++) {
System.out.println("第" + j + "次");
String out = game.pringStr();//接收字符串
String in = input.next();//接收玩家輸入的字符串
game.printResult(out, in);//比較並打印結果
}
this.curScore += level.getPerSore();
System.out.println("通過第" + (this.level + 1) + "關,積分:" + curScore);
this.level++;//等級增加
//一次通關
}
}
}
public class Game {
private Player player;//玩家
public Game(){}//構造
public Game(Player player){this.player=player;}//構造
String pringStr()//輸出字符串方法
{
Level level = LevelParam.levels[this.player.getLevel()];
Random random = new Random();
StringBuffer sb=new StringBuffer();
for (int i = 0; i < level.getStrLength(); i++) {
int rand = random.nextInt(6);
switch(rand)
{
case 0:
sb.append("a");
break;
case 1:
sb.append("b");
break;
case 2:
sb.append("c");
break;
case 3:
sb.append("d");
break;
case 4:
sb.append("e");
break;
case 5:
sb.append("f");
break;
}
}
System.out.println(sb.toString());
return sb.toString();
}

public void printResult(String out,String in)//比較結果
{
if(out.equals(in))
{
System.out.println("輸入成功");
long endtime=System.currentTimeMillis();
double timecha=(endtime-this.player.getStartTime())*1.0/1000;
if(timecha>(LevelParam.levels[this.player.getLevel()].getTimeLimit()))
{
System.out.println("輸入超時游戲結束");
System.exit(0);
}
}
else{
System.out.println("輸入失敗,退出游戲");
System.exit(0);
}
}
}
public class Level {
public int getLevelON() {
return levelON;
}

public void setLevelON(int levelON) {
this.levelON = levelON;
}

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 getPerSore() {
return perSore;
}

public void setPerSore(int perSore) {
this.perSore = perSore;
}

private int levelON=0;//等級
private int strLength=0;//字符串長度
private int strTime=0;//輸入次數
private int timeLimit=0;//時間限制
private int perSore=0;//該等級得分
//構造
public Level(){}
public Level(int levelON, int strLength, int strTime, int timeLimit, int perSore) {
this.levelON = levelON;
this.strLength = strLength;
this.strTime = strTime;
this.timeLimit = timeLimit;
this.perSore = perSore;
}
//構造
}
public class LevelParam {
public static final Level levels[]=new Level[6];//常量數組屬性
static{//設置並初始化等級難度
//等級,輸入字數,輸入次數,時間限制,得分
levels[0]=new Level(1,2,2,30,1);
levels[1]=new Level(1,3,2,30,2);
levels[2]=new Level(1,4,2,30,3);
levels[3]=new Level(1,5,2,30,4);
levels[4]=new Level(1,6,2,30,5);
levels[5]=new Level(1,7,2,30,6);
}
}
這個項目中,切實體會的是項目的面向對象設計,以及編程思想的重要。
其中使用構造以及static來初始化參數和傳參,對於初學的我來說
的確是很巧妙的運用方法,
在我試圖使用C++來寫這個程序的時候,也切實體會到了編程語言上的實際差異
很多東西使用c++實現起來沒有java方便,當然主要是我的理解還不夠以及
對於C++的編程思路和技巧掌握不到位,以至於我現在還沒有完成。我會盡量
完成它以提高自己的理解。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM