how do I get random images with two of the same?

edited March 2018 in Questions about Code

Hello,

I have to make a memory game and as I am new to programming, I am stuck. I have a code which loads the images and add them random on a board, but he uses all the images and none of them is the same. If I change the numbers of cards, I will get 3 of the same cards. How do I always gets couples?

    int vlakGrootte=100;
    int posX= 10;
    int marge = 15;
    int kaartBreedte = 100;
    PImage []kaart = new PImage[32];
    int kolom = 8;
    int rij = 3;



    void setup () {
      //size (600, 600);
      fullScreen();

      for (int i = 0; i <kaart.length; i++) {
        for (int j = 0; j<=rij; j++) {
         // rect(width/3  + (vlakGrootte +marge)*i, (vlakGrootte+ marge) * j, vlakGrootte, vlakGrootte);
          kaart[i] = loadImage("kaart" + i + ".png");
        }
      }
      for (int i = 0; i< kolom; i++) {
        for ( int j = 0; j< rij; j++) {
             image(kaart[(int) random(32)],width/3  + (vlakGrootte +marge)*i, (vlakGrootte+ marge) * j+50, vlakGrootte, vlakGrootte);

        }
      }
    }

    void draw() {


    }

Answers

  • Don't pick them at random, start with a set that contains the cards you want and then shuffle them.

  • So I have to copy them in a new array and then shuffle?

  • well, you already have an array of PImages, you could shuffle that. or add a new array of indexes (32 ints, 16 pairs) and shuffle that.

    actually, do you have 32 cards or 64? and how many separate images?

  • import java.util.Collections;
    import java.util.List;
    
    List<Integer> a = new ArrayList<Integer>(20);
    
    // add two of each 0 - 9
    for (int i = 0 ; i < 10 ; i++) {
      a.add(i);
      a.add(i);
    }
    
    Collections.shuffle(a);
    
    println(a);
    
  • thx, can I do this without import the java thing?

  • edited March 2018

    can I use the IntList in an existing array? Like in this one? (part of the code)

    //Bepaal plaatjes
    void bepaalPlaatjes (){
      for (int i = 0; i< kolom; i++) { //kolom ipv 1
        for ( int j = 0; j< rij; j++) { //rij ipv 1
          image(kaart[(int) random(25)],x/*width/3 */ + (vlakGrootte +marge)*i, (vlakGrootte+ marge) * j+y, vlakGrootte, vlakGrootte);
        }
      } 
    }
    
  • something like this. use the nth element of the shuffled intlist to get the index of the image to print at this location

    void bepaalPlaatjes (){
      int n = 0;
      for (int i = 0; i< kolom; i++) { //kolom ipv 1
        for ( int j = 0; j< rij; j++) { //rij ipv 1
          int imageIndex = shuffledList.get(n);
          image(kaart[imageIndex], width / 3  + (vlakGrootte +marge) * i, (vlakGrootte+ marge) * j + 50, vlakGrootte, vlakGrootte);
          n++;
        }
      } 
    }
    
  • edited March 2018

    Tjess, if you wanna stick w/ Java's regular arrays[], you can use the custom shuffle() utility function from this old forum post: B-)

    https://Forum.Processing.org/two/discussion/20529/retrieving-elements-from-a-list-randomly#Item_5

    Simply replace all of datatype char used there w/ the 1 you need; for example the int datatype. ;)

Sign In or Register to comment.