4

プレイヤーキャラクターの作成を自動化するプログラムを作成しています。以下は私のPlayerCharacterクラスです。さまざまな変数に対して多くの操作を繰り返していることに気付きました。

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());
    }

}

同じタスクをより効率的な方法で達成するにはどうすればよいですか?

4

7 に答える 7

0

すべてのプロパティを int[] に保持できます。したがって、強さ、器用さ、体質、知性、知恵、カリスマ性を別々の変数として持つのではなく、

int[] characteristics;

0 は強度になります。1は器用さなどです。

ロール値を保持するには、int[][] が必要です。

于 2013-04-27T13:06:41.297 に答える
-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());
  }

}
于 2013-04-27T13:14:34.953 に答える