RPG角色生成器
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.顯示資訊
最後向用戶顯示該角色的所有資訊,然後詢問使用者是否滿意,如使用者不滿意則重新建立,若使用者滿意則程式結束,並將使用者建立角色的相關資訊寫入檔案儲存。
基本要求:
- 遵循面向物件的7大原則進行程式的設計。
- 實現控制檯上的角色的建立。
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.Random; import java.util.Scanner;
public class Test50 {
public static void main(String[] args) throws IOException { Scanner sc=new Scanner(System.in); int player_choice = 1; BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("E:\\out.txt")); while(player_choice==1){ Race race = new Race(); Race1 race1 = new Race1(); race.input_name_sex(); race1.race(); Attribute attribute=new Attribute(); attribute.getAttribute(); System.out.println("================================="); System.out.println("姓名:"+"\t\t"+race.name); System.out.println("================================="); System.out.println("性別:"+"\t\t"+race.sex); System.out.println("================================="); System.out.println("種族:"+"\t\t"+race1.race); System.out.println("================================="); System.out.println("職業:"+"\t\t"+race1.occupation); System.out.println("================================="); System.out.println("力量:"+"\t\t"+attribute.strength); System.out.println("================================="); System.out.println("敏捷:"+"\t\t"+attribute.agility); System.out.println("================================="); System.out.println("體力:"+"\t\t"+attribute.physical); System.out.println("================================="); System.out.println("智力:"+"\t\t"+attribute.intelligence); System.out.println("================================="); System.out.println("智慧:"+"\t\t"+attribute.wisdom); System.out.println("================================="); System.out.println("生命值:"+"\t\t"+attribute.HP); System.out.println("================================="); System.out.println("法力值:"+"\t\t"+attribute.MP); System.out.println("================================="); //檔案輸出 bufferedWriter.write("姓名:"+"\t\t"+race.name+"\r\n"); bufferedWriter.write("性別:"+"\t\t"+race.sex+"\r\n"); bufferedWriter.write("種族:"+"\t\t"+race1.race+"\r\n"); bufferedWriter.write("職業:"+"\t\t"+race1.occupation+"\r\n"); bufferedWriter.write("力量:"+"\t\t"+attribute.strength+"\r\n"); bufferedWriter.write("敏捷:"+"\t\t"+attribute.agility+"\r\n"); bufferedWriter.write("體力:"+"\t\t"+attribute.physical+"\r\n"); bufferedWriter.write("智力:"+"\t\t"+attribute.intelligence+"\r\n"); bufferedWriter.write("智慧:"+"\t\t"+attribute.wisdom+"\r\n"); bufferedWriter.write("生命值:"+"\t\t"+attribute.HP+"\r\n"); bufferedWriter.write("法力值:"+"\t\t"+attribute.MP+"\r\n"); bufferedWriter.newLine(); System.out.println("是否繼續生成遊戲角色?(0.結束生成 1.繼續生成):"); player_choice=sc.nextInt(); } //關閉輸出流 bufferedWriter.close(); } }
class Race {//儲存角色的姓名,性別 Scanner sc = new Scanner(System.in); String name; String sex; boolean flag = true;
public void input_name_sex() {//輸入角色名和性別 System.out.print("請輸入您遊戲角色的姓名:"); name = sc.nextLine(); while (flag) { System.out.print("請選擇您遊戲角色的性別(0男性,1女性):"); int sex_choice = sc.nextInt(); switch (sex_choice) { case 0: sex = "男性"; flag = false; break; case 1: sex = "女性"; flag = false; break; default: System.out.print("輸入錯誤,請重新輸入:"); break; } } } }
class Race1 {//記錄角色的種族、職業 Scanner sc = new Scanner(System.in); boolean flag = true; int race_choice; int occupation_choice;//玩家所選擇的職業的序號 String race; String occupation;
public void race() {//選擇種族和職業 while (flag) { System.out.print("請選擇角色的種族(0.人類,1.精靈,2.獸人,3.矮人,4.元素):"); race_choice = sc.nextInt(); switch (race_choice) { case 0: race = "人類"; flag = false; break; case 1: race = "精靈"; flag = false; break; case 2: race = "獸人"; flag = false; break; case 3: race = "矮人"; flag = false; break; case 4: race = "元素"; flag = false; break; default: System.out.print("輸入錯誤,請重新輸入:"); break; } } while (true) { switch (race_choice) { case 0: System.out.print("請輸入您的職業(0.狂戰士,1.聖騎士,2.刺客,3.獵手,4.祭司,5.巫師):"); break; case 1: System.out.print("請輸入您的職業(2.刺客,3.獵手,4.祭司,5.巫師):"); break; case 2: System.out.print("請輸入您的職業(0.狂戰士,3.獵手,4.祭司):"); break; case 3: System.out.print("請輸入您的職業(0.狂戰士,1.聖騎士,4.祭司):"); break; case 4: System.out.print("請輸入您的職業(4.祭司,5.巫師)):"); break;
} occupation_choice = sc.nextInt(); if (race_choice == 0 && (occupation_choice >= 0 && occupation_choice <= 5)) { break; } else if (race_choice == 1 && (occupation_choice > 1 && occupation_choice < 6)) { break; } else if (race_choice == 2 && (occupation_choice == 0 || occupation_choice == 3 || occupation_choice == 4)) { break; } else if (race_choice == 3 && (occupation_choice == 0 && occupation_choice == 1 || occupation_choice == 4)) { break; } else if (race_choice == 4 && (occupation_choice > 3 && occupation_choice < 6)) { break; } else { System.out.print("輸入錯誤,請重新輸入:"); } } if (occupation_choice == 0) { occupation = "狂戰士"; } if (occupation_choice == 1) { occupation = "聖騎士"; } if (occupation_choice == 2) { occupation = "刺客"; } if (occupation_choice == 3) { occupation = "獵手"; } if (occupation_choice == 4) { occupation = "祭司"; } if (occupation_choice == 4) { occupation = "巫師"; } } }
// 記錄角色的屬性 class Attribute { int occupation_choice; Random random = new Random(); int strength; // 力量 int agility; // 敏捷 int physical; // 體力 int intelligence; // 智力 int wisdom; // 智慧 int HP; // 生命值 int MP; // 法力值 //隨機生成每項屬性的值,abcd為該屬性的最小值,e為第五個屬性的最大值 public void getRandom(int a, int b, int c, int d, int e) { int sum; // 前4項屬性之和 do { strength = a + random.nextInt() % 10; agility = b + random.nextInt() % 10; physical = c + random.nextInt() % 10; intelligence = d + random.nextInt() % 10; sum = strength + agility + physical + intelligence; } while (((100 - e) < sum) && (sum < 100)); wisdom = 100 - sum; HP = physical * 20; MP = (wisdom + intelligence) * 10; } // 根據選擇的職業,向getRamdom傳不同的最小值 public void getAttribute(){ if (occupation_choice == 0) getRandom(40, 20, 30, 5, 5 ); if (occupation_choice == 1) getRandom(25, 15, 30, 20, 10); if (occupation_choice == 2) getRandom(20, 35, 20, 15, 10); if (occupation_choice == 3) getRandom(15, 40, 15, 10, 20); if (occupation_choice == 4) getRandom(15, 20, 15, 35, 15); if (occupation_choice == 5) getRandom(10, 20, 10, 20, 40); } }