JAVA实现RPG生成器

一. 题目要求

1.功能描述

几乎所有的RPG游戏(一种源自《龙与地下城》的游戏类型)在进入游戏时都会让用户自己来创建自己喜欢的角色。本次上机要求编写一个简化的创建游戏角色的程序。
在这里插入图片描述

2.游戏角色应有的属性

本题目要求的游戏角色应有以下属性:名字、性别、种族、职业、力量、敏捷、体力、智力、智慧、生命值和魔法值。
名字:不超过50个字符。
性别:可以选择男性和女性。
种族:一共可选五个种族,人类、精灵、兽人、矮人和元素。
职业:可选六种职业,狂战士、圣骑士、刺客、猎手、祭司和巫师。
其余属性均为整数。
本题目要求首先用户输入角色姓名,然后由用户选择角色性别,然后由用户选择种族,然后选择职业,然后自动分配力量、敏捷、体力、智力和智慧属性,并计算生命值和魔法值。
生命值=体力*20。
魔法值=(智力+智慧)*10。

3.职业限制

很多职业会限制某些种族选择,例如兽人不能就职圣骑士等等,种族和职业的限制表如下:
在这里插入图片描述
所以在要求用户选择职业时,输出信息里面只能有用户所选择种族可以就职的职业。

4.初始属性

本题目要求力量、敏捷、体力、智力和智慧要求是随机值(利用随机数函数来取得随机数),但是五项属性的总和应该是100,并且应该和职业相关。例如狂战士的体力和力量就要比较高,而巫师需要较高的智力,而祭司则需要较高的智慧。各职业初始属性的大致比例应遵从下表:
在这里插入图片描述
例如,前面示意图中的祭司的初始属性,大致满足该比例,但是应该是随机的。
然后利用属性值计算生命值和魔法值。

5.显示信息

最后向用户显示该角色的所有信息,然后询问用户是否满意,如用户不满意则重新创建,若用户满意则程序结束,并将用户创建角色的相关信息写入文件保存。

二. 类图设计

在这里插入图片描述

三. 算法实现

1.Main类

import java.io.IOException;

public class Main {
	public static void main(String[] args) throws IOException {
		boolean flagMain =true; //flagMain用于判断是否满意,满意则结束
		while(flagMain) {
		Role r1 =new Role();
		r1.menu();
		flagMain = r1.accept();
		if(flagMain) {
			r1 = null;
		}
		}
		System.out.println("角色已经创建");
	}
}


2.Role类

import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

