My Dice Simulator

I need help adding another die

Page 1 of 1

2 Replies - 2372 Views - Last Post: 04 January 2011 - 01:28 PM Rate Topic: -----

#1 isaiah1994  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-November 10

My Dice Simulator

Posted 04 January 2011 - 12:58 PM

Hello guys, im going back to an old dice simulator i made awhile ago and im going to edit it by adding another die. Im a bit rusty and i was wondering if someone can help get started again on where i should add another die
/*
Die.java
Isaiah Antoine
Ver 2.4 Date 12/7/10
Rolls a die
*/

import javax.swing.*;
   
class Die
{
	public int roll()
	{
		int rollNum = 0;
		double randNum = (int) (Math.random() * 6);
		rollNum = (int) (randNum + 1);
		
		return(rollNum);
	}
	
	
	
//***********************************	
	public void oneDie()
	{
		String numRolls = JOptionPane.showInputDialog("How many times would you want to roll the die big boy? ;)/>");
		
		int num = Integer.parseInt(numRolls);
		
		int count = 0;
		int total = 0;
		int amount1 = 0;
		int amount2 = 0;
		int amount3 = 0;
		int amount4 = 0;
		int amount5 = 0;
		int amount6 = 0;
	    //the number integers	
		for(count = 1; count <= num; count++)// the die itself
		
		
		
		
	
		{
			Die cube = new Die();//the die rolls count :)/>
			int value1 = cube.roll();
			total += value1;
			switch(value1)
			{
			case 1:
			amount1++;
			break;
			case 2:
			amount2++;
			break;
			case 3:
			amount3++;
			break;
			case 4:
			amount4++;
			break;
			case 5:
			amount5++;
			break;
			case 6:
			amount6++;
			break;
			}
			
		}
		
		double average = total / count;
		String average1 = "The average for " + (count - 1) + " rolls is a " + average;
		String numbers = "\n# of 1's: " + amount1 + "\n# of 2's: " + amount2 + "\n# of 3's: " + amount3 + "\n# of 4's: " + amount4 + "\n# of 5's: " + amount5 + "\n# of 6's: " + amount6;

		JOptionPane.showMessageDialog(null, average1 + numbers);
	}
	
	
	
	public void twoDie()// the loop for the second run
	{
		String numRolls = JOptionPane.showInputDialog("How many times do you want to roll the die big boy ;)/>");
		int num = Integer.parseInt(numRolls);
		Die cube = new Die();
		Die cube2 = new Die();

		int count = 0;
		int total = 0;
		int amount2 = 0;
		int amount3 = 0;
		int amount4 = 0;
		int amount5 = 0;
		int amount6 = 0;
		int amount7 = 0;
		int amount8 = 0;
		int amount9 = 0;
		int amount10 = 0;
		int amount11 = 0;
		int amount12 = 0;
		
		for(count = 1; count <= num; count++)
		{
			
			int value1 = cube.roll();
			int value2 = cube2.roll();
			
			int value = value1 + value2;
			
			
			switch(value)
			{
				case 2:
					amount2++;
					break;
				case 3:
					amount3++;
					break;
				case 4:
					amount4++;
					break;
				case 5:
					amount5++;
					break;
				case 6:
					amount6++;
					break;
				case 7:
					amount7++;
					break;
				case 8:
					amount8++;
					break;
				case 9:
					amount9++;
					break;
				case 10:
					amount10++;
					break;
				case 11:
					amount11++;
					break;
				case 12:
					amount12++;
					break;
			}
			
			
			
			total += value;
		}
		
		double average = total / count;
		
		String average1 = "The average for " + (count - 1) + " rolls is a " + average;
		
		String numbers1 = "\n# of 2's: " + amount2 + "\n# of 3's: " + amount3 + "\n# of 4's: " + amount4 + "\n# of 5's: " + amount5 + "\n# of 6's: " + amount6;
		String numbers2 = "\n# of 7's: " + amount7 + "\n# of 8's: " + amount8 + "\n# of 9's: " + amount9 + "\n# of 10's: " + amount10 + "\n# of 11's: " + amount11 + "\n# of 12's: " + amount12;
		
		JOptionPane.showMessageDialog(null, average1 + numbers1 + numbers2);
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: My Dice Simulator

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2717
  • View blog
  • Posts: 11,428
  • Joined: 20-September 08

Re: My Dice Simulator

Posted 04 January 2011 - 01:12 PM

You don't need to create any more than one instance of Die, which you do inside a loop. You don't even need to do that in order to simulate rolling multiple dice. All you need do is fill in the following method:

public int[] roll(int numSimultaneous) {
...
}


This post has been edited by g00se: 04 January 2011 - 01:13 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: My Dice Simulator

Posted 04 January 2011 - 01:28 PM

A lot of code for nothing and you should learn how to use array :)
import java.util.Random;

public class Dice {

	// constructor receives number of dice and number of roll
	Dice(int nbDices, int nbRoll) {
		Random ran = new Random();
        // store number return
		int[] total = new int[nbDices * 6 + 1];
		
		for(int i = 0; i < nbRoll; ++i) {
			// total for nbDices
			int val = 0;
			for(int j = 0; j < nbDices; ++j) {
				val += ran.nextInt(6) + 1;
			}
			// cumul total
			++total[val];
		}
		// display result
		System.out.println("\n" + nbDices + " dices " + nbRoll + " rolls:");
		for(int i = nbDices; i < total.length; ++i) {
			System.out.print(" [" + i + "]=" + total[i]);
		}
		System.out.println();
	}
	
	public static void main(String[] args) {
		new Dice(1, 10);
		new Dice(2, 10);
		new Dice(2, 5);
		new Dice(3, 10);
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1