优化康威生命游戏以在Arduino上运行

huangapple go评论42阅读模式
英文:

Optimizing Conway's Game of Life for an Arduino

问题

我正在用Arduino和一个64x64的点阵格子制作康威生命游戏。它正在工作,但在运行完整尺寸时有点慢。我认为以下代码是运行时间最长的部分:

// 我的代码可以在不同大小上运行,因此这些需要是可写的。
int width1 = 64;
int height = 64;
int row0[WIDTH]; // WIDTH只是常数64。

void check(int y) {
  int alive = 0;

  for (int x = 0; x < width1; ++x) {
    alive = 0;
    // ... 一些逻辑代码 ...

    god_Conway(x, y, alive);
  }
}

void god_Conway(int x, int y, int a) {
  int born[] = {3};
  int survive[] = {2, 3};
  int kill = 0;
  bool birth1 = 0;
  int living = getPixelColor(x, y);
  
  // ... 一些逻辑代码 ...
  
  if (kill == -1 || birth1 == 1) {
    for (int c = 0; c < width1; c++) {
      if (row0[c] == -1) {
        row0[c] = x;
        if (c, width1) {
          row0[c + 1] = -1;
        }
        break;
      }
    }
  }
  if (kill == 1 || birth1 == 0) {
    same++;
  }
}
英文:

I am making Conway's Game of life on an Arduino with a 64x64 dot matrix grid. it is working but its a little slow while running the full size. This is the code that I think is taking the longest:

int same;
int c;

// My code can run in different sizes so these needed to be writable.
int width1=64;
int height=64;

int row0[WIDTH]; // WIDTH is just the constant 64.

void check(int y)
{
  int alive=0;

  for(int x=0;x &lt; width1;++x)
  {
    alive=0;
    if(x &gt; 0)
    {
      if(getPixelColor(x-1, y) &gt; 0)
      {
        alive+=1;
        //Serial.println(&quot;(left)&quot;);
      }
    
    }
    if(x &lt; width1-1)
    {
      if(getPixelColor(x+1, y) &gt; 0)
      {
        alive+=1;
        //Serial.println(&quot;(right)&quot;);
      }
    }
    if(y &gt; 0)
    {
      if(getPixelColor(x, y-1) &gt; 0)
      {
        alive+=1;
        //Serial.println(&quot;(top)&quot;);
      }

      if(x &gt; 0)
      {
        if(getPixelColor(x-1, y-1) &gt; 0)
        {
          alive+=1;
          //Serial.println(&quot;(top left)&quot;);
        }
      }
      if(x &lt; width1-1)
      {
        if(getPixelColor(x+1, y-1) &gt; 0)
        {
          alive+=1;
          //Serial.println(&quot;(top right)&quot;);
        }
      }
    }
    if(row &lt; height-1)
    {
      if(getPixelColor(x, y+1) &gt; 0)
      {
        alive+=1;
        //Serial.println(&quot;(bottom)&quot;);
      }

      if(x &gt; 0)
      {
        if(getPixelColor(x-1, y+1) &gt; 0)
        {
          alive+=1;
          //Serial.println(&quot;(bottom left)&quot;);
        }
      }

      if(x &lt; width1-1)
      {
        if(getPixelColor(x+1, y+1) &gt; 0)
        {
          alive+=1;
          //Serial.println(&quot;(bottom right)&quot;);
        }
      }
    }
    god_Conway(x, y, alive);
  }
}

void god_Conway(int x, int y, int a)
{
  int born[]={3};
  int survive[]={2, 3};
  int kill=0;
  bool birth1=0;
  int living=getPixelColor(x, y);

  if(living &gt; 0)
  {
    if (a == 2 || a == 3)
    {
      kill=1;
    }
  else
  {
    kill=-1;
  }    
  }
  else
  {
    if (a == 3)
    {
      birth1=1;
    }
  }


  if (kill == -1 || birth1 == 1)
  {
    for(int c=0;c&lt;width1;c++)
    {
      if(row0[c]==-1)
      {
        row0[c]=x;
        if(c,width1)
        {
          row0[c+1]=-1;
        }
        break;
      }
    }
  }
  if(kill == 1 || birth1 == 0)
  {
    same++;
  }
}

This code checks around each pixel in a row and discovers how many pixels are on around a certain pixel. getPixelColor(x, y) is code I found for the matrix the reads the pixel's color and return a number greater than 0 if on. The check function takes about 29-30ms per row. Every millisecond counts.

I've tried a big if for just the non-edge pixels. getPixelColor(x, y) does not always return same number so dividing it by expected return number is not always accurate. I made a function to return 1 and 0 automatically then do alive+=That_function(x, y); but it slowed it down.

It only writes down the y of the pixels that needs changing on row0. The code that prints this stops when there is a -1.

答案1

得分: 1

不要使用耗时的函数 getPixelColor 从显示器中读取像素。相反,将棋盘保存在内存中,例如 static char board[64][64];,并在内存中保留前一代的副本。如果没有用于一次显示整个图像的库函数,您至少需要仅对更改了的像素调用假定的 setPixelColor 函数。您甚至可以使用 static char board[66][66];,其中图像索引为 1 到 64,避免处理边缘情况。

英文:

Don't use the time consuming function getPixelColor to read the pixels from the display. Instead maintain the board in memory, e. g. static char board[64][64];, and keep a copy of the previous generation also in memory. If there is no library function to display the whole image at once, you need at least to call the presumed setPixelColor function only for the changed pixels.
You might even use a static char board[66][66]; with the image indexes 1..64 and spare yourself the edge considerations.

huangapple
  • 本文由 发表于 2023年2月7日 02:57:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定