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

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
于 2013-04-27T13:02:53.330 回答
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]);
}
于 2013-04-27T13:01:25.747 回答
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、Max HP、Level、XP 等。

于 2013-04-27T13:01:36.350 回答
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 的结果。

于 2013-04-27T13:03:51.893 回答
0

您可以将所有属性保存在 int[] 中。因此,与其将力量、灵巧、体质、智力、智慧、魅力作为单独的变量,不如将其设为一个

int[] characteristics;

那么 0 就是强度;1将是灵巧等等。

要保持滚动值,您需要一个 int[][]。

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

}
于 2013-04-27T13:36:02.200 回答
-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 回答