public class Role {
	String name;
	String sex;
	String racial;
	String career;
	int strength;//力量
	int agilitty;//敏捷
	int energy;//体力
	int intelligence;//智力
	int wisdom;//智慧
	int HP; //血量
	int MP;//魔法量
	Scanner sc = new Scanner(System.in);
	//初始化name
	void inPutName() {
		System.out.print("请输入您的角色姓名:");
		String name = new String();
		boolean flag =true;
		while(flag) {
			name = sc.nextLine();
			if(name.length()>50)
				System.out.print("名字不合法,请重新输入:");
			if(name.length()<=50)
				flag =false;
		}
		if(!flag)
			this.name= name;
	}
	//初始化性别
	void selectSex() {
		System.out.print("请选择您的游戏角色性别(1:男 2:女):");
		boolean flag = true;
		while(flag) {
			String s = sc.nextLine();
			switch(s) {
		case "1": 
			this.sex = "男";
			flag = false;	
			break;
		case "2": 
			this.sex = "女";
			flag = false;
			break;
		default: 
			System.out.print("请在1和2中选择:");
			break;
			}
	}
	}
	//初始化种族
	void selectRacial() {
		System.out.print("请选择您的游戏角色种族(1:人类 2:精灵 3:兽人 4:矮人 5:元素):");
		boolean flag = true;
		while(flag) {
			String s =sc.nextLine();
		switch(s) {
		case "1": this.racial = "人类";	flag = false; break;
		case "2": this.racial = "精灵";	flag = false; break;
		case "3": this.racial = "兽人";	flag = false; break;
		case "4": this.racial = "矮人";	flag = false; break;
		case "5": this.racial = "元素";	flag = false; break;
		default: System.out.print("请在1-5之间选择:"); break;
		}
		
		}
	}
	//初始化职业
	void selectCareer(String currentRacial) {
		System.out.print("请选择职业");
		boolean flag = true;
		while(flag) {
			String s;
		switch(currentRacial) {
		case "人类": 
			System.out.print("(1:狂战士 2:圣骑士 3:刺客 4:猎手 5:祭司 6:巫师):");
			s=sc.next();
			switch(s) {
			case "1":	this.career="狂战士";		flag = false; break;
			case "2":	this.career="圣骑士";		flag = false; break;
			case "3":	this.career="刺客"; 		flag = false; break;
			case "4":	this.career="猎手"; 		flag = false; break;
			case "5":	this.career="祭司"; 		flag = false; break;
			case "6":	this.career="巫师"; 		flag = false; break;
			default :	System.out.println("输入有误,请重新在以下选项中选择");	  break;
			}
			break;
		
		case "精灵": 
			System.out.print("( 1:刺客 2:猎手 3:祭司 4:巫师 )  :");
			s=sc.next();
			switch(s) {
			case "1":	this.career="刺客"; 		flag = false; break;
			case "2":	this.career="猎手"; 		flag = false; break;
			case "3":	this.career="祭司"; 		flag = false; break;
			case "4":	this.career="巫师"; 		flag = false; break;
			default :	System.out.println("输入有误,请重新在以下选项中选择"); break;
			}
			break;
	
		case "兽人": 
			System.out.print("(1:狂战士  2:猎手 3:祭司 ):");
			s=sc.next();
			switch(s) {
			case "1":	this.career="狂战士";		flag = false; break;
			case "2":	this.career="猎手"; 		flag = false; break;
			case "3":	this.career="祭司"; 		flag = false; break;
			default :	System.out.println("输入有误,请重新在以下选项中选择"); break;
			}
			break;
		
		case "矮人": 
			System.out.print("(1:狂战士 2:圣骑士 3:祭司 ):");
			s=sc.next();
			switch(s) {
			case "1":	this.career="狂战士";		flag = false; break;
			case "2":	this.career="圣骑士";		flag = false; break;
			case "3":	this.career="祭司"; 		flag = false; break;
			default :	System.out.println("输入有误,请重新在以下选项中选择"); break;
			}
			break;
		case "元素": 
			System.out.print("(1:祭司 2:巫师):");
			s=sc.next();
			switch(s) {
			case "1":	this.career="祭司"; 		flag = false; break;
			case "2":	this.career="巫师"; 		flag = false; break;
			default :	System.out.println("输入有误,请重新在以下选项中选择"); break;
			}
			break;
		default :	break;
		}
		}
		
	}
	//初始化属性
	void initializeAttribute(String currentCareer) {
		System.out.print("请选择您的职业:");
		Random rd = new Random();
		String s = new String(currentCareer);
		switch(s) {
		case "狂战士":	
			this.strength = rd.nextInt(3)+39;
			this.agilitty = rd.nextInt(3)+19;
			this.energy = rd.nextInt(3)+29;
			this.intelligence = rd.nextInt(3)+4;
			this.wisdom = 100-(this.strength+this.agilitty+this.energy+this.intelligence);
			this.HP = this.energy*20;
			this.MP = (this.intelligence+this.wisdom)*10;
			break;
		case "圣骑士":
			this.strength = rd.nextInt(3)+24;
			this.agilitty = rd.nextInt(3)+14;
			this.energy = rd.nextInt(3)+29;
			this.intelligence = rd.nextInt(3)+19;
			this.wisdom = 100-(this.strength+this.agilitty+this.energy+this.intelligence);
			this.HP = this.energy*20;
			this.MP = (this.intelligence+this.wisdom)*10;
			break;
		case "刺客":	
			this.strength = rd.nextInt(3)+19;
			this.agilitty = rd.nextInt(3)+34;
			this.energy = rd.nextInt(3)+19;
			this.intelligence = rd.nextInt(3)+14;
			this.wisdom = 100-(this.strength+this.agilitty+this.energy+this.intelligence);
			this.HP = this.energy*20;
			this.MP = (this.intelligence+this.wisdom)*10;
			break;
		case "猎手":	
			this.strength = rd.nextInt(3)+14;
			this.agilitty = rd.nextInt(3)+39;
			this.energy = rd.nextInt(3)+14;
			this.intelligence = rd.nextInt(3)+9;
			this.wisdom = 100-(this.strength+this.agilitty+this.energy+this.intelligence);
			this.HP = this.energy*20;
			this.MP = (this.intelligence+this.wisdom)*10;
			break;
		case "祭司":
			this.strength = rd.nextInt(3)+14;
			this.agilitty = rd.nextInt(3)+19;
			this.energy = rd.nextInt(3)+14;
			this.intelligence = rd.nextInt(3)+34;
			this.wisdom = 100-(this.strength+this.agilitty+this.energy+this.intelligence);
			this.HP = this.energy*20;
			this.MP = (this.intelligence+this.wisdom)*10;
			break;
		case "巫师":	
			this.strength = rd.nextInt(3)+9;
			this.agilitty = rd.nextInt(3)+19;
			this.energy = rd.nextInt(3)+9;
			this.intelligence = rd.nextInt(3)+19;
			this.wisdom = 100-(this.strength+this.agilitty+this.energy+this.intelligence);
			this.HP = this.energy*20;
			this.MP = (this.intelligence+this.wisdom)*10;
			break;
		default: break;
		}
	}
	//输出角色的各个属性
	void display() {
		System.out.println("您创建的角色各个属性如下:");
		System.out.println("=================");
		System.out.println("名字:"+this.name);
		System.out.println("性别:"+this.sex);
		System.out.println("种族:"+this.racial);
		System.out.println("职业:"+this.career);
		System.out.println("力量:"+this.strength);
		System.out.println("敏捷:"+this.agilitty);
		System.out.println("体力:"+this.energy);
		System.out.println("智力:"+this.intelligence);
		System.out.println("智慧:"+this.wisdom);
		System.out.println("血量:"+this.HP);
		System.out.println("魔法量:"+this.MP);
		System.out.println("=================");
	}
	// 询问是否满意,若满意存入文件,若不满意,重新创建角色
	boolean accept() throws IOException {
		System.out.println("以上信息您是否满意(1.满意 ,2.不满意):");
		boolean flagIn = true;
		boolean flagMain = true;
		while(flagIn) {
			String s = sc.nextLine();
			switch(s) {
			//满意,将角色信息写入文档
		case "1": 
			FileWriter fw = new FileWriter("RoleData.txt",true);
			String str = new String(this.name+" "+this.sex+" "+this.racial+" "+this.career+" "+this.strength+" "+this.agilitty+" "+this.energy+" "+this.intelligence+" "+this.wisdom+" "+this.HP+" "+this.MP+"\r\n");
			fw.write(str);
			fw.close();
			flagIn = false;	
			flagMain =false;
			break;
			//不满意,删除并重新创建
		case "2": 	
			flagIn = false;
			flagMain =true;
			break;
		default: 
			break;
			}
	}
		return flagMain;
	}
	//显示信息,进行选择创建
	void menu() {
		this.inPutName();
		this.selectSex();
		this.selectRacial();	
		this.selectCareer(this.racial);
		this.initializeAttribute(this.career);
		this.display();
	}
	
}


四. 调试、测试及运行结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

五. 经验归纳

1. 学习使用类图

由于设计程序时,只设计了Role类,对程序类图做的太简单。

1. 设计友好的输入

使用读取字符串的方式让用户从键盘输入,避免有非法字符时报出异常,并使用while(flag){…}的形式,若输入不合理,可循环输入,知道输入合法合理。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值