How to Access the Numbers (or Names) of Randomized Images?

edited March 2014 in JavaScript Mode

Hi, Windows 7 with Processing 2.1.1 in JavaScript mode, here.
In Coursera's Intro to Python I made a simple memory game using images instead of numbers. I'm trying to do something similar in Processing, but I'm stuck at mapping the randomized images to their numbers - in python you can just use a dictionary. In other words, if you click on a tile with the image of a number 1, I need to translate that into the integer number 1.
Any help would be appreciated.

Answers

  • edited March 2014

    Dictionaries in Java are Map structures. Processing officially supports HashMap as such implementation:

    http://processing.org/reference/HashMap.html

  • edited March 2014

    A simpler implementation scheme w/o HashMap: %%-

    // forum.processing.org/two/discussion/3449/
    // how-to-access-the-numbers-or-names-of-randomized-images
    
    static final int ROWS = 3, COLS = 4, NUM = ROWS * COLS;
    
    static final PImage[] imgs = new PImage[NUM];
    static final IntList grid = IntList.fromRange(NUM);
    
    void setup() {
      size(800, 600, JAVA2D);
      smooth(4);
      //noLoop();
    
      for ( int i = 0; i != NUM; imgs[i++] = loadImage("img" + i) );
    
      println(grid);
    
      grid.shuffle(this);
      println(grid);
    }
    
    void draw() {
      background(-1);
    
      for (int r = 0; r != ROWS; r++)  for (int c = 0; c != COLS; c++) {
        final int idx = r*COLS + c;
        final PImage img = imgs[grid.get(idx)];
        //set(c*img.width + 0100, r*img.height + 0100, img);
      }
    }
    
  • Thanks. I've been trying to do that, but not correctly, obviously. For instance, does the key have to be a string? And if it does, how do I get the key from the value - .getKey() doesn't take arguments. Is it possible for the key to be the image and the value the integer, so I can just use .getValue on an image?

  • edited March 2014 Answer ✓

    Is it possible for the key to be the image and the value the integer,...

    final HashMap<PImage, Byte> imgs = new HashMap();
    

    Updated my example above! L-)

  • Oops, we cross-posted. I'm going to try your suggestion right now.

  • edited March 2014

    Ha-ha success! I got a "ReferenceError: IntList is not defined" (?) with the IntList (even though It was defined), so I tried the "HashMap<PImage, Byte> imgs = new HashMap();" with this code in setup:

     for (int i=0; i < numberImages.length; i++) {
        numberImages[i] = loadImage(i + ".png");
        imgs.put(numberImages[i], i);
      }
      shuffleArray(numberImages, 20);
    

    and this in my drawTiles() function:

     
    for (int j = 0; j < 20;  j += 5) {
      for (int i = 0; i < 5;  i++) {
        if (state == 1 ) {  
          image(numberImages[i+j], x, y);
          int val = imgs.get(numberImages[i+j]);
          println(val);
        }
      }
    }  
    

    and get the appropriate number every time I click a tile. Thanks a lot for your help. :)

  • edited March 2014 Answer ✓

    "ReferenceError: IntList is not defined" (?)

    Forgot that you wanted it on JS Mode, even though you haven't place this thread on that section! b-(
    IntList is for Processing 2+. While JS Mode is eternally @ v1.4.1! :-L

Sign In or Register to comment.