一.实验内容
1.功能描述
几乎所有的RPG游戏(一种源自《龙与地下城》的游戏类型)在进入游戏时都会让用户自己来创建自己喜欢的角色。本次上机要求编写一个简化的创建游戏角色的程序。
2.游戏角色应有的属性
本题目要求的游戏角色应有以下属性:名字、性别、种族、职业、力量、敏捷、体力、智力、智慧、生命值和魔法值。
名字:不超过50个字符。
性别:可以选择男性和女性。
种族:一共可选五个种族,人类、精灵、兽人、矮人和元素。
职业:可选六种职业,狂战士、圣骑士、刺客、猎手、祭司和巫师。
其余属性均为整数。
本题目要求首先用户输入角色姓名,然后由用户选择角色性别,然后由用户选择种族,然后选择职业,然后自动分配力量、敏捷、体力、智力和智慧属性,并计算生命值和魔法值。
生命值=体力*20。
魔法值=(智力+智慧)*10。
3.职业限制
很多职业会限制某些种族选择,例如兽人不能就职圣骑士等等,种族和职业的限制表如下:
所以在要求用户选择职业时,输出信息里面只能有用户所选择种族可以就职的职业。
4.初始属性
本题目要求力量、敏捷、体力、智力和智慧要求是随机值(利用随机数函数来取得随机数),但是五项属性的总和应该是100,并且应该和职业相关。例如狂战士的体力和力量就要比较高,而巫师需要较高的智力,而祭司则需要较高的智慧。各职业初始属性的大致比例应遵从下表:
例如,前面示意图中的祭司的初始属性,大致满足该比例,但是应该是随机的。然后利用属性值计算生命值和魔法值。
5.显示信息
最后向用户显示该角色的所有信息,然后询问用户是否满意,如用户不满意则重新创建,若用户满意则程序结束,并将用户创建角色的相关信息写入文件保存。
二、算法设计
1.解题思路
首先以面向对象的方法,创建好人物角色的各个属性及获取他们的方法,之后初始化一些属性对应的值,最后通过随机函数随机计算他们的职业属性,使得五项属性的总和为100,最后进行判断是否满意,如果不满意则重新创建角色,如果满意则将角色对应的所有属性存入文件中。
2.类图
三、代码
1.记录姓名,性别的Base类
package 信息;
import java.util.Scanner;
public class Base { //基类
protected String name;//记录姓名
protected String sex;//记录性别
protected int sex_choice;//定义性别选择
private static Scanner sc;
public void getBase(){//定义姓名和性别
System.out.println("请输入你想要创建的角色姓名:");
sc=new Scanner(System.in);
name=sc.next();
boolean tag=true;
while(tag){
System.out.println("请输入你想要创建的角色性别:");
System.out.println("1.男 2.女");
sex_choice=sc.nextInt();
switch(sex_choice){
case 1:
sex="男";tag=false;break;
case 2:
sex="女";tag=false;break;
default:
System.out.println("输入错误!请重新输入!");break;
}
}
}
}
2.记录种族,职业的occupation_race类
package 信息;
import java.util.Scanner;
public class occupation_race extends Base {//记录角色的种族和职业
protected int occupation_choice;//职业选择
protected int race_choice;//种族选择
protected String race;//记录种族
protected String occupation;//记录职业
private Scanner sc;
public void getOccupation_Race(){//种族和职业的选择
sc=new Scanner(System.in);
boolean tag=true;
while(tag){
System.out.println("请选择角色种族:");
System.out.println("1.人类 2.精灵 3.兽人 4.矮人 5.元素");
race_choice=sc.nextInt();//从键盘输入
switch(race_choice){
case 1:
race="人类";tag=false;break;
case 2:
race="精灵";tag=false;break;
case 3:
race="兽人";tag=false;break;
case 4:
race="矮人";tag=false;break;
case 5:
race="元素";tag=false;break;
default:
System.out.println("输入错误!请重新输入!");
}
}
while(true){
System.out.println("该种族可以使用的职业:");
switch(race_choice){
case 1:
System.out.println("1.狂战士 2.圣骑士 3.刺客 4.猎手 5.祭司 6.巫师");break;
case 2:
System.out.println("3.刺客 4.猎手 5.祭司 6.巫师");break;
case 3:
System.out.println("1.狂战士 4.猎手 5.祭司");break;
case 4:
System.out.println("1.狂战士 2.圣骑士 5.祭司 ");break;
case 5:
System.out.println("5.祭司 6.巫师");break;
}
occupation_choice=sc.nextInt();
if(race_choice==1&&(occupation_choice>=1&&occupation_choice<=6))
break;
else if(race_choice==2&&(occupation_choice>=3&&occupation_choice<=6))
break;
else if(race_choice==3&&(occupation_choice==1||occupation_choice==4||occupation_choice==5))
break;
else if(race_choice==4&&(occupation_choice==1||occupation_choice==2||occupation_choice==5))
break;
else if(race_choice==5&&(occupation_choice==5||occupation_choice==6))
break;
else System.out.println("输入错误!请重新输入!");
}
if(occupation_choice==1) occupation="狂战士";
if(occupation_choice==2) occupation="圣骑士";
if(occupation_choice==3) occupation="刺客";
if(occupation_choice==4) occupation="猎手";
if(occupation_choice==5) occupation="祭司";
if(occupation_choice==6) occupation="巫师";
}
}
3.记录属性的Attribute类
package 信息;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
public class Attribute extends occupation_race{//记录属性
protected int strength;//力量
protected int agility;//敏捷
protected int physical_power;//体力
protected int intelligence;//智力
protected int wisdom;//智慧
protected int HP;//生命值
protected int MP;//魔法值
public void getAttribute(){
if(occupation_choice==1)
getRandom(40,20,30,5,5);//狂战士
if(occupation_choice==2)
getRandom(25,15,30,20,10);//圣骑士
if(occupation_choice==3)
getRandom(20,35,20,15,10);//刺客
if(occupation_choice==4)
getRandom(15,40,15,10,20);//猎手
if(occupation_choice==5)
getRandom(15,20,15,35,15);//祭司
if(occupation_choice==6)
getRandom(10,20,10,20,40);//巫师
}
public void getRandom(int a,int b,int c,int d,int e){
int sum=0;
do{
strength=(int)(a+Math.random()*10-5);
agility=(int)(b+Math.random()*10-5);
physical_power=(int)(c+Math.random()*10-5);
intelligence=(int)(d+Math.random()*10-5);
wisdom=(int)(e+Math.random()*10-5);
sum=strength+agility+physical_power+intelligence+wisdom;
}while(sum!=100);
HP=physical_power*20;
MP=(intelligence+wisdom)*10;
}
public void show(){//显示出所有信息
System.out.println("********该角色的所有信息*******");
System.out.println("——————————————————————————");
System.out.println("姓名:" +name);
System.out.println("——————————————————————————");
System.out.println("性别:" +sex);
System.out.println("——————————————————————————");
System.out.println("种族:" +race);
System.out.println("——————————————————————————");
System.out.println("职业:" +occupation);
System.out.println("——————————————————————————");
System.out.println("力量:" +strength);
System.out.println("——————————————————————————");
System.out.println("敏捷:" +agility);
System.out.println("——————————————————————————");
System.out.println("体力:" +physical_power);
System.out.println("——————————————————————————");
System.out.println("智力:" +intelligence);
System.out.println("——————————————————————————");
System.out.println("智慧:" +wisdom);
System.out.println("——————————————————————————");
System.out.println("生命值:" +HP);
System.out.println("——————————————————————————");
System.out.println("法力值:" +MP);
System.out.println("——————————————————————————");
}
public void write() throws FileNotFoundException{//将信息写入data.txt文件中
File fp=new File("data.txt");
PrintWriter php=new PrintWriter(fp);
php.println("********该角色的所有信息*******");
php.println("——————————————————————————");
php.println("姓名:" +name);
php.println("——————————————————————————");
php.println("性别:" +sex);
php.println("——————————————————————————");
php.println("种族:" +race);
php.println("——————————————————————————");
php.println("职业:" +occupation);
php.println("——————————————————————————");
php.println("力量:" +strength);
php.println("——————————————————————————");
php.println("敏捷:" +agility);
php.println("——————————————————————————");
php.println("体力:" +physical_power);
php.println("——————————————————————————");
php.println("智力:" +intelligence);
php.println("——————————————————————————");
php.println("智慧:" +wisdom);
php.println("——————————————————————————");
php.println("生命值:" +HP);
php.println("——————————————————————————");
php.println("法力值:" +MP);
php.println("——————————————————————————");
php.close();
}
}
4.运行RPG类
package 信息;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class RPG extends Attribute{
protected static int choice;
private static Scanner sc;
public static void main(String[] args){
boolean tag=true;
sc=new Scanner(System.in);
while(tag){
RPG m=new RPG();
m.getBase();//调用函数定义姓名和性别
m.getOccupation_Race();//调用函数选择种族和职业
m.getAttribute();//调用函数获得属性
m.show();//调用函数显示角色信息
System.out.println("是否满意所创建的角色信息?");
System.out.println("1.是 2.否");
choice=sc.nextInt();
switch(choice){
case 1:
try{
m.write();//调用函数将角色信息写入文件
}catch(FileNotFoundException n){
n.printStackTrace();
}
System.out.println("角色信息成功写入文件!");
tag=false;break;
case 2:
tag=true;break;
default:
System.out.println("输入错误!请重新输入!");break;
}
}
}
}
四、调试与测试
1、调试
2.测试
五、总结
Java中Math类的random()方法可以生成[0,1)之间的随机浮点数。而double类型数据强制转换成int类型,整数部分赋值给int类型变量,小数点之后的小数部分将会丢失。如果要生成[0,n]的随机整数的话,只需要Math.random()乘以n+1,生成[0,n+1)的浮点数,再强制类型转换为int类型,只取其整数部分,即可得到[0,n]的整数。