根据项目的要求分别建出几个类
有游戏类
玩家类
测试类
等级类
等级时间类
一以下类图:
游戏类:
public class Game { private Player player; public Game(Player player) { super(); this.player = player; } public String printStr(){ //对字符串进行增删改查 StringBuffer buffer = new StringBuffer(); //创建随机数对象 Random random = new Random(); //循环生成对应玩家等级长度的字符串 for (int i = 0;i < LevelParam.levels[player.getLevelNo() - 1].getStrLength();i++ ) { //每次循环就产生一个随机数 int rand = random.nextInt(6); //拼接产生随机数所对应的字符串 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.toString()); //返回生成的字符串 return buffer.toString(); } //对比系统生成的字符串和玩家输入的字符串是否一样 public void printResult(String out,String in){ if(out.equals(in)){ long currentTime=System.currentTimeMillis(); //如果超时 if((currentTime-player.getStartTime())/1000>LevelParam.levels[player.getLevelNo()-1].getTimeLimit()){ System.out.print("你的速度连乌龟都比不上,已经超时,退出!"); System.exit(1); //如果没有超时 }else { //计算玩家当前积分 player.setCurrScore(player.getCurrScore()+LevelParam.levels[player.getLevelNo()-1].getPerScore()); //计算玩家以用时间 player.setElapsedTime((int) ((currentTime - player .getStartTime()) / 1000)); //输出玩家当前级别,当前积分和以用时间 System.out.println("输入正确,您的级别"+player.getLevelNo()+",您的积分"+player.getCurrScore()+",已用时间"+player.getElapsedTime()+"秒"); //判断用户是否已经闯过最后一关并处理 if(player.getLevelNo()==6){ int score=LevelParam.levels[player.getLevelNo() - 1].getPerScore() * LevelParam.levels[player.getLevelNo() - 1].getStrTime(); if(player.getCurrScore()==score){ System.out.println("恭喜您,你真是个人才!"); }else{ System.out.println("藤球你被淘汰了,退出!"); System.exit(0); } } } }else{ System.out.println("输入错误"); System.out.println("正确的是"+out); System.exit(0); } }
玩家类:
public class Player { //当前级别号 private int levelNo; //当前级别积分 private int currScore; //当前级别开始时间 private long startTime=0; //当前级别以用时间 private int elapsedTime; 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 getElapsedTime() { return elapsedTime; } public void setElapsedTime(long l) { this.elapsedTime = (int) l; } public Player(int levelNo, int currScore, long startTime, int elapsedTime) { super(); this.levelNo = levelNo; this.currScore = currScore; this.startTime = startTime; this.elapsedTime = elapsedTime; } public Player() { super(); // TODO Auto-generated constructor stub } public void play(){ System.out.println("游戏开始请输入"); Game game=new Game(this); // game.setPlayer(this); Scanner scanner = new Scanner(System.in); //外层循环代表着等级 for (int i = 0;i < LevelParam.levels.length ;i++ ) { //玩家晋级 levelNo += 1; //获得新的游戏开始时间 startTime = System.currentTimeMillis(); //每次晋级玩家积分清零 currScore = 0; //内层循环代表着每个级别所需要玩的次数 for (int j = 0;j < LevelParam.levels[i].getStrTime() ;j++ ) { //系统生成的字符串 String out = game.printStr(); //玩家输入的字符串 String in = scanner.next(); //比较,产生结果 game.printResult(out,in); } }
等级类:
public class Level { private int levelNo; // 级别号 private int strLength; // 各级别一次输出字符串的长度 private int strTime; // 各级别输出字符 串的次数 private int timeLimit; // 各级别闯关的时间限制 private int perScore; // 各级别成功输入一次字符串后增加的分值 public Level(int levelNo, int strLength, int strTime, int timeLimit, int perScore) { super(); this.levelNo = levelNo; this.strLength = strLength; this.strTime = strTime; this.timeLimit = timeLimit; this.perScore = perScore; } public int getLevelNo() { return levelNo; } public void setLevelNo(int levelNo) { this.levelNo = levelNo; } public int getStrTime() { return strTime; } public void setStrTime(int strTime) { this.strTime = strTime; } public Level() { super(); } public int getLeveNo() { return levelNo; } public void setLeveNo(int leveNo, int levelNo) { this.levelNo = levelNo; } public int getStrLength() { return strLength; } public void setStrLength(int strLength) { this.strLength = strLength; } public int getStrTimes() { return strTime; } public void setStrTimes(int strTimes) { this.strTime = 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 class LevelParam { 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); } }
测试类:
public static void main(String[] args) { // TODO Auto-generated method stub Player player = new Player(); player.play(); }