1 package com.ketang.game;
 2 
 3 /**
 4  * 游戏级别类
 5  * @author 
 6  *
 7  */
 8 public class Level {
 9     private int levelNo;  //各级别编号
10     private int strLength;  //字符串长度
11     private int strTimes;  //字符串的次数
12     private int timeLimit;  //各级别闯关的时间限制
13     private int perScore;  //各级别正确输入一次的得分
14     
15     public Level() {
16     }
17     
18     public Level(int levelNo, int strLength, int strTimes, int timeLimit, int perScore) {
19         this.levelNo = levelNo;
20         this.strLength = strLength;
21         this.strTimes = strTimes;
22         this.timeLimit = timeLimit;
23         this.perScore = perScore;
24     }
25 
26     public int getLevelNo() {
27         return levelNo;
28     }
29     public void setLevelNo(int levelNo) {
30         this.levelNo = levelNo;
31     }
32     public int getStrLength() {
33         return strLength;
34     }
35     public void setStrLength(int strLength) {
36         this.strLength = strLength;
37     }
38     public int getStrTimes() {
39         return strTimes;
40     }
41     public void setStrTimes(int strTimes) {
42         this.strTimes = strTimes;
43     }
44     public int getTimeLimit() {
45         return timeLimit;
46     }
47     public void setTimeLimit(int timeLimit) {
48         this.timeLimit = timeLimit;
49     }
50     public int getPerScore() {
51         return perScore;
52     }
53     public void setPerScore(int perScore) {
54         this.perScore = perScore;
55     }
56     
57 }
 1 package com.ketang.game;
 2 
 3 /**
 4  * 级别参数类,配置各个级别参数
 5  * @author 
 6  *
 7  */
 8 public class LevelParam {
 9     public static final Level[] levels=new Level[6];
10     static {
11         levels[0]=new Level(1,2,10,30,1);
12         levels[1]=new Level(2,3,9,26,2);
13         levels[2]=new Level(3,4,8,22,5);
14         levels[3]=new Level(4,5,7,18,8);
15         levels[4]=new Level(5,6,6,15,10);
16         levels[5]=new Level(6,7,5,12,15);
17     }
18 
19 }
 1 package com.ketang.game;
 2 
 3 import java.util.Scanner;
 4 
 5 /**
 6  * 玩家类
 7  * @author 
 8  *
 9  */
