主题:请教game of life的c代码 !想不明白!
dongfengaa
[专家分:0] 发布于 2008-03-05 16:48:00
谢谢[em18]
回复列表 (共2个回复)
沙发
天天学习 [专家分:4570] 发布于 2008-03-25 18:45:00
看过它的VB版的代码。
是用链表来实现的。至于它的生长、死亡等限条件,就看你的理解力了
板凳
冷石_jasv [专家分:1570] 发布于 2008-05-14 20:04:00
QuickBasic4.5写的。要不要?
DEFINT A-Z
SCREEN 13
'************************** 主程序开始 **************************
INPUT "Input 50~160:", ma
CLS
IF ma < 50 THEN ma = 50
IF ma > 160 THEN ma = 160
map = ma '世界地图范围大小
left = 12
right = 12
col = 13 '生命颜色
dicol = 1 '底色
PAINT (1, 1), dicol
DIM sm(map, map)
DIM temp(map, map)
FOR i = 1 TO 7
LINE (left - i, right - i)-STEP(map + 2 * i + 1, map + 2 * i + 1), i + 40, B
NEXT
FOR i = 1 TO map STEP 2
FOR j = 1 TO map
IF i > 10 AND i < map - 10 THEN
IF j > 10 AND j < map - 10 THEN
sm(i, j) = INT(RND + .5)
END IF
END IF
IF sm(i, j) <> 0 THEN
PSET (i + left, j + right), sm(i, j) + col
ELSE
PSET (i + left, j + right), dicol
END IF
NEXT
NEXT
SLEEP
LINE (10, 184)-STEP(310, 20), dicol, BF
'生命开始
DO
FOR i = 1 TO map
FOR j = 1 TO map
flag = 0 '定义周边邻居的数量,包括自身
FOR n = -1 TO 1
FOR m = -1 TO 1
ax = i + n: IF ax < 1 THEN ax = map ELSE IF ax > map THEN ax = 1
ay = j + m: IF ay < 1 THEN ay = map ELSE IF ay > map THEN ay = 1
IF sm(ax, ay) = 1 THEN flag = flag + 1
IF sm(i, j) = 1 THEN p = 1 ELSE p = 0
NEXT
NEXT
'生存条件,2,3个邻居不变,3个邻居重生,大于3小于2死亡
IF (p = 1 AND flag = 3) OR (p = 1 AND flag = 4) OR (p = 0 AND flag = 3) THEN
temp(i, j) = 1
END IF
IF (p = 1 AND flag < 3) OR (p = 1 AND flag > 4) THEN
temp(i, j) = 0
END IF
NEXT
NEXT
'显示世界变化
tos = 0
FOR i = 1 TO map
FOR j = 1 TO map
sm(i, j) = temp(i, j)
IF sm(i, j) = 1 THEN tos = tos + 1
IF tos > max THEN max = tos
IF tos < min THEN min = tos
IF sm(i, j) <> 0 THEN
PSET (i + left, j + right), sm(i, j) + INT(RND * 3 + 1) + 40
ELSE
PSET (i + left, j + right), dicol
END IF
NEXT
NEXT
LOOP WHILE INKEY$ <> CHR$(27)
'************************** 主程序结束 **************************
CLOSE
END
我来回复