Simple Yahtzee Simulation

edited July 18 in Questions about Code

I am attempting to do a very simple dice roll of 5 dice on a mouse click. So far I have this code and I am very close but cannot write drawDiceRoll(), however, I do know that I must call rollDice() and run it NUM_DICE times. I also think I have called drawDie() wrong but I'm not 100% sure.

/***  Constants  ***/
final int NUM_SIDES = 6;   //Sides on the dice
final int NUM_DICE = 5;    //The number of dice used

/***  Variables  ***/
int[] roll = new int[NUM_DICE]; //array to store dice roll

int rollDice() //roll dice
{
  for (int i=1; i<=NUM_DICE; i++)
  {
    roll[i]=int(random(NUM_SIDES));
  }
  return roll;
}

void showDiceRoll() //show dice roll
{
  drawDie(int(roll[NUM_DICE]), rollDice());
}

void setup()
{
  size(500,500);
  displayMessage("Click to roll");
}

void draw()
{
  showDiceRoll();
}

void mouseClicked()
{
  rollDice();
  showDiceRoll();
}

void displayMessage(String message)
{
  //Display the given message in the centre of the window.
  textSize(24);
  fill(0);
  text(message,(width-textWidth(message))/2,height/2);
}

void drawDie(int position, int value)
{
  /* Draw one die in the canvas.
   *   position - must be 0..NUM_DICE-1, indicating which die is being drawn
   *   value - must be 1..6, the amount showing on that die
   */
  final float X_SPACING = (float)width/NUM_DICE; //X spacing of the dice
  final float DIE_SIZE = X_SPACING*0.8; //width and height of one die
  final float X_LEFT_DIE = X_SPACING*0.1; //left side of the leftmost die
  final float Y_OFFSET = X_SPACING*0.15; //slight Y offset of the odd-numbered ones
  final float Y_POSITION = height-DIE_SIZE-Y_OFFSET; //Y coordinate of most dice
  final float PIP_OFFSET = DIE_SIZE/3.5; //Distance from centre to pips, and between pips
  final float PIP_DIAM = DIE_SIZE/5; //Diameter of the pips (dots)

  //From the constants above, and which die it is, find its top left corner
  float dieX = X_LEFT_DIE+position*X_SPACING;
  float dieY = Y_POSITION-Y_OFFSET*(position%2);

  //1.Draw a red square with a black outline
  stroke(0); //Black outline
  fill(255,0,0); //Red fill
  rect(dieX,dieY,DIE_SIZE,DIE_SIZE);

  //2.Draw the pips (dots)
  fill(255); //White dots
  stroke(255); //White outline

  //The centre dot (if the value is odd)
  if(1 == value%2)
    ellipse(dieX+DIE_SIZE/2,dieY+DIE_SIZE/2,PIP_DIAM,PIP_DIAM);

  //The top-left and bottom-right dots (if the value is more than 1)
  if(value>1)
  {
    ellipse(dieX+DIE_SIZE/2-PIP_OFFSET,
            dieY+DIE_SIZE/2+PIP_OFFSET,PIP_DIAM,PIP_DIAM);
    ellipse(dieX+DIE_SIZE/2+PIP_OFFSET,
            dieY+DIE_SIZE/2-PIP_OFFSET,PIP_DIAM,PIP_DIAM);
  }

  //The bottom-left and top-right dots (if the value is more than 3)
  if(value>3)
  {
    ellipse(dieX+DIE_SIZE/2+PIP_OFFSET,
            dieY+DIE_SIZE/2+PIP_OFFSET,PIP_DIAM,PIP_DIAM);
    ellipse(dieX+DIE_SIZE/2-PIP_OFFSET,
            dieY+DIE_SIZE/2-PIP_OFFSET,PIP_DIAM,PIP_DIAM);
  }

  //The left and right dots (only if the value is 6)
  if(value==6)
  {
    ellipse(dieX+DIE_SIZE/2-PIP_OFFSET,
            dieY+DIE_SIZE/2,PIP_DIAM,PIP_DIAM);
    ellipse(dieX+DIE_SIZE/2+PIP_OFFSET,
            dieY+DIE_SIZE/2,PIP_DIAM,PIP_DIAM);
  }
}
Tagged:

