我的 DnD 游戏项目取得了很大进展!,但我在输出过程中遇到了困难。这个程序的目的是模拟“掷一把骰子”。我决定通过使用对象数组列表并创建一个 while 循环来完成此操作,该循环创建一个新对象并将其添加到此类数组列表中。 然而,我的主要问题是弄清楚如何存储每个对象中滚动的每个数字,例如五个 D20 骰子,因此对象将是 D20,然后打印出滚动的数字。
到目前为止我的代码:
// Dice.java
import java.util.Random;
public class Dice {
protected int numRolled;
final Random rand = new Random();
public int getNumRolled() {
return numRolled;
}
public void roll() {
numRolled = rand.nextInt(5) + 1;
}
}
}
//VarDice.java
import java.util.Scanner;
public class VarDice extends Dice {
//Scanner scnr = new Scanner(System.in);
int numSides;
int numDice;
int rolledNum;
public void setNumSides(int sides) {
numSides = sides;
}
public int getNumSides() {
return numSides;
}
public void setNumDice(int numOfDice) {
numDice = numOfDice;
}
public int getNumDice() {
return numDice;
}
@Override
public void roll() {
numRolled = rand.nextInt(numSides) + 1;
}
public void printInfo() {
System.out.print(numRolled + ", ");
}
}
//主.java
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
public static void printArrayList(ArrayList<VarDice> diceRolls) {
int i;
for(i = 0; i < diceRolls.size(); ++i) {
System.out.print("D" + diceRolls.get(i).getNumSides() + ":");
diceRolls.get(i).printInfo();
System.out.println();
}
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
//Create boolean play that is true
Boolean play = true;
//Create int numSides and int numDice for how many dice are rolled
int numSides;
int numDice;
int numRolled;
//Create an int variable for results of the rolls (int results = 0;)
int results = 0;
//Create array list of object VarDice
ArrayList<VarDice> diceRolls = new ArrayList<VarDice>();
while (play) {
//Greet user
System.out.println("Enter the number of dice and number of sides (enter 0 to exit):");
VarDice dice = new VarDice();
try {
//Get user input for numSides
numDice = scnr.nextInt();
//Get user input for numDice
numSides = scnr.nextInt();
if (numDice < 0 || numSides < 0) {
throw new Exception ("Invalid input, both should be a positive number");
}
}
//Create catch block for a inputmismatch
catch (InputMismatchException excpt) {
System.out.println("Invalid input.");
break;
}
//Create catch block for all expectations
catch (Exception excpt) {
System.out.println(excpt.getMessage());
break;
}
if (numDice == 0 && numSides == 0) {
play = false;
}
else {
//A for loop is made for number of times a die is rolled according to user input
dice.setNumSides(numSides);
for (int i = 0; i < numDice; ++i) {
//numRolled = dice.roll();
//dice.printInfo();
//results += numRolled;
}
diceRolls.add(dice);
}
}
System.out.println();
System.out.println("Results:");
printArrayList(diceRolls);
System.out.println("Total: " + results);
}
}
旁注:除了不知道如何存储/打印为每种类型的骰子掷出的每个数字之外,我不确定如何给出获得的总分,即每个掷出的数字加在一起。
就个人而言,我会让每个
Die
返回滚动的值,而不是试图维护那个实例“最后”滚动的值。
在我看来,
Die
不“存储”状态,它是一个生成器。
例如...
public interface Die {
public int getNumberOfSides();
public int roll();
}
public abstract class AbstractDie implements Die {
private Random random = new Random();
protected Random getRandom() {
return random;
}
@Override
public int roll() {
return getRandom().nextInt(getNumberOfSides()) + 1;
}
}
public class D20 extends AbstractDie {
@Override
public int getNumberOfSides() {
return 20;
}
}
然后你可以使用类似...
int total = 0;
Die die = new D20();
for (int index = 0; index < 5; index++) {
int roll = die.roll();
System.out.println("[" + index + "] " + roll);
total += roll;
}
System.out.println("Total = " + total);
为什么要这样设计课程?好吧,简单地说,你可以创建一个
Die
的“袋子”,然后将它们全部卷起来......
List<Die> toRoll = new ArrayList<Die>(7);
toRoll.add(new D4());
toRoll.add(new D8());
toRoll.add(new D10());
toRoll.add(new D12());
toRoll.add(new D20());
toRoll.add(new D6());
toRoll.add(new D6());
现在,你可以循环这个并依次滚动每个
Die
而根本不关心 - 因为,真的,你只对结果感兴趣。
这种方法还意味着您为每个需要的
Die
维护一个实例,并继续滚动它们而不会产生错误的结果