rpg角色生成器java版本

作业说明:
题目:
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.显示信息
最后向用户显示该角色的所有信息,然后询问用户是否满意,如用户不满意则重新创建,若用户满意则程序结束,并将用户创建角色的相关信息写入文件保存。

思路:
用面向对象思路,创建好输入姓名,性别,种族,职业函数,用键盘输入数字,选择信息。根据所选职业随机分配属性,然后询问是否愿意,如果不满意则重新建立,满意则保存在txt文件中。
职业限制的方法:用race_chocie保存种族信息,然后用swtich语句根据race_choice来选择不同的职业。
随机分配属性的办法:用基本属性+Math.random函数分配

运行结果:

在这里插入图片描述

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class rgpCreate  {
	public static String dsex[]={"男","女"};             
	public static String drace[]={"人类","精灵","兽人","矮人","元素"};
	public static String doccuption[]={"狂战士","圣骑士","刺客","猎手","祭司","巫师"};
	public int race_choice;                    //种族选择
	public int occuption_choice;               //职业选择
	public String name;
	public int sex_choice;                    //性别选择
	public int strength;	//力量
	public int agility;	//敏捷
	public int physical;	//体力
    public int intelligence;	//智力
	public int wisdom;	//智慧
	public int HP;	//生命值
	public int MP;	//法力值
	

	public static void main(String[] args) {
		rgpCreate rgp=new rgpCreate();                 //创建一个rgp对象
		int i = 1;
		while(i != 0) {
			rgp.getBaseinformation();
			rgp.getRace();
			rgp.getOccuption();
			rgp.random();
			System.out.println("0愿意,1不愿意");
			Scanner sc=new Scanner(System.in);
			i = sc.nextInt();
		}
		System.out.println("创建成功");
		rgp.outPut();
		rgp.save();

	}
	/*
	 * 姓名性别输入函数 
	 * */
	public int getBaseinformation(){
    	Scanner sc=new Scanner(System.in);
    	
		System.out.println("请输入您的昵称:");
		name=sc.nextLine();	
		while(name.length()>50){
			
			System.out.println("昵称超过50位字符!,请重新输入");
		}
		while(true){ 
			System.out.println("请选择您的性别:0为男性,1为女性。");         
		    sex_choice=sc.nextInt();
			if(sex_choice==0||sex_choice==1){
				break;                                //验证输入正确性,正确则跳出,否则输出提示。
			}
			else
				System.out.println("请输入0或1选择性别");
		}
		return sex_choice;
	}
/*
 * 种族选择函数
 * */
	public int getRace(){
		Scanner sc =new Scanner(System.in);
		System.out.println("请选择种族:0为人类,1为精灵,2为兽人,3为矮人,4为元素");
		race_choice=sc.nextInt();
		while(true){
			if(race_choice>=0&&race_choice<=4){        //验证输入正确性,正确则跳出,否则输出提示。
				break;
			}
			else
				
				System.out.println("请输入0-4的数字选择种族");
		}
		return race_choice;
	  }
	/*
	 * 职业选择函数
	 * */
	  public int getOccuption() {
		Scanner sc=new Scanner(System.in);
		switch(race_choice){
		case 0:
			while(true){
				System.out.println("请选择您的职业:0狂战士,1圣骑士,2刺客,3猎手,4祭司,5巫师");  //人类所选职业
				occuption_choice=sc.nextInt();
				if(occuption_choice>=0&&occuption_choice<=5){      //验证输入正确性,正确则跳出,否则输出提示。
					break;
				}
				else
					System.out.println("请输入0-5之间的数字选择职业!!");
			}
			break;
		case 1:	
			while(true){
				System.out.println("请选择您的职业:2刺客,3猎手,4祭司,5巫师");  //精灵所选职业
				occuption_choice=sc.nextInt();
				if(occuption_choice>=2&&occuption_choice<=5){     //验证输入正确性,正确则跳出,否则输出提示。
					break;
				}
				else
					System.out.println("请输入2-5之间的数字选择职业!!");
			}
			break;
		case 2:
			while(true){
				System.out.println("请选择您的职业:0狂战士,3猎手,4祭司");  //兽人所选职业
				occuption_choice=sc.nextInt();
				if(occuption_choice==0||occuption_choice==3||occuption_choice==4){
					break;                                   //验证输入正确性,正确则跳出,否则输出提示。
				}
				else
					System.out.println("请输入0或3或4来选择职业!!");
			}
			break;
		case 3:
			while(true){
				System.out.println("请选择您的职业:0狂战士,1圣骑士,4祭司");  //矮人所选职业
				occuption_choice=sc.nextInt();
				if(occuption_choice==0||occuption_choice==1||occuption_choice==4){
					break;                                //验证输入正确性,正确则跳出,否则输出提示。
				}
				else
					System.out.println("请输入0或1或4来选择职业!!");
			}
			break;
		case 4:
			while(true){
				System.out.println("请选择您的职业:4祭司,5巫师");  //元素所选职业
				occuption_choice=sc.nextInt();
				if(occuption_choice==4||occuption_choice==5){   //验证输入正确性,正确则跳出,否则输出提示。
					break;
				}
				else
					System.out.println("请输入4或5来选择职业!!");
			}
			break;
		}
		return occuption_choice;
	 }
/*
 * 属性随机分配函数
 * */	  
	public void random(){
			if(occuption_choice==0){                       //人类的属性值分配
				strength=(int)(40+Math.random()*10-5);
				agility=(int)(20+Math.random()*10-5);
				physical=(int)(30+Math.random()*10-5);
				intelligence=(int)(5+Math.random()*10-5);
				wisdom=100-strength-agility-physical-intelligence;
				HP = physical * 20;//生命值为体力的20倍
				MP = (wisdom + intelligence) * 10;//魔法值为智力与智慧的10倍

			}
			else if(occuption_choice==1){                        //圣骑士的属性值分配
				strength=(int)(25+Math.random()*10-5);
				agility=(int)(15+Math.random()*10-5);
				physical=(int)(30+Math.random()*10-5);
				intelligence=(int)(20+Math.random()*10-5);
				wisdom=100-strength-agility-physical-intelligence;
				HP = physical * 20;//生命值为体力的20倍
				MP = (wisdom + intelligence) * 10;//魔法值为智力与智慧的10倍

			}
			else if(occuption_choice==2){                       //刺客的属性值分配
				strength=(int)(20+Math.random()*10-5);
				agility=(int)(35+Math.random()*10-5);
				physical=(int)(20+Math.random()*10-5);
				intelligence=(int)(15+Math.random()*10-5);
				wisdom=100-strength-agility-physical-intelligence;
				HP = physical * 20;//生命值为体力的20倍
				MP = (wisdom + intelligence) * 10;//魔法值为智力与智慧的10倍

			}
			else if(occuption_choice==3){                       //猎手的属性值分配
				strength=(int)(15+Math.random()*10-5);
				agility=(int)(40+Math.random()*10-5);
				physical=(int)(15+Math.random()*10-5);
				intelligence=(int)(10+Math.random()*10-5);
				wisdom=100-strength-agility-physical-intelligence;
				HP = physical * 20;//生命值为体力的20倍
				MP = (wisdom + intelligence) * 10;//魔法值为智力与智慧的10倍

			}
			else if(occuption_choice==4){                       //祭司的属性值分配
				strength=(int)(15+Math.random()*10-5);
				agility=(int)(20+Math.random()*10-5);
				physical=(int)(15+Math.random()*10-5);
				intelligence=(int)(35+Math.random()*10-5);
				wisdom=100-strength-agility-physical-intelligence;
				HP = physical * 20;//生命值为体力的20倍
				MP = (wisdom + intelligence) * 10;//魔法值为智力与智慧的10倍

			}
			else if(occuption_choice==5){                       //巫师的属性值分配
				strength=(int)(10+Math.random()*10-5);
				agility=(int)(20+Math.random()*10-5);
				physical=(int)(10+Math.random()*10-5);
				intelligence=(int)(20+Math.random()*10-5);
				wisdom=100-strength-agility-physical-intelligence;
				HP = physical * 20;//生命值为体力的20倍
				MP = (wisdom + intelligence) * 10;//魔法值为智力与智慧的10倍

			}
		}
	/*
	 * 打印出玩家信息函数
	 * */
		public String outPut(){
			System.out.println("===============================");
			System.out.println("姓名: "+name);
			System.out.println("===============================");
			System.out.println("性别 : "+dsex[sex_choice]);
			System.out.println("===============================");
			System.out.println("种族 : "+drace[race_choice]);
			System.out.println("===============================");
			System.out.println("职业 : "+doccuption[occuption_choice]);
			System.out.println("===============================");
			System.out.println("力量 : "+strength);
			System.out.println("===============================");
			System.out.println("敏捷 : "+agility);
			System.out.println("===============================");
			System.out.println("体力 : "+physical);
			System.out.println("===============================");
			System.out.println("智力 : "+intelligence);
			System.out.println("===============================");
			System.out.println("智慧 : "+wisdom);
			System.out.println("===============================");
			System.out.println("HP: "+HP);
			System.out.println("===============================");
			System.out.println("MP: "+MP);
			System.out.println("===============================");
			return (name+dsex[sex_choice]+drace[race_choice]+doccuption[occuption_choice]+strength+agility+physical+intelligence+wisdom+HP+MP);
		}

/*
 * 将玩家信息储存进文件函数
 * */
public void save(){
	    File file = new File("D://d.txt");			
		try (PrintWriter output = new PrintWriter(file);){
			output.println("姓名: "+name);
			output.println("性别 : "+dsex[sex_choice]);
			output.println("种族 : "+drace[race_choice]);
			output.println("职业 : "+doccuption[occuption_choice]);
			output.println("力量 : "+strength);
			output.println("敏捷 : "+agility);
			output.println("体力 : "+physical);
			output.println("智力 : "+intelligence);
			output.println("智慧 : "+wisdom);
			output.println("HP: "+HP);
			output.println("MP: "+MP);
			output.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();	
		}
		
	}
 
		
}

猜你喜欢

转载自blog.csdn.net/qq_42510221/article/details/82957000