- #include <stdio.h>
-
#include <stdlib.h>
-
#include <ctype.h>
-
-
#define MAXROW 10
-
#define MAXCOL 25
-
#define DEAD 0
-
#define ALIVE 1
-
-
void output();
-
void copy(int[][MAXCOL], int[][MAXCOL]);
-
void init(int[][MAXCOL]);
-
int neighbors(int[][MAXCOL], int, int);
-
-
int main() {
-
puts("Game of life Program");
-
puts("Enter x, y where x, y is living cell");
-
printf("0 <= x <= %d, 0 <= y <= %d\n", MAXROW-1, MAXCOL-1);
-
puts("Terminate with x, y = -1 -1");
-
-
int map[MAXROW][MAXCOL];
-
int newmap[MAXROW][MAXCOL];
-
-
init(map);
-
-
while(1) {
-
output(map);
-
-
int row, col;
-
for(row = 0; row < MAXROW; row++) {
-
for(col = 0; col < MAXCOL; col++) {
-
switch (neighbors(map, row, col)) {
-
case 0: case 1: case 4: case 5: case 6: case 7: case 8:
-
newmap[row][col] = DEAD; break;
-
case 2:
-
newmap[row][col] = map[row][col]; break;
-
case 3:
-
newmap[row][col] = ALIVE; break;
-
}
-
}
-
}
-
-
copy(map, newmap);
-
-
printf("\nContinue next Generation ? ");
-
getchar();
-
char ans = toupper(getchar());
-
if(ans != 'Y')
-
break;
-
}
-
-
return 0;
-
}
-
-
void init(int map[][MAXCOL]) {
-
int row, col;
-
for(row = 0; row < MAXROW; row++)
-
for(col = 0; col < MAXCOL; col++)
-
map[row][col] = DEAD;
-
-
while(1) {
-
scanf("%d %d", &row, &col);
-
if(0 <= row && row < MAXROW &&
-
0 <= col && col < MAXCOL)
-
map[row][col] = ALIVE;
-
else if(row == -1 || col == -1)
-
break;
-
else
-
printf("(x, y) exceeds map ranage!");
-
}
-
}
-
-
int neighbors(int map[][MAXCOL], int row, int col) {
-
int count = 0;
-
-
int c, r;
-
for(r = row-1; r <= row+1; r++)
-
for(c = col-1; c <= col+1; c++) {
-
if(r < 0 || r >= MAXROW || c < 0 || c >= MAXCOL)
-
continue;
-
if(map[r][c] == ALIVE)
-
count++;
-
}
-
-
if(map[row][col] == ALIVE)
-
count--;
-
-
return count;
-
}
-
-
void output(int map[][MAXCOL]) {
-
printf("\n\nGame of life cell status\n");
-
-
int row, col;
-
for(row = 0; row < MAXROW; row++) {
-
printf("\n%20c", ' ');
-
for(col = 0; col < MAXCOL; col++)
-
if(map[row][col] == ALIVE)
-
putchar('#');
-
else
-
putchar('-');
-
}
-
}
-
-
void copy(int map[][MAXCOL], int newmap[][MAXCOL]) {
-
int row, col;
-
for(row = 0; row < MAXROW; row++)
-
for(col = 0; col < MAXCOL; col++)
-
map[row][col] = newmap[row][col];
-
}
- class LifeGame:
-
def __init__(self, maxRow, maxColumn):
-
self.__map = []
-
self.__newMap = []
-
for i in range(maxRow):
-
self.__map.append([False] * maxColumn)
-
self.__newMap.append([False] * maxColumn)
-
def cell(self, x, y):
-
self.__map[x][y] = True
-
def next(self):
-
for row in range(len(self.__map)):
-
for col in range(len(self.__map[0])):
-
n = self.__neighbors(row, col)
-
if n in [0, 1, 4, 5, 6, 7, 8]:
-
self.__newMap[row][col] = False
-
elif n == 2:
-
self.__newMap[row][col] = self.__map[row][col]
-
elif n == 3:
-
self.__newMap[row][col] = True
-
for row in range(len(self.__map)):
-
for col in range(len(self.__map[0])):
-
self.__map[row][col] = self.__newMap[row][col]
-
def print(self):
-
print("\nGame of life cell status")
-
for row in range(len(self.__map)):
-
print()
-
for col in range(len(self.__map[0])):
-
if self.__map[row][col]:
-
print("#", end="")
-
else:
-
print("-", end="")
-
def __neighbors(self, row, col):
-
count = 0
-
for r in range(row - 1, row + 2):
-
for c in range(col - 1, col + 2):
-
rc = r < 0 or r >= len(self.__map)
-
cc = c < 0 or c >= len(self.__map[0])
-
if rc or cc:
-
continue
-
if self.__map[r][c]:
-
count += 1
-
if self.__map[row][col] == True:
-
count -= 1
-
return count
-
-
game = LifeGame(10, 25)
-
print("Game of life Program")
-
print("Enter x, y where x, y is living cell")
-
print("0 <= x < 10, 0 <= y < 25")
-
print("Terminate with x, y = -1, -1")
-
while(True):
-
strs = input().split(",")
-
row = int(strs[0])
-
col = int(strs[1])
-
if row >= 0 and row < 10 and col >= 0 and col < 25:
-
game.cell(row, col)
-
elif row == -1 or col == -1:
-
break
-
-
while(True):
-
game.print()
-
game.next()
-
print("\nContinue next Generation ?", end="")
-
if input().upper() == "N":
-
break
- class LifeGame(maxRow: Int, maxColumn: Int) {
-
val map = new Array[Array[Boolean]](maxRow, maxColumn)
-
val newmap = new Array[Array[Boolean]](maxRow, maxColumn)
-
def cell(x: Int, y: Int) { map(x)(y) = true }
-
def next() {
-
for{
-
row <- 0 until map.length;
-
col <- 0 until map(0).length
-
} neighbors(row, col) match {
-
case 0|1|4|5|6|7|8 => newmap(row)(col) = false
-
case 2 => newmap(row)(col) = map(row)(col)
-
case 3 => newmap(row)(col) = true
-
}
-
for{
-
row <- 0 until map.length;
-
col <- 0 until map(0).length
-
} map(row)(col) = newmap(row)(col)
-
}
-
-
private def neighbors(row: Int, col: Int) = {
-
var count = 0
-
for(r <- (row - 1) to (row + 1);
-
c <- (col - 1) until (col + 1)
-
) if(r >= 0 && r < map.length &&
-
c >= 0 && c < map(0).length
-
&& map(r)(c)) {
-
count += 1
-
}
-
-
if(map(row)(col)) count -= 1
-
count
-
}
-
-
def print() {
-
println("\n\nGame of life cell status");
-
for(row <- 0 until map.length) {
-
println()
-
for(col <- 0 until map(0).length) {
-
printf(if(map(row)(col)) "#" else "-")
-
}
-
}
-
}
-
}
-
-
println("Game of life Program")
-
println("Enter x y where x y is living cell")
-
println("0 <= x < 10, 0 <= y < 25")
-
println("Terminate with x y = -1 -1")
-
-
val game = new LifeGame(10, 25)
-
var row = -1
-
var col = -1
-
do {
-
val strs = readLine split(" ")
-
row = strs(0).toInt
-
col = strs(1).toInt
-
if(0 <= row && row < 10 && 0 <= col && row < 25)
-
game.cell(row, col)
-
} while(row != -1)
-
-
do {
-
game.print()
-
game.next()
-
print("\nContinue next Generation ? ")
-
} while(readLine().toUpperCase() != "N")
阅读(856) | 评论(0) | 转发(0) |