shuffle in a 2d array

edited October 2013 in Using Processing

hi guys,

A few days ago you helped me on this topic: Processing 2.0 Random without duplicate as interger is it possible?

you send in your code to shuffle values in an array. could you help me implement it in this.

greetings,

Ben

    int value=0;
    int abc = 0;
    int cols = 10;
    int rows = 10;
    int[][] myArray = new int[cols][rows];

    void setup() {
      size(550, 550);
    }

    void draw() {
      while (abc < 100) {
        background(255, 0, 0);
        for (int i = 0; i < cols; i++) {
          for (int j = 0; j < rows; j++) {

            fill(255);
            myArray[i][j] = abc++;
            rect(25+50*i, 50*j, 25, 25);
            rect(width/3,height/18*17,width/3,height/18); // shuffle value
            fill(0);
            text(i+","+j, 30+ 50*j, 20+ 50*i);
            text("shuffle",width/2 -20,height/18*17.7);
            text(myArray[i][j], 30+ 50*j, 40+ 50*i);
          }
        }
      }
    }

    void mouseReleased() {
      if (value == 0 && mouseX > width/3 && mouseX < width/3 + width/3 && mouseY > height/18*17 && mouseY < height/18*17 + height/18){  
      //Shuffle Action

      }
      for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
          if (value == 0 && mouseX > 25+(50*i) && mouseX < 25+(50*i) +25 && mouseY > 50*j && mouseY < (50*j)+25) {
            value = 255;
            println("1st value"+ j);
            println("2nd value"+ i );
            fill(0, 255, 0);
            rect(25+(50*i), 50*j, 25, 25);

            int getal = myArray[j][i] ;            
            println("number: "+ getal);

            if (j == 3 && i == 5) {
              println("oki!");
            }


            rect(25+(50*i), 50*j, 25, 25);
          }
          else {
            value = 0;
          }
        }
      }
    }

Answers

  • Answer ✓

    I have modified your code the array is initialised in setup and drawn in draw. I have removed the while(abc... loop because it is not needed.

    The shuffleArray method will work with any 2D integer array.

    int value=0;
    int abc = 0;
    int cols = 10;
    int rows = 10;
    int[][] myArray = new int[cols][rows];
    
    void setup() {
      size(550, 550);
      for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
          myArray[i][j] = abc++;
        }
      }
    }
    
    void draw() {
      background(255, 0, 0);
      for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
          fill(255);
          rect(25+50*i, 50*j, 25, 25);
          rect(width/3, height/18*17, width/3, height/18); // shuffle value
          fill(0);
          text(i+","+j, 30+ 50*j, 20+ 50*i);
          text("shuffle", width/2 -20, height/18*17.7);
          text(myArray[i][j], 30+ 50*j, 40+ 50*i);
        }
      }
    }
    
    void shuffleArray(int[][] a) {
      int nbrCols = a.length;
      int nbrRows = a[0].length;
      for (int c = 0; c < nbrCols; c++) {
        for (int r = 0; r < nbrRows; r++) {
          int nc = (int)random(nbrCols);
          int nr = (int)random(nbrRows);
          int temp = a[c][r];
          a[c][r] = a[nc][nr];
          a[nc][nr] = temp;
        }
      }
    }
    
    void mouseReleased() {
      if (value == 0 && mouseX > width/3 && mouseX < width/3 + width/3 && mouseY > height/18*17 && mouseY < height/18*17 + height/18) {  
        shuffleArray(myArray);
      }
      for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
          if (value == 0 && mouseX > 25+(50*i) && mouseX < 25+(50*i) +25 && mouseY > 50*j && mouseY < (50*j)+25) {
            value = 255;
            println("1st value"+ j);
            println("2nd value"+ i );
            fill(0, 255, 0);
            rect(25+(50*i), 50*j, 25, 25);
    
            int getal = myArray[j][i] ;            
            println("number: "+ getal);
    
            if (j == 3 && i == 5) {
              println("oki!");
            }
            rect(25+(50*i), 50*j, 25, 25);
          }
          else {
            value = 0;
          }
        }
      }
    }
    
  • That's a perfect solution many thanks!!

  • edited October 2013

    An alternative approach for shuffleArray() function: ;))

    void shuffle2dArray(int[][] arr) {
      final int cols = arr.length;
      final int rows = arr[0].length;
    
      for (int nc = (int) random(cols), c = 0; c != cols; c++) {
        final int[] col = arr[c], ncol = arr[nc];
    
        for (int nr = (int) random(rows), r = 0; r != rows; r++)
          if (nc != c | nr != r) {
            ncol[nr] ^= col[r] ^= ncol[nr];
            col[r] ^= ncol[nr];
          }
      }
    }
    
Sign In or Register to comment.