Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- copperd
- Date:
- 2010-12-21
- Revision:
- 0:3e9d11eb5a2e
- Child:
- 1:714e68b0ba58
File content as of revision 0:3e9d11eb5a2e:
#include "mbed.h"
#include "TFT_4DGL.h"
unsigned short int displayold[26][35];
unsigned short int displaynew[26][35];
int sx,sy,clst,x,y,test,loops;
TFT_4DGL ecran(p9,p10,p11); // serial tx, serial rx, reset pin;
int main() {
srand(1); // Random seed
ecran.baudrate(115200);
ecran.background_color(WHITE);
ecran.pen_size(SOLID);
// Set up Grid
// Display field is 34 x 25
y = 0;
x = 0;
for (sy = 1; sy <=34;sy++)
{
y=sy+1*sy*8;
ecran.line(9,y,233,y,BLUE);
y=(sy+1*sy*8)+8;
ecran.line(9,y,233,y,BLUE);
}
for (sx = 1; sx <=25;sx++)
{
x=sx+1*sx*8;
ecran.line(x,9,x,314,BLUE);
x=(sx+1*sx*8)+8;
ecran.line(x,9,x,314,BLUE);
}
// forever run the next code to create a random field every 500 turns and play it out
while(1)
{
// Set up a random field and display
for (sy = 1; sy <= 34; sy++)
{
for (sx = 1; sx <= 25; sx++)
{
if (rand()%10 == 4)
{
displayold[sx][sy] = 1;
ecran.rectangle(((9*sx)+1),((9*sy)+1),((9*sx)+7),((9*sy)+7),RED);
}
}
}
// create new field from old field and run for 500 turns
for (loops = 0;loops <= 500; loops++)
{
for (sy = 1; sy <= 34; sy++)
{
for (sx = 1; sx <= 25; sx++)
{
// Cheacking a block if its dead or alive then for neighbors and counting them and putting that in test
if (displayold[sx][sy] == 1)
{
test = displayold[sx-1][sy-1] + displayold[sx][sy-1] + displayold[sx+1][sy-1] + displayold[sx-1][sy] + displayold[sx+1][sy] + displayold[sx-1][sy+1] + displayold[sx][sy+1] + displayold[sx+1][sy+1];
if (test == 2 || test == 3)
{
displaynew[sx][sy] = 1;
}
else displaynew[sx][sy] = 0;
}
else
{
test = displayold[sx-1][sy-1] + displayold[sx][sy-1] + displayold[sx+1][sy-1] + displayold[sx-1][sy] + displayold[sx+1][sy] + displayold[sx-1][sy+1] + displayold[sx][sy+1] + displayold[sx+1][sy+1];
if (test == 3)
{
displaynew[sx][sy] = 1;
}
}
}
}
// Now we have a new field created we can update the squares only if they are differnt and move the new array to the old one
for (sy = 1; sy <= 34; sy++)
{
for (sx = 1; sx <= 25; sx++)
{
if (displaynew[sx][sy] != displayold[sx][sy])
{
displayold[sx][sy] = displaynew[sx][sy];
if (displaynew[sx][sy] == 1) ecran.rectangle(((9*sx)+1),((9*sy)+1),((9*sx)+7),((9*sy)+7),RED);
else ecran.rectangle(((9*sx)+1),((9*sy)+1),((9*sx)+7),((9*sy)+7),WHITE);
}
}
}
}
}
}