1、级别参数类
package myQuickHit;
/*
* 我想休息
* 不,你没有钱!
*@ Time : 2023/7/22 15:14
*@Author: Nyg
*
* ** 级别参数类
*/
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);
}
}
2、等级类
package myQuickHit;
/*
* 我想休息
* 不,你没有钱!
*@ Time : 2023/7/22 14:41
*@Author: Nyg
*
* ** 等级类
*/
public class Level {
private int levelNo; // 各级别编号
private int strLength; // 各级别一次输出字符串的长度
private int strCount; // 各级别输出字符串的次数
private int timeLimit; // 各级别闯关的时间限制
private int perScore; // 各级别正确输入一次的得分
public Level() {
}
public Level(int levelNo, int strLength, int strCount, int timeLimit, int perScore) {
this.levelNo = levelNo;
this.strLength = strLength;
this.strCount = strCount;
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 getStrCount() {
return strCount;
}
public void setStrCount(int strCount) {
this.strCount = strCount;
}
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;
}
}
3、游戏类
package myQuickHit;
import java.util.Random;
/*
* 我想休息
* 不,你没有钱!
*@ Time : 2023/7/22 14:40
*@Author: Nyg
*
* ** 游戏类
*/
public class Game {
private Player player; // 玩家
public Game(Player player) {
super();
this.player = player;
}
public Game() {
}
// 输出字符串,用于和玩家输入的字符串进行比较
public String printStr() {
// 根据级别号-1,对应级别参数数组
int strLength = LevelParam.levels[player.getLevelNo() - 1].getStrLength();
// 随机生成字符串
StringBuffer buffer = new StringBuffer();
Random r = new Random();
// 1、通过循环生成要输出的字符串
for (int i = 0; i < strLength; i++) {
// 产生随机数
int rand = r.nextInt(strLength);
// 根据随机数拼接字符串
switch (rand) {
case 0:
buffer.append("w");
break;
case 1:
buffer.append("a");
break;
case 2:
buffer.append("s");
break;
case 3:
buffer.append("d");
break;
case 4:
buffer.append("e");
break;
case 5:
buffer.append("q");
break;
default:
break;
}
}
// 输出字符串
System.out.println("***>>> Game:printStr():buffer \n\t\t\t" + buffer);
// 返回字符串用于和玩家输入相比较
return buffer.toString();
}
// 比较游戏输出和玩家输入,根据比较结果输出相应信息
public void printResult(String outStr, String inStr) {
// 保存比较后的结果
boolean flag = false;
// 使用equals比较字符串是否相等
if (outStr.equals(inStr) == true) {
flag = true; // 表示玩家答对
} else {
flag = false; // 表示玩家答错
}
// 如果答对了
if (flag == true) {
// 拿到当前系统时间
long currentTime = System.currentTimeMillis();
// 如果超时
if ((currentTime - player.getStartTime()) / 1000 > LevelParam.levels[player.getLevelNo() - 1].getTimeLimit()) {
long second = (currentTime - player.getStartTime()) / 1000;
System.out.println();
System.out.println("您本局用时:" + second + " 秒");
System.out.println("你输入太慢了,已经超时,退出游戏!");
System.exit(1);
} else { // 如果未超时
// 计算当前积分:玩家已有分数,当前玩的这个级别通过应该加的分数
player.setCurScore(player.getCurScore() + LevelParam.levels[player.getLevelNo() - 1].getPerScore());
// 计算已用时间
player.setElapsedTime((int) ((currentTime - 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].getStrCount();
if (player.getCurScore() == score) { // 玩家分数和闯关的分数一样,闯关成功
System.out.println("恭喜您,闯关成功!!!");
System.out.println("将自动退出游戏!!!");
System.exit(0);// 正常退出
}
}
}
} else { // 如果输出错误
System.out.println("您输入错误,退出游戏!");
System.exit(1);
}
}
}
4、玩家类
package myQuickHit;
import java.util.Scanner;
/*
* 我想休息
* 不,你没有钱!
*@ Time : 2023/7/22 14:40
*@Author: Nyg
*
* ** 玩家类
*/
public class Player {
private int levelNo; // 当前级别号
private int curScore; // 当前级别积分
private long startTime; // 当前级别开始时间
private int 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 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 void play() {
// 创建游戏对象
Game g = new Game(this);
// 玩家通过键盘输入
Scanner sc = new Scanner(System.in);
// 外层循环控制游戏晋级
for (int i = 0; i < LevelParam.levels.length; i++) {
// 等级从1级开始
this.levelNo += 1;
// 晋级后计时清零
this.startTime = System.currentTimeMillis();
// 积分清零
this.curScore = 0;
// 内循环:循环一次完成一次比较系统输出和玩家输入
for (int j = 0; j < LevelParam.levels[levelNo - 1].getStrCount(); j++) {
// 系统输出字符串
String outStr = g.printStr();
// 玩家输入字符串
System.out.print("请玩家输入:");
String inStr = sc.next();
// 判断玩家输入的字符并输出相应的结果
g.printResult(outStr,inStr);
}
}
}
}
5、测试类
package myQuickHit;
/*
* 我想休息
* 不,你没有钱!
*@ Time : 2023/7/22 23:35
*@Author: Nyg
*
* ** 玩家玩游戏,测试类
*/
public class Test {
public static void main(String[] args) {
Player player=new Player();
player.play();
}
}