2 Replies - 9920 Views - Last Post: 07 May 2015 - 11:18 AM

#1 Flukeshot   User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

A Self-Recording Dice Class

Posted 16 March 2014 - 01:29 AM

This is a modular Dice class I built for a game I'm working on, I thought I'd share it with you guys.

In short, this class will construct a single die that records the roll values in a list so they can be accessed later.

The list is maintained for each die individually, meaning that if you wish to search through the history of roll values for multiple dice, you must reference to each specific die. The list can also be reset, and the method to do this has a fail-safe confirmation dialog before it erases the roll history.

import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 * Creates a {@code self-recording die} with N sides. Rolls are stored and can be
 * referenced at a later point in time.
 */
public class SelfRecordingDie {
	
	/**
	 * The number of sides that this die has.
	 */
	private int sides;
	
	/**
	 * The history of rolls made from this die.
	 */
	private ArrayList<Integer> rollHistory = new ArrayList<Integer>();
	
	/**
	 * Creates a default, 6-sided die.
	 */
	public SelfRecordingDie() {
		
		this(6);
	}
	
	/**
	 * Constructor for a custom number of sides.
	 * @param sides - the number of sides this die has.
	 */
	public SelfRecordingDie(int sides) {
		
		this.sides = sides;
	}
	
	/**
	 * Short-hand method, for when you just want to roll.
	 */
	public int roll() {
		
		return roll(false);
	}
	
	/**
	 * "Rolls" the die, generating a pseudo-random result. Optional console output.
	 * @param
	 * consoleOutput - if true, console displays a message and the value of the roll.
	 * @return
	 * A 1 to N representation of the side on which the die has landed, where
	 * N is the number of sides the die has.
	 */
	public int roll(boolean consoleOutput) {
		
		int thisRoll = (int)(Math.random()*sides)+1;
		
		rollHistory.add(thisRoll);
		rollHistory.add(10);
		
		if(consoleOutput==true) {
			System.out.println("The die has been cast, this roll was a " + thisRoll);
		}
		
		return thisRoll;
	}
	
	/**
	 * Searches roll history to find specified roll value.
	 * <blockquote>This method is <i>index-1</i>: {@code getRoll(1)} will return the first roll.</blockquote>
	 * @param rollNumber - the index of the roll.
	 * @return The value of the roll at specified roll index if found. -1 if not.
	 */
	public int getRoll(int rollNumber) {
		
		if(rollNumber < 1 || rollNumber > rollHistory.size()) {
			System.out.println("Roll number " + rollNumber + " doesn't seem to be recorded..");
			return -1;
		}
		
		return rollHistory.get(--rollNumber);
	}
	
	/**
	 * Searches through the roll history for a given value.
	 * <blockquote>This method is <i>index-1</i>: a return value of 1 is the first roll.</blockquote>
	 * @param value - The value whose index(es) you wish to return.
	 * @return An integer array if 1 or more values are matched, or a single element integer array containing the value of -1 if nothing is found.
	 */
	public int[] searchByValue(int value) {
		
		ArrayList<Integer> findings = new ArrayList<Integer>();
		
		for(int i = 0; i < rollHistory.size(); i++) {
			if(rollHistory.get(i)==value) {
				findings.add(i+1);
			}
		}
		
		if(findings.isEmpty()) return new int[] {-1};
		
		int[] output = new int[findings.size()];
		
		for(int i = 0; i < output.length; i++) {
			output[i] = findings.get(i);
		}
		
		return output;
	}
	
	/**
	 * Clears all previous roll history. Comes with confirmation dialog.
	 */
	public void clearRollHistory() {
		
		int choice = JOptionPane.showOptionDialog(
				null, "Are you sure?", 
				"Clearing roll history...", 
				JOptionPane.OK_CANCEL_OPTION, 
				JOptionPane.QUESTION_MESSAGE, 
				null, null, null);
		
		if(choice != JOptionPane.OK_OPTION) return;
		
		rollHistory = new ArrayList<Integer>();
		System.out.println("Roll History has been cleared!");
		System.gc();
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: A Self-Recording Dice Class

#2 rasil razz   User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 22
  • Joined: 04-April 15

Re: A Self-Recording Dice Class

Posted 07 April 2015 - 09:26 AM

Wow, Cool,looking eagerly to make work this in eclipse tomorrow thanks
Was This Post Helpful? 0
  • +
  • -

#3 Severin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-May 15

Re: A Self-Recording Dice Class

Posted 07 May 2015 - 11:18 AM

Nice work!!!

Maybe I missed something but in your roll function, after you add the number rolled in the history, you add a 10 in the history.

It's probably just an error or to help you debug. If not maybe add a comment to say why you're doing that, I think it's a little bit confussing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1