game of life

This commit is contained in:
robelix 2018-01-21 19:02:10 +01:00
parent ac8a575227
commit e4c6f3c56b

View file

@ -37,7 +37,6 @@ X X X X
XX XX XX XX
*/ */
byte invaderA_1[11] = {112,24,125,182,188,60,188,182,125,24,112};
#define LATCHPIN 5 #define LATCHPIN 5
#define DATAPIN 4 #define DATAPIN 4
@ -46,6 +45,9 @@ byte ledsData[8];
byte currentled = 0; byte currentled = 0;
// low level functions
void clear() { void clear() {
for (int i=0; i < 8; i++) { for (int i=0; i < 8; i++) {
// low is led on // low is led on
@ -76,25 +78,30 @@ void pushout() {
digitalWrite(LATCHPIN, HIGH); digitalWrite(LATCHPIN, HIGH);
} }
void setup() { // space invader
pinMode(LATCHPIN, OUTPUT);
pinMode(DATAPIN, OUTPUT);
pinMode(CLOCKPIN, OUTPUT);
clear(); //byte invaderA0[11] = {112,24,125,182,188,60,188,182,125,24,112};
pushout(); //byte invaderA1[11] = {14,24,189,118,60,60,60,118,189,24,14};
static byte invaderA0[8] = {152,92,182,95,95,182,92,152};
static byte invaderA1[8] = {88,188,22,63,63,22,188,88};
bool invaderread(int x, int y, bool invaderstate) {
if (invaderstate) {
return bitRead(invaderA0[x],y);
} else {
return bitRead(invaderA1[x],y);
}
} }
void drawinvader(int xposition, int yposition) { void drawinvader(int xposition, int yposition, bool invaderstate) {
// TODO yposition for (int x=0; x<8; x++) {
for (int x=0; x<=11; x++) {
int screenx = x+xposition; int screenx = x+xposition;
if (screenx >=0 && screenx < 8) { if (screenx >=0 && screenx < 8) {
for (int y=0; y<8; y++) { for (int y=0; y<8; y++) {
int screeny = y+yposition; int screeny = y+yposition;
if (screeny >=0 && screeny < 8) { if (screeny >=0 && screeny < 8) {
if (bitRead(invaderA_1[x],y) == HIGH ) { if ( invaderread(x,y,invaderstate) ) {
writexy(screenx,screeny); writexy(screenx,screeny);
} }
} }
@ -110,10 +117,201 @@ bool direction = true;
int invaderstart = -12; int invaderstart = -12;
int invaderend = 8; int invaderend = 8;
int invaderposition = invaderstart; int invaderposition = invaderstart;
bool invaderstate = true;
int statecount = 0;
// Game of life
byte gol_last[8];
byte gol_next[8];
byte gol_last2[8];
byte gol_last3[8];
byte gol_last4[8];
long gol_stepcount;
int gol_deadcount = 0;
void gol_setup() {
gol_deadcount = 0;
for (int x=0; x<8; x++) {
gol_last[x] = 0;
for (int y=0; y<8; y++) {
if (random(4)<1) {
bitWrite(gol_last[x], y, HIGH);
}
}
}
}
void setup() {
pinMode(LATCHPIN, OUTPUT);
pinMode(DATAPIN, OUTPUT);
pinMode(CLOCKPIN, OUTPUT);
gol_setup();
clear();
pushout();
}
void gol_clearnext() {
for (int i=0; i<8; i++) {
gol_next[i] = 0;
}
}
bool gol_getstate(int x, int y) {
// warp around
if (x<0) {
x=7;
}
if (x>7) {
x=0;
}
if (y<0) {
y=7;
}
if (y>7) {
y=7;
}
return bitRead(gol_last[x],y);
}
byte gol_countNeighbours(int x, int y) {
byte count = 0;
for (int xoff=-1; xoff <= 1; xoff++) {
for (int yoff=-1; yoff <= 1; yoff++) {
if ( !(xoff==0 && yoff==0) ) {
if (gol_getstate(x+xoff, y+yoff) ) {
count++;
}
}
}
}
return count;
}
void gol_setnext(int x, int y) {
bitWrite( gol_next[x], y, HIGH);
}
void gol_writeLEDS() {
for(int x=0; x<8; x++) {
for (int y=0; y<8; y++) {
if (bitRead(gol_next[x],y)) {
writexy(x, y);
}
}
}
}
int gol_pixelcount;
int gol_diffrences;
int gol_diffrences2;
int gol_diffrences3;
int gol_diffrences4;
void gol_step() {
gol_clearnext();
gol_pixelcount = 0;
for (int x=0; x<8; x++) {
for (int y=0; y<8; y++) {
bool myself = gol_getstate(x,y);
byte neighbours = gol_countNeighbours(x,y);
if ( neighbours==3) {
gol_setnext(x,y);
gol_pixelcount++;
}
if ( neighbours==2 && myself) {
gol_setnext(x,y);
gol_pixelcount++;
}
}
}
gol_writeLEDS();
pushout();
gol_diffrences = 0;
gol_diffrences2 = 0;
gol_diffrences3 = 0;
gol_diffrences4 = 0;
for (int i=0; i<8; i++) {
if (gol_last4[i] != gol_next[i]) {
gol_diffrences4++;
}
gol_last4[i] = gol_last3[i];
if (gol_last3[i] != gol_next[i]) {
gol_diffrences3++;
}
gol_last3[i] = gol_last2[i];
if (gol_last2[i] != gol_next[i]) {
gol_diffrences2++;
}
gol_last2[i] = gol_last[i];
if (gol_last[i] != gol_next[i]) {
gol_diffrences++;
}
gol_last[i] = gol_next[i];
}
if (gol_diffrences == 0 || gol_diffrences2==0 || gol_diffrences3 == 0 || gol_diffrences4 == 0 || gol_pixelcount == 0) {
//went static
gol_deadcount++;
}
gol_stepcount++;
delay(200);
}
// main loop
int mode = 0;
#define NRMODES 1
void switchmode( ){
mode = random(NRMODES);
switch(mode) {
case 0: //game of life
gol_stepcount = 0;
gol_setup();
break;
}
}
void loop() { void loop() {
clear(); clear();
// check for end
switch(mode) {
case 0: // game of life
if (gol_stepcount >200) {
switchmode();
}
if (gol_deadcount > 12) {
switchmode();
}
break;
}
switch(mode) {
case 0: // game of life
gol_step();
break;
}
/* /*
// x-line // x-line
for(byte i=0; i<8; i++) { for(byte i=0; i<8; i++) {
@ -125,15 +323,26 @@ void loop() {
} }
*/ */
drawinvader(invaderposition,0); /* invaders
invaderposition++; drawinvader(0,0, invaderstate);
if (invaderposition >=invaderend) {
invaderposition = invaderstart;
}
pushout(); pushout();
invaderstate = !invaderstate;
statecount++;
if (statecount >=4) {
invaderposition++;
if (invaderposition >=invaderend) {
invaderposition = invaderstart;
}
statecount = 0;
}
invaders end */
/* /*
if(direction) { if(direction) {
currentcol++; currentcol++;
@ -149,5 +358,4 @@ void loop() {
direction = true; direction = true;
} }
*/ */
delay(300);
} }