本文介绍了仅用 2 个数组实现生命游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想节省内存这个(码本性质的shiffman的生命密码游戏).如何将下面的代码更改为只有两个数组并不断交换它们,将下一组状态写入不是当前数组的任何一个?

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);
      }
    }
  }
}

推荐答案

你可能不喜欢这个,但诚实的回答是:别打扰.

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

如何将以下代码更改为只有两个数组并不断交换它们,将下一组状态写入非当前数组中的任何一个

这正是代码所做的.

生命游戏需要两个数组.如果您遇到真正的性能问题,请寻找其他改进领域.关注数组的是红鲱鱼.

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.

你可以改进的一个明显的事情是:为什么你使用 int[] 数组而不是 boolean[] 数组?你只需要存储两种状态:活着或死了,所以使用 int 值似乎没有必要.如果您切换到 boolean[] 数组,您将节省一点内存,但同样,您可能甚至不会注意到改进.

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.

这篇关于仅用 2 个数组实现生命游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 16:45