Answers

  • Answer ✓
    /***  Constants  ***/
    final int NUM_SIDES = 6;   //Sides on the dice
    final int NUM_DICE = 5;    //The number of dice used
    
    /***  Variables  ***/
    int[] rolls = new int[NUM_DICE]; //array to store dice roll
    
    void setup() {
      size(500, 500);
      message_draw("Click to roll");
      dice_roll();
    }
    
    void draw() {
      for ( int d = 0; d < NUM_DICE; d++) {
        die_draw( d, rolls[d] );
      }
    }
    
    void mousePressed() {
      dice_roll();
    }
    
    void message_draw(String message) {
      //Display the given message in the centre of the window.
      textSize(24);
      fill(0);
      text(message, (width-textWidth(message))/2, height/2);
    }
    
    void dice_roll() {
      for (int i=0; i < NUM_DICE; i++) {
        rolls[i]=1 + int(random(NUM_SIDES));
      }
    }
    
    void die_draw(int position, int value) {
      /* Draw one die in the canvas.
       *   position - must be 0..NUM_DICE-1, indicating which die is being drawn
       *   value - must be 1..6, the amount showing on that die
       */
      final float X_SPACING = (float)width/NUM_DICE; //X spacing of the dice
      final float DIE_SIZE = X_SPACING*0.8; //width and height of one die
      final float X_LEFT_DIE = X_SPACING*0.1; //left side of the leftmost die
      final float Y_OFFSET = X_SPACING*0.15; //slight Y offset of the odd-numbered ones
      final float Y_POSITION = height-DIE_SIZE-Y_OFFSET; //Y coordinate of most dice
      final float PIP_OFFSET = DIE_SIZE/3.5; //Distance from centre to pips, and between pips
      final float PIP_DIAM = DIE_SIZE/5; //Diameter of the pips (dots)
    
      //From the constants above, and which die it is, find its top left corner
      float dieX = X_LEFT_DIE+position*X_SPACING;
      float dieY = Y_POSITION-Y_OFFSET*(position%2);
    
      //1.Draw a red square with a black outline
      stroke(0); //Black outline
      fill(255, 0, 0); //Red fill
      rect(dieX, dieY, DIE_SIZE, DIE_SIZE);
    
      //2.Draw the pips (dots)
      fill(255); //White dots
      stroke(255); //White outline
    
      //The centre dot (if the value is odd)
      if (1 == value%2){
        ellipse(dieX+DIE_SIZE/2, dieY+DIE_SIZE/2, PIP_DIAM, PIP_DIAM);
      }
    
      //The top-left and bottom-right dots (if the value is more than 1)
      if (value>1) {
        ellipse(dieX+DIE_SIZE/2-PIP_OFFSET, 
          dieY+DIE_SIZE/2+PIP_OFFSET, PIP_DIAM, PIP_DIAM);
        ellipse(dieX+DIE_SIZE/2+PIP_OFFSET, 
          dieY+DIE_SIZE/2-PIP_OFFSET, PIP_DIAM, PIP_DIAM);
      }
    
      //The bottom-left and top-right dots (if the value is more than 3)
      if (value>3) {
        ellipse(dieX+DIE_SIZE/2+PIP_OFFSET, 
          dieY+DIE_SIZE/2+PIP_OFFSET, PIP_DIAM, PIP_DIAM);
        ellipse(dieX+DIE_SIZE/2-PIP_OFFSET, 
          dieY+DIE_SIZE/2-PIP_OFFSET, PIP_DIAM, PIP_DIAM);
      }
    
      //The left and right dots (only if the value is 6)
      if (value==6) {
        ellipse(dieX+DIE_SIZE/2-PIP_OFFSET, 
          dieY+DIE_SIZE/2, PIP_DIAM, PIP_DIAM);
        ellipse(dieX+DIE_SIZE/2+PIP_OFFSET, 
          dieY+DIE_SIZE/2, PIP_DIAM, PIP_DIAM);
      }
    }
    

    Your rollDice() function (now dice_roll()) rolls all the dice at once. It doesn't need to return a value at all because the rolled values are stored in the array roll[] (now rolls[]).

  • Awesome. Thanks

  • Very nice start, @albobz.

    For Yahtzee, one next step is to give dice_roll() an argument dice_roll(boolean[] dice): a list of which dice you want to re-roll.

    Now the user could, for example, type 1 2 3 4 5 to toggle which dice (which boolean arguments in the re-roll list) they want to re-roll and which ones they want to keep. Next, click to re-roll selected dice only. Yahtzee!

Sign In or Register to comment.