### added "naive" and "wraparound" algorithms to lpfklife

Wed, 03 Sep 2008 17:15:47 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Wed, 03 Sep 2008 17:15:47 +0100
changeset 10
f76c929f65e6
parent 9
1519a9a8d924
child 11
bfaaf539bc4d

added "naive" and "wraparound" algorithms to lpfklife

 test/lpfklife.c file | annotate | diff | revisions
```     1.1 --- a/test/lpfklife.c	Tue Aug 26 22:13:49 2008 +0100
1.2 +++ b/test/lpfklife.c	Wed Sep 03 17:15:47 2008 +0100
1.3 @@ -5,6 +5,8 @@
1.4  #include <string.h>
1.5  #include "liblpfk.h"
1.6
1.7 +#define LIFE_ALGORITHM_NAIVE
1.8 +
1.9  /************************
1.10   * copied from http://linux-sxs.org/programming/kbhit.html
1.11   */
1.12 @@ -153,19 +155,79 @@
1.13  				nei = 0;
1.14
1.15  				// count neighbours
1.16 +#ifdef LIFE_ALGORITHM_NAIVE
1.17 +				/// NAIVE ALGORITHM
1.18 +				// Assumes every cell outside the grid is dead.
1.19  				if (y > 0) {
1.20  					if (x > 0) { if (old_gamegrid[y-1][x-1]) nei++; }
1.21  					if (old_gamegrid[y-1][x]) nei++;
1.22  					if (x < 5) { if (old_gamegrid[y-1][x+1]) nei++; }
1.23  				}
1.24 +
1.25  				if (x > 0) { if (old_gamegrid[y][x-1]) nei++; }
1.26  				// old_gamegrid[x][y] is us!
1.27  				if (x < 5) { if (old_gamegrid[y][x+1]) nei++; }
1.28 +
1.29  				if (y < 5) {
1.30  					if (x > 0) { if (old_gamegrid[y+1][x-1]) nei++; }
1.31  					if (old_gamegrid[y+1][x]) nei++;
1.32  					if (x < 5) { if (old_gamegrid[y+1][x+1]) nei++; }
1.33  				}
1.34 +#else
1.35 +				/// WRAPPING ALGORITHM
1.36 +				// Accesses off of one side of the grid are wrapped to the
1.37 +				// opposite side.
1.38 +				//
1.39 +				// WARNING: this algorithm has NOT been extensively tested,
1.40 +				// and completely screws up the glider test.
1.41 +				//
1.42 +				// TODO: test and debug:
1.43 +				if (y > 0) {
1.44 +					if (x > 0)	{ if (old_gamegrid[y-1][x-1]) nei++; }
1.45 +						else	{ if (old_gamegrid[y-1][5]) nei++; }
1.46 +
1.47 +					if (old_gamegrid[y-1][x]) nei++;
1.48 +
1.49 +					if (x < 5)	{ if (old_gamegrid[y-1][x+1]) nei++; }
1.50 +						else	{ if (old_gamegrid[y-1][0]) nei++; }
1.51 +				} else {
1.52 +					if (x > 0)	{ if (old_gamegrid[5][x-1]) nei++; }
1.53 +						else	{ if (old_gamegrid[5][5]) nei++; }
1.54 +
1.55 +					if (old_gamegrid[y-1][x]) nei++;
1.56 +
1.57 +					if (x < 5)	{ if (old_gamegrid[5][x+1]) nei++; }
1.58 +						else	{ if (old_gamegrid[5][0]) nei++; }
1.59 +				}
1.60 +
1.61 +
1.62 +				if (x > 0)	{ if (old_gamegrid[y][x-1]) nei++; }
1.63 +					else	{ if (old_gamegrid[y][5]) nei++; }
1.64 +
1.65 +				// old_gamegrid[x][y] is us!
1.66 +
1.67 +				if (x < 5)	{ if (old_gamegrid[y][x+1]) nei++; }
1.68 +					else	{ if (old_gamegrid[y][0]) nei++; }
1.69 +
1.70 +
1.71 +				if (y < 5) {
1.72 +					if (x > 0) { if (old_gamegrid[y+1][x-1]) nei++; }
1.73 +						else	{ if (old_gamegrid[y+1][5]) nei++; }	///
1.74 +
1.75 +					if (old_gamegrid[y+1][x]) nei++;
1.76 +
1.77 +					if (x < 5) { if (old_gamegrid[y+1][x+1]) nei++; }
1.78 +						else	{ if (old_gamegrid[y+1][0]) nei++; }	///
1.79 +				} else {
1.80 +					if (x > 0) { if (old_gamegrid[y+1][x-1]) nei++; }
1.81 +						else	{ if (old_gamegrid[y+1][5]) nei++; }	///
1.82 +
1.83 +					if (old_gamegrid[y+1][x]) nei++;
1.84 +
1.85 +					if (x < 5) { if (old_gamegrid[y+1][x+1]) nei++; }
1.86 +						else	{ if (old_gamegrid[y-1][0]) nei++; }	///
1.87 +				}
1.88 +#endif
1.89
1.90  				// so what happens to our cell?
1.91  				if (old_gamegrid[y][x]) {
```