Skip to content

Instantly share code, notes, and snippets.

@ibuclaw
Forked from paultag/gol.c
Created February 22, 2011 19:22
Show Gist options
  • Save ibuclaw/839195 to your computer and use it in GitHub Desktop.
Save ibuclaw/839195 to your computer and use it in GitHub Desktop.
import std.stdio;
import core.stdc.limits;
import std.math;
uint WORLD;
uint LASTWORLD;
uint NEWWORLD;
uint ROW_L;
int offset( uint x, uint y ) {
uint offset_bit = (y*ROW_L)+x;
uint numeric_val = pow(2,offset_bit);
return numeric_val;
}
int pokeCell( int x, int y ) {
if (
x >= 0 &&
y >= 0 &&
x < ROW_L &&
y < ROW_L
) {
int bit = offset(x, y);
return ( WORLD & bit ) != 0;
} else {
return -1;
}
}
void setCell(uint x, uint y) {
uint bit = offset(x,y);
WORLD += bit;
}
void unsetCell(uint x, uint y) {
uint bit = offset(x,y);
WORLD -= bit;
}
uint getX( uint offset ) {
return ( offset % ROW_L );
}
uint getY( uint offset ) {
return ( offset / ROW_L );
}
uint getDelta() {
uint ret = (WORLD & LASTWORLD);
return (WORLD-ret);
}
uint getNextOffset( int MIDWORLD ) {
uint oldmidworld = MIDWORLD;
MIDWORLD = ((MIDWORLD) & (MIDWORLD-1));
if ( MIDWORLD == 0 ) {
if ( oldmidworld != 0 )
return oldmidworld;
else
return 0;
} else {
return oldmidworld^MIDWORLD;
}
}
void processNode( int x, int y ) {
// std::cout << x << ", " << y << std::endl;
int nCount = 0;
int bitflag = offset(x,y);
if ( pokeCell(x,y-1) == 1 )
nCount++;
if ( pokeCell(x-1,y-1) == 1 )
nCount++;
if ( pokeCell(x-1,y) == 1 )
nCount++;
if ( pokeCell(x+1,y-1) == 1 )
nCount++;
if ( pokeCell(x-1,y+1) == 1 )
nCount++;
if ( pokeCell(x+1,y) == 1 )
nCount++;
if ( pokeCell(x+1,y+1) == 1 )
nCount++;
if ( pokeCell(x,y+1) == 1 )
nCount++;
if ( nCount < 2 )
if ( ( NEWWORLD & bitflag ) > 0 )
NEWWORLD -= bitflag;
if ( nCount > 3 )
if ( ( NEWWORLD & bitflag ) > 0 )
NEWWORLD -= bitflag;
if ( nCount == 3 )
if ( ( NEWWORLD & bitflag ) == 0 )
NEWWORLD += bitflag;
}
void processNode( int x, int y, int flag ) {
processNode(x,y);
if ( flag ) {
processNode(x,y-1);
processNode(x-1,y-1);
processNode(x-1,y);
processNode(x+1,y-1);
processNode(x-1,y+1);
processNode(x+1,y);
processNode(x+1,y+1);
processNode(x,y+1);
}
}
void renderWorld() {
for ( int i = 0; i < ROW_L; ++i ) {
for ( int n = 0; n < ROW_L; ++n ) {
if ( pokeCell(i,n) ) {
write(" O");
} else {
write(" X");
}
}
writeln();
}
writeln();
}
void main( char[][] args ) {
WORLD = 0;
LASTWORLD = 0;
ROW_L = 7;
// std::cout << "Max Chunk XY: " << ROW_L << std::endl;
setCell(1,1);
setCell(1,2);
setCell(1,3);
int turn = 0;
int CAP = 100;
while ( WORLD != LASTWORLD
&& turn < CAP
) {
turn++;
renderWorld();
writeln(WORLD);
NEWWORLD = WORLD;
uint delt = getDelta();
uint next = getNextOffset(delt);
delt -= next;
while ( next != 0 ) {
uint x = getX(cast(uint)log2(next));
uint y = getY(cast(uint)log2(next));
processNode(x,y,1);
next = getNextOffset(delt);
delt -= next;
}
LASTWORLD = WORLD;
WORLD = NEWWORLD;
}
renderWorld();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment