Java:RPG角色生成器
RPG角色生成器
1.功能描述
几乎所有的RPG游戏(一种源自《龙与地下城》的游戏类型)在进入游戏时都会让用户自己来创建自己喜欢的角色。
2.游戏角色应有的属性
本题目要求的游戏角色应有以下属性:名字、性别、种族、职业、力量、敏捷、体力、智力、智慧、生命值和魔法值。
名字:不超过50个字符。
性别:可以选择男性和女性。
种族:一共可选五个种族,人类、精灵、兽人、矮人和元素。
职业:可选六种职业,狂战士、圣骑士、刺客、猎手、祭司和巫师。
其余属性均为整数。
本题目要求首先用户输入角色姓名,然后由用户选择角色性别,然后由用户选择种族,然后选择职业,然后自动分配力量、敏捷、体力、智力和智慧属性,并计算生命值和魔法值。
生命值=体力*20。
魔法值=(智力+智慧)*10。
3.职业限制
很多职业会限制某些种族选择,例如兽人不能就职圣骑士等等,种族和职业的限制表如下:
种族/职业 |
狂战士 |
圣骑士 |
刺客 |
猎手 |
祭司 |
巫师 |
人类 |
允许 |
允许 |
允许 |
允许 |
允许 |
允许 |
精灵 |
不允许 |
不允许 |
允许 |
允许 |
允许 |
允许 |
兽人 |
允许 |
不允许 |
不允许 |
允许 |
允许 |
不允许 |
矮人 |
允许 |
允许 |
不允许 |
不允许 |
允许 |
不允许 |
元素 |
不允许 |
不允许 |
不允许 |
不允许 |
允许 |
允许 |
所以在要求用户选择职业时,输出信息里面只能有用户所选择种族可以就职的职业。
4.初始属性
本题目要求力量、敏捷、体力、智力和智慧要求是随机值(利用随机数函数来取得随机数),但是五项属性的总和应该是100,并且应该和职业相关。例如狂战士的体力和力量就要比较高,而巫师需要较高的智力,而祭司则需要较高的智慧。各职业初始属性的大致比例应遵从下表:
职业/属性 |
力量 |
敏捷 |
体力 |
智力 |
智慧 |
狂战士 |
40 |
20 |
30 |
5 |
5 |
圣骑士 |
25 |
15 |
30 |
20 |
10 |
刺客 |
20 |
35 |
20 |
15 |
10 |
猎手 |
15 |
40 |
15 |
10 |
20 |
祭司 |
15 |
20 |
15 |
35 |
15 |
巫师 |
10 |
20 |
10 |
20 |
40 |
例如,前面示意图中的祭司的初始属性,大致满足该比例,但是应该是随机的。
然后利用属性值计算生命值和魔法值。
5.显示信息
最后向用户显示该角色的所有信息,然后询问用户是否满意,如用户不满意则重新创建,若用户满意则程序结束,并将用户创建角色的相关信息写入文件保存。
源代码:
RoleDefinition类:
package com._520it.chapter02;
import java.util.Scanner;
/**
* 角色定义
* @author Jack
* @date 2018-10-01
* @version 1.0
*/
public class RoleDefinition {
private String name; // 角色姓名
private int gender; // 角色性别
/**
* 获取角色姓名
* @return 角色姓名
*/
public String getName() {
return name;
}
/**
* 设置角色姓名
* @param name 角色姓名
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取角色性别
* @return 角色性别
*/
public int getGender() {
return gender;
}
/**
* 设置角色性别
* @param gender 角色性别
*/
public void setGender(int gender) {
this.gender = gender;
}
/**
* 设置角色的姓名和性别
* @return 角色性别
*/
public int inputNameAndGender() {
System.out.println("请输入您游戏角色的姓名:");
Scanner sc = new Scanner(System.in);
this.name = sc.next();
while (true) {
System.out.println("请选择您游戏角色的性别(0男性,1女性):");
this.gender = sc.nextInt();
// 判断性别输入是否正确
if (gender == 0 || gender == 1) {
// 输入正确
break;
} else {
System.out.println("请输入0或1来选择性别!");
}
}
return gender;
}
/**
* 输出角色的姓名和性别信息
*/
public void outputNameAndGender() {
System.out.println("==============================");
System.out.println(" 姓名\t\t\t" + this.name);
System.out.println("==============================");
if (this.gender == 0) {
System.out.println(" 性别\t\t\t" + "男性");
} else {
System.out.println(" 性别\t\t\t" + "女性");
}
}
}
RaceAndProfession类:
package com._520it.chapter02;
import java.util.Scanner;
/**
* 角色的种族和职业
* @author Jack
* @date 2018-10-01
* @version 1.0
*/
public class RaceAndProfession {
private int race; // 种族
private int profession; // 职业
private String[] races = { "人类", "精灵", "兽人", "矮人", "元素" };
private String[] professions = { "狂战士", "圣骑士", "刺客", "猎手", "祭司", "巫师" };
/**
* 获取角色职业信息
* @return 角色职业信息
*/
public int getProfession() {
return this.profession;
}
/**
* 获取角色种族信息
* @return 角色种族信息
*/
public int getRace() {
return race;
}
/**
* 设置角色种族
* @param race 角色种族
*/
public void setRace(int race) {
this.race = race;
}
/**
* 选择角色种族
* @return 角色种族信息
*/
public int selectRace() {
while (true) {
System.out.println("请选择您游戏角色的种族(0人类,1精灵,2兽人,3矮人,4元素):");
Scanner sc = new Scanner(System.in);
this.race = sc.nextInt();
// 判断种族输入是否正确
if (race >= 0 && race <= 4) {
// 输入正确跳出循环
break;
} else {
System.out.println("请输入0到4之间的数字来选择种族!");
}
}
return race;
}
/**
* 选择角色职业
* @param race 角色种族信息
* @return 角色职业信息
*/
public int selectProfession(int race) {
switch (race) {
case 0:
while (true) {
System.out.println("请选择您的职业(0狂战士,1圣骑士,2刺客,3猎手,4祭司,5巫师):");
Scanner sc = new Scanner(System.in);
this.profession = sc.nextInt();
// 判断职业输入是否正确
if (profession >= 0 && profession <= 5) {
// 输入正确跳出循环
break;
} else {
System.out.println("请输入0到5之间的数字来选择职业!");
}
}
break;
case 1:
while (true) {
System.out.println("请选择您的职业(2刺客,3猎手,4祭司,5巫师):");
Scanner sc = new Scanner(System.in);
this.profession = sc.nextInt();
// 判断职业输入是否正确
if (profession >= 2 && profession <= 5) {
// 输入正确跳出循环
break;
} else {
System.out.println("请输入2到5之间的数字选择职业!");
}
}
break;
case 2:
while (true) {
System.out.println("请选择您的职业(0狂战士,3猎手,4祭司):");
Scanner sc = new Scanner(System.in);
this.profession = sc.nextInt();
// 判断职业输入是否正确
if (profession == 0 || profession == 3 || profession == 4) {
// 输入正确跳出循环
break;
} else {
System.out.println("请输入数字0、3或4来选择职业!");
}
}
break;
case 3:
while (true) {
System.out.println("请选择您的职业(0狂战士,1圣骑士,4祭司):");
Scanner sc = new Scanner(System.in);
this.profession = sc.nextInt();
// 判断职业输入是否正确
if (profession == 0 || profession == 1 || profession == 4) {
// 输入正确跳出循环
break;
} else {
System.out.println("请输入数字0、1或4来选择职业!");
}
}
break;
case 4:
while (true) {
System.out.println("请选择您的职业(4祭司,5巫师):");
Scanner sc = new Scanner(System.in);
this.profession = sc.nextInt();
// 判断种族输入是否正确
if (profession == 4 || profession == 5) {
// 输入正确跳出循环
break;
} else {
System.out.println("请输入数字4或5来选择职业!");
}
}
break;
default:
break;
}
return profession;
}
/**
* 输出角色的种族和职业信息
*/
public void outputRaceAndProfession() {
System.out.println("==============================");
System.out.println(" 种族\t\t\t" + races[this.race]);
System.out.println("==============================");
System.out.println(" 职业\t\t\t" + professions[this.profession]);
}
}
ProfessionAttribute类:
package com._520it.chapter02;
import java.util.Random;
/**
* 角色的职业属性
* @author Jack
* @date 2018-10-01
* @version 1.0
*/
public class ProfessionAttribute {
private int strength; // 力量
private int agility; // 敏捷
private int physical; // 体力
private int intelligence; // 智力
private int wisdom; // 智慧
private int HP; // 生命值ֵ
private int MP; // 魔法值ֵ
/**
* 获取角色的力量属性
* @return 力量属性
*/
public int getStrength() {
return strength;
}
/**
* 获取角色的敏捷属性
* @return 敏捷属性
*/
public int getAgility() {
return agility;
}
/**
* 获取角色的体力属性
* @return 体力属性
*/
public int getPhysical() {
return physical;
}
/**
* 获取角色的智力属性
* @return 智力属性
*/
public int getIntelligence() {
return intelligence;
}
/**
* 获取角色的智慧属性
* @return 智慧属性
*/
public int getWisdom() {
return wisdom;
}
/**
* 获取角色的生命值属性
* @return 生命值属性
*/
public int getHP() {
return HP;
}
/**
* 获取角色的魔法值属性
* @return 魔法值属性
*/
public int getMP() {
return MP;
}
/**
* 自动生成角色属性
* @param str 角色初始力量属性
* @param agi 角色初始敏捷属性
* @param phy 角色初始体力属性
* @param intell 角色初始智力属性
* @param wis 角色初始智慧属性
*/
public void AutoGenerateAttribute(int str, int agi, int phy, int intell, int wis) {
int sum = 0;
Random random = new Random();
do {
strength = random.nextInt(5) % 10 + str;
agility = random.nextInt(5) % 10 + agi;
physical = random.nextInt(5) % 10 + phy;
intelligence = random.nextInt(5) % 10 + intell;
wisdom = random.nextInt(5) % 10 + wis;
sum = strength + agility + physical + intelligence + wisdom;
} while (sum != 100);
// 生命值为体力的20倍
HP = physical * 20;
// 魔法值为智力与智慧之和的10倍
MP = (wisdom + intelligence) * 10;
}
/**
* 初始化角色属性
* @param profession 角色职业信息
*/
public void initialAttributes(int profession) {
if (profession == 0) {
AutoGenerateAttribute(40, 20, 30, 5, 5);
}
if (profession == 1) {
AutoGenerateAttribute(25, 15, 30, 20, 10);
}
if (profession == 2) {
AutoGenerateAttribute(20, 35, 20, 15, 10);
}
if (profession == 3) {
AutoGenerateAttribute(15, 40, 15, 10, 20);
}
if (profession == 4) {
AutoGenerateAttribute(15, 20, 15, 35, 15);
}
if (profession == 5) {
AutoGenerateAttribute(10, 20, 10, 20, 40);
}
}
/**
* 输出角色的属性信息
*/
public void outputProfessionAttribute() {
System.out.println("==============================");
System.out.println(" 力量\t\t\t" + this.strength);
System.out.println("==============================");
System.out.println(" 敏捷\t\t\t" + this.agility);
System.out.println("==============================");
System.out.println(" 体力\t\t\t" + this.physical);
System.out.println("==============================");
System.out.println(" 智力\t\t\t" + this.intelligence);
System.out.println("==============================");
System.out.println(" 智慧\t\t\t" + this.wisdom);
System.out.println("==============================");
System.out.println(" 生命值\t\t\t" + this.HP);
System.out.println("==============================");
System.out.println(" 魔法值\t\t\t" + this.MP);
System.out.println("==============================");
}
}
RolePlaying类:
package com._520it.chapter02;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
/**
* 角色生成器测试类
* @author Jack
* @date 2018-10-01
* @version 1.0
*/
public class RolePlaying {
public static void main(String[] args) {
boolean flag = true;
Scanner sc = new Scanner(System.in);
// 创建角色对象
RoleDefinition role = new RoleDefinition();
RaceAndProfession rap = new RaceAndProfession();
ProfessionAttribute pa = new ProfessionAttribute();
do {
role.inputNameAndGender();
int race = rap.selectRace();
rap.selectProfession(race);
// 输出角色基本信息
role.outputNameAndGender();
rap.outputRaceAndProfession();
pa.initialAttributes(rap.getProfession());
pa.outputProfessionAttribute();
// 判断用户是否满意
System.out.println("是否满意角色属性?(Y/N)若不满意,则重新创建!");
String str = sc.next();
if ("Y".equals(str) || "y".equals(str)) {
break;
}
} while (flag);
// 将角色信息保存到文件中
saveRoleInformation(role, rap, pa);
System.out.println("角色信息已成功保存!");
}
/**
* 将角色信息保存到文件中
* @param role 角色类对象
* @param rap 种族职业类对象
* @param pa 职业属性类对象
*/
public static void saveRoleInformation(RoleDefinition role, RaceAndProfession rap, ProfessionAttribute pa) {
try {
// 创建字符输出流对象
FileWriter desFile = new FileWriter("resources/roles_information.txt", true);
// 字符缓冲输出流
BufferedWriter out = new BufferedWriter(desFile);
out.write(" 姓名\t\t\t" + role.getName());
// 输出换行
out.newLine();
if (role.getGender() == 0) {
out.write(" 性别\t\t\t" + "男性");
} else {
out.write(" 性别\t\t\t" + "女性");
}
out.newLine();
switch (rap.getRace()) {
case 0:
out.write(" 种族\t\t\t" + "人类");
break;
case 1:
out.write(" 种族\t\t\t" + "精灵");
break;
case 2:
out.write(" 种族\t\t\t" + "兽人");
break;
case 3:
out.write(" 种族\t\t\t" + "矮人");
break;
case 4:
out.write(" 种族\t\t\t" + "元素");
break;
default:
break;
}
out.newLine();
switch (rap.getProfession()) {
case 0:
out.write(" 职业\t\t\t" + "狂战士");
break;
case 1:
out.write(" 职业\t\t\t" + "圣骑士");
break;
case 2:
out.write(" 职业\t\t\t" + "刺客");
break;
case 3:
out.write(" 职业\t\t\t" + "猎手");
break;
case 4:
out.write(" 职业\t\t\t" + "祭司");
break;
case 5:
out.write(" 职业\t\t\t" + "巫师");
break;
default:
break;
}
out.newLine();
out.write(" 力量\t\t\t" + pa.getStrength());
out.newLine();
out.write(" 敏捷\t\t\t" + pa.getAgility());
out.newLine();
out.write(" 体力\t\t\t" + pa.getPhysical());
out.newLine();
out.write(" 智力\t\t\t" + pa.getIntelligence());
out.newLine();
out.write(" 智慧\t\t\t" + pa.getWisdom());
out.newLine();
out.write(" 生命值\t\t\t" + pa.getHP());
out.newLine();
out.write(" 魔法值\t\t\t" + pa.getMP());
out.newLine();
// 关闭资源
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行效果展示