一、实验内容
1.功能描述
几乎所有的RPG游戏(一种源自《龙与地下城》的游戏类型)在进入游戏时都会让用户自己来创建自己喜欢的角色。本次上机要求编写一个简化的创建游戏角色的程序。
2.游戏角色应有的属性
本题目要求的游戏角色应有以下属性:名字、性别、种族、职业、力量、敏捷、体力、智力、智慧、生命值和魔法值。
名字:不超过50个字符。
性别:可以选择男性和女性。
种族:一共可选五个种族,人类、精灵、兽人、矮人和元素。
职业:可选六种职业,狂战士、圣骑士、刺客、猎手、祭司和巫师。
其余属性均为整数。
本题目要求首先用户输入角色姓名,然后由用户选择角色性别,然后由用户选择种族,然后选择职业,然后自动分配力量、敏捷、体力、智力和智慧属性,并计算生命值和魔法值。
生命值=体力*20。
魔法值=(智力+智慧)*10。
3.职业限制
很多职业会限制某些种族选择,例如兽人不能就职圣骑士等等,种族和职业的限制表如下:
种族/职业狂战士圣骑士刺客猎手祭司巫师
人类允许允许允许允许允许允许
精灵不允许不允许允许允许允许允许
兽人允许不允许不允许允许允许不允许
矮人允许允许不允许不允许允许不允许
元素不允许不允许不允许不允许允许允许
所以在要求用户选择职业时,输出信息里面只能有用户所选择种族可以就职的职业。
4.初始属性
本题目要求力量、敏捷、体力、智力和智慧要求是随机值(利用随机数函数来取得随机数),但是五项属性的总和应该是100,并且应该和职业相关。例如狂战士的体力和力量就要比较高,而巫师需要较高的智力,而祭司则需要较高的智慧。各职业初始属性的大致比例应遵从下表:
职业/属性力量敏捷体力智力智慧
狂战士40203055
圣骑士2515302010
刺客2035201510
猎手1540151020
祭司1520153515
巫师1020102040
例如,前面示意图中的祭司的初始属性,大致满足该比例,但是应该是随机的。
然后利用属性值计算生命值和魔法值。
5.显示信息
最后向用户显示该角色的所有信息,然后询问用户是否满意,如用户不满意则重新创建,若用户满意则程序结束,并将用户创建角色的相关信息写入文件保存。
二、源代码
import java.util.Random;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
class Base{
int i;
String name;
String sex;
String racial;//种族 0为人类 1为精灵 2为兽人 3为矮人 4为元素
void setRacial() {
@SuppressWarnings("resource")
Scanner s=new Scanner(System.in);
System.out.print("0为人类 1为精灵 2为兽人 3为矮人 4为元素 请输入你的种族:"+"\n");
i=s.nextInt();
switch(i)
{case 0:racial="人类";break;
case 1:racial="精灵";break;
case 2:racial="兽人";break;
case 3:racial="矮人";break;
case 4:racial="元素";break;
}
}
String setName() {
Scanner s=new Scanner(System.in);
System.out.print("请输入你的姓名:"+"\n");
name=s.next();return name;
}
String Setsex() {
@SuppressWarnings("resource")
Scanner s=new Scanner(System.in);
System.out.print("请输入你的性别:"+"\n");
sex=s.next();return sex;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public String getRacial() {
return racial;
}
}
class Racial extends Base{
int j;
String job;//职业 0为狂战士 1 为圣战士 2为刺客 3为猎手 4为祭司 5为巫师
void SetJob(Base b) {
switch(b.i)
{
case(0):Scanner s=new Scanner(System.in);System.out.print("0为狂战士 1为圣战士 2为刺客 3为猎手 4为祭司 5为巫师 请输入你的职业:"+"\n");
j=s.nextInt();break;
case(1): s=new Scanner(System.in);System.out.print("2为刺客 3为猎手 4为祭司 5为巫师 请输入你的职业:"+"\n");
j=s.nextInt(); s.close();break;
case(2):s=new Scanner(System.in);System.out.print("0为狂战士 3为猎手 4为祭司 请输入你的职业:"+"\n");
j=s.nextInt();s.close();break;
case(3):s=new Scanner(System.in);System.out.print("0为狂战士 1 为圣战士 4为祭司 请输入你的职业:"+"\n");
j=s.nextInt(); s.close();break;
case(4): s=new Scanner(System.in);System.out.print("4为祭司 5为巫师 请输入你的职业:"+"\n");
j=s.nextInt(); s.close();break;
}
switch(j)
{case 0:job="狂战士";break;
case 1:job="圣战士";break;
case 2:job="刺客";break;
case 3:job="猎手";break;
case 4:job="祭司";break;
case 5:job="巫师";break;
}
}
public String getJob() {
return job;
}
}
class Attribute extends Racial{
int strength;//力量
int quick;//敏捷
int phys;//体力
int intell;//智力
int wisdom;//智慧
int life;//生命值
int magic=phys*20;//魔法值
void setAttribute(Racial r){
int sum=0;Random random=new Random();
switch(r.j)
{
case(0): //狂战士
do{
strength=random.nextInt(45)+35;
quick=random.nextInt(25)+(15);//敏捷
phys=random.nextInt(35)+(25);//体力
intell=random.nextInt(8)+(3);//智力
wisdom=random.nextInt(8)+(3);//智慧
sum=strength+quick+phys+intell+wisdom;
} while(sum!=100);break;
case(1)://圣骑士
do{
strength=random.nextInt(30)+20;
quick=random.nextInt(20)+(10);//敏捷
phys=random.nextInt(35)+(25);//体力
intell=random.nextInt(25)+(15);//智力
wisdom=random.nextInt(8)+(15);//智慧
sum=strength+quick+phys+intell+wisdom;
} while(sum!=100);
break;
case(2)://刺客
do{
strength=random.nextInt(25)+15;
quick=random.nextInt(40)+(30);//敏捷
phys=random.nextInt(25)+(15);//体力
intell=random.nextInt(20)+(10);//智力
wisdom=random.nextInt(15)+(5);//智慧
sum=strength+quick+phys+intell+wisdom;
} while(sum!=100);
break;
case(3)://猎手
do{
strength=random.nextInt(20)+10;
quick=random.nextInt(45)+(35);//敏捷
phys=random.nextInt(20)+(10);//体力
intell=random.nextInt(15)+(8);//智力
wisdom=random.nextInt(25)+(15);//智慧
sum=strength+quick+phys+intell+wisdom;
} while(sum!=100);
break;
case(4)://祭司
do{
strength=random.nextInt(20)+10;
quick=random.nextInt(25)+(20);//敏捷
phys=random.nextInt(15)+(8);//体力
intell=random.nextInt(40)+(30);//智力
wisdom=random.nextInt(20)+(12);//智慧
sum=strength+quick+phys+intell+wisdom;
} while(sum!=100);
break;
case(5)://巫师
do{
strength=random.nextInt(15)+8;
quick=random.nextInt(25)+(15);//敏捷
phys=random.nextInt(15)+(8);//体力
intell=random.nextInt(25)+(15);//智力
wisdom=random.nextInt(45)+(35);//智慧
sum=strength+quick+phys+intell+wisdom;
} while(sum!=100);
break;
}
}
public int getStrength() {
return strength;
}
public int getQuick() {
return quick;
}
public int getPhys() {
return phys;
}
public int getIntell() {
return intell;
}
public int getWisdom() {
return wisdom;
}
public int getLife() {
return (phys*20);
}
public int getMagic() {
return ((wisdom+intell)*10);
}
void save(Base base,Racial racial ) {
try {
FileWriter desFile=new FileWriter("game.txt",true);
BufferedWriter out=new BufferedWriter(desFile);
out.write("姓名:"+base.getName()+" 性别:"+base.getSex()+" 种族:"+base.getRacial()+" 职业:"+racial.getJob()+
" 属性:"+"力量:"+this.getStrength()+" 敏捷:"+this.getQuick()+" 体力:"+this.getPhys()+" 智力:"
+this.getIntell()+" 智慧:"+this.getWisdom()+" 生命值:"+this.getLife()+
" 魔法值:"+this.getMagic());out.close();
} catch(FileNotFoundException e) {e.printStackTrace();}
catch(IOException e) {e.printStackTrace();}
System.out.println("保存成功!!!");
}
}
public class Test {
public static void main(String[] args) {
Base base=new Base();
base.setName();
base.Setsex();
base.setRacial();
Racial racial=new Racial();
racial.SetJob(base);
Attribute attribute=new Attribute();
attribute.setAttribute(racial);
System.out.println("你的角色的具体属性为:\n"+"姓名:"+base.getName()+" 性别:"+base.getSex()+" 种族:"+base.getRacial()+" 职业:"+racial.getJob()+
" 属性:"+"力量:"+attribute.getStrength()+" 敏捷:"+attribute.getQuick()+" 体力:"+attribute.getPhys()+" 智力:" +attribute.getIntell()+" 智慧:"+attribute.getWisdom()+" 生命值:"+attribute.getLife()+" 魔法值:"+attribute.getMagic());
attribute.save(base, racial);
}
}
三、学习心得
使用FileWriter时,如果指定的文件夹不存在,会自动创建一个文件,如果指定的文件已经存在那么会覆盖写入,原来文件中的内容会被覆盖掉。如果不想被覆盖掉,而是在原来内容的基础上写入,就需要用另外一种重载形式的构造方法,FileWriter(String FileName,Boolean append),第二个参数如果是true就追加写入。
标签:case,JAVA,游戏,random,20,break,nextInt,RPG,15
来源: https://blog.csdn.net/qq_44267206/article/details/89522592
这个Java程序允许用户创建RPG角色,包括选择名字、性别、种族、职业,并自动分配属性。根据用户选择的种族和职业,程序会生成随机但符合职业特征的属性值,如力量、敏捷、体力、智力和智慧,以及计算生命值和魔法值。用户可以选择不满意并重新创建,满意则保存角色信息到文件。
1765

被折叠的 条评论
为什么被折叠?



