Loading...
Logo
Processing Forum
Hello all.

I just had the array problem more or less dealt with ( Saving Game Level), but now it has given me a new problem.
I have the program rearranging the 2D array into a 1D array, and I'm using the Processing reference example for loadBytes() to test the efficiency and effectiveness of saveBytes() in my program ( loadBytes()).

What saveBytes() is actually doing, by the looks of it, is that it is mirroring the entire array on some sort of diagonal line.
Or the main program overall is made improperly.
Here are some images of what is happening:

This is the way the main
program shows the level:


Then the reference program
interprets the array like this:


Which is the same as this:


It's as though the program flipped the image vertically, then turned it quarter turn clockwise.
Why is the main program saving the array like this?
Code in the next post.

- MenteCode.

Replies(11)

Here's the code:

Copy code
  1. int rows = 5;
  2. int cols = 7;
  3. int x; //Random entrance position x
  4. int y; //Random entrance position y
  5. int timer = 1;
  6. byte[][] level = new byte[rows][cols];
  7. byte[] sve = new byte[rows * cols]; //Saving array
  8.  
  9. void setup() {
  10.   size(1000, 600);
  11. }

  12. void draw() {
  13.   gen(); //Level generation function
  14.   sav(); //Saving function
  15.   display(); //Display function
  16. }

  17. void gen() {
  18.   level[0][0] = 3;
  19.   level[rows - 1][0] = 3;
  20.   level[0][cols - 1] = 3;
  21.   level[rows - 1][cols - 1] = 3;
  22.   for(int fx = 1; fx < rows - 1; fx++) {
  23.     for(int fy = 1; fy < cols - 1; fy++) {
  24.       level[fx][fy] = 2;
  25.     }
  26.   }
  27.   if(timer > 0) {
  28.     x = int(random(rows));
  29.     y = int(random(cols));
  30.     if(level[x][y] == 0) {
  31.       level[x][y] = 1;
  32.       noLoop();
  33.     }
  34.   }
  35.   else {
  36.     timer = 1;
  37.   }
  38. }

  39. void sav() {
  40.   for(int i = 0; i < rows; i++) {
  41.     int srcp = i * cols;
  42.     arrayCopy(level[i], 0, sve, srcp, cols);
  43.   }
  44.   saveBytes("Level.dat", sve);
  45. }

  46. void display() {
  47.   for(int i = 0; i < rows; i++) {
  48.     for(int j = 0; j < cols; j++) {
  49.       fill(level[i][j] * 255);
  50.       rect(i * 20, j * 20, 20, 20);
  51.     }
  52.   }
  53. }
The reading code is above (loadBytes).

- MenteCode.

P. S. Don't mind the difference between the pictures and your run. The entrance is generated randomly.
I believe you want an image which is 5x7. That is, 5 columns & 7 rows, right?
But what I see in your code is -> rows = 5 & cols = 7!!!
I don't get that either. I'm trying to get 7 columns and 5 rows.

I keep trying to make it iterate through the number of columns, and the number of rows.

Instead, for some reason, it iterates through the length of the columns, and the length of the rows,
and what I'm assuming the program does, is that saveBytes() is doing what it is supposed to do, but
the display function and the rest of the program overall is doing very stupid things, like I mentioned above.

- MenteCode.
I don't understand where you load the level.
Oh, sorry.

I didn't put that in yet, but instead I'm using the loadBytes() example that is shown at the top.
I does the same thing, so use that for now.

- MenteCode
Is that pixelated zero what you're trying to achieve?
For my eyes, it is 5 cols x 7 rows!  

So, it should be something like ---> final static byte levels = 10, cols = 5, rows = 7;

You only have to pay attention when rendering from the Array level[row][col].
Since in graphics you specify the scalar x (col) 1st, then y (row).
Copy code
    final static byte levels = 10, cols = 5, rows = 7;

    final static byte[][][] game = new byte[levels][rows][cols];
    final static byte[][] level = new byte[rows][cols];
    final static byte[] sve = new byte[rows * cols]; //Saving array final static byte tileExit = 1;
    final static byte tileWall = 2;
    final static byte tileEmpty = 3;

    void setup() {
      size(0400, 0300);
      noLoop();
    }

    void draw() {
      gen(); //Level generation function   sav(); //Saving function   display(); //Display function } void gen() {
      level[0][0] = tileEmpty;
      level[rows - 1][0] = tileEmpty;
      level[0][cols - 1] = tileEmpty;
      level[rows - 1][cols - 1] = tileEmpty;

      for (int fx = 1; fx < rows - 1; fx++)
        for (int fy = 1; fy < cols - 1; fy++)
          level[fx][fy] = tileWall;

      int x = (int) random(rows);
      int y = (int) random(cols);

      if (level[x][y] == 0) level[x][y] = tileExit;
    }

    void sav() {
      for (int row = 0; row < rows; row++) {
        int srcp = row * cols;
        arrayCopy(level[row], 0, sve, srcp, cols);
        println(level[row]);
      }
      saveBytes("Level.dat", sve);
      println(sve);
    }

    void display() {
      for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++) {
          fill(level[i][j] * 255);
          rect(j*20, i*20, 20, 20);

          fill(sve[i*cols + j] * 255);
          rect(j*20 + 0200, i*20, 20, 20);
        }
    }
That is WEIRD!

What's the difference between your version of the code, and my version of the code listed above, besides you using final static byte, and you using variables to define the tiles?

- MenteCode
Ohhhhhhhhh!!!

In void display(), I used int j and int i improperly! Why does it work that way!? Can someone tell me how to remember to use the displaying integers this way?

Thanks for everything!

      - MenteCode.

In void display(), I used int j and int i improperly! Why does it work that way!?
Already told you:
Canvas coordinates are x (or column) and y (or row). But your Array is row 1st, then col.
So you gotta swap them (besides scaling them up) when rendering!  

 Already told you:
Canvas coordinates are x (or column) and y (or row). But your Array is row 1st, then col.
Oh. I didn't quite catch that the first time. But now it all makes sense and fits together the way it is supposed to.
Thanks again.

      - MenteCode.