Sat Jul 18, 2009 11:43 am
#include <stdio.h>
#define HEIGHT 25
#define WIDTH 25
void clearScreen(char tableA[HEIGHT][WIDTH], char tableB[HEIGHT][WIDTH])
{
int height;
int width;
for (height = 0; height < HEIGHT; height++) {
for (width = 0; height < HEIGHT; height++)
tableA[height][width] = 0;
tableB[height][width] = 0;
}
for (height = 0; height < HEIGHT; height++) {
printf("\n");
for (width = 0; width < WIDTH; width++)
printf("-");
}
printf("\n");
}
void askUser(char tableA[HEIGHT][WIDTH])
{
int i;
int n;
int height, width;
printf("Enter the amount of initial organisms: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter dimensions (x y) where organism %d will live: ", i + 1);
scanf("%d%d", &height, &width);
tableA[height][width] = 1;
}
}
void calculate(char tableA[HEIGHT][WIDTH], char tableB[HEIGHT][WIDTH])
{
int neighbor;
int height;
int width;
for (height = 0; height < HEIGHT; height++) {
for (width = 0; width < WIDTH; width++)
neighbor = 0;
if (tableA[height - 1][width - 1] == 1) {
neighbor++;
}
if (tableA[height - 1][width] == 1) {
neighbor++;
}
if (tableA[height - 1][width + 1] == 1) {
neighbor++;
}
if (tableA[height][width - 1] == 1) {
neighbor++;
}
if (tableA[height][width + 1] == 1) {
neighbor++;
}
if (tableA[height + 1][width - 1] == 1) {
neighbor++;
}
if (tableA[height + 1][width] == 1) {
neighbor++;
}
if (tableA[height + 1][width + 1] == 1) {
neighbor++;
}
if (tableA[height][width] == 1 && neighbor < 2) {
tableB[height][width] = 0;
}
else if (tableA[height][width] == 1 && neighbor > 3) {
tableB[height][width] = 0;
}
else if (tableA[height][width] == 1 && (neighbor == 2 || neighbor == 3)) {
tableB[height][width] = 1;
}
else if (tableA[height][width] == 0 && neighbor == 3) {
tableB[height][width] = 1;
}
}
}
void swap(char tableA[HEIGHT][WIDTH], char tableB[HEIGHT][WIDTH])
{
int height;
int width;
for (height = 0; height < HEIGHT; height++) {
for (width = 0; width < WIDTH; width++)
tableA[height][width] = tableB[height][width];
}
}
void printTable(char tableA[HEIGHT][WIDTH])
{
int height;
int width;
for (height = 0; height < HEIGHT; height++) {
printf("\n");
for (width = 0; width < WIDTH; width++)
if (tableA[height][width] == 1) {
printf("X");
}
else {
printf("-");
}
}
printf("\n");
}
int main(void)
{
char tableA[HEIGHT][WIDTH];
char tableB[HEIGHT][WIDTH];
char end;
end = 'w';
clearScreen(tableA, tableB);
askUser(tableA);
while (end != 'q') {
calculate(tableA, tableB);
swap(tableA, tableB);
printTable(tableA);
printf("Press q to quit or 1 to continue: ");
scanf(" %c", &end);
}
return 0;
}
Sat Jul 18, 2009 1:09 pm
Sat Jul 18, 2009 1:34 pm
Sat Jul 18, 2009 3:19 pm
Sat Jul 18, 2009 3:54 pm
"Enter the amount of initial organisms: "
Sat Jul 18, 2009 4:46 pm
Sat Jul 18, 2009 8:41 pm
#include <stdio.h>
#define HEIGHT 25
#define WIDTH 25
#define LIFE_YES 1
#define LIFE_NO 0
typedef int TableType[HEIGHT][WIDTH];
void printTable(TableType table) {
int height, width;
for (height = 0; height < HEIGHT; height++) {
for (width = 0; width < WIDTH; width++) {
if (table[height][width] == LIFE_YES) {
printf("X");
} else {
printf("-");
}
}
printf("\n");
}
printf("\n");
}
void clearTable(TableType table) {
int height, width;
for (height = 0; height < HEIGHT; height++) {
for (width = 0; width < WIDTH; width++) {
table[height][width] = LIFE_NO;
}
}
}
void askUser(TableType tableA) {
int i;
int n;
int height, width;
printf("Enter the amount of initial organisms: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter dimensions (x y) where organism %d will live: ", i + 1);
scanf("%d %d", &height, &width);
tableA[height][width] = LIFE_YES;
}
printTable(tableA);
printf("Generation 0");
}
int getNeighborValue(TableType table, int row, int col) {
if (row < 0 || row >= HEIGHT
|| col < 0 || col >= WIDTH
|| table[row][col] != LIFE_YES )
{
return 0;
} else {
return 1;
}
}
int getNeighborCount(TableType table, int row, int col) {
int neighbor = 0;
neighbor += getNeighborValue(table, row - 1, col - 1);
neighbor += getNeighborValue(table, row - 1, col);
neighbor += getNeighborValue(table, row - 1, col + 1);
neighbor += getNeighborValue(table, row, col - 1);
neighbor += getNeighborValue(table, row, col + 1);
neighbor += getNeighborValue(table, row + 1, col - 1);
neighbor += getNeighborValue(table, row + 1, col);
neighbor += getNeighborValue(table, row + 1, col + 1);
return neighbor;
}
void calculate(TableType tableA) {
TableType tableB;
int neighbor, height, width;
for (height = 0; height < HEIGHT; height++) {
for (width = 0; width < WIDTH; width++) {
neighbor = getNeighborCount(tableA, height, width);
if (neighbor==3) {
tableB[height][width] = LIFE_YES;
} else if (neighbor == 2 && tableA[height][width] == LIFE_YES) {
tableB[height][width] = LIFE_YES;
} else {
tableB[height][width] = LIFE_NO;
}
}
}
for (height = 0; height < HEIGHT; height++) {
for (width = 0; width < WIDTH; width++) {
tableA[height][width] = tableB[height][width];
}
}
}
void loadTestData(TableType table) {
// toggle
table[3][4] = LIFE_YES;
table[3][5] = LIFE_YES;
table[3][6] = LIFE_YES;
// glider
table[10][4] = LIFE_YES;
table[10][5] = LIFE_YES;
table[10][6] = LIFE_YES;
table[11][6] = LIFE_YES;
table[12][5] = LIFE_YES;
}
int main(void) {
TableType table;
char end;
int generation = 0;
clearTable(table);
// askUser(table);
loadTestData(table);
printTable(table);
do {
calculate(table);
printTable(table);
printf("Generation %d\n", ++generation);
printf("Press q to quit or 1 to continue: ");
scanf(" %c", &end);
} while (end != 'q') ;
return 0;
}
Sun Jul 19, 2009 12:26 am
Sun Jul 19, 2009 1:08 am
Sun Jul 19, 2009 1:55 am
Mon Jul 20, 2009 12:04 am
Mon Jul 20, 2009 2:05 pm
Mon Jul 20, 2009 2:43 pm