Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
Centralize RNG
Browse files Browse the repository at this point in the history
I'd still rather implement and use the random.org API for this purpose.
  • Loading branch information
StrangerCoug committed Oct 7, 2023
1 parent 88ff57b commit 6f93bea
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
11 changes: 10 additions & 1 deletion src/main/java/com/github/strangercoug/freecasino/FreeCasino.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,23 @@
import com.github.strangercoug.freecasino.games.model.table.Craps;
import com.github.strangercoug.freecasino.games.model.table.RedDog;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.random.RandomGenerator;

/**
*
* @author Jeffrey Hope <strangercoug@hotmail.com>
*/
class FreeCasino {
public class FreeCasino {
/**
* Backup RNG for offline use. Using the random.org API should be preferred
* to calling this RNG, especially if it is necessary to shuffle a large
* number of cards.
*/
public static final RandomGenerator rng = new SecureRandom();

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean validInput = false;
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/com/github/strangercoug/freecasino/objs/Deck.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.security.SecureRandom;
import java.util.LinkedList;

import static com.github.strangercoug.freecasino.FreeCasino.rng;

/**
*
* @author Jeffrey Hope <strangercoug@hotmail.com>
Expand All @@ -45,7 +47,6 @@ public class Deck {
private final int numDecks;
private final boolean usesBlackJoker;
private final boolean usesRedJoker;
private final SecureRandom rng = new SecureRandom();

public Deck(int numDecks, boolean usesBlackJoker, boolean usesRedJoker) {
cards = new LinkedList<>();
Expand Down Expand Up @@ -82,16 +83,16 @@ public void populateDeck() {
}
}

/* TODO: This is fine for alpha and beta testing, but at a later point I
* would like to be able to detect whether there is an Internet connection and
* use the random.org API to shuffle if possible. If something goes wrong, we
* fall back to this.
*/
public void shuffleDeck() {
/* TODO: This is fine for alpha and beta testing, but at a later point I would
* like to be able to detect whether there is an Internet connection and use
* the random.org API to shuffle if possible. If something goes wrong, we fall
* back to this.
*/
for (int i = cards.size() - 1; i > 0; i--) {
Card temp = cards.get(i);
int j = rng.nextInt(i + 1); /* Without the +1 this becomes a Sattolo shuffle,
* which we don't want */
int j = rng.nextInt(i + 1); /* Without the +1 this becomes a Sattolo
* shuffle, which we don't want */
cards.set(i, cards.get(j));
cards.set(j, temp);
}
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/github/strangercoug/freecasino/objs/Dice.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@

import java.security.SecureRandom;

import static com.github.strangercoug.freecasino.FreeCasino.rng;

/**
*
* @author Jeffrey Hope <strangercoug@hotmail.com>
*/
public class Dice {
private final int[] dieFaces;
private final int sidesPerDie;
private final SecureRandom rng = new SecureRandom();

/**
* Creates a number of dice.
Expand Down Expand Up @@ -112,7 +113,13 @@ public int getTotal() {
* Rolls the dice.
*/
public void rollDice() {
for (int i = 0; i < dieFaces.length; i++)
dieFaces[i] = (rng.nextInt(sidesPerDie) + 1);
/* TODO: This is fine for alpha and beta testing, but at a later point I would
* like to be able to detect whether there is an Internet connection and use
* the random.org API to roll if possible. If something goes wrong, we fall
* back to this.
*/
for (int i = 0; i < dieFaces.length; i++) {
dieFaces[i] = (rng.nextInt(1, sidesPerDie + 1));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
package com.github.strangercoug.freecasino.objs;

import java.security.SecureRandom;
import static com.github.strangercoug.freecasino.FreeCasino.rng;

/**
*
Expand All @@ -39,7 +39,6 @@
public class Wheel {
private final Object[] stops;
private int position;
private final SecureRandom rng = new SecureRandom();

public Wheel(Object[] stops) {
this.stops = stops;
Expand All @@ -51,6 +50,11 @@ public Object getWheelResult() {
}

public void spinWheel() {
/* TODO: This is fine for alpha and beta testing, but at a later point I would
* like to be able to detect whether there is an Internet connection and use
* the random.org API to roll if possible. If something goes wrong, we fall
* back to this.
*/
position = rng.nextInt(stops.length);
}
}

0 comments on commit 6f93bea

Please sign in to comment.