文章出處

一、項目分析

根據輸入速率和正確率將玩家分為不同等級,級別越高,一次顯示的字符數越多,玩家正確輸入一次的得分也越高。如果玩家在規定時間內完成規定次數的輸入,正確率達到規定要求,則玩家升級。玩家最高級別為6級,初始級別一律為一級!

二、掌握的技能點

①面向對象設計的思想

②使用類圖理解類的關系

③類的封裝

④構造方法的使用

⑤this和static關鍵字的使用

 

類的屬性:

①玩家(Player)類的屬性:當前級別號levelNo、當前級別積分currScore、當前級別開始時間startTime和當前級別已用時間elapsedTime

②級別(Level)類的屬性:各級別編號levelNo、各級別一次輸出字符串的長度strLength、各級別輸出字符串的次數strTime、各級別闖關的時間限制timeLimit和各級別正確輸入一次得分

類的方法:

游戲Game類的主要方法有2個:輸出字符串、返回字符串用于和玩家的輸入進行比較[String printStr]確認玩家輸入是否正確[void printResult(String out,String in)],比較游戲輸出out和玩家輸入in

玩家Player類的方法:玩游戲play()

LevelParam類:定義一個長度為6的Level數組,用來存放各級別的具體參數信息

關鍵代碼:

Player類:

package cn.happy;

import java.util.Scanner;

public class Player {
    public int levelNo;  //級別號
    
    public int curScore;  //積分
    
    public long startTime; //開始時間
    
    public 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 Player()
    {}
    
    public Player(int levelNo,int curScore,long startTime,int elapsedTime)
    {
        this.levelNo=levelNo;
        this.curScore=curScore;
        this.startTime=startTime;
        this.elapsedTime=elapsedTime;
        
    }
    //玩游戲的方法
    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.startTime=System.currentTimeMillis();
            this.curScore=0;
            //內層循環 循環一次完成一次字符串的輸入,輸出,比較
            
            for (int j = 0; j < LevelParam.levels[levelNo-1].getStrTimes(); j++) {
                //游戲輸出字符串
                String outstr=game.printStr();
                //接收用戶輸入
                String instr=input.next();
                //游戲判斷玩家輸入的是否正確
                game.printResult(outstr,instr);
            }
        }
        
    }

}

Level類:

package cn.happy;

public class Level {
    public int levelNo; //各級別編號
    
    public int strLength;// 各級別一次輸出字符串的長度
    
    public int strTimes;// 各級別輸出字符串的次數
    
    public int timeLimit;// 各級別闖關的時間限制
    
    public 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 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;
    }

    public Level()
    {}
    
    public Level(int levelNo,int strLength,int strTimes,int timeLimit,int perScore)
    {
        this.levelNo=levelNo;
        this.strLength = strLength;
        this.strTimes = strTimes;
        this.timeLimit = timeLimit;
        this.perScore = perScore;
    }
    
    

}

LevelParam類

package cn.happy;

public class LevelParam {
    //級別參數類,配置各個級別參數
    
    //對應6個級別
    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,15,10);
        levels[5]=new Level(6,7,5,12,15);
    }

}

Game類:

package cn.happy;

import java.util.Random;

public class Game {
    //玩家
    private Player player;
    
    public Game()
    {}
    
    public Game(Player player)
    {
        this.player=player;
    }

    public String printStr()
    {
            
        // 獲取級別對應的要輸出字符串的長度
        int strLength = LevelParam.levels[player.getLevelNo() -1].getStrLength();
        
        StringBuffer buffer=new StringBuffer();
        
        Random random=new Random();
        //通過循環生成要輸出的字符串
        for (int i = 0; i < strLength; i++) {
            //產生隨機數
            int rand=random.nextInt(strLength);
            //根據隨機數拼接字符串
            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;
            }
            
        }
         // 輸出字符串
        System.out.println(buffer);
        // 返回該字符串的值,用于和用戶輸入字符串的值作比較
        return buffer.toString();
    }
    
    //判斷玩家輸入的是否正確,并輸出相應的結果
    public String printResult(String outstr,String instr)
    {
        boolean flag=false;
        if(outstr.equals(instr))
        {
            flag=true;
        }
        else
        {
            System.out.println("輸入錯誤!哈哈");
            System.exit(0);
        }
        if(flag)
        {
            long currentTime=System.currentTimeMillis();
            //如果超時
            if((currentTime-player.getStartTime())/100>LevelParam.levels[player.getLevelNo()-1].getTimeLimit())
                    {
                System.out.println("你輸入的太慢了,已經超時,退出!");
                System.exit(1);
                    }
            
            //計算玩家當前積分
            player.setCurScore(player.getCurScore()+LevelParam.levels[player.getLevelNo()-1].getPerScore());
        
            //計算玩家已用時間
            player.setElapsedTime((int)(currentTime-player.getStartTime())/1000);
            
            //輸出玩家當前級別,當前積分,當前時間
            System.out.println("輸入正確,您的級別是"+player.levelNo+",您的積分是"+player.curScore+",已用時間"+player.elapsedTime+"");
        
        
        }        
        return "hh";
    }
    
}

測試Test類:

package cn.happy;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Player player=new Player();
        player.play();

    }

}


 

 

 

 

 

 

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()