Skip to content
Snippets Groups Projects
Commit 0573f8c5 authored by clemo's avatar clemo
Browse files

init


Signed-off-by: default avatarclemo <clemo@cbcode.at>
parents
No related branches found
No related tags found
No related merge requests found
Pipeline #4068 passed
build
stages:
- test
test:
image: gcc
stage: test
script:
- make test
artifacts:
paths:
- build
Makefile 0 → 100644
all: build
prebuild: clean
mkdir -p build
build: prebuild
gcc -O3 src/gol.c -c -o build/gol.o
buildDebug: prebuild
gcc -O3 src/gol.c -c -D GOL_DEBUG -o build/gol.o
test: buildDebug
gcc -O3 test/test.c build/gol.o -o build/test
./build/test > build/testOutput.txt
diff build/testOutput.txt test/output.txt
clean:
rm -fr build
src/gol.c 0 → 100644
#include "gol.h"
#include <stdlib.h>
#ifdef GOL_DEBUG
#include <stdio.h>
#endif
/**
* @param int width - width in byte (/=8)
* @param int height - height in bit
*/
gol *gol_setup(unsigned int width, unsigned int height)
{
gol *gameData = calloc(sizeof(gol), 1);
gameData->width = width;
gameData->height = height;
gameData->next = calloc(width * height, sizeof(unsigned int));
gameData->current = calloc(width * height, sizeof(unsigned int));
return gameData;
}
void gol_free(gol *data)
{
free(data->next);
free(data->current);
free(data);
}
unsigned int getCell(gol *g, unsigned int row, unsigned int colmn, unsigned int coffset)
{
return g->current[row * g->width + colmn] & (1 << (7 - coffset)) ? 1 : 0;
}
unsigned int sumNeighbours(gol *g, unsigned int row, unsigned int colmn, unsigned int coffset)
{
unsigned int sum = 0;
//up
unsigned int urow = row - 1;
if (row < 1)
{
urow = g->height - 1;
}
//left
unsigned int loffset = coffset - 1;
unsigned int lcolmn = colmn;
if (coffset < 1)
{
loffset = 7;
lcolmn = colmn - 1;
if (colmn < 1)
{
lcolmn = g->width - 1;
}
}
//right
unsigned int roffset = coffset + 1;
unsigned int rcolmn = colmn;
if (7 < roffset)
{
roffset = 0;
rcolmn += 1;
if (g->width <= rcolmn)
{
rcolmn = 0;
}
}
//down
unsigned int drow = row + 1;
if (g->height <= drow)
{
drow = 0;
}
//up - left
sum += getCell(g, urow, lcolmn, loffset);
//up
sum += getCell(g, urow, colmn, coffset);
//up -right
sum += getCell(g, urow, rcolmn, roffset);
//right
sum += getCell(g, row, rcolmn, roffset);
//down -right
sum += getCell(g, drow, rcolmn, roffset);
//down
sum += getCell(g, drow, colmn, coffset);
//down-left
sum += getCell(g, drow, lcolmn, loffset);
//left
sum += getCell(g, row, lcolmn, loffset);
return sum;
}
void printBits(unsigned int i, gol_print_colmn colmnFn)
{
for (unsigned int j = 0; j < 8; j++)
{
colmnFn((i & (1 << (7 - j)) ? 1 : 0));
}
}
void gol_each(gol *g, gol_print_colmn colmnFn, gol_print_ln lnFn)
{
for (unsigned int row = 0; row < g->height; row++)
{
for (unsigned int colmn = 0; colmn < g->width; colmn++)
{
#ifdef GOL_DEBUG
printf("<%d:%d>(%d) %d\t", row, colmn, row * g->width + colmn, g->current[row * g->width + colmn]);
#endif
printBits(g->current[row * g->width + colmn], colmnFn);
}
lnFn(row);
}
}
void gol_tick(gol *g)
{
for (unsigned int row = 0; row < g->height; row++)
{
for (unsigned int colmn = 0; colmn < g->width; colmn++)
{
g->next[row * g->width + colmn] = 0;
for (unsigned int coff = 0; coff < 8; coff++)
{
const unsigned int sn = sumNeighbours(g, row, colmn, coff);
if (getCell(g, row, colmn, coff) == 1)
{
//living cell
if (1 < sn && sn < 4)
{
g->next[row * g->width + colmn] |= (1 << (7 - coff));
}
}
else
{
//dead cell
if (sn == 3)
{
g->next[row * g->width + colmn] |= (1 << (7 - coff));
}
}
}
}
}
unsigned int *tmp = g->current;
g->current = g->next;
g->next = tmp;
}
#ifndef GOL_H
#define GOL_H
struct GOL
{
unsigned int *current;
unsigned int *next;
unsigned int width;
unsigned int height;
} typedef gol;
typedef void (*gol_print_colmn)(unsigned int value);
typedef void (*gol_print_ln)(unsigned int line);
gol *gol_setup(unsigned int width, unsigned int height);
void gol_tick(gol *g);
void gol_each(gol *g, gol_print_colmn colmn, gol_print_ln ln);
void gol_free(gol *data);
#endif
\ No newline at end of file
round: 0
<0:0>(0) 0 00000000<0:1>(1) 0 00000000<0:2>(2) 0 00000000
<1:0>(3) 0 00000000<1:1>(4) 0 00000000<1:2>(5) 0 00000000
<2:0>(6) 0 00000000<2:1>(7) 28 00011100<2:2>(8) 0 00000000
<3:0>(9) 0 00000000<3:1>(10) 0 00000000<3:2>(11) 0 00000000
<4:0>(12) 0 00000000<4:1>(13) 0 00000000<4:2>(14) 0 00000000
round: 1
<0:0>(0) 0 00000000<0:1>(1) 0 00000000<0:2>(2) 0 00000000
<1:0>(3) 0 00000000<1:1>(4) 8 00001000<1:2>(5) 0 00000000
<2:0>(6) 0 00000000<2:1>(7) 8 00001000<2:2>(8) 0 00000000
<3:0>(9) 0 00000000<3:1>(10) 8 00001000<3:2>(11) 0 00000000
<4:0>(12) 0 00000000<4:1>(13) 0 00000000<4:2>(14) 0 00000000
round: 2
<0:0>(0) 0 00000000<0:1>(1) 0 00000000<0:2>(2) 0 00000000
<1:0>(3) 0 00000000<1:1>(4) 0 00000000<1:2>(5) 0 00000000
<2:0>(6) 0 00000000<2:1>(7) 28 00011100<2:2>(8) 0 00000000
<3:0>(9) 0 00000000<3:1>(10) 0 00000000<3:2>(11) 0 00000000
<4:0>(12) 0 00000000<4:1>(13) 0 00000000<4:2>(14) 0 00000000
#include "../src/gol.h"
#include <assert.h>
#include <stdio.h>
int ROUNDS = 2;
void print_colmn(unsigned int value)
{
printf("%d", value);
}
void print_row(unsigned int line)
{
printf("\n");
}
int main(int argc, char **argv)
{
gol *game = gol_setup(3, 5);
game->current[7] = 28; // set --- (blinker)
int ROUNDS = 2;
unsigned int round = 0;
while (1)
{
printf("round: %d \n", round++);
gol_each(game, &print_colmn, &print_row);
if (round > ROUNDS)
{
break;
}
gol_tick(game);
}
gol_free(game);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment