Life

Una cavolata al volo per rilassarsi e fare due saldature due…

Un mini-life per far girare il glider:

The Adobe Flash Plugin is needed to display this content.

Ecco il sorgente, non e' bellissimo perche' ho lasciato anche tutte le prove…

//#include <Ethernet.h>
//#include <FlexiTimer2.h>

/*
  Life 5x5
 */

#define MINDELAY 2
#define DELAY 15
#define XMAX 4
#define YMAX 4

#define DEBUG 0

// da 12 a 8
#define PINX 12-

// da 7 a 3
#define PINY 7-

byte screen[XMAX+1][YMAX+1];
byte shadow[XMAX+1][YMAX+1]; // per fare i conti tra un ciclo e l'altro

void setup() {
    // init seriale
    Serial.begin (9600);

    // initialize digital pins as an output.
    for(byte p=2; p<=13; p++) {
        pinMode(p, OUTPUT);
    }

    resettaScreen();
    reset();

    //tanto per fare un test...
    //randomSeed(analogRead(0));


    // GLIDER
    screen[0][0]=1;
    screen[0][1]=1;
    screen[0][2]=1;
    screen[2][1]=1;
    screen[1][2]=1;

    // BLINKER
    /* in mezzo
        screen[2][1]=1;
        screen[2][2]=1;
        screen[2][3]=1;
    */
    /* su bordo
    screen[4][1]=1;
    screen[4][2]=1;
    screen[4][3]=1;
    */



    // ci ho provato... si pianta tutto
    //FlexiTimer2::set(250, displayScreen);
    //FlexiTimer2::start();
}

void loop() {
    //delay(MINDELAY);

    if(DEBUG) {
        printScreen();
        lifeCycle();
        delay(1000);
    } else {
        displayScreen();
        // computa solo ogni tanto
        if(millis()%5==0) {
            flash(13); // just a check
            lifeCycle();
        }
    }


    //tanto per fare un test...
    //screen[random(XMAX+1)][random(YMAX+1)]=random(2);

    delay(DELAY);
}

// calcola intorno cella e restituisce gia' il nuovo valore (0/1) di vita
char life(char x, char y) {
    char alive=0, edgex=0, edgey=0;

    // calcola num celle vive intorno a x,y
    for(char vx=(x-1); vx<=(x+1); vx++) {
        for(char vy=(y-1); vy<=(y+1); vy++) {
            if(vx==x && vy==y) continue;

            if(DEBUG) {
                Serial.print("vx e vy (");
                Serial.print((int)vx);
                Serial.print(",");
                Serial.print((int)vy);
                Serial.println(")");
            }

            // edges
            edgex=vx;
            if(vx==-1) edgex=XMAX;
            else if(vx==XMAX+1) edgex=0;

            edgey=vy;
            if(vy==-1) edgey=YMAX;
            else if(vy==YMAX+1) edgey=0;
            //

            if(DEBUG) {
                Serial.print("vx e vy EDGE (");
                Serial.print((int)edgex);
                Serial.print(",");
                Serial.print((int)edgey);
                Serial.print("), alive=");
                Serial.println(screen[edgex][edgey]);
            }

            //calc
            alive+=screen[edgex][edgey];
        }
    }

    if(DEBUG) {
        Serial.print("(");
        Serial.print((int)x);
        Serial.print(",");
        Serial.print((int)y);
        Serial.print(")");
        Serial.print(", alive: ");
        Serial.print((int)alive);
        Serial.println();
    }

    // ritorna 0/1 in funzione dello stato cella e del numero trovato
    if(screen[x][y]==1)
    {
        // Any live cell with fewer than two live neighbours dies, as if caused by under-population.
        if(alive<2) return 0;

        // Any live cell with two or three live neighbours lives on to the next generation.
        if(alive==2 || alive==3) return 1;

        // Any live cell with more than three live neighbours dies, as if by overcrowding.
        if(alive>3) return 0;
    }
    else
    {
        // Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
        if(alive==3) return 1;
    }

    return 0; // in teoria NON dovrebbe mai passare di qui
}

void lifeCycle() {
// cicla sulle celle, calcola lo stato nuovo (in shadow), edge toroide
    for(byte x=0; x<=XMAX; x++) {
        for(byte y=0; y<=YMAX; y++) {
            shadow[x][y]=life(x,y);
        }
    }

// copia shadow su screen
    for(byte x=0; x<=XMAX; x++) {
        for(byte y=0; y<=YMAX; y++) {
            screen[x][y]=shadow[x][y];
            shadow[x][y]=0;
        }
    }
}


void resettaScreen() {
    for(byte x=0; x<=XMAX; x++) {
        for(byte y=0; y<=YMAX; y++) {
            screen[x][y]=0;
        }
    }
}

void displayScreen() {
    //flash(13); // just a check
    for(byte x=0; x<=XMAX; x++) {
        for(byte y=0; y<=YMAX; y++) {
            if(screen[x][y]==1) {
                //onLed(x,y);
                flashLed(x,y);
            }
        }
    }
}

void printScreen() {
    //flash(13); // just a check
    for(byte x=0; x<=XMAX; x++) {
        for(byte y=0; y<=YMAX; y++) {
            Serial.print((char)(screen[x][y]+'0'));
        }
        Serial.println();
    }
    Serial.println("------");
}


void flash(byte pin) {
    digitalWrite(pin, HIGH);   // set the LED on
    delay(1);              // wait for a second
    digitalWrite(pin, LOW);    // set the LED off
}

void reset() {
    for(byte p=2; p<=13; p++) {
        digitalWrite(p, LOW);
    }
}


void flashLed(byte x, byte y) {
    //was... onLed(x,y);
    for(byte yy=0; yy<=YMAX; yy++) {
        if(yy==y) {
            digitalWrite(PINY yy, LOW);
        }
        else {
            digitalWrite(PINY yy, HIGH);
        }
    }
    digitalWrite(PINX x, HIGH);

    delay(MINDELAY);
    reset();
}

Un primo test:

The Adobe Flash Plugin is needed to display this content.

You could leave a comment if you were logged in.
pub/arduino/life.txt · Last modified: 2013/07/16 09:09 by atrent
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki