题目
1.1功能描述
几乎所有的RPG游戏(一种源自《龙与地下城》的游戏类型)在进入游戏时都会让用户自己来创建自己喜欢的角色。
目的:编写一个简化的创建游戏角色的程序。
1.2游戏角色应有的属性
本题目要求的游戏角色应有以下属性:名字、性别、种族、职业、力量、敏捷、体力、智力、智慧、生命值和魔法值。
名字:不超过50个字符。
性别:可以选择男性和女性。
种族:一共可选五个种族,人类、精灵、兽人、矮人和元素。
职业:可选六种职业,狂战士、圣骑士、刺客、猎手、祭司和巫师。
其余属性均为整数。
本题目要求首先用户输入角色姓名,然后由用户选择角色性别,然后由用户选择种族,然后选择职业,然后自动分配力量、敏捷、体力、智力和智慧属性,并计算生命值和魔法值。
生命值=体力*20。
魔法值=(智力+智慧)*10。
1.3职业限制
很多职业会限制某些种族选择,例如兽人不能就职圣骑士等等,种族和职业的限制表如下:
所以在要求用户选择职业时,输出信息里面只能有用户所选择种族可以就职的职业。
1.4初始属性
本题目要求力量、敏捷、体力、智力和智慧要求是随机值(利用随机数函数来取得随机数),但是五项属性的总和应该是100,并且应该和职业相关。例如狂战士的体力和力量就要比较高,而巫师需要较高的智力,而祭司则需要较高的智慧。各职业初始属性的大致比例应遵从下表:
例如,前面示意图中的祭司的初始属性,大致满足该比例,但是应该是随机的。
然后利用属性值计算生命值和魔法值。
1.5显示信息
最后向用户显示该角色的所有信息,然后询问用户是否满意,如用户不满意则重新创建,若用户满意则程序结束,并将用户创建角色的相关信息写入文件保存。
思路:
首先我们创建一个类,类里面创建各种方法,方法里面实现角色名称,种族,职业的选择以及属性的随机生成功能!最后再创建一个关于文件保存功能的方法和实现菜单功能的方法!
利用主函数调用类的方法实现RPG生成器!
程序实现
下面给出源代码供参考交流:
package zy;
import java.io.*;
import java.util.*;
public class Rpg {
private String name = null;//角色名字
private String sex = null;//角色性别
private String race = null;//角色种族
private String career = null;//角色职业
private int power = 0;//角色力量
private int speed = 0;//角色速度
private int physical = 0;//角色体力
private int intelligence = 0;//角色智力
private int wit = 0;//角色智慧
private int HP = 0;//生命值
private int MP = 0;//魔法值
static Scanner in = new Scanner(System.in);
//创建名字
public void setname() {
System.out.print("请输入你的角色名称:");
String s;
s = in.nextLine();
while(s.length()>50) {
System.out.println("名字过长,请重新输入!");
s = in.nextLine();
}
this.name = s;
}
//设置性别
public void setsex() {
String s;
int i;
System.out.println("你的性别是?"+" " + "0.男" + " " + "1.女");
s = in.nextLine();
i = panduan(s);
if(i == 0)
this.sex = "男";
else if(i == 1)
this.sex = "女";
else {
System.out.println("你未选择任意一个性别默认为男!");
this.sex = "男";
}
}
//创建种族
public void setrace() {
String s;
int i;
System.out.println("请选择种族,不同的种族擅长的方面也不同(比如狂战士体力和力量很大,但速度低,请谨慎选择!)");
System.out.println("0.人类 1.精灵 2.兽人 3.矮人 4.元素");
s = in.nextLine();
i = panduan(s);
if(i == 0) {
this.race = "人类";
return;
}
if(i == 1)
{
this.race = "精灵";
return;
}
if(i == 2) {
this.race = "兽人";
return;
}
if(i == 3) {
this.race = "矮人";
return;
}
if(i == 4) {
this.race = "元素";
return;
}
else {
System.out.println("你未选择任意一个种族默认为人类!");
this.race = "人类";
}
}
//创建职业
public void setcareer() {
if(this.race == "人类") {
System.out.println("请选择种职业:0.狂战士 1.圣骑士 2.刺客 3.猎手 4.祭司 5.巫师");
String s;
s = in.nextLine();
int i = panduan(s);
if(i == 0) {
this.career = "狂战士";
return;
}
if(i == 1) {
this.career = "圣骑士";
return;
}
if(i == 2) {
this.career = "刺客";
return;
}
if(i == 3) {
this.career = "猎手";
return;
}
if(i == 4) {
this.career = "祭司";
return;
}
if(i == 5) {
this.career = "巫师";
return;
}
else {
System.out.println("你未选择任意一个职业默认为狂战士!");
this.career = "狂战士";
}
}
if(this.race == "精灵") {
System.out.println("请选择种职业: 0.刺客 1.猎手 2.祭司 3.巫师");
String s;
s = in.nextLine();
int i = panduan(s);
if(i == 0) {
this.career = "刺客";
return;
}
if(i == 1) {
this.career = "猎手";
return;
}
if(i == 2) {
this.career = "祭司";
return;
}
if(i == 3) {
this.career = "巫师";
return;
}
else {
System.out.println("你未选择任意一个职业默认为刺客!");
this.career = "刺客";
}
}
if(this.race == "兽人") {
System.out.println("请选择种职业:0.狂战士 1.猎手 2.祭司");
String s;
s = in.nextLine();
int i = panduan(s);
if(i == 0) {
this.career = "狂战士";
return;
}
if(i == 1) {
this.career = "猎手";
return;
}
if(i == 2) {
this.career = "祭司";
return;
}
else {
System.out.println("你未选择任意一个职业默认为狂战士!");
this.career = "狂战士";
}
}
if(this.race == "矮人") {
System.out.println("请选择种职业:0.狂战士 1.圣骑士 2.祭司");
String s;
s = in.nextLine();
int i = panduan(s);
if(i == 0) {
this.career = "狂战士";
return;
}
if(i == 1) {
this.career = "圣骑士";
return;
}
if(i == 2) {
this.career = "祭司";
return;
}
else {
System.out.println("你未选择任意一个职业默认为狂战士!");
this.career = "狂战士";
}
}
if(this.race == "元素") {
System.out.println("请选择种职业:0.祭司 1.巫师");
String s;
s = in.nextLine();
int i = panduan(s);
if(i == 0) {
this.career = "祭司";
return;
}
if(i == 1) {
this.career = "巫师";
return;
}
else {
System.out.println("你未选择任意一个职业默认为祭司!");
this.career = "祭司";
}
}
}
//随机生成属性值
public void attribute() {
Random r = new Random();
if(this.career == "狂战士") {
while(this.power + this.speed + this.physical + this.intelligence + this.wit != 100) {
this.power = r.nextInt(10)+35;//角色力量在35-45之间浮动,下面相同
this.speed = r.nextInt(10)+15;//角色速度
this.physical = r.nextInt(10)+25;//角色体力
this.intelligence = r.nextInt(10);//角色智力
this.wit = r.nextInt(10);//角色智慧
}
this.HP = this.physical * 20;
this.MP = (this.intelligence + this.wit) * 10;
return;
}
if(this.career == "圣骑士") {
while(this.power + this.speed + this.physical + this.intelligence + this.wit != 100) {
this.power = r.nextInt(10)+20;//角色力量在20-30之间浮动,下面相同
this.speed = r.nextInt(10)+10;//角色速度
this.physical = r.nextInt(10)+25;//角色体力
this.intelligence = r.nextInt(10)+15;//角色智力
this.wit = r.nextInt(10)+5;//角色智慧
}
this.HP = this.physical * 20;
this.MP = (this.intelligence + this.wit) * 10;
return;
}
if(this.career == "刺客") {
while(this.power + this.speed + this.physical + this.intelligence + this.wit != 100) {
this.power = r.nextInt(10)+15;//角色力量在15-25之间浮动,下面相同
this.speed = r.nextInt(10)+30;//角色速度
this.physical = r.nextInt(10)+15;//角色体力
this.intelligence = r.nextInt(10)+10;//角色智力
this.wit = r.nextInt(10)+5;//角色智慧
}
this.HP = this.physical * 20;
this.MP = (this.intelligence + this.wit) * 10;
return;
}
if(this.career == "猎手") {
while(this.power + this.speed + this.physical + this.intelligence + this.wit != 100) {
this.power = r.nextInt(10)+10;//角色力量在10-20之间浮动,下面相同
this.speed = r.nextInt(10)+35;//角色速度
this.physical = r.nextInt(10)+10;//角色体力
this.intelligence = r.nextInt(10)+5;//角色智力
this.wit = r.nextInt(10)+15;//角色智慧
}
this.HP = this.physical * 20;
this.MP = (this.intelligence + this.wit) * 10;
return;
}
if(this.career == "祭司") {
while(this.power + this.speed + this.physical + this.intelligence + this.wit != 100) {
this.power = r.nextInt(10)+10;//角色力量在10-20之间浮动,下面相同
this.speed = r.nextInt(10)+15;//角色速度
this.physical = r.nextInt(10)+10;//角色体力
this.intelligence = r.nextInt(10)+30;//角色智力
this.wit = r.nextInt(10)+10;//角色智慧
}
this.HP = this.physical * 20;
this.MP = (this.intelligence + this.wit) * 10;
return;
}
if(this.career == "巫师") {
while(this.power + this.speed + this.physical + this.intelligence + this.wit != 100) {
this.power = r.nextInt(10)+5;//角色力量在5-15之间浮动,下面相同
this.speed = r.nextInt(10)+15;//角色速度
this.physical = r.nextInt(10)+5;//角色体力
this.intelligence = r.nextInt(10)+15;//角色智力
this.wit = r.nextInt(10)+35;//角色智慧
}
this.HP = this.physical * 20;
this.MP = (this.intelligence + this.wit) * 10;
return;
}
}
//获得所有属性值
public void getattribute() {
System.out.println("召唤师,你的属性为:");
System.out.println("********************************");
System.out.println("姓名:"+ " " + this.name);
System.out.println("********************************");
System.out.println("性别:"+ " " + this.sex);
System.out.println("********************************");
System.out.println("种族:"+ " " + this.race);
System.out.println("********************************");
System.out.println("职业:"+ " " + this.career);
System.out.println("********************************");
System.out.println("力量:"+ " " + this.power);
System.out.println("********************************");
System.out.println("敏捷:"+ " " + this.speed);
System.out.println("********************************");
System.out.println("体力:"+ " " + this.physical);
System.out.println("********************************");
System.out.println("智力:"+ " " + this.intelligence);
System.out.println("********************************");
System.out.println("智慧:"+ " " + this.wit);
System.out.println("********************************");
System.out.println("HP:" + " " + this.HP);
System.out.println("********************************");
System.out.println("MP:" + " " + this.MP);
}
//判断选项的方法
public int panduan(String s) {
boolean t = true;//结束条件
while(t) { //选择判断方法
if(s.length() == 1 && s.charAt(0) >= '0' && s.charAt(0) <= '5') {
return (Integer.valueOf(s));
}
else {
System.out.println("请输入正确选项!");
s = in.nextLine();
}
}
return (Integer.valueOf(s));
}
//文件输入
public void write() {
File f = new File("roles.txt");
try {
FileWriter f1 = new FileWriter(f);
f1.write(this.name);
f1.write("\r\n");
f1.write(this.sex);
f1.write("\r\n");
f1.write(this.race);
f1.write("\r\n");
f1.write(this.career);
f1.write("\r\n");
f1.write(String.valueOf(this.power));
f1.write("\r\n");
f1.write(String.valueOf(this.speed));
f1.write("\r\n");
f1.write(String.valueOf(this.physical));
f1.write("\r\n");
f1.write(String.valueOf(this.intelligence));
f1.write("\r\n");
f1.write(String.valueOf(this.wit));
f1.write("\r\n");
f1.write(String.valueOf(this.HP));
f1.write("\r\n");
f1.write(String.valueOf(this.MP));
f1.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public void menu() {
System.out.println("欢迎来到《《PDD历险记》》请进行初始人物创建:");
setname();//设置姓名
setsex();//设置性别
setrace();//设置种族
setcareer();//设置职业
attribute();//生成属性
getattribute();//展示属性
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
Rpg rpg = new Rpg();
rpg.menu();
while(true) {
System.out.println("召唤师,你满意你的角色吗?(0.YES 1.NO)");
String s;
s = in.nextLine();
int i = rpg.panduan(s);
if(i == 0)
break;
else
rpg.menu();
}
rpg.write();
}
}
文件操作的结果展示(至于运行结果展示各位读者可自行运行):
经验
RPG生成器虽然是一个简单的程序,但是也有细节值得学习,比如说文件操作的时候,在文件输出流中写入字符串“\r\n”,就可以实现文件中的信息的换行!