日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

RPG角色生成器

發(fā)布時間:2023/12/10 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 RPG角色生成器 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目要求

1.功能描述
幾乎所有的RPG游戲(一種源自《龍與地下城》的游戲類型)在進入游戲時都會讓用戶自己來創(chuàng)建自己喜歡的角色。本次上機要求編寫一個簡化的創(chuàng)建游戲角色的程序。
2.游戲角色應(yīng)有的屬性
本題目要求的游戲角色應(yīng)有以下屬性:名字、性別、種族、職業(yè)、力量、敏捷、體力、智力、智慧、生命值和魔法值。
名字:不超過50個字符。
性別:可以選擇男性和女性。
種族:一共可選五個種族,人類、精靈、獸人、矮人和元素。
職業(yè):可選六種職業(yè),狂戰(zhàn)士、圣騎士、刺客、獵手、祭司和巫師。
其余屬性均為整數(shù)。
本題目要求首先用戶輸入角色姓名,然后由用戶選擇角色性別,然后由用戶選擇種族,然后選擇職業(yè),然后自動分配力量、敏捷、體力、智力和智慧屬性,并計算生命值和魔法值。
生命值=體力*20。
魔法值=(智力+智慧)*10。
3.職業(yè)限制
很多職業(yè)會限制某些種族選擇,例如獸人不能就職圣騎士等等,種族和職業(yè)的限制表如下:
種族/職業(yè) 狂戰(zhàn)士 圣騎士 刺客 獵手 祭司 巫師
人類 允許 允許 允許 允許 允許 允許
精靈 不允許 不允許 允許 允許 允許 允許
獸人 允許 不允許 不允許 允許 允許 不允許
矮人 允許 允許 不允許 不允許 允許 不允許
元素 不允許 不允許 不允許 不允許 允許 允許
所以在要求用戶選擇職業(yè)時,輸出信息里面只能有用戶所選擇種族可以就職的職業(yè)。
4.初始屬性
本題目要求力量、敏捷、體力、智力和智慧要求是隨機值(利用隨機數(shù)函數(shù)來取得隨機數(shù)),但是五項屬性的總和應(yīng)該是100,并且應(yīng)該和職業(yè)相關(guān)。例如狂戰(zhàn)士的體力和力量就要比較高,而巫師需要較高的智力,而祭司則需要較高的智慧。各職業(yè)初始屬性的大致比例應(yīng)遵從下表:
職業(yè)/屬性 力量 敏捷 體力 智力 智慧
狂戰(zhàn)士 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
例如,前面示意圖中的祭司的初始屬性,大致滿足該比例,但是應(yīng)該是隨機的。
然后利用屬性值計算生命值和魔法值。
5.顯示信息
最后向用戶顯示該角色的所有信息,然后詢問用戶是否滿意,如用戶不滿意則重新創(chuàng)建,若用戶滿意則程序結(jié)束,并將用戶創(chuàng)建角色的相關(guān)信息寫入文件保存。

源碼

