Skip to content

Instantly share code, notes, and snippets.

@kamiyaowl
Created March 6, 2014 18:22
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 kamiyaowl/9396136 to your computer and use it in GitHub Desktop.
Save kamiyaowl/9396136 to your computer and use it in GitHub Desktop.
lifegameをavr向けに再実装
#include<stdio.h>
#include<time.h>
#include "linear_rand.h"
#define WORLD_W 128
#define WORLD_H 64
#define TRUE 1
#define FALSE 0
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
void world_init(uint8_t (*world)[WORLD_H]){
uint16_t i,j;
for(j = 0 ; j < WORLD_H ; ++j){
for(i = 0 ; i < WORLD_W ; ++i){
world[i][j] = rnd_next() > 0x8000;
}
}
}
uint8_t world_around_count(uint8_t (*world)[WORLD_H], int x,int y){
uint8_t count = 0;
if(x > 0 && world[x - 1][y ]) ++count;//left
if(x < WORLD_W && world[x + 1][y ]) ++count;//right
if(y > 0 && world[x ][y - 1]) ++count;//up
if(y < WORLD_H && world[x ][y + 1]) ++count;//down
if(x > 0 && y > 0 && world[x - 1][y - 1]) ++count;//up left
if(x < WORLD_W && y > 0 && world[x + 1][y - 1]) ++count;//up right
if(x > 0 && y < WORLD_H && world[x - 1][y + 1]) ++count;//down left
if(x < WORLD_W && y < WORLD_H && world[x + 1][y + 1]) ++count;//down right
return count;
}
void world_print(uint8_t (*world)[WORLD_H]){
uint16_t i,j;
for(j = 0 ; j < WORLD_H ; ++j){
for(i = 0 ; i < WORLD_W ; ++i){
printf("%c",world[i][j] ? '#' : ' ');
}
printf("\n");
}
}
uint8_t world_live(uint8_t (*world)[WORLD_H],int x, int y) {
uint8_t count = world_around_count(world,x,y);
if(!world[x][y]){
if(count == 3) return TRUE;
else return FALSE;
} else {
if(count == 2 || count == 3) return TRUE;
else return FALSE;
}
}
void world_next(uint8_t (*old_world)[WORLD_H],uint8_t (*new_world)[WORLD_H]){
uint16_t i,j;
for(j = 0 ; j < WORLD_H ; ++j){
for(i = 0 ; i < WORLD_W ; ++i){
new_world[i][j] = world_live(old_world,i,j);
}
}
}
uint8_t world_eval(uint8_t (*world)[WORLD_H]){
uint16_t i,j,count = 0;
for(j = 0 ; j < WORLD_H ; ++j){
for(i = 0 ; i < WORLD_W ; ++i){
if(world[i][j]) ++count;
}
}
return count;
}
void world_copy(uint8_t (*old_world)[WORLD_H],uint8_t (*new_world)[WORLD_H]){
uint16_t i,j;
for(j = 0 ; j < WORLD_H ; ++j){
for(i = 0 ; i < WORLD_W ; ++i){
new_world[i][j] = old_world[i][j];
}
}
}
//////////////////////////////////////////////
int main(void){
uint8_t eval = 0,cnt = 0;
uint8_t world[WORLD_W][WORLD_H];
uint8_t tmp[WORLD_W][WORLD_H];
rnd_init(clock());
world_init(world);
for(; ; ++cnt) {
world_print(world);
world_next(world,tmp);
world_copy(tmp,world);
}
return 0;
}
#include<stdio.h>
#define LINEAR_RAND_A 61
#define LINEAR_RAND_C 5
unsigned int rnd_current = 45;//TODO:Enviroment value here
unsigned int rnd_init(unsigned int seed){
rnd_current = seed;
}
unsigned int rnd_next(){
rnd_current = (rnd_current * LINEAR_RAND_A + LINEAR_RAND_C) & 0xffff;
return rnd_current;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment