一:创建所需的类
Player(玩家类),Game(游戏类),Level(级别类),LevelParam(级别具体参数信息类),MyTest类
Player(玩家类),Game(游戏类),Level(级别类),LevelParam(级别具体参数信息类),MyTest类
二:各类的属性或方法:
Player类:
public class Player {
private int levelNo; // 级别号
private int curScore; // 当前积分
private long startTime = 0; // 各级别开始时间
private int elapsedTime; // 各级别已用时间
private Scanner input;
public long getStartTime() {
return startTime;
}
public void setStartTime(long startTime) {
this.startTime = startTime;
}
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 int getElapsedTime() {
return elapsedTime;
}
public void setElapsedTime(int elapseTime) {
this.elapsedTime = elapseTime;
}
public void play() {
Game game = new Game(this);
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].getStrTime(); j++) {
// 游戏输出字符串
String outStr = game.printStr();
// 接收用户输入
String inStr = input.next();
// 游戏判断玩家输入是否正确并输出
game.printResult(outStr, inStr);
}
}
}
<pre code_snippet_id="2312492" snippet_file_name="blog_20170405_2_6046868" name="code" class="java">public class Game {
private Player player;
public Game(Player player) {
super();
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("1");
break;
case 1:
buffer.append("1");
break;
case 2:
buffer.append("1");
break;
case 3:
buffer.append("1");
break;
case 4:
buffer.append("1");
break;
case 5:
buffer.append("1");
break;
}
}
System.out.println(buffer);
return buffer.toString();
}
public void GetOutOrPrint(String out, String in) {
boolean flag;
if (out.equals(in)) {
flag = true;
} else {
flag = false;
}
// 如果输入的正确
// 超时的话
if (flag) {
long Time = System.currentTimeMillis(); // 获取当前系统时间
if ((Time - player.getStartTime()) / 1000 > LevelParam.levels[player
.getLevelNo() - 1].getTimeLimit()) {
System.out.println("输入太慢,已经超时!");
System.exit(1);
} else {
// 没有超时的话
// 计算当前的积分,已用的时间,然后输出
player.setCurScore(player.getCurScore()
+ LevelParam.levels[player.getLevelNo() - 1]
.getPerScore());
player.setElapsedTime((int) (Time - player.getStartTime()) / 1000);
System.out.println("输入正确,您的级别:" + player.getLevelNo()
+ ",您的积分:" + player.getCurScore() + "已用时间:"
+ player.getElapsedTime());
// 判断是否闯关最后一关
// 获取闯关积分
if (player.getLevelNo() == 6) {
int score = LevelParam.levels[player.getLevelNo() - 1]
.getPerScore()
* LevelParam.levels[player.getLevelNo() - 1]
.getStrTimes();
// 判斷積分是否達到
if (player.getCurScore() == score) {
System.out.println("您已经通关");
System.exit(1);
}
}
}
} else {
// 如果輸入錯誤
System.out.println("輸入錯誤!程序退出......");
System.exit(1);
}
}
}</pre><br>
<br>
<pre></pre>
<p></p>
<pre></pre>
<span style="font-size:13.3333px"> </span><pre code_snippet_id="2312492" snippet_file_name="blog_20170405_3_6886683" name="code" class="java"><span style="font-size: 13.3333px;"> </span><strong><span style="font-size:18px;color:#ff0000;">Level类</span></strong><span style="font-size: 13.3333px;">
</span></pre><span style="font-size:13.3333px"> </span><span style="font-size:18px; color:rgb(255,0,0)"><strong></strong></span>
<p></p>
<p></p>
<pre code_snippet_id="2312492" snippet_file_name="blog_20170405_4_583418" name="code" class="java">public class Level {
public int levelNo;//級別編號
public int strLength;//輸出字符串的長度
public int strTimes;//輸入字符串的次數
public int timeLimit;//時間限制
public int perScore;//得分
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;
}
}</pre><span style="font-size:18px"><span style="color:#ff0000"><strong> LevelParam类:</strong></span></span><pre code_snippet_id="2312492" snippet_file_name="blog_20170405_5_2012660" name="code" class="java">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);
}
}</pre><span style="color:#ff0000"> <strong>MyTest类:</strong></span><br>
<p></p>
<p><span style="font-size:18px"><span style="font-size:13.3333px"></span></span></p>
<pre code_snippet_id="2312492" snippet_file_name="blog_20170405_6_4106211" name="code" class="java">public class <span style="font-size: 13.3333px;">MyTest</span>{
/**
* @param args
*/
public static void main(String[] args) {
Player p=new Player();
p.play();
}
}</pre><br>
<br>
<p></p>