Array isn't loading any values

**//(Dice game, roll two dice 36 times and store each value, then after you determine how many times 12 showed in that array, but the array 1-36 is all filled with 0's,  and it prints that shows 12 0 times. New to coding)//**
float sum = 12;
int i=1;
int sumNumber = 0;
float[] diceRolls = new float[36];
int tValue = (1/36);
float dice1 = random(1,6);
float dice2 = random(1,6);
int eValue;
void setup(){
printArray(diceRolls);
print(sumNumber);
}
void roll()
{
  for (float diceRolls=0;diceRolls<=36;diceRolls++)  {
    diceRolls = dice1 + dice2;
  }
}

void sumCounter()
{
  for (float diceRolls=0;diceRolls<=36;diceRolls++) {
  if (diceRolls == 12)
  {
    sumNumber = sumNumber + 1;
  }
  eValue = sumNumber/36;
  }
}
void winLoss() {
  if (eValue > tValue)
    {
    print ("You win!");
    }
    else
    {
    print ("You Lose!");
    }
}
1 Like

hi @bs.17

-a- is that the spec of a homework assignment?

-b- please format your code
use the

</> menu button 

and paste your code inside.


also i not understand your program flow

  • global variables
  • void setup(){}
  • void draw(){}

for me it looks like you print from setup a declared but empty array??

1 Like

thanks for getting back ive been struggling @kll . yes for my ap computer science principles class
–ok so you roll two dice and store them in a array(i named it diceRolls) its length shouldnt be longer then 36, void roll() is supposed to randomize dice 1 and dice 2 then put them in the array (length 36), then we were given a number mine was 12, program is supposed to go over that whole array and increase the variable sumNumber by 1 everytime 12 appears on that list

1 Like

https://processing.org/tutorials/arrays/
https://processing.org/reference/Array.html

:slight_smile:

sorry, coding does not work that way.

you have 3 functions

void roll(){}
void sumCounter(){}
void winLoss(){}

and i can not see where they are called.
and that means you did not test them ( or not even write them? )

what is called by processing is the

void setup() {}

and that should run
( if you would not have that bad documentation line )

and prints a empty array.


basic of a dice is a random number ( 1 …6 )
so you study
https://processing.org/reference/random_.html
and make it a function for a single dice roll
with a print on the result
and call that from the setup(){}
function.

1 Like

sorry again beginner so Im still trying to figure out everything. what do you mean by I didn’t call them? could you please be more specific and point out @kll

float dice1 = random(1,6); 
float dice2 = random(1,6);

this 2 variables ( from the variable declaration )
would give you 2 fix float numbers from 1 … 5
that you not want.

int dice1,dice2;

void setup() {
 dice1 = rollDice();
 dice2 = rollDice();
 println(dice1,dice2);
}

int rollDice() {
  return (int)random(1,7);
}

start from this.

1 Like

okay so now it prints the random values of dice 1 and dice 2, but my main question was how am I supposed to store them in the list? i tried doing diceRolls = dice1 + dice2 and shouldnt that store them? thing is though its not @kll

this is due tmrw night and kinda freaking out

did you read about the array? @glv linked you?
now you store the dice1 and dice2 values to array.
you have the roll function where you add the two numbers,
but store to array not work this way,
nevertheless you would not even find out until you
call the roll() after the println of our little randomtestprogram

I read about it but doesnt help much, it assigns variables to each place in the array. i cant determine my variables because they r random. i tried playing around with what the article said yet still no progress its still only doing println(dice1,dice2); and thats it every other thing is empty, this is my first time with arrays so the struggle is real ive been on this since 5est @kll

but that is what you need to do,
make int array ( not float - - unless you have a dice what show 3.54 )
and store the sum of 2 dice rolls into each ?cell?

show me the actual code where you combine a array (? for loop over index?)
with my above

rollDice()

function.

float sum = 12;
int i=1;
int sumNumber = 0;
int[] diceRolls = new int[36];
int tValue = (1/36);
int dice1,dice2;
int eValue;
void setup(){
dice1 = rollDice();
dice2 = rollDice();
println(dice1,dice2);
printArray(diceRolls);
print(sumNumber);
}
int rollDice() {
  return (int)random(1,6);
}
void roll()
{
  for (int diceRolls=0;diceRolls<=36;diceRolls++)  {
    diceRolls = dice1 + dice2;
  }
}

void sumCounter()
{
  for (int diceRolls=0;diceRolls<=36;diceRolls++) {
  if (diceRolls == 12)
  {
    sumNumber = sumNumber + 1;
  }
  eValue = sumNumber/36;
  }
}
//@kll 

-a- you not copy my 5 line code?
you not read random(1,6) means 6 is EXCLUDED

-b- you not call the roll function?
so you not even see that it not works???

int dice1, dice2;
int[] diceRolls = new int[36];

void setup() {
  roll();
}

int rollDice() {
  return (int)random(1, 7);
}

void roll() {
  for (int i =0; i < 36; i++) {
    dice1 = rollDice();
    dice2 = rollDice();
    int sum  = dice1 + dice2;
    diceRolls[i] = sum;
    println(i, dice1, dice2,"sum ", sum);
  }
}

2 Likes

@kll thanks man, I see where I made a mistake I will study this! thanks for having patience really appreciated :smiling_face_with_three_hearts::smiling_face_with_three_hearts:

1 Like

not related:

just for fun
// v 0.1 use mousePressed and mouseReleased
// v 0.2 give speed to slow down rotation
// v 0.3 and rotate 5 times ( not use mouseReleased )
String rev = " v0.3";
int x=100, y=x, w=50, h=w, grid=3, many=grid*grid, diam=30;
int dice = 0;
int[][] points = {
  {0, 0, 0, 0, 0, 0, 0, 0, 0}, // none
  {0, 0, 0, 0, 1, 0, 0, 0, 0}, //1
  {0, 0, 1, 0, 0, 0, 1, 0, 0}, //2
  {0, 0, 1, 0, 1, 0, 1, 0, 0}, //3
  {1, 0, 1, 0, 0, 0, 1, 0, 1}, //4
  {1, 0, 1, 0, 1, 0, 1, 0, 1}, //5
  {1, 0, 1, 1, 0, 1, 1, 0, 1}, //6
  {1, 1, 1, 1, 1, 1, 1, 1, 1}  //all              used as dice = 7 rolling
};
PShape squircle;
float speed, slower = 0.0001, ang = 0;          // animation: dice rolling
int roll = 5;                                   // roll limit

void setup() {
  size(300, 300);
  surface.setTitle("myDice"+rev);
  println("mouse click makes the magic");
  noStroke();                                      
  my_squircle(1, 90, 90);                       // generate the dice shape
}

void draw() {
  background(200, 200, 0);
  draw_dice();
}

void mousePressed() {
  dice = 7;
  ang = 0;
  speed = 0.10;
}

//__________________________________________________________ DICE
void draw_dice() {
  if ( dice == 7 ) {                            // dice rolling
    translate(width/2, height/2);
    speed -= slower;                            // v0.2 slow down rotation
    if ( speed < 0 ) speed = 0;                 // avoid rolling back
    ang += speed;
    rotate(ang);
    if ( speed == 0 || ang > roll * TWO_PI ) {  // v 0.3 roll limit 
      dice = int(random(1, 7));
      println("new dice: "+dice);
    }
    translate(-width/2, -height/2);
  }
  shape(squircle, width/2, height/2);           // shape first
  for (int i = 0; i < many; i++) {
    fill(180, 180, 180);                        // same dice squircle border, 190 would be invisible
    if ( points[dice][i] == 1 ) fill(200, 0, 0);
    ellipse(x+(i%grid)*w, y+(floor(i/grid))*h, diam, diam);
  }
}

//__________________________________________________________ SQUIRCLE
void my_squircle(float r, float wx, float wy) {
  float x1, y1;
  squircle = createShape();
  squircle.beginShape();
  squircle.stroke(180, 180, 180);
  squircle.strokeWeight(5);
  squircle.fill(190, 190, 190);
  for (float t = 0; t < TWO_PI; t += TWO_PI/160) {          // 160 vertex points is too much
    x1 = pow(abs(cos(t)), 0.5) * r*wx * sign(cos(t));
    y1 = pow(abs(sin(t)), 0.5) * r*wy * sign(sin(t));
    squircle.vertex(x1, y1);
  }
  squircle.endShape(CLOSE);
}

float sign(float input) {
  if (input < 0)  return -1.0;
  if (input > 0)  return  1.0;
  return 0.0;
}

and processing should be FUN

1 Like