1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
/*
** A quick "life" (2-d cellular automaton) implementation done in Turbo C 2.0
** on the spur-of-the-moment by Jonathan Guthrie 9/20/1992 and donated to the
** public domain.
**
** In keeping with the guidelines of the C_ECHO, this program has been tested,
** and does seem to operate properly.
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#ifndef random
#define random(num) (int)(((long)rand()*(num))/RAND_MAX)
#endif
/*
** From VIDPORT.C, also in SNIPPETS
*/
void GotoXY(int col, int row);
void ClrScrn(int vattrib);
#ifndef randomize
#define randomize() srand(((unsigned int)time(NULL))|1)
#endif
#define ROWS 24
#define COLS 80
#define GENERATIONS 10
int civ1[ROWS][COLS], civ2[ROWS][COLS];
void update_generation(int old[ROWS][COLS], int new[ROWS][COLS])
{
int i, j, count;
for (i = 0; i < ROWS; ++i)
{
for (j = 0; j < COLS; ++j)
{
count = old[(i + ROWS - 1) % ROWS][(j + COLS - 1) % COLS] +
old[(i + ROWS - 1) % ROWS][j] +
old[(i + ROWS - 1) % ROWS][(j + 1) % COLS] +
old[i][(j + COLS - 1) % COLS] +
old[i][(j + 1) % COLS] +
old[(i + 1) % ROWS][(j + COLS - 1) % COLS] +
old[(i + 1) % ROWS][j] +
old[(i + 1) % ROWS][(j + 1) % COLS];
switch(count)
{
case 0:
case 1:
case 4:
case 5:
case 6:
case 7:
case 8:
new[i][j] = 0;
break;
case 2:
new[i][j] = old[i][j];
break;
case 3:
new[i][j] = 1;
break;
}
GotoXY(j+1, i+1);
putch(new[i][j] ? '*' : ' ');
}
}
}
void initialize(void)
{
int i, j;
randomize();
ClrScrn(7);
for (i = 0; i < ROWS; ++i)
{
for (j = 0; j < COLS; ++j)
{
civ1[i][j] = random(2);
GotoXY(j+1, i+1);
putch(civ1[i][j] ? '*' : ' ');
}
}
}
int main(void)
{
int i;
initialize();
for (i = 0; i < GENERATIONS; ++i)
{
update_generation(civ1, civ2);
update_generation(civ2, civ1);
}
return EXIT_SUCCESS;
}
|