package rpg.role;import java.util.Random; import java.util.Scanner;/*** 項目名稱:RPG角色生成器* 編輯時間:2019年4月18日* @author 萬物甦醒* 思路:定義一個角色類,角色類封裝角色的各個屬性和字段,并沒有特殊的方法實現(xiàn)功能* 方法主要是get來獲取屬性和字段,set初始化各項屬性和字段。這個方法和其他類的實例化對象* 接觸的比較多,因為需要這個方法來獲取其他類傳遞而來的屬性值*/public class GameRole {String name;char sex;String racial;String career;private int strength; //力量private int agility;//敏捷度private int physical;//體力private int intelligence;//智力private int wisdom; //智慧private int lifevalue; //生命值private int magicvalue;//魔法值public String getName() {return name;}public char getSex() {return sex;}public String getRacial() {return racial;}public String getCareer() {return career;}public int getStrength() {return strength;}public int getAgility() {return agility;}public int getPhysical() {return physical;}public int getIntelligence() {return intelligence;}public int getWisdom() {return wisdom;}public int getLifevalue() {return lifevalue;}public int getMagicvalue() {return magicvalue;}public void setName(String name) {this.name = name;}public void setSex(char sex) {this.sex = sex;}public void setRacial(String racial) {this.racial = racial;}public void setCareer(String career) {this.career = career;}public void setStrength(int strength) {this.strength = strength;}public void setAgility(int agility) {this.agility = agility;}public void setPhysical(int physical) {this.physical = physical;}public void setIntelligence(int intelligence) {this.intelligence = intelligence;}public void setWisdom(int wisdom) {this.wisdom = wisdom;}public void setLifevalue(int lifevalue) {this.lifevalue = lifevalue;}public void setMagicvalue(int magicvalue) {this.magicvalue = magicvalue;} } package rpg.role; /*** 項目名稱:RPG角色生成器* 編輯時間:2019年4月18日* @author 萬物甦醒* 思路:1.定義一個職業(yè)選擇類,從主函數(shù)獲取種族,在這個類中將種族傳遞給角色類;* 2.根據(jù)傳來的種族選擇所能夠匹配的職業(yè),將職業(yè)傳遞給角色類來初始化*/import java.util.Scanner;public class ChoiceCareer {//String racial;String career;//要傳給角色類的職業(yè)//將職業(yè)的值傳給GameRole類public void careertoRole(GameRole gr){ // GameRole gr = new GameRole();gr.setCareer(career);}/**根據(jù)種族選擇職業(yè)* 根據(jù)種族提示用戶哪些可供選擇職業(yè),利用switch實現(xiàn)* 不過還是覺得有些冗余~~~* @param racial*/public void setCareer(int m,GameRole gr) {if(m == 1){gr.setRacial("人類");//設(shè)置角色類的種族為人類//開始個根據(jù)種族選擇職業(yè)System.out.println("請輸入你的而游戲角色的職業(yè):1 狂戰(zhàn)士 2圣騎士 3刺客 4獵手 5祭司 6 巫師");int choice;Scanner scan = new Scanner(System.in);choice = scan.nextInt();switch(choice){case 1: this.career = "狂戰(zhàn)士";break;case 2: this.career = "圣騎士";break;case 3: this.career = "刺客";break;case 4: this.career = "獵手";break;case 5: this.career = "祭司";break;case 6: this.career = "巫師";break;default:this.career =null;break;}}else if(m ==2){gr.setRacial("精靈");System.out.println("請輸入你的而游戲角色的職業(yè):1 刺客 2獵手 3祭司 4 巫師");int choice;Scanner scan = new Scanner(System.in);choice = scan.nextInt();switch(choice){case 1: this.career = "刺客";break;case 2: this.career = "獵手";break;case 3: this.career = "祭司";break;case 4: this.career = "巫師";break;default:this.career =null;}}else if(m == 3){gr.setRacial("獸人");System.out.println("請輸入你的而游戲角色的職業(yè):1 狂戰(zhàn)士 2獵手 3祭司 4 巫師");int choice;Scanner scan = new Scanner(System.in);choice = scan.nextInt();switch(choice){case 1: this.career = "狂戰(zhàn)士";break;case 2: this.career = "獵手";break;case 3: this.career = "祭司";break;case 4: this.career = "巫師";break;default:this.career =null;break;}}else if(m == 4){gr.setRacial("矮人");System.out.println("請輸入你的而游戲角色的職業(yè):1 狂戰(zhàn)士 2圣騎士 3祭司 ");int choice;Scanner scan = new Scanner(System.in);choice = scan.nextInt();switch(choice){case 1: this.career = "狂戰(zhàn)士";break;case 2: this.career = "圣騎士";break;case 3: this.career = "祭司";break;default:this.career =null;break;}}else if(m == 5){gr.setRacial("元素");System.out.println("請輸入你的而游戲角色的職業(yè):1 祭司 2巫師 ");int choice;Scanner scan = new Scanner(System.in);choice = scan.nextInt();switch(choice){case 1: this.career = "祭司";break;case 2: this.career = "巫師";break;default:this.career =null;break;}}else {System.out.println("輸入錯誤");}careertoRole(gr);} } package rpg.role;import java.util.Random; /*** 項目名稱:RPG角色生成器* 編輯時間:2019年4月18日* @author 萬物甦醒* 思路:1.定義一個屬性類,根據(jù)職業(yè)初始角色各項屬性,分配一個大致的比例* 2.生成隨機數(shù)在固定分配的值上有所增減,但保證總和為100,大致符合比例* 3.將計算后的屬性值傳遞給角色類*/public class Attribute {int strength ; //力量int agility ;//敏捷度int physical ;//體力int intelligence; //智力int wisdom; //智慧int lifevalue; //生命值int magicvalue;//魔法值/*** 設(shè)置屬性,根據(jù)不同的職業(yè)分配五大屬性不同的比例* @param career*/void setAttribution(GameRole gr){int sum ;//定義一個角色類對象,并獲取角色的職業(yè) // //GameRole gr = new GameRole();String career = gr.getCareer();//根據(jù)職業(yè)的不同分配不同的屬性參數(shù)計算,不同的職業(yè)給的比例不同if(career.equals("狂戰(zhàn)士")){calculateAttribution(40,20,30,5,5);}if(career.equals("圣騎士")){calculateAttribution(25,15,30,20,10);}if(career.equals("刺客")){calculateAttribution(20,35,20,15,10);}if(career.equals("獵手")){calculateAttribution(15,40,15,10,20);}if(career.equals("祭司")){calculateAttribution(15,20,15,35,15);}if(career.equals("巫師")){calculateAttribution(10,20,10,20,40);}deliverAttribution(gr);}/*** 計算屬性值* 根據(jù)傳入的參數(shù)a,b,c.d,e為前五個屬性的比例分配* 利用Random類的隨機生成整數(shù)函數(shù)nextInt()生成各項屬性值* 由于題目要求需滿足在分配數(shù)值左右浮動,所以nextInt()方法在其分配數(shù)值的基礎(chǔ)上增加一個[-5,5]的數(shù)字*/void calculateAttribution(int a ,int b,int c,int d,int e){int sum ;//定義隨即類,用于后面隨機生成屬性Random rand = new Random();do{//在根據(jù)職業(yè)所提供的比例上適當(dāng)有所增減,能夠保證總和為100,又能夠基本保持比例,沒有極大的浮動//職業(yè)分配分數(shù)固定,在有一個[-2,5]的浮動,這個范圍開發(fā)人員可任意修改,波動無需太大strength = a + rand.nextInt(5) - 2 ;agility = b + rand.nextInt(5) - 2 ;physical = c + rand.nextInt(5) - 2 ;intelligence = d + rand.nextInt(5) - 2 ;wisdom = e + rand.nextInt(5) - 2 ;lifevalue = physical*20;magicvalue = (wisdom + intelligence)*10;sum = strength +agility + physical + intelligence +wisdom;}while(sum != 100);}/*** 將計算后的值傳送給GameRole類來初始化角色類各項數(shù)據(jù)*/void deliverAttribution(GameRole gr){//定義一個角色類對象,并獲取角色的職業(yè)//GameRole gr = new GameRole();//依次設(shè)置各項屬性,將計算后gr.setStrength(strength);gr.setAgility(agility);gr.setPhysical(physical);gr.setIntelligence(intelligence);gr.setWisdom(wisdom);gr.setLifevalue(lifevalue);gr.setMagicvalue(magicvalue);} } package rpg.role;import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner =new Scanner(System.in);GameRole gr = new GameRole();/*** 輸入姓名,判斷姓名字符串長度是否小于50 利用judge()方法實現(xiàn)*/String name;//輸入姓名do {System.out.print("請輸入游戲角色的姓名:");name = scanner.nextLine();gr.setName(name);//角色姓名}while(judgename(name));/*** java中Scanner類沒有關(guān)于輸入字符的函數(shù),因此還需利用* String類中的charAt()方法提取字符串的第一個字符*///輸入性別System.out.print("請輸入的你游戲角色的性別(男OR女):");char sex = scanner.next().charAt(0);gr.setSex(sex);;//角色性別//實例化職業(yè)選擇類,初始化角色類的careerChoiceCareer cc = new ChoiceCareer();//定義職業(yè)選擇類//輸入種族System.out.print("請輸入游戲角色的種族(1.人類2.精靈3.獸人4.矮人5.元素):");int m = scanner.nextInt(); //定義變量m存種族序號cc.setCareer(m,gr);//角色種族//實例化參數(shù)類,初始化角色類的屬性值Attribute attribute =new Attribute();attribute.setAttribution(gr);//依次輸出角色的所有信息System.out.println("角色姓名 :"+gr.getName());System.out.println("角色性別 :"+gr.getSex());System.out.println("角色種族 :"+gr.getRacial());System.out.println("角色職業(yè) :"+gr.getCareer());System.out.println("角色力量 :"+gr.getStrength());System.out.println("角色敏捷 :"+gr.getAgility());System.out.println("角色體力 :"+gr.getPhysical());System.out.println("角色智力 :"+gr.getIntelligence());System.out.println("角色智慧 :"+gr.getWisdom());System.out.println("角色生命值:"+gr.getLifevalue());System.out.println("角色魔法值:"+gr.getMagicvalue());}//判斷姓名是否輸入正確private static boolean judgename(String name) {// TODO 自動生成的方法存根if(name.length()>50)return true;elsereturn false;}}

運行結(jié)果


總結(jié)

以上是生活随笔為你收集整理的RPG角色生成器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。