第四次项目《字符打怪兽》

2024-02-26 10:38
文章标签 项目 字符 第四次 怪兽

本文主要是介绍第四次项目《字符打怪兽》,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

/*** * 游戏* @author 周太阳* @version 1.0*/
public class Game {// 玩家private static Player player;// 初始化玩家变量public Game(Player player) {super();// 将Player的属性传给Game,可以使Game操作同一个对象this.player = player;}public Game() {super();// TODO Auto-generated constructor stub}/**输出字符串*/public String printStr() {// 获取怪物信息monsterPrint();StringBuilder sb = new StringBuilder();// 数字强转成字符for (int i = 0; i < Test.level[player.getLevelNo()-1].getStrLength(); i++) {char word = (char) (Math.random() * (127 - 33) + 33);sb.append(word);}// StringBuilder转为String类型return sb.toString();}/**比较游戏输出和玩家输入*/public void printResult(String out, String in) {if (in.equals(out)) {// 获取当前时间毫秒数long currentTime = System.currentTimeMillis();// 计算时间int time = (int) ((currentTime - player.getStartTime())/1000);// 判断是否超时if (time < Test.level[player.getLevelNo()-1].getTimeLimit()) {// 获取怪物生命值mosterHp();// 计算当前耗时player.setElapsedTime(time);// 计算当前分数score();// 输出积分、时间信息userPrints();if (player.getLevelNo() == Test.level.length) {System.out.println("恭喜您通关了!");System.out.println("您的总分为:"+player.getCurrScore());System.exit(0);}}else {System.err.println("太迟了,怪物已经攻过来了,"+player.getName()+"被怪物蹂躏致死!\n『胜败乃兵家常事,少侠请重新来过~』");System.exit(1);}}else {System.err.println("少侠手滑了一下!"+player.getName()+"被怪物蹂躏致死!\n『胜败乃兵家常事,少侠请重新来过~』");System.exit(1);}}/**输出积分、时间信息*/static void userPrints() {System.out.println(player.getName()+"当前积分为:"+player.getCurrScore());System.out.println(player.getName()+"用时:"+player.getElapsedTime()+"秒");}/**计算分数*/private static void score() {int s = Test.level[player.getLevelNo()-1].getPerScore() * (Test.level[player.getLevelNo()-1].getTimeLimit()-player.getElapsedTime());player.setCurrScore(player.getCurrScore()+s);}/**输出怪物信息*/static void monsterPrint() {System.out.println("⊙怪物:"+ Test.level[player.getLevelNo() - 1].getMonsterName());System.out.println("⊙怪物生命值:"+ Test.level[player.getLevelNo() - 1].getHealthPoints());}/**输出怪物生命值*/static void mosterHp() {// 获取当前怪物生命值int hp = Test.level[player.getLevelNo() - 1].getHealthPoints();// 获取攻击后怪物生命值int currentHp = hp - player.getAttack();// 赋值给怪物Test.level[player.getLevelNo() - 1].setHealthPoints(currentHp);System.out.println("※干得漂亮!命中怪物!当前怪物生命值为:"+ (hp-player.getAttack()));}
}
/*** 级别类* @author 周太阳* @version 1.0*/
public class Level{/**各级别编号*/private int levelNo;/**各级别一次输出字符串长度*/private int strLength;/**各级别输出字符串的次数*/private int strTime;/**各级别闯关的时间限制*/private int timeLimit;/**各级别输入正确得分*/private int perScore;/**怪物血量*/private int healthPoints;/**怪物姓名*/private String monsterName;public Level(int levelNo, int strLength, int strTime, int timeLimit,int perScore, int healthPoints, String monsterName) {super();this.levelNo = levelNo;this.strLength = strLength;this.strTime = strTime;this.timeLimit = timeLimit;this.perScore = perScore;this.healthPoints = healthPoints;this.monsterName = monsterName;}public int getHealthPoints() {return healthPoints;}public void setHealthPoints(int healthPoints) {this.healthPoints = healthPoints;}public String getMonsterName() {return monsterName;}public void setMonsterName(String monsterName) {this.monsterName = monsterName;}public Level() {super();// TODO Auto-generated constructor stub}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 getStrTime() {return strTime;}public void setStrTime(int strTime) {this.strTime = strTime;}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[] normal = new Level[6];/**困难难度*/public final static Level[] hard = new Level[6];static {normal[0] = new Level(1, 1, 2, 10, 1, 100, "『大海龟』");normal[1] = new Level(2, 1, 2, 10, 3, 500, "『骷髅怪』");normal[2] = new Level(3, 2, 2, 8, 5, 1000, "『黑熊精』");normal[3] = new Level(4, 2, 2, 8, 7, 3000, "『雷鸟人』");normal[4] = new Level(5, 3, 2, 7, 9, 5000, "『天将』");normal[5] = new Level(6, 4, 2, 6, 11, 10000, "『地狱战神』");hard[0] = new Level(1, 2, 2, 10, 3, 100, "『大海龟』");hard[1] = new Level(2, 4, 2, 9, 5, 500, "『骷髅怪』");hard[2] = new Level(3, 4, 2, 8, 7, 1000, "『黑熊精』");hard[3] = new Level(4, 6, 2, 7, 9, 3000, "『雷鸟人』");hard[4] = new Level(5, 6, 2, 6, 11, 5000, "『天将』");hard[5] = new Level(6, 8, 2, 6, 13, 10000, "『地狱战神』");}
}
/*** 玩家类* @author 周太阳* @version 1.0* @date 2019年4月12日 下午9:29:40*/
public class Player {/**当前级别号*/private int levelNo;/**当前级别得分*/private int currScore;/**开始时间*/private long startTime;/**当前级别已用时间*/private int elapsedTime;/**玩家攻击力*/private int attack;/**玩家姓名*/private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAttack() {return attack;}public void setAttack(int attack) {this.attack = attack;}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(int elapsedTime) {this.elapsedTime = elapsedTime;}/**玩游戏*/public void play() {Game game = new Game(this);Level lv = new Level();System.out.println("游戏开始!");// 循环输入次数for (int i = 0; i < Test.level.length; i++) {// 升级levelNo++;// 计时清零startTime = 0;// 玩家攻击力attack = Test.level[i].getHealthPoints() -Test.level[i].getHealthPoints()/Test.level[levelNo - 1].getStrTime();System.out.println("★"+name+"当前级别为:"+levelNo+"级");System.out.println("★"+name+"当前攻击力为:"+attack);System.out.println("〉〉〉怪物来啦!!!(⊙_⊙)");for (int j = 0; j < Test.level[i].getStrTime(); j++) {// 随机输出字符String out = game.printStr();System.out.println("¤"+name+"抓紧时间!请快输入:【"+out+"】怪物将在"+Test.level[levelNo-1].getTimeLimit()+"秒后开始攻击!!!");// 开始时间startTime = System.currentTimeMillis();Scanner input = new Scanner(System.in);String in = input.next();// 输出结果game.printResult(out, in);}System.out.println("↑↑↑↑↑少侠升级啦!!!↑↑↑↑↑");}}
}
/*** 玩家玩游戏*/
public class Test {/**选择难度数组*/static Level[] level = new Level[6];public static void main(String[] args) {Scanner input = new Scanner(System.in);Player player = new Player();System.out.println("————————————————————————————");System.out.println("|\t\t☆欢迎来到字符打怪兽☆   \t\t|");System.out.println("————————————————————————————");System.out.print("请输入您的姓名:");String name = input.next();// 玩家姓名player.setName(name);do {System.out.print("请选择难度(1、【正常】 2、【困难】):");level = input.nextInt() == 1 ? LevelParam.normal : LevelParam.hard;// 开始玩游戏player.play();// 是否继续}while(isContinue());System.out.println("===游戏结束。欢迎下次再来!===");}/**是否继续游戏*/public static boolean isContinue() {Scanner input = new Scanner(System.in);System.out.println("是否继续(y/n)?");String select = input.next();if(select.equals("y")) {return true;}else {return false;}}
}

这篇关于第四次项目《字符打怪兽》的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/748581

相关文章

Node.js 数据库 CRUD 项目示例详解(完美解决方案)

《Node.js数据库CRUD项目示例详解(完美解决方案)》:本文主要介绍Node.js数据库CRUD项目示例详解(完美解决方案),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考... 目录项目结构1. 初始化项目2. 配置数据库连接 (config/db.js)3. 创建模型 (models/

springboot项目中常用的工具类和api详解

《springboot项目中常用的工具类和api详解》在SpringBoot项目中,开发者通常会依赖一些工具类和API来简化开发、提高效率,以下是一些常用的工具类及其典型应用场景,涵盖Spring原生... 目录1. Spring Framework 自带工具类(1) StringUtils(2) Coll

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

一文教你如何将maven项目转成web项目

《一文教你如何将maven项目转成web项目》在软件开发过程中,有时我们需要将一个普通的Maven项目转换为Web项目,以便能够部署到Web容器中运行,本文将详细介绍如何通过简单的步骤完成这一转换过程... 目录准备工作步骤一:修改​​pom.XML​​1.1 添加​​packaging​​标签1.2 添加

tomcat多实例部署的项目实践

《tomcat多实例部署的项目实践》Tomcat多实例是指在一台设备上运行多个Tomcat服务,这些Tomcat相互独立,本文主要介绍了tomcat多实例部署的项目实践,具有一定的参考价值,感兴趣的可... 目录1.创建项目目录,测试文China编程件2js.创建实例的安装目录3.准备实例的配置文件4.编辑实例的

springboot集成Deepseek4j的项目实践

《springboot集成Deepseek4j的项目实践》本文主要介绍了springboot集成Deepseek4j的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录Deepseek4j快速开始Maven 依js赖基础配置基础使用示例1. 流式返回示例2. 进阶

SpringBoot项目启动报错"找不到或无法加载主类"的解决方法

《SpringBoot项目启动报错找不到或无法加载主类的解决方法》在使用IntelliJIDEA开发基于SpringBoot框架的Java程序时,可能会出现找不到或无法加载主类com.example.... 目录一、问题描述二、排查过程三、解决方案一、问题描述在使用 IntelliJ IDEA 开发基于

SpringBoot项目使用MDC给日志增加唯一标识的实现步骤

《SpringBoot项目使用MDC给日志增加唯一标识的实现步骤》本文介绍了如何在SpringBoot项目中使用MDC(MappedDiagnosticContext)为日志增加唯一标识,以便于日... 目录【Java】SpringBoot项目使用MDC给日志增加唯一标识,方便日志追踪1.日志效果2.实现步

C语言字符函数和字符串函数示例详解

《C语言字符函数和字符串函数示例详解》本文详细介绍了C语言中字符分类函数、字符转换函数及字符串操作函数的使用方法,并通过示例代码展示了如何实现这些功能,通过这些内容,读者可以深入理解并掌握C语言中的字... 目录一、字符分类函数二、字符转换函数三、strlen的使用和模拟实现3.1strlen函数3.2st