Source: http://www.math.com/students/wonders/life/life.html
用Arduino寫成的Game of Life程式如下:
void gameOfLife()
{
for(int i=0; i<49;>
{
int thisLED = markLED[1][i];
int y = i / 7;
int x = i - y * 7;
int up = i - 7;
int down = i + 7;
int left = i - 1;
int right = i + 1;
int n1 = markLED[1][up];
int n2 = markLED[1][down];
int n3 = markLED[1][left];
int n4 = markLED[1][right];
int leftUpCorner = i - 7 - 1;
int rightUpCorner = i - 7 + 1;
int leftDownCorner = i + 7 - 1;
int rightDownCorner = i + 7 + 1;
int n5 = markLED[1][leftUpCorner];
int n6 = markLED[1][rightUpCorner];
int n7 = markLED[1][leftDownCorner];
int n8 = markLED[1][rightDownCorner];
if (y == 0) n1 = n5 = n6 = 0;
if (y == 6) n2 = n7 = n8 = 0;
if (x == 0) n3 = n5 = n7 = 0;
if (x == 6) n4 = n6 = n8 = 0;
int numNeibr = n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8;
if (thisLED == 0) // Check Dead Cell
{
if (numNeibr == 3)
{
lc.setLed(0, y, x, true);
markLED[0][x + y * 7] = 1;
}
}
if (thisLED == 1) // Check Live Cell
{
if (numNeibr <> 3) // die
{
lc.setLed(0, y, x, false);
markLED[0][x + y * 7] = 0;
}
if (numNeibr == 2 || numNeibr == 3) // live
{
lc.setLed(0, y, x, true);
markLED[0][x + y * 7] = 1;
}
}
}
}
(以上只是程式的一部分)
將這個程式upload到Arduino Board,可以使LED產生如同Game of Life的效果。
沒有留言:
張貼留言