2013-04-27 33 views
4

我正在創建一個能夠自動創建玩家角色的程序。以下是我的PlayerCharacter班。我注意到我對不同的變量重複了許多操作。Java - 尋找更高效的代碼類方法

public class PlayerCharacter { 

    int strength, dexterity, constitution, intelligence, wisdom, charisma; 
    int[] strRolls, dexRolls, conRolls, intRolls, wisRolls, charRolls; 

    public void generateAbilityScoresMethod1() { 

     strRolls = new int[3]; 
     dexRolls = new int[3]; 
     conRolls = new int[3]; 
     intRolls = new int[3]; 
     wisRolls = new int[3]; 
     charRolls = new int[3]; 

     for(int i = 0; i < 3; i++) { 

      strRolls[i] = dice.Dice.D6.getNewRoll(); 
      strength += strRolls[i]; 

      dexRolls[i] = dice.Dice.D6.getNewRoll(); 
      dexterity += dexRolls[i]; 

      conRolls[i] = dice.Dice.D6.getNewRoll(); 
      constitution += conRolls[i]; 

      intRolls[i] = dice.Dice.D6.getNewRoll(); 
      intelligence += intRolls[i]; 

      wisRolls[i] = dice.Dice.D6.getNewRoll(); 
      wisdom += wisRolls[i]; 

      charRolls[i] = dice.Dice.D6.getNewRoll(); 
      charisma += charRolls[i]; 

     } 


    } 

    public int getStrength() { 
     return strength; 
    } 

    public void printStrRolls() { 
     System.out.println("Str: roll 1 = " + strRolls[0]); 
     System.out.println("Str: roll 2 = " + strRolls[1]); 
     System.out.println("Str: roll 3 = " + strRolls[2]); 
    } 

    public int getDexterity() { 
     return dexterity; 
    } 

    public void printDexRolls() { 
     System.out.println("Dex: roll 1 = " + dexRolls[0]); 
     System.out.println("Dex: roll 2 = " + dexRolls[1]); 
     System.out.println("Dex: roll 3 = " + dexRolls[2]); 
    } 

    public int getConsitution() { 
     return constitution; 
    } 

    public void printConRolls() { 
     System.out.println("Con: roll 1 = " + conRolls[0]); 
     System.out.println("Con: roll 2 = " + conRolls[1]); 
     System.out.println("Con: roll 3 = " + conRolls[2]); 
    } 

    public int getIntelligence() { 
     return intelligence; 
    } 

    public void printIntRolls() { 
     System.out.println("Int: roll 1 = " + intRolls[0]); 
     System.out.println("Int: roll 2 = " + intRolls[1]); 
     System.out.println("Int: roll 3 = " + intRolls[2]); 
    } 

    public int getWisdom() { 
     return wisdom; 
    } 

    public void printWisRolls() { 
     System.out.println("Wis: roll 1 = " + wisRolls[0]); 
     System.out.println("Wis: roll 2 = " + wisRolls[1]); 
     System.out.println("Wis: roll 3 = " + wisRolls[2]); 
    } 

    public int getCharisma() { 
     return charisma; 
    } 

    public void printCharRolls() { 
     System.out.println("Char: roll 1 = " + charRolls[0]); 
     System.out.println("Char: roll 2 = " + charRolls[1]); 
     System.out.println("Char: roll 3 = " + charRolls[2]); 
    } 

    public void printAbilities() { 
     System.out.println("Str = " + getStrength()); 
     System.out.println("Dex = " + getDexterity()); 
     System.out.println("Con = " + getConsitution()); 
     System.out.println("Int = " + getIntelligence()); 
     System.out.println("Wis = " + getWisdom()); 
     System.out.println("Char = " + getCharisma()); 
    } 

} 

如何以更高效的方式完成相同的任務?

+0

你爲什麼要存放Rolls數組呢? – UmNyobe 2013-04-27 13:00:07

+0

大部分人都很好奇,看看我的骰子怎麼樣。 – 2013-04-27 13:01:56

回答

3

您可以聲明一個類Ability並使strength, dexterity,...其實例。下面的代碼片段可能是一個開始:

class Ability { 

    private final int[] rolls; 
    private int value; 

    public Ability(int dice) { 
     rolls = new int[dice]; 
    } 

    public int roll() { 
     value = 0; 
     for (int i = 0; i < rolls.length; i++) { 
      rolls[i] = dice.Dice.D6.getNewRoll(); 
      value += rolls[i]; 
     } 
     return value; 
    } 

