Skip to content

Instantly share code, notes, and snippets.

@biomood
Created September 18, 2011 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biomood/1225560 to your computer and use it in GitHub Desktop.
Save biomood/1225560 to your computer and use it in GitHub Desktop.
Conway's game of life for the Meggy Jr
#include <stdlib.h>
#include <MeggyJrSimple.h>
byte screen[8][8] = {{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}};
const byte LIVE = 4;
const byte DEAD = 0;
const byte CURSOR = 1;
byte current = 0;
char cursorx, cursory, iteration = 0;
boolean paused = true;
void setup() {
MeggyJrSimpleSetup();
display();
}
void loop() {
CheckButtonsPress();
if (Button_A)
paused = !paused;
if (!paused)
game_loop();
else
iterate_pause();
display();
}
void game_loop() {
delay(500);
copy_display(screen);
ClearSlate();
byte board[8][8] = {{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}};
iterate(board);
iteration++;
int i;
for (i=0; i<8; i++) {
int j;
for (j=0; j<8; j++) {
screen[i][j] = board[i][j];
}
}
}
// Display screen buffer
void display() {
SetAuxLEDs(iteration);
int i;
for (i=0; i<8; i++) {
int j;
for (j=0; j<8; j++) {
DrawPx(j, 7-i, screen[i][j]);
}
}
if (paused)
DrawPx(cursory, 7-cursorx, CURSOR);
DisplaySlate();
}
// Copy the screen buffer to board
void copy_display(byte board[][8]) {
int i;
for (i=0; i<8; i++) {
int j;
for (j=0; j<8; j++) {
board[i][j] = ReadPx(j, 7-i);
}
}
}
// A single iteration of gameoflife
void iterate(byte board[][8]) {
int i;
for (i=0; i<8; i++) {
int j;
for (j=0; j<8; j++) {
int neighbours = live_neighbours(screen, i, j);
switch (neighbours) {
case 0:
case 1:
board[i][j] = DEAD;
break;
case 2:
if (screen[i][j] == LIVE)
board[i][j] = LIVE;
break;
case 3:
if (screen[i][j] == LIVE)
board[i][j] = LIVE;
if (screen[i][j] == DEAD)
board[i][j] = LIVE;
break;
case 4:
case 5:
case 6:
case 7:
case 8:
if (screen[i][j] == LIVE)
board[i][j] = DEAD;
break;
}
}
}
}
// Returns the number of live neighbours for a given position and board
char live_neighbours(byte board[8][8], char i, char j) {
char neighbours = 0;
char above = i-1;
char below = i+1;
char left = j-1;
char right = j+1;
// wrap everything around
if (above == -1)
above = 7;
if (below == 8)
below = 0;
if (left == -1)
left = 7;
if (right == 8)
right = 0;
// check neighbours
if (board[above][left] == LIVE)
neighbours++;
if (board[above][j] == LIVE)
neighbours++;
if (board[above][right] == LIVE)
neighbours++;
if (board[i][left] == LIVE)
neighbours++;
if (board[i][right] == LIVE)
neighbours++;
if (board[below][left] == LIVE)
neighbours++;
if (board[below][j] == LIVE)
neighbours++;
if (board[below][right] == LIVE)
neighbours++;
return neighbours;
}
void iterate_pause() {
if (Button_B) {
screen[cursorx][cursory] = LIVE;
}
else if(Button_Right) {
cursory++;
if (cursory == 8)
cursory = 0;
}
else if(Button_Left) {
cursory--;
if (cursory == -1)
cursory = 7;
}
else if(Button_Up) {
cursorx--;
if (cursorx == -1)
cursorx = 7;
}
else if(Button_Down) {
cursorx++;
if (cursorx == 8)
cursorx = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment