From 96d1980ae4b740f9881b526bafd00c029a899b49 Mon Sep 17 00:00:00 2001 From: payneda Date: Thu, 12 Nov 2015 15:11:07 -0500 Subject: [PATCH 1/3] New changes Wip Change DnDDiceRoller.java now has for loops that tokenize the file for output. --- nbproject/private/private.properties | 2 +- nbproject/private/private.xml | 7 ++ nbproject/project.properties | 2 +- .../DiceFileReader.java | 43 +++++++++-- .../DiceReaderTest.java | 4 +- src/assignment/dnddiceroller/DiceTower.java | 47 ++++++++++++ src/assignment/dnddiceroller/Die.java | 18 +++++ .../dnddiceroller/DnDDiceRoller.java | 74 +++++++++++++++++++ src/assignment/dnddiceroller/FudgeDice.java | 33 +++++++++ src/assignment/dnddiceroller/LoadedDice.java | 54 ++++++++++++++ src/assignment/dnddiceroller/NumberedDie.java | 32 ++++++++ 11 files changed, 306 insertions(+), 10 deletions(-) create mode 100644 nbproject/private/private.xml rename src/assignment/{pkg9 => dnddiceroller}/DiceFileReader.java (54%) rename src/assignment/{pkg9 => dnddiceroller}/DiceReaderTest.java (88%) create mode 100644 src/assignment/dnddiceroller/DiceTower.java create mode 100644 src/assignment/dnddiceroller/Die.java create mode 100644 src/assignment/dnddiceroller/DnDDiceRoller.java create mode 100644 src/assignment/dnddiceroller/FudgeDice.java create mode 100644 src/assignment/dnddiceroller/LoadedDice.java create mode 100644 src/assignment/dnddiceroller/NumberedDie.java diff --git a/nbproject/private/private.properties b/nbproject/private/private.properties index 8cf3a2f..267353f 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\\Inspiron\\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..6807a2b --- /dev/null +++ b/nbproject/private/private.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/nbproject/project.properties b/nbproject/project.properties index 32e13a7..5038070 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=assignment.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/dnddiceroller/DiceFileReader.java similarity index 54% rename from src/assignment/pkg9/DiceFileReader.java rename to src/assignment/dnddiceroller/DiceFileReader.java index 5d009ce..fd07cba 100644 --- a/src/assignment/pkg9/DiceFileReader.java +++ b/src/assignment/dnddiceroller/DiceFileReader.java @@ -3,10 +3,12 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package assignment.pkg9; +package assignment.dnddiceroller; +import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; +import java.io.FileReader; import java.util.ArrayList; import java.util.List; import java.util.Scanner; @@ -39,12 +41,39 @@ public DiceFileReader(String filePath) { } } - public List getLines(){ + + + public DiceFileReader(String filePath, List lines) { + this.filePath = filePath; + this.lines = lines; + } + public List getLines(){ return this.lines; - } + } - public void read(){ - this.lines = new ArrayList(); - // Put you read logic and populate lines + public void read(){ + this.lines = new ArrayList(); + + try { + while(input.hasNextLine()) { + String temp = input.nextLine(); + if(temp.charAt(0) != '#'){ + lines.add(temp); + } + } + } + catch (Exception e) + { + System.out.println(e.getLocalizedMessage()); + } + } + + } -} + + + + + + + diff --git a/src/assignment/pkg9/DiceReaderTest.java b/src/assignment/dnddiceroller/DiceReaderTest.java similarity index 88% rename from src/assignment/pkg9/DiceReaderTest.java rename to src/assignment/dnddiceroller/DiceReaderTest.java index 72987c9..79c8a60 100644 --- a/src/assignment/pkg9/DiceReaderTest.java +++ b/src/assignment/dnddiceroller/DiceReaderTest.java @@ -3,7 +3,9 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package assignment.pkg9; +package assignment.dnddiceroller; + +import assignment.dnddiceroller.DiceFileReader; /** * diff --git a/src/assignment/dnddiceroller/DiceTower.java b/src/assignment/dnddiceroller/DiceTower.java new file mode 100644 index 0000000..229a296 --- /dev/null +++ b/src/assignment/dnddiceroller/DiceTower.java @@ -0,0 +1,47 @@ +package assignment.dnddiceroller; + +import java.util.ArrayList; +import java.util.List; + +/** + * 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 { + final int PANEL_COUNT = 3; + List dice; + + public DiceTower() { + this.dice = new ArrayList(); + } + + public DiceTower(List dice) { + this.dice = dice; + } + + public void dropDice() { + for (int i = 0; i < PANEL_COUNT; i++) { + for (int j = 0; j < dice.size(); j++) { + Die temp = dice.get(j); + temp.roll(); + } + } + } + + public int trayValue() { + int totalValue = 0; + for (int i = 0; i < dice.size(); i++) { + Die temp = dice.get(i); + totalValue = totalValue + temp.value(); + + } + return totalValue; + } +} diff --git a/src/assignment/dnddiceroller/Die.java b/src/assignment/dnddiceroller/Die.java new file mode 100644 index 0000000..00ace8e --- /dev/null +++ b/src/assignment/dnddiceroller/Die.java @@ -0,0 +1,18 @@ +/* + * 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 assignment.dnddiceroller; + +/** + * + * @author Inspiron + */ +public abstract interface Die { + + void roll(); + int value(); + + +} diff --git a/src/assignment/dnddiceroller/DnDDiceRoller.java b/src/assignment/dnddiceroller/DnDDiceRoller.java new file mode 100644 index 0000000..54096d0 --- /dev/null +++ b/src/assignment/dnddiceroller/DnDDiceRoller.java @@ -0,0 +1,74 @@ +package assignment.dnddiceroller; + +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +/** + * + * @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) { + String File = "dice.txt"; + List DiceList; + DiceFileReader DiceFile = new DiceFileReader(File); + + DiceFile.read(); + DiceList = DiceFile.getLines(); + for(int i = 0; i < DiceList.size(); i++) { + + List dice = new ArrayList(); + + //System.out.println("Line " + (i+1) + ": "); + for (StringTokenizer stringTokenizer = new StringTokenizer(DiceList.get(i)," "); stringTokenizer.hasMoreTokens();) { + int sides; + String type; + int loaded; + Die temp; + + String token = stringTokenizer.nextToken(); + + //System.out.println(token); + + StringTokenizer dieTokens = new StringTokenizer(token,":"); + + sides = Integer.parseInt(dieTokens.nextToken()); + type = dieTokens.nextToken(); + + if(type.equals("loaded")) + { + loaded = Integer.parseInt(dieTokens.nextToken()); + System.out.println(sides + " " + loaded); + + } + else if(type.equals("fudge")) + { + System.out.println(sides + "fudge"); + } + if(type.equals("numbered")) + { + System.out.println(sides + "numbered"); + } + + + + //Use type to determine what type of die to make + //Throw die into dice + } + + //Create dice tower using dice + //roll dice in dice tower + //output results + + } + + + } +} + diff --git a/src/assignment/dnddiceroller/FudgeDice.java b/src/assignment/dnddiceroller/FudgeDice.java new file mode 100644 index 0000000..a501401 --- /dev/null +++ b/src/assignment/dnddiceroller/FudgeDice.java @@ -0,0 +1,33 @@ +/* + * 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 assignment.dnddiceroller; + +import java.util.Random; + +/** + * + * @author Inspiron + */ +public class FudgeDice implements Die{ + private int die; + private int value; + private Random FudgeRand = new Random(); + public FudgeDice(int die){ + this.die = die; + } + + + @Override + public void roll() { + this.value = FudgeRand.nextInt(3)-1; + } + + @Override + public int value() { + return this.value; + } + +} diff --git a/src/assignment/dnddiceroller/LoadedDice.java b/src/assignment/dnddiceroller/LoadedDice.java new file mode 100644 index 0000000..fc76410 --- /dev/null +++ b/src/assignment/dnddiceroller/LoadedDice.java @@ -0,0 +1,54 @@ +/* + * 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 assignment.dnddiceroller; + +import java.util.Random; + +/** + * + * @author Inspiron + */ +public class LoadedDice implements Die{ + private final int WEIGHT = 2; + private int die; + private int value; + private int loadedValue; + private Random randValue = new Random(); + public LoadedDice(int die){ + this.die = die; + this.loadedValue = die; + } + + public LoadedDice(int die, int loadedValue){ + this.die = die; + this.loadedValue = loadedValue; + } + + @Override + public void roll() { + int tempValue = 0; + for (int i = 0; i < WEIGHT && this.value != loadedValue; i++) { + tempValue = randValue.nextInt(die)+1; + if (tempValue == loadedValue){ + this.value = tempValue; + } + + } + + if (this.value != loadedValue){ + this.value = tempValue; + } + + + + } + + @Override + public int value() { + return this.value; + } + +} diff --git a/src/assignment/dnddiceroller/NumberedDie.java b/src/assignment/dnddiceroller/NumberedDie.java new file mode 100644 index 0000000..bafebd6 --- /dev/null +++ b/src/assignment/dnddiceroller/NumberedDie.java @@ -0,0 +1,32 @@ +package assignment.dnddiceroller; + +import java.util.Random; + +/** + * A NumberedDie is a many sided object that when rolled provides a random value from + 1 through the number of sides on the object. Some dice are 6 sided and have + * the numbers 1-6 on them. Some dice are 20 sided with the numbers 1-20 on them. + * Others are called fudge dice and have the values of -1 0 or +1 + * @author Paul Scarrone + */ +public class NumberedDie implements Die{ + private int die; + private int value; + private Random dieSide = new Random(); + + public NumberedDie(int die) { + this.die = die; + +} + + @Override + public void roll() { + value = dieSide.nextInt(die) + 1;// dieSide is equal to 0 thri (die -1). + //System.out.println(value); + } + + @Override + public int value(){ + return this.value; + } +} \ No newline at end of file From 0bcdfe4369b3cd172b1e7fe16b09bdd1fdfb3a0b Mon Sep 17 00:00:00 2001 From: payneda Date: Thu, 12 Nov 2015 15:54:04 -0500 Subject: [PATCH 2/3] New Changes Dice are now sent to there class's. Made new dice tower. --- .../dnddiceroller/DnDDiceRoller.java | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/assignment/dnddiceroller/DnDDiceRoller.java b/src/assignment/dnddiceroller/DnDDiceRoller.java index 54096d0..29e6e3e 100644 --- a/src/assignment/dnddiceroller/DnDDiceRoller.java +++ b/src/assignment/dnddiceroller/DnDDiceRoller.java @@ -26,11 +26,12 @@ public static void main(String[] args) { List dice = new ArrayList(); //System.out.println("Line " + (i+1) + ": "); - for (StringTokenizer stringTokenizer = new StringTokenizer(DiceList.get(i)," "); stringTokenizer.hasMoreTokens();) { - int sides; + for (StringTokenizer stringTokenizer = new StringTokenizer(DiceList.get(i) + ," "); stringTokenizer.hasMoreTokens();) { + int die; String type; int loaded; - Die temp; + String token = stringTokenizer.nextToken(); @@ -38,37 +39,32 @@ public static void main(String[] args) { StringTokenizer dieTokens = new StringTokenizer(token,":"); - sides = Integer.parseInt(dieTokens.nextToken()); + die = Integer.parseInt(dieTokens.nextToken()); type = dieTokens.nextToken(); if(type.equals("loaded")) { loaded = Integer.parseInt(dieTokens.nextToken()); - System.out.println(sides + " " + loaded); - + LoadedDice loadedValue = new LoadedDice(die,loaded); + } else if(type.equals("fudge")) { - System.out.println(sides + "fudge"); + FudgeDice fudgeValue = new FudgeDice(die); } if(type.equals("numbered")) { - System.out.println(sides + "numbered"); - } - + NumberedDie numberedValue = new NumberedDie(die); - - //Use type to determine what type of die to make - //Throw die into dice + } } + + DiceTower diceTower = new DiceTower(dice); //Create dice tower using dice //roll dice in dice tower //output results - } - - + } -} - +} \ No newline at end of file From 3f948b8e4e6c4e3cc9bd3f1f67165436200f1836 Mon Sep 17 00:00:00 2001 From: payneda Date: Tue, 17 Nov 2015 19:21:29 -0500 Subject: [PATCH 3/3] New changes final version. Added an output file. --- Dice Output.txt | 10 ++ nbproject/private/private.xml | 6 +- .../dnddiceroller/DnDDiceRoller.java | 124 +++++++++++------- 3 files changed, 92 insertions(+), 48 deletions(-) create mode 100644 Dice Output.txt diff --git a/Dice Output.txt b/Dice Output.txt new file mode 100644 index 0000000..df5f293 --- /dev/null +++ b/Dice Output.txt @@ -0,0 +1,10 @@ +Line 1: 20:loaded:6, 10:numbered, 6:fudge, 10:fudge, 20:fudge +Line 1 total: 1 +Line 2: 4:loaded:1, 10:numbered, 6:fudge, 100:numbered, 5:numbered +Line 2 total: 20 +Line 3: 8:fudge, 8:numbered, 8:loaded:4 +Line 3 total: 3 +Line 4: 10:numbered, 10:numbered, 10:numbered, 10:numbered +Line 4 total: 17 +Line 5: 6:fudge, 6:loaded:2, 6:loaded:4, 6:loaded:6 +Line 5 total: 10 diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml index 6807a2b..2db379c 100644 --- a/nbproject/private/private.xml +++ b/nbproject/private/private.xml @@ -2,6 +2,10 @@ - + + file:/C:/Users/Inspiron/Documents/GitHub/CPT-163-27-F2015-Assignment-9-Files-Strings/src/assignment/dnddiceroller/DiceFileReader.java + file:/C:/Users/Inspiron/Documents/GitHub/CPT-163-27-F2015-Assignment-9-Files-Strings/src/assignment/dnddiceroller/DnDDiceRoller.java + file:/C:/Users/Inspiron/Documents/GitHub/CPT-163-27-F2015-Assignment-9-Files-Strings/src/assignment/dnddiceroller/LoadedDice.java + diff --git a/src/assignment/dnddiceroller/DnDDiceRoller.java b/src/assignment/dnddiceroller/DnDDiceRoller.java index 29e6e3e..e6c936f 100644 --- a/src/assignment/dnddiceroller/DnDDiceRoller.java +++ b/src/assignment/dnddiceroller/DnDDiceRoller.java @@ -1,5 +1,9 @@ package assignment.dnddiceroller; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; @@ -18,53 +22,79 @@ public static void main(String[] args) { String File = "dice.txt"; List DiceList; DiceFileReader DiceFile = new DiceFileReader(File); + Die temp; + try { + File diceOutput = new File("Dice Output.txt"); + PrintWriter output = new PrintWriter(diceOutput); + - DiceFile.read(); - DiceList = DiceFile.getLines(); - for(int i = 0; i < DiceList.size(); i++) { - - List dice = new ArrayList(); - - //System.out.println("Line " + (i+1) + ": "); - for (StringTokenizer stringTokenizer = new StringTokenizer(DiceList.get(i) - ," "); stringTokenizer.hasMoreTokens();) { - int die; - String type; - int loaded; + DiceFile.read(); + DiceList = DiceFile.getLines(); + + for(int i = 0; i < DiceList.size(); i++) { + + List dice = new ArrayList(); + + output.print("Line " + (i+1) + ": "); + for (StringTokenizer stringTokenizer = new StringTokenizer(DiceList.get(i) + ," "); stringTokenizer.hasMoreTokens();) { + int die; + String type; + int loaded; + + String token = stringTokenizer.nextToken(); + + output.print(token); + + StringTokenizer dieTokens = new StringTokenizer(token,":"); + + die = Integer.parseInt(dieTokens.nextToken()); + type = dieTokens.nextToken(); + + if (type.equals("loaded")) + { + loaded = Integer.parseInt(dieTokens.nextToken()); + temp = new LoadedDice(die,loaded); + } + else if (type.equals("fudge")) + { + temp = new FudgeDice(die); + } + else if (type.equals("numbered")) + { + temp = new NumberedDie(die); + } + else + { + throw new Exception("Invalid die type found on line " + (i+1)); + } + + dice.add(temp); + + if(stringTokenizer.hasMoreElements()) + { + output.print(", "); + } + else + output.println(); + + } + //Create dice tower using dice + //roll dice in dice tower + //output results + DiceTower tower = new DiceTower(dice); + tower.dropDice(); + output.println("Line " + (i+1) + " total: " + tower.trayValue()); - - String token = stringTokenizer.nextToken(); - - //System.out.println(token); - - StringTokenizer dieTokens = new StringTokenizer(token,":"); - - die = Integer.parseInt(dieTokens.nextToken()); - type = dieTokens.nextToken(); - - if(type.equals("loaded")) - { - loaded = Integer.parseInt(dieTokens.nextToken()); - LoadedDice loadedValue = new LoadedDice(die,loaded); - - } - else if(type.equals("fudge")) - { - FudgeDice fudgeValue = new FudgeDice(die); - } - if(type.equals("numbered")) - { - NumberedDie numberedValue = new NumberedDie(die); - - } - } - - DiceTower diceTower = new DiceTower(dice); - - //Create dice tower using dice - //roll dice in dice tower - //output results - } - - } + + } + + output.close(); + + } + catch (Exception e) + { + System.out.println(e.getMessage()); + } + } } \ No newline at end of file