FileDocCategorySizeDatePackage
Dice.javaAPI DocExample1935Sun Feb 22 18:28:26 GMT 1998None

Dice

public class Dice extends Frame

Fields Summary
private int
dice
private int[]
results
Random
roll
Constructors Summary
public Dice(int noOfDice)

  dice = noOfDice;
      setTitle( "Dice: " + dice );
      setSize( 400, 300 );
      results = new int[ 5*dice+1 ];
      throwDice();   

      setVisible( true );
   
Methods Summary
public voidpaint(java.awt.Graphics g)

  Dimension d = getSize();
      int left = 10;
      int bottom = d.height - 20;
      int width = d.width-20;
      int height = d.height - 50;
      int die;
      int max=0;
      for( die = 0; die < results.length; die += 1 )
      {  if( max < results[die] )
         {  max = results[die];
         }
      }
      int vertUnit = height/max;
      int horizUnit = width/(5*dice+1);
      for( die = 0; die < results.length; die += 1 )
      {  g.drawRect( left+die*horizUnit,
                   bottom-results[die]*vertUnit,
                   horizUnit,
                   results[die]*vertUnit );
      }            
   
private voidthrowDice()


   // toss dice 1000 times
     
     int loop;
      for( loop = 0; loop < 1000; loop += 1 )
      {  int score = 0;
         for( int die = 0; die < dice; die += 1 )
         {  int value = Math.abs(roll.nextInt()) % 6 + 1;
            score += value;
         }
         results[ score-dice ] += 1;
      }