Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Commit #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Output.txt
Original file line number Diff line number Diff line change
@@ -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------------
4 changes: 0 additions & 4 deletions dice.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#### This file describes handfuls of dice you need to calculate
#### Legend <sides><type> 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
Expand Down
2 changes: 1 addition & 1 deletion nbproject/private/private.properties
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions nbproject/private/private.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/C:/Users/Tehold/Documents/GitHub/CPT-163-27-F2015-Assignment-9-Files-Strings/src/assignment/pkg9/DiceReaderTest.java</file>
<file>file:/C:/Users/Tehold/Documents/GitHub/CPT-163-27-F2015-Assignment-9-Files-Strings/src/assignment/pkg9/DiceFileReader.java</file>
<file>file:/C:/Users/Tehold/Documents/GitHub/CPT-163-27-F2015-Assignment-9-Files-Strings/src/dnddiceroller/DiceTower.java</file>
<file>file:/C:/Users/Tehold/Documents/GitHub/CPT-163-27-F2015-Assignment-9-Files-Strings/src/dnddiceroller/DnDDiceRoller.java</file>
</group>
</open-files>
</project-private>
2 changes: 1 addition & 1 deletion nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/assignment/pkg9/DiceFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class DiceFileReader {
private String filePath;
private Scanner input = null;
private List<String> lines;
private ArrayList<String> lines;

/*
Use this with
Expand All @@ -39,12 +39,17 @@ public DiceFileReader(String filePath) {
}
}

public List<String> getLines(){
public ArrayList<String> getLines(){
return this.lines;
}

public void read(){
this.lines = new ArrayList();
// Put you read logic and populate lines
while(this.input.hasNext())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where you would test if a line has valid input

{
String line = this.input.nextLine();
this.lines.add(line);
}
}
}
53 changes: 53 additions & 0 deletions src/dnddiceroller/DiceTower.java
Original file line number Diff line number Diff line change
@@ -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<Die> 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;
}

}
29 changes: 29 additions & 0 deletions src/dnddiceroller/Die.java
Original file line number Diff line number Diff line change
@@ -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();

}
85 changes: 85 additions & 0 deletions src/dnddiceroller/DnDDiceRoller.java
Original file line number Diff line number Diff line change
@@ -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<Die> 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<String> 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
}
}
57 changes: 57 additions & 0 deletions src/dnddiceroller/FudgedDie.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
70 changes: 70 additions & 0 deletions src/dnddiceroller/LoadedDie.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
Loading