Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_ST7735.h>
- #include <Adafruit_seesaw.h>
- #include <Adafruit_TFTShield18.h>
- Adafruit_TFTShield18 ss;
- const int w = 20;
- const int h = 16;
- const int initialPopulation = 50;
- byte board[2][w][h];
- byte g = 0;
- #define SD_CS 4
- #define TFT_CS 10
- #define TFT_DC 8
- #define TFT_RST -1
- Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
- void setup(void) {
- ss.begin();
- ss.tftReset();
- tft.initR(INITR_GREENTAB);
- tft.fillScreen(0x0000);
- tft.setRotation(3);
- Serial.begin(9600);
- for (int32_t i=TFTSHIELD_BACKLIGHT_OFF; i<TFTSHIELD_BACKLIGHT_ON; i+=100) {
- ss.setBacklight(i);
- delay(1);
- }
- }
- void seedRandomGeneration(char *buf,int count)
- {
- randomSeed(analogRead(A1));
- int x = random(w);
- int y = random(h);
- board[g][x][y] = 1;
- int tab[2];
- tab[0]=x;
- tab[1]=y;
- Serial.println(x);
- Serial.println(y);
- return tab;
- }
- void displayBoard()
- {
- tft.fillScreen(0x0000);
- for (int x = 0; x < w; x++)
- {
- for (int y = 0; y < h; y++)
- {
- if (board[g][x][y])
- {
- tft.fillRect(x * 8, y * 8, 8, 8, ST7735_BLUE);
- }
- }
- }
- }
- void loop()
- {
- uint32_t buttons = ss.readButtons();
- if(! (buttons & TFTSHIELD_BUTTON_2)){
- displayBoard();
- int x1,y1;
- int p[2];
- p=seedRandomGeneration();
- x1=p(0);
- y1=p(1);
- Serial.println(x1);
- Serial.println(y1);
- if(! (buttons & TFTSHIELD_BUTTON_DOWN)){
- if (board[g][x1][y1])
- {
- board[g][x1][y1] = 0;
- board[g][x1][y1-8] = 1;
- displayBoard();
- }
- }
- if(! (buttons & TFTSHIELD_BUTTON_LEFT)){
- }
- if(! (buttons & TFTSHIELD_BUTTON_UP)){
- }
- if(! (buttons & TFTSHIELD_BUTTON_RIGHT)){
- }
- }
- if(! (buttons & TFTSHIELD_BUTTON_1)){
- displayBoard();
- updateGeneration();
- g = !g;
- }
- delay(100);
- }
- void updateGeneration()
- {
- for (int x = 0; x < w; x++)
- {
- for (int y = 0; y < h; y++)
- {
- board[!g][x][y] = updateCell(x, y);
- }
- }
- }
- byte updateCell(int x, int y)
- {
- int n = numNeighbors(x, y);
- if (board[g][x][y] == 1)
- {
- if (n < 2) return 0;
- else if (n == 2 || n == 3) return 1;
- else if (n > 3) return 0;
- }
- else
- {
- return (n == 3);
- }
- }
- int numNeighbors(int x, int y)
- {
- int n = 0;
- for (int dx = -1; dx <= 1; dx++)
- {
- for (int dy = -1; dy <= 1; dy++)
- {
- int x1 = x + dx;
- int y1 = y + dy;
- boolean middle = (dx == 0 && dy == 0);
- if (!middle && x1 > 0 && x1 < w && y1 > 0 && y1 < h)
- {
- n += board[g][x1][y1];
- }
- }
- }
- Serial.print(n);
- return n;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement