How to define this array?

edited October 2016 in How To...

Hi all,

Im once again stuck with my code, if i may ask your help one more time with the following it would be lovely.

I have three rectangles in a for loop, there is a price under one of the rectangles assigned randomly. If a user clicks on a rectangle the program will remove one rectangle that does not have a price underneath it. After that the user can select one of the two remaining rectangles again, regardless if the user has selected a winning rectangle or not the program has to remove all the rectangles after the second click. If the price is under the selected rectangle the user will get a point, if not nothing will happen.

I think i have to make an array like this:

Right now i calculate on which rectangle the user has clicked with: println(floor (mouseX/(width/rectangles)));

Im getting more confused by the minute on how i should calculate and write program as described above. Any help will be appreciated.

Tagged:

Answers

  • it depends on your skill level

    int rectangles = 3; 
    
    boolean visible1=true; 
    boolean visible2=true; 
    boolean visible3=true;
    
    boolean hasPrice1=false; 
    boolean hasPrice2=true; 
    boolean hasPrice3=false;
    
    
    
    
    void setup() {
      size(1222, 222 );
    }
    
    void draw() {
      //println(floor (mouseX/(width/rectangles)));
    }
    
    void mousePressed() {
    
      // 
    
      int num = floor (mouseX/(width/rectangles)); 
    
      if (num==0)
      {
        println("ONE");
      } 
      // ---
      else if (num==1)
      {
        println("Two");
      }
      // ---
      else if (num==2)
      {
        println("Three");
      }
    }
    
  • edited October 2016

    Alternatives

    in theory / later you could:

    of course you could use parallel arrays visible and hasPrice instead

    or a class RectangleWithPrice and an array of that

    What you need to do

    What you need to do to move on with my code above:

    Now, when rect 1 is clicked, in mousePressed() you

    • check hasPrice1 and

    • set visible1 to false.

    In draw display only those rects where

    if(visible1)
          displayRectangle1(); 
    

    or so

  • Answer ✓
    int rectangles = 3; 
    
    boolean[] visible  = new boolean[rectangles]; 
    boolean[] hasPrice = new boolean[rectangles];
    
    void setup() {
      // 
      size(1222, 222 );
      // ---------
      // init
      for (int i=0; i<rectangles; i++) {
        visible[i]=true;
      }
      for (int i=0; i<rectangles; i++) { 
        hasPrice[i]=false;
      }
      // --------
      // put one price in 
      int randomValue=int(random(rectangles));
      hasPrice[randomValue]=true;
      // --------
      for (int i=0; i<rectangles; i++) { 
        print(hasPrice[i], " ");
      }
      // --------
      println("");
      background(111);
    }//func
    
  • I will give it a shot, thanks for your clear answer :)

Sign In or Register to comment.