diff --git a/Output.txt b/Output.txt new file mode 100644 index 0000000..41de896 --- /dev/null +++ b/Output.txt @@ -0,0 +1,26 @@ +Loaded:18 +Numbered:4 +Fudged:-1 +Fudged:0 +Fudged:-1 +----------END OF SET------------ +Loaded:1 +Numbered:6 +Fudged:1 +Numbered:15 +Numbered:5 +----------END OF SET------------ +Fudged:0 +Numbered:6 +Loaded:8 +----------END OF SET------------ +Numbered:2 +Numbered:2 +Numbered:5 +Numbered:2 +----------END OF SET------------ +Fudged:1 +Loaded:6 +Loaded:4 +Loaded:2 +----------END OF SET------------ diff --git a/dice.txt b/dice.txt index 4fcb67d..027d506 100644 --- a/dice.txt +++ b/dice.txt @@ -1,7 +1,3 @@ -#### This file describes handfuls of dice you need to calculate -#### Legend eg. d6 fudge dice look like 6:fudge -#### 20 numbered - 20:numbered -#### Loaded 10 sided with 6 loaded - 10:loaded:6 20:loaded:6 10:numbered 6:fudge 10:fudge 20:fudge 4:loaded:1 10:numbered 6:fudge 100:numbered 5:numbered 8:fudge 8:numbered 8:loaded:4 diff --git a/nbproject/private/private.properties b/nbproject/private/private.properties index 8cf3a2f..29be7c1 100644 --- a/nbproject/private/private.properties +++ b/nbproject/private/private.properties @@ -1,2 +1,2 @@ compile.on.save=true -user.properties.file=/Users/samuraipanzer/Library/Application Support/NetBeans/8.0.2/build.properties +user.properties.file=C:\\Users\\Tehold\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml new file mode 100644 index 0000000..94b8c03 --- /dev/null +++ b/nbproject/private/private.xml @@ -0,0 +1,12 @@ + + + + + + file:/C:/Users/Tehold/Documents/GitHub/CPT-163-27-F2015-Assignment-9-Files-Strings/src/assignment/pkg9/DiceReaderTest.java + file:/C:/Users/Tehold/Documents/GitHub/CPT-163-27-F2015-Assignment-9-Files-Strings/src/assignment/pkg9/DiceFileReader.java + file:/C:/Users/Tehold/Documents/GitHub/CPT-163-27-F2015-Assignment-9-Files-Strings/src/dnddiceroller/DiceTower.java + file:/C:/Users/Tehold/Documents/GitHub/CPT-163-27-F2015-Assignment-9-Files-Strings/src/dnddiceroller/DnDDiceRoller.java + + + diff --git a/nbproject/project.properties b/nbproject/project.properties index 32e13a7..cdbbff4 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -53,7 +53,7 @@ javadoc.splitindex=true javadoc.use=true javadoc.version=false javadoc.windowtitle= -main.class=assignment.pkg9.DiceReaderDriver +main.class=dnddiceroller.DnDDiceRoller manifest.file=manifest.mf meta.inf.dir=${src.dir}/META-INF mkdist.disabled=false diff --git a/src/assignment/pkg9/DiceFileReader.java b/src/assignment/pkg9/DiceFileReader.java index 5d009ce..a8e2270 100644 --- a/src/assignment/pkg9/DiceFileReader.java +++ b/src/assignment/pkg9/DiceFileReader.java @@ -20,7 +20,7 @@ public class DiceFileReader { private String filePath; private Scanner input = null; - private List lines; + private ArrayList lines; /* Use this with @@ -39,12 +39,17 @@ public DiceFileReader(String filePath) { } } - public List getLines(){ + public ArrayList getLines(){ return this.lines; } public void read(){ this.lines = new ArrayList(); // Put you read logic and populate lines + while(this.input.hasNext()) + { + String line = this.input.nextLine(); + this.lines.add(line); + } } } diff --git a/src/dnddiceroller/DiceTower.java b/src/dnddiceroller/DiceTower.java new file mode 100644 index 0000000..3e02351 --- /dev/null +++ b/src/dnddiceroller/DiceTower.java @@ -0,0 +1,53 @@ +package dnddiceroller; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +/** + * Dice Tower. + * A Dice Tower is a tool used by serious gamers use to roll many dice at once. + * It looks like this https://www.miniaturescenery.com/Images/PortableDiceTowerLarge.jpg + * An instance of a dice tower is defined by the number panels it contains to help + * provide a more regular distribution of die values. The die bounce from panel to + * panel until they exit the dice tower at the bottom tray. + * A dice tower will accept a collection of dice and reports their results when + * they reach the tray at the bottom + * @author Paul Scarrone + */ +public class DiceTower { + private final int PANEL_COUNT = 3; + private int tray; + private List dice; + + public DiceTower() { + this.dice = new ArrayList(); + this.tray = 0; + } + + public DiceTower(List dice) { + this.dice = dice; + this.tray = 0; + } + + public void dropDice(){ + for(int sides = 0; sides < this.PANEL_COUNT; sides++){ + for(Die droppedDie: this.dice){ + droppedDie.roll(); + } + } + } + + /** + * Returns the value of all dice in the tray + * @return Tray Value + */ + public int trayValue(){ + this.tray = 0; + for(Die die: dice){ + this.tray += die.value(); + } + return this.tray; + } + +} diff --git a/src/dnddiceroller/Die.java b/src/dnddiceroller/Die.java new file mode 100644 index 0000000..e18233e --- /dev/null +++ b/src/dnddiceroller/Die.java @@ -0,0 +1,29 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package dnddiceroller; + +/** + * + * @author Tehold + */ +public interface Die { + /** + * Returns value of a Die + * @return integer Value of Die + */ + public int value(); + /** + * Returns name of Die + * @return type of Die + */ + public String name(); + /** + * Roll method to simulate rolling a Die + * And setting value to a random integer + */ + public void roll(); + +} diff --git a/src/dnddiceroller/DnDDiceRoller.java b/src/dnddiceroller/DnDDiceRoller.java new file mode 100644 index 0000000..1d406a1 --- /dev/null +++ b/src/dnddiceroller/DnDDiceRoller.java @@ -0,0 +1,85 @@ +package dnddiceroller; + +import assignment.pkg9.DiceFileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; + + +/** + * + * @author Paul Scarrone + */ +public class DnDDiceRoller { + + /** + * Try out your dies and dice tower + * @param args the command line arguments + */ + public static void main(String[] args) { + //ArrayList that will hold our dice + ArrayList dieList = new ArrayList(); + + // Send file to DiceFileReader and have it process the file + // Then assign that information to the lines ArrayList; + DiceFileReader fileReader = new DiceFileReader("dice.txt"); + fileReader.read(); + ArrayList lines = fileReader.getLines(); + + // Access each line one at a time + for (String singleLine : lines) + { + // Split line at spaces + String[] spaceTokens = singleLine.split(" "); + // Access each die information one at a time + for (String dieTokens: spaceTokens) + { + // Split line at colons + String[] colonTokens = dieTokens.split(":"); + // Declare variables that will be used to create different Dies + Integer sides; + Integer loadedSide; + // Switch statement based on type of die that creates a Die in the + // ArrayList + switch (colonTokens[1]) { + case "numbered": + sides = Integer.parseInt(colonTokens[0]); + dieList.add(new NumberedDie(sides)); + break; + case "fudge": + sides = Integer.parseInt(colonTokens[0]); + dieList.add(new FudgedDie(sides)); + break; + default: + sides = Integer.parseInt(colonTokens[0]); + loadedSide = Integer.parseInt(colonTokens[2]); + dieList.add(new LoadedDie(sides, loadedSide)); + break; + } // End Switch that Assigns Die ArrayList + } // End For that breaks each token at colons + + // Create Dice tower and give it the Array List and have it drop dice + DiceTower diceTower = new DiceTower(dieList); + diceTower.dropDice(); + + // Create a FileWriter object to save output + try{ + FileWriter rolledDice = new FileWriter("Output.txt", true); + PrintWriter outputFile = new PrintWriter(rolledDice); + // Step Through each Die in Array and Output info to file + for(Die die: dieList){ + outputFile.println(die.name() + ":" + die.value()); + } + // Close file and empty ArrayList for line of dice + outputFile.println("----------END OF SET------------"); + rolledDice.close(); + dieList.removeAll(dieList); + } + catch(IOException e) + { + System.out.println(e.getMessage()); + } + } // End For that reads each line + } +} diff --git a/src/dnddiceroller/FudgedDie.java b/src/dnddiceroller/FudgedDie.java new file mode 100644 index 0000000..659128e --- /dev/null +++ b/src/dnddiceroller/FudgedDie.java @@ -0,0 +1,57 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package dnddiceroller; +import java.util.Random; +/** + * Fudged Die class that implements Die interface + * When created the user can give the Die as many sides + * as they want. The method doesn't care and returns -1, 0, or 1. + * @author Tehold + */ +public class FudgedDie implements Die{ + private int value; + private int sides; + private final String name = "Fudged"; + + /** + * Default Constructor + */ + public FudgedDie() { + this.value = 0; + this.sides = 3; + } + /** + * Constructor with Parameter for sides to make the user feel good + * @param sides + */ + public FudgedDie(int sides) { + this.sides = sides; + this.value = 0; + } + /** + * Returns the value of a FudgeDie + * @return Integer Value of Die + */ + @Override + public int value() { + return this.value; + } + /** + * Returns name of Die + * @return String Fudged + */ + @Override + public String name() { + return this.name; + } + /** + * Roll Method to determine if the FudgedDie is -1, 0, or 1. + */ + @Override + public void roll() { + this.value = (new Random()).nextInt(3) - 1; + } +} diff --git a/src/dnddiceroller/LoadedDie.java b/src/dnddiceroller/LoadedDie.java new file mode 100644 index 0000000..7f8271e --- /dev/null +++ b/src/dnddiceroller/LoadedDie.java @@ -0,0 +1,70 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package dnddiceroller; + +import java.util.Random; + +/** + *Loaded Die that favors a specified number when rolling. + * @author Tehold + */ +public class LoadedDie implements Die { + private int value; + private int sides; + private int loadedValue; + private final String name = "Loaded"; + + /** + * Default Constructor for LoadedDie + */ + public LoadedDie(){ + this.sides = 6; + this.value = 1; + this.loadedValue = 6; + } + /** + * Constructor with parameters for number of sides, and desired value + * @param sides + * @param loadedValue + */ + public LoadedDie(int sides, int loadedValue) { + this.sides = sides; + this.loadedValue = loadedValue; + this.value = 1; + } + + /** + * Returns the value of a LoadedDie + * @return integer Value of LoadedDie + */ + @Override + public int value(){ + return this.value; + } + /** + * Returns name of LoadedDie + * @return String Loaded + */ + @Override + public String name() { + return this.name; + } + + /** + * Method to roll the die and assign random value + * If value is not equal to loaded value roll twice more or until + * it is equal to loaded value + */ + @Override + public void roll() { + this.value = (new Random()).nextInt(this.sides) +1; + for(int i = 0; i < 2; i++) { + if(this.value != this.loadedValue) { + this.value = (new Random()).nextInt(this.sides) +1; + } + } + } +} diff --git a/src/dnddiceroller/NumberedDie.java b/src/dnddiceroller/NumberedDie.java new file mode 100644 index 0000000..f7d6aa2 --- /dev/null +++ b/src/dnddiceroller/NumberedDie.java @@ -0,0 +1,55 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package dnddiceroller; + +import java.util.Random; + +/** + * + * @author Tehold + */ +public class NumberedDie implements Die { + private int value; + private int sides; + private final String name = "Numbered"; + /** + * Default constructor for a NumberedDie + */ + public NumberedDie(){ + this.sides = 6; + this.value = 1; + } + /** + * Constructor with parameter for number of sides + * @param sides + */ + public NumberedDie(int sides){ + this.sides = sides; + } + /** + * Returns the value of a NumberedDie + * @return Integer Value of NumberedDie + */ + @Override + public int value(){ + return this.value; + } + /** + * Returns name of Die + * @return String Numbered + */ + @Override + public String name(){ + return this.name; + } + /** + * Roll method to assign random value to NumberedDie + */ + @Override + public void roll(){ + this.value = (new Random()).nextInt(this.sides) +1; + } +}