10 public class Player {
11     private int levelNo;  //玩家当前级别号
12     private int curScore;  //玩家当前级别积分
13     private long startTime;  //当前级别开始时间
14     private int elapsedTime;  //当前级别已用时间
15     
16     public Player() {
17         super();
18     }
19     
20     public Player(int levelNo, int curScore, long startTime, int elapsedTime) {
21         this.levelNo = levelNo;
22         this.curScore = curScore;
23         this.startTime = startTime;
24         this.elapsedTime = elapsedTime;
25     }
26 
27     public int getLevelNo() {
28         return levelNo;
29     }
30     public void setLevelNo(int levelNo) {
31         this.levelNo = levelNo;
32     }
33     public int getCurScore() {
34         return curScore;
35     }
36     public void setCurScore(int curScore) {
37         this.curScore = curScore;
38     }
39     public long getStartTime() {
40         return startTime;
41     }
42     public void setStartTime(long startTime) {
43         this.startTime = startTime;
44     }
45     public int getElapsedTime() {
46         return elapsedTime;
47     }
48     public void setElapsedTime(int elapsedTime) {
49         this.elapsedTime = elapsedTime;
50     }
51     
52     //玩家玩游戏
53     public void play() {
54         Game game=new Game(this);
55         Scanner input=new Scanner(System.in);
56         for(int i=0;i<LevelParam.levels.length;i++) {
57             //晋级
58             this.levelNo+=1;
59             //晋级后计时清零,积分清零
60             this.startTime=System.currentTimeMillis();
61             this.curScore=0;
62             //内循环,循环一次完成一次字符串的输出、输入、比较
63             for(int j=0;j<LevelParam.levels[levelNo-1].getStrTimes();j++) {
64                 //游戏输出字符串
65                 String outStr = game.printStr();
66                 //接收用户输入
67                 String inStr = input.next();
68                 //游戏判断玩家输入是否正确,并输出相应结果信息
69                 game.printResult(outStr, inStr);
70             }
71         }
72     }
73 }
  1 package com.ketang.game;
  2 
  3 import java.util.Random;
  4 
  5 /**
  6  * 游戏类
  7  * @author 
  8  *
  9  */
 10 public class Game {
 11     private Player player;  //玩家
 12 
 13     public Game() {
 14     }
 15 
 16     /**
 17      * 构造方法,传入玩家信息
 18      * @param player  玩家
 19      */
 20     public Game(Player player) {
 21         super();
 22         this.player = player;
 23     }
 24 
 25     public Player getPlayer() {
 26         return player;
 27     }
 28 
 29     public void setPlayer(Player player) {
 30         this.player = player;
 31     }
 32 
 33     /**
 34      * 输出指定级别规定长度的字符串
 35      * @return 输出的字符串,用于和用户输入比较
 36      */
 37     public String printStr() {
 38         int strLength=LevelParam.levels[player.getLevelNo()-1].getStrLength();
 39         StringBuffer buffer=new StringBuffer();
 40         Random random=new Random();
 41         //通过循坏生成要输出的字符串
 42         for(int i=0;i<strLength;i++) {
 43             //产生随机数
 44             int rand=random.nextInt(strLength);
 45             //根据字符串拼接字符串
 46             switch(rand) {
 47             case 0:
 48                 buffer.append(">");
 49                 break;
 50             case 1:
 51                 buffer.append("<");
 52                 break;
 53             case 2:
 54                 buffer.append("*");
 55                 break;
 56             case 3:
 57                 buffer.append("&");
 58                 break;
 59             case 4:
 60                 buffer.append("%");
 61                 break;
 62             case 5:
 63                 buffer.append("#");
 64                 break;
 65             }        
 66         }
 67         //输出字符串
 68         System.out.println(buffer);
 69         //返回字符串用于和玩家输入相比较
 70         return buffer.toString();
 71     }
 72     
 73     /**
 74      * 判断玩家输入是否正确,并输出相应结果信息
 75      * @param out  游戏输出的字符串
 76      * @param in  玩家输入的字符串
 77      */
 78     public void printResult(String out, String in) {
 79         boolean flag=false;
 80         if(out.equals(in)) {
 81             flag=true;
 82         }else {
 83             flag=false;
 84         }
 85         //如果输入正确
 86         if(flag) {
 87             long currentTime=System.currentTimeMillis();
 88             //如果超时
 89             if((currentTime-player.getStartTime())/1000>LevelParam.levels[player.getLevelNo()-1].getTimeLimit()) {
 90                 System.out.println("你输入太慢了,已经超时,退出!");
 91                 System.exit(1);
 92                 //如果没有超时
 93             }else {
 94                 //计算当前积分
 95                 player.setCurScore(player.getCurScore()+LevelParam.levels[player.getLevelNo()-1].getPerScore());
 96                 //计算已用时间
 97                 player.setElapsedTime((int)((currentTime-player.getStartTime())/1000));
 98                 //输出当前积分、当前级别、已用时间
 99                 System.out.println("输入正确,您的级别"+player.getLevelNo()+",您的积分"+player.getCurScore()
100                 +",已用时间"+player.getElapsedTime()+"秒。");
101                 //判断用户是否已经闯过最后一关
102                 if(player.getLevelNo()==6) {
103                     int score=LevelParam.levels[player.getLevelNo()-1].getPerScore()
104                             *LevelParam.levels[player.getLevelNo()-1].getStrTimes();
105                     if(player.getCurScore()==score) {
106                             System.out.println("你已闯关成功,成为绝世高手,恭喜你!");
107                             System.exit(0);
108                     }
109                 }
110             }
111             //如果输入错误
112         }else {
113             System.out.println("输入错误,退出!");
114             System.exit(1);
115         }
116     }
117 }
1 package com.ketang.game;
2 
3 public class Test {
4     public static void main(String[] args) {
5         Player player=new Player();
6         player.play();
7     }
8 }

 

posted on 2018-12-05 22:23  从零开始-白  阅读(1061)  评论(0编辑  收藏  举报