-3

I want to memory-efficient this (the game of life code of shiffman in the nature of code book). how can change the below code to have only two arrays and constantly swap them, writing the next set of states into whichever one isn’t the current array?

class GOL {
  int w = 8;
  int columns, rows;
  int[][] board;

  GOL() {
    // Initialize rows, columns and set-up arrays
    columns = width / w;
    rows = height / w;
    board = new int[columns][rows];
    //next = new int[columns][rows];
    // Call function to fill array with random values 0 or 1
    init();
  }

  void init() {
    for (int i = 1; i < columns - 1; i++) {
      for (int j = 1; j < rows - 1; j++) {
        board[i][j] = (int) random(2);
      }
    }
  }

  // The process of creating the new generation
  void generate() {

    int[][] next = new int[columns][rows];

    // Loop through every spot in our 2D array and check spots neighbors
    for (int x = 1; x < columns - 1; x++) {
      for (int y = 1; y < rows - 1; y++) {

        // Add up all the states in a 3x3 surrounding grid
        int neighbors = 0;
        for (int i = -1; i <= 1; i++) {
          for (int j = -1; j <= 1; j++) {
            neighbors += board[x + i][y + j];
          }
        }

        // A little trick to subtract the current cell's state since
        // we added it in the above loop
        neighbors -= board[x][y];

        // Rules of Life
        if ((board[x][y] == 1) && (neighbors < 2)) next[x][y] = 0;
        else if ((board[x][y] == 1) && (neighbors > 3)) next[x][y] = 0;
        else if ((board[x][y] == 0) && (neighbors == 3)) next[x][y] = 1;
        else next[x][y] = board[x][y];
      }
    }

    // Next is now our board
    board = next;
  }

  // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0'
  void display() {
    for (int i = 0; i < columns; i++) {
      for (int j = 0; j < rows; j++) {
        if ((board[i][j] == 1)) fill(0);
        else fill(255);
        stroke(0);
        rect(i * w, j * w, w, w);
      }
    }
  }
}
any
  • 325
  • 5
  • 17
  • In the middle of your negative votes, I thank you for consider that one person here need help! – any Jul 11 '16 at 14:07
  • If it's only about swapping arrays then just do that, e.g. `int[] a = ..; int[] b=...;` now swap: `int[] temp = a; a = b; b = temp;`. As for the down votes I'd have to guess but commonly just posting a wall of code and asking for changes without showing what you've already tried isn't received that well here. As a basic rule: the more effort _you_ put into a question (e.g. by showing what you've tried) the more others will try to help and the less you'll risk downvotes. – Thomas Jul 11 '16 at 14:08
  • @any posting poorly-formatted non-compiling code is especially badly received. – Andy Turner Jul 11 '16 at 14:10
  • @Thomas It creates a new 2D array for every frame of animation. I want to optimize it. thank you for your description about negative vote. – any Jul 11 '16 at 14:16
  • ´It creates a new 2D array for every frame of animation` - that's not necessarily a problem since the array can be removed by the garbage collector. If you want to use just 2 arrays then create two you you did with `board` (i.e. add another one) and then swap them each frame. – Thomas Jul 11 '16 at 14:58
  • @OysterD I read in one post you do this with two array and swap. would you please let me more about that? – any Jul 12 '16 at 04:13
  • @Andy Turner you had removed my link. – any Jul 13 '16 at 04:23

1 Answers1

1

You might not like this, but the honest answer is: don't bother.

how can change the below code to have only two arrays and constantly swap them, writing the next set of states into whichever one isn’t the current array

This is already exactly what the code does.

The Game of Life requires two arrays. If you're coming up against real performance issues, then look for other areas of improvement. Focusing on the array is a red herring.

There's an old saying: premature optimization is the root of all evil. In other words, you shouldn't waste time trying to fix code before it's broken.

One obvious thing you might improve is: why are you using an int[] array instead of a boolean[] array? You only need to store two states: alive or dead, so using int values seems unnecessary. You'll save a little bit of memory if you switch to a boolean[] array, but again, you probably won't even notice the improvement.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • this code creates a new 2D array for every frame of animation. – any Jul 11 '16 at 15:10
  • @any And that's really not a problem. If you really really wanted to, you could use another sketch-level array and switch between them, but that isn't going to save you any memory. – Kevin Workman Jul 11 '16 at 15:13
  • this is an exercise. I don't know how can I do that. I'm new in processing. – any Jul 11 '16 at 15:16
  • @any What do you mean it's an exercise? Do you mean it's a homework? If you're just doing it for fun, you should really know that you aren't going to notice any improvement by making this change. – Kevin Workman Jul 11 '16 at 15:16
  • @any In that case, please post exactly what you've tried and where you're stuck. We can't do your homework for you. It's hard to answer general "how do I do this" type questions. Instead, try to ask a specific "I tried X, expected Y, but got Z instead" type question. – Kevin Workman Jul 11 '16 at 15:19
  • I said I am new in processing. I want to know where to start. for example I must remove int[][] next = new int[columns][rows]; and get only two array or not? – any Jul 11 '16 at 15:24
  • @any It's okay that you're new to Processing. We can help you step through your own thinking, but we can't do your homework for you. You've asked about a particular approach: what happened when you tried that? Can you try to explain exactly what you're confused about instead of saying you don't know how to start? – Kevin Workman Jul 11 '16 at 15:26
  • I DON'T WANT YOU TO DO MY HOMEWORK! you don't understand what I say. – any Jul 11 '16 at 15:29
  • @any I understand what you're saying. I'm trying to help you. I've suggested how you might get help, and I've asked you a question. You seem to be ignoring everything I'm saying. That's up to you, but it makes it difficult to help you. Good luck. – Kevin Workman Jul 11 '16 at 15:34
  • thank you for your trying to help me but you answer my question with questions. – any Jul 11 '16 at 15:44
  • @any That's because I'm trying to help you work through **solving the problem yourself**. That's the whole point of the homework. You've outlined a plan, and I really suggest you try it out and post a [mcve] along with a more specific question in a new post if you get stuck. – Kevin Workman Jul 11 '16 at 15:46
  • I don't know in this homework, the problem related to which 2D array? board or next? Is this problem only for generate method or not? – any Jul 11 '16 at 15:53
  • @any It sounds like your homework is trying to get you to convert the `next` array into a sketch-level variable instead of creating a new array every frame. – Kevin Workman Jul 11 '16 at 15:58
  • thanks a lot. where can I learn how to do that? I don't know what is sketch-level variable. – any Jul 11 '16 at 16:08
  • @any I guess I should say class-level variable, since your code is using a class. For example, your `board` variable is a class-level variable, but your `next` variable is a **local** variable since it's created inside a function. – Kevin Workman Jul 11 '16 at 16:12
  • I understand it, I must only do this change? that means just move next = new int[columns][rows]; in the constructor of class? in this case I don't need to 2 aray definition. – any Jul 11 '16 at 17:30
  • @any What happened when you tried that? But you are going to need two arrays. – Kevin Workman Jul 11 '16 at 17:42
  • I execute that code this is not the same execution as before. – any Jul 11 '16 at 17:49
  • My homework said that: While the above solution (Example 7.2) is convenient, it is not particularly memory-efficient. It creates a new 2D array for every frame of animation! This matters very little for a Processing desktop application, but if you were implementing the Game of Life on a microcontroller or mobile device, you’d want to be more careful. One solution is to have only two arrays and constantly swap them, writing the next set of states into whichever one isn’t the current array. Implement this particular solution. – any Jul 11 '16 at 17:49
  • @any That uses the same amount of memory, but I suppose that's beside the point. You're going to need two arrays, otherwise you're checking against a board that's in between two frames. – Kevin Workman Jul 11 '16 at 17:51
  • would you please guide me more? I should send my homework as soon as posible. you say that I move next = new int[columns][rows]; in the constructor of class and define two array in generate method? – any Jul 11 '16 at 18:19
  • @any I'm not sure what you're asking. What happened when you tried that? Instead of focusing on the code, you might want to take a step back and describe, in English, exactly what needs to happen. Don't worry about how to code it just yet. Think about it this way: what if you wanted to do Game of Life in real life, but you only had two checkerboards? How would you use the two boards? Again, write down the steps you would do in English before you even think about converting those steps to code. – Kevin Workman Jul 11 '16 at 18:24
  • thank you. I understand you want to help me. but my homework expired and I don't want to continue. – any Jul 12 '16 at 06:44
  • @any That's a shame. I really do want to help you, but the best way to help you is by walking you through the process of solving the problem, not solving the problem for you. If you want to quit that's up to you, but you're really missing out. – Kevin Workman Jul 12 '16 at 12:51