I have made a lot of progress on my DnD game project!, but I am stumped on my outputting process. The purpose of this program is to simulate "rolling a handful of dice". I decided to accomplish this by using an object array list and creating a while loop that creates a new object and adds it to such array list. My main issue however is figuring out how to store each number rolled in each object such as five D20 dice, so object would be the D20, but then printing out number rolled.
My code so far:
// 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 + ", ");
}
}
//Main.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);
}
}
Side notes: In addition to not knowing how to store/print each number rolled for each type of dice, I am unsure how to give the total score obtained which would be every number rolled added together.
Aucun commentaire:
Enregistrer un commentaire