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

Cameron Murray Java Challenge Submission #2

Open
wants to merge 4 commits into
base: main
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
98 changes: 98 additions & 0 deletions Die.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package bcr;
import java.util.Random;

public class Die {
private static int totalDiceCreated = 0; //starts total dice created at 0
private static int totalRolls = 0; //starts total rolls at 0
private static int[] globalNumCounts; // creates int for the # of times a # has been rolled

private int numSides; //int that gives number of sides of a die
private int lastRoll; //reads last roll

public Die() {
this(6); //default die, 6 sides
}

public Die(int numSides) {
if (numSides <= 1) {
throw new IllegalArgumentException("Number of sides must be greater than 1"); //makes sure dice have at least 2 sides
}
this.numSides = numSides;
totalDiceCreated++;
}

public int numSides() {
return numSides; //method that returns numSides
}

public int roll() { //method that rolls dice
Random random = new Random();
lastRoll = random.nextInt(numSides) + 1;
globalNumCounts[lastRoll]++;
totalRolls++;
return lastRoll;
}

public int readLastRoll() { //method that reads last roll
if (lastRoll == 0) {
return -1;
}
return lastRoll;
}

public static int totalCreated() {
return totalDiceCreated; //returns amount of dice created
}

public static int totalRolls() {
return totalRolls;
}

public static int numCount(int number) {
if (number < 1 || number > globalNumCounts.length - 1) {
throw new IllegalArgumentException("Invalid number"); //if '1' is entered, returns "invalid number" because who wants a 1-sided die
}
return globalNumCounts[number]; //returns times each num was rolled
}

public static void main(String[] args) { //ADD EXTRA DICE HERE
Die sixSidedDie = new Die();
Die eightSidedDie = new Die(8);
Die twelveSidedDie = new Die(12);
Die sixteenSidedDie = new Die (16);

//if you want to add another die, follow this:
//int maxNumSides = Math.max(firstDie.numSides(), Math.max(secondDie.numSides(), Math.max(thirdDie.numSides(), Math.max(fourthDie.numSides(), fifthDie.numSides())))) **assuming there are 5 dice
//please go in ascending order going down, ex:
//Die twoSidedDie = new Die (2);
//Die threeSidedDie = new Die (3);
//etc.

int maxNumSides = Math.max(sixSidedDie.numSides(), Math.max(eightSidedDie.numSides(), Math.max(twelveSidedDie.numSides(), sixteenSidedDie.numSides())));
globalNumCounts = new int[maxNumSides + 1];

for (int i = 0; i < 70; i++) {
sixSidedDie.roll();
eightSidedDie.roll();
twelveSidedDie.roll();
sixteenSidedDie.roll();
}
//make sure to add fifth die above too (if adding 5th)

System.out.println("Total dice created: " + Die.totalCreated()); //prints total dice created
System.out.println("Total rolls: " + Die.totalRolls); //prints total rolls performed
System.out.println(""); //adds line of space (for easier visibility)

System.out.println("Last roll (6-sided die): " + sixSidedDie.readLastRoll()); //last roll (of the 70) for 6-sided
System.out.println("Last roll (8-sided die): " + eightSidedDie.readLastRoll()); //last roll (of the 70) for 8-sided
System.out.println("Last roll (12-sided die): " + twelveSidedDie.readLastRoll()); //last roll (of the 70) for 12-sided
System.out.println("Last roll (16-sided die): " + sixteenSidedDie.readLastRoll()); //last roll (of the 70) for 16-sided
System.out.println(""); //adds line of space (for easier visibility)

System.out.println("Number of times each number was rolled:"); //prints this statement
System.out.println(""); //adds line of space (for easier visibility)
for (int i = 1; i < globalNumCounts.length; i++) {
System.out.println("Number of " + i + "s rolled: " + numCount(i)); //prints number of times each number (1-16 (16 being highest value of all dice) was rolled)
}
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Java Challenge submission for BCR
16 changes: 16 additions & 0 deletions dieRoll.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package bcr;
import java.lang.Math;
public class dieRoll{
public static void main(String[] args) {
//below sets a range for the random, which, on a standard 6-sided die, is 1-6
int min = 1;
int max = 6;
int range = max - min + 1;

//below gets random value 20 different times and prints out each value along with message and # of times
for (int i = 1; i <= 20; i++){
int roll = (int)(Math.random()*range+min);
System.out.println("Roll #" + i + " = " + roll);
}
}
}