    public int getValue() { 
     return value; 
    } 

    public void printRolls() { 
     // ... tbd ... 
    } 

} 

您可以使用該技能像...

Ability strength; 
strength = new Ability(3); 
strength.roll(); // get new value 
System.out.println(strength.getValue()); // e.g. print 
strength.printRolls(); // e.g. print rolls 
0

如何:

public void printRolls(String label, int[] rolls) { 
    System.out.println(label + ": roll 1 = " + rolls[0]); 
    System.out.println(label + ": roll 2 = " + rolls[1]); 
    System.out.println(label + ": roll 3 = " + rolls[2]); 
} 
0

可以減少重複的代碼行此

enum Attribute { 
    Str, Con, Dex, Int, Wis, Cha 
} 

class PlayerCharacter { 
    static final Random rand = new Random(); 
    static final Attribute[] ATTRS = Attributes.values(); 
    final int[] attr = new int[ATTRS.length]; 

    PlayerCharacter() { 
     for(int i = 0; i < attr.length; i++) 
      attr[i] = rand.nextInt(6) + rand.nextInt(6) + rand.nextInt(6) + 3; 
    } 

    public int getAttr(Attribute attr) { 
     return attrs[attr.ordinal()]; 
    } 

    public void printAbilities() { 
     for(int i = 0; i < ATTRS.length; i++) 
      System.out.println(ATTRS[i]+ " = " + attrs[i]); 
    } 

注意:您可能需要的任何其他屬性,如HP,AC,最大HP,等級,XP,等等。

0

我會做的唯一的事情有創建一個靜態的骰子滾動方法,該方法返回陣列需要,這樣就可以產生任何類型的骰子擲骰數組:

public static int[] roll(int numberOfDice, int sidesOnEachDie) { 
    int[] result = new int[numberOfDice]; 
    for(int i = 0; i < numberOfDice; ++i) { 
     result[i] = 1 + (int) Math.floor(Math.random() * 
       (double) sidesOnEachDie); 
    } 
    return result; 
} 

然後就可以調用,例如:

intRolls = roll(3, 6); 

這將給出在一個int數組中滾動3D6的結果。

+0

我的卷由Dice枚舉類處理。 – 2013-04-27 13:07:22

+0

添加選項以一次性返回多個捲到Dice類的時間太晚了嗎?如果您可以讓Dice類通過一個方法調用返回多個結果,它將消除在代碼的主流程中添加for循環的需要。 – Bobulous 2013-04-27 13:10:27

+0

我相信這當然是一個可行的選擇。 – 2013-04-27 13:12:27

0

您可以將所有屬性保留在int []中。因此,而不是力量,敏捷,體質,智力,智慧,魅力作爲獨立變量有一個

int[] characteristics; 

然後0將強度; 1會靈巧等等。

要保持滾動值,您需要一個int [] []。

-1

這是較少的代碼;它在CPU使用或內存使用方面並不「高效」,但會使代碼縮短以便讀寫。我認爲骰子卷是隨機的,不管他們的順序如何。

package example; 

public class PlayerCharacter 
{ 

    int strength, dexterity, constitution, intelligence, wisdom, charisma; 
    int[] strRolls, dexRolls, conRolls, intRolls, wisRolls, charRolls; 

    private int getScore(Dice d, int[] storage) 
    { 
    for (int i=0; i<storage.length; i++) 
    { 
     storage[i] = d.getNewRoll(); 
    } 
    int result = 0; 
    for (int i=0; i<storage.length; i++) { result += storage[i]; } 

    } 

    public void generateAbilityScoresMethod1() 
    { 

    strRolls = new int[3]; 
    dexRolls = new int[3]; 
    conRolls = new int[3]; 
    intRolls = new int[3]; 
    wisRolls = new int[3]; 
    charRolls = new int[3]; 

    for (int i = 0; i < 3; i++) 
    { 
     strength  = getScore(dice.Dice.D6, strRolls); 
     dexterity  = getScore(dice.Dice.D6, dexRolls); 
     constitution = getScore(dice.Dice.D6, conRolls); 
     intelligence = getScore(dice.Dice.D6, intRolls); 
     wisdom  = getScore(dice.Dice.D6, wisRolls); 
     charisma  = getScore(dice.Dice.D6, charRolls); 

    } 

    } 

    public int getStrength() 
    { 
    return strength; 
    } 

    private void printArrayRolls(String label, int[] rolls) 
    { 
    for (int i=0; i < rolls.length; i++) 
    { 
     System.out.println(label + ": roll " + rolls[i]); 
    } 
    } 

    public void printStrRolls() 
    { 
    printArrayRolls("Str", strRolls); 
    } 

    public int getDexterity() 
    { 
    return dexterity; 
    } 

    public void printDexRolls() 
    { 
    printArrayRolls("Dex", dexRolls); 
    } 

    public int getConsitution() 
    { 
    return constitution; 
    } 

    public void printConRolls() 
    { 
    printArrayRolls("Con", conRolls); 
    } 

    public int getIntelligence() 
    { 
    return intelligence; 
    } 

    public void printIntRolls() 
    { 
    printArrayRolls("Int", intRolls); 
    } 

    public int getWisdom() 
    { 
    return wisdom; 
    } 

    public void printWisRolls() 
    { 
    printArrayRolls("Wis", wisRolls); 
    } 

    public int getCharisma() 
    { 
    return charisma; 
    } 

    public void printCharRolls() 
    { 
    printArrayRolls("Char", charRolls); 
    } 

    public void printAbilities() 
    { 
    System.out.println("Str = " + getStrength()); 
    System.out.println("Dex = " + getDexterity()); 
    System.out.println("Con = " + getConsitution()); 
    System.out.println("Int = " + getIntelligence()); 
    System.out.println("Wis = " + getWisdom()); 
    System.out.println("Char = " + getCharisma()); 
    } 

} 
+0

誰會低估這種關懷來解釋原因? – arcy 2013-04-27 13:37:36

0

這是少代碼;它在CPU使用或內存使用方面並不是「更高效」,但是使得代碼更短以便寫入和讀取。我認爲骰子卷是隨機的,不管他們的順序如何。

package example; 

public class PlayerCharacter 
{ 

    int strength, dexterity, constitution, intelligence, wisdom, charisma; 
    int[] strRolls, dexRolls, conRolls, intRolls, wisRolls, charRolls; 

    private int getScore(Dice d, int[] storage) 
    { 
    for (int i=0; i<storage.length; i++) 
    { 
     storage[i] = d.getNewRoll(); 
    } 
    int result = 0; 
    for (int i=0; i<storage.length; i++) { result += storage[i]; } 

    } 

    public void generateAbilityScoresMethod1() 
    { 

    strRolls = new int[3]; 
    dexRolls = new int[3]; 
    conRolls = new int[3]; 
    intRolls = new int[3]; 
    wisRolls = new int[3]; 
    charRolls = new int[3]; 

    for (int i = 0; i < 3; i++) 
    { 
     strength  = getScore(dice.Dice.D6, strRolls); 
     dexterity  = getScore(dice.Dice.D6, dexRolls); 
     constitution = getScore(dice.Dice.D6, conRolls); 
     intelligence = getScore(dice.Dice.D6, intRolls); 
     wisdom  = getScore(dice.Dice.D6, wisRolls); 
     charisma  = getScore(dice.Dice.D6, charRolls); 

    } 

    } 

    public int getStrength() 
    { 
    return strength; 
    } 

    private void printArrayRolls(String label, int[] rolls) 
    { 
    for (int i=0; i < rolls.length; i++) 
    { 
     System.out.println(label + ": roll " + rolls[i]); 
    } 
    } 

    public void printStrRolls() 
    { 
    printArrayRolls("Str", strRolls); 
    } 

    public int getDexterity() 
    { 
    return dexterity; 
    } 

    public void printDexRolls() 
    { 
    printArrayRolls("Dex", dexRolls); 
    } 

    public int getConsitution() 
    { 
    return constitution; 
    } 

    public void printConRolls() 
    { 
    printArrayRolls("Con", conRolls); 
    } 

    public int getIntelligence() 
    { 
    return intelligence; 
    } 

    public void printIntRolls() 
    { 
    printArrayRolls("Int", intRolls); 
    } 

    public int getWisdom() 
    { 
    return wisdom; 
    } 

    public void printWisRolls() 
    { 
    printArrayRolls("Wis", wisRolls); 
    } 

    public int getCharisma() 
    { 
    return charisma; 
    } 

    public void printCharRolls() 
    { 
    printArrayRolls("Char", charRolls); 
    } 

    public void printAbilities() 
    { 
    System.out.println("Str = " + getStrength()); 
    System.out.println("Dex = " + getDexterity()); 
    System.out.println("Con = " + getConsitution()); 
    System.out.println("Int = " + getIntelligence()); 
    System.out.println("Wis = " + getWisdom()); 
    System.out.println("Char = " + getCharisma()); 
    } 

}