【Python】py里py气的生命游戏示例,你的第一行代码何必是hello,world

语法糖吃的有点傻~

import copy
import os

m, n = 10, 10
world = [[0 for _ in range(0, m)] for _ in range(0, n)]
world[0][1], world[1][2], world[2][0], world[2][1], world[2][2] = 1, 1, 1, 1, 1

while True:
    os.system('cls')
    _world = copy.deepcopy(world)
    for i in range(0, m):
        for j in range(0, n):
            state = 0
            if not (i == 0 or j == 0):
                state += world[i - 1][j - 1]
            if not i == 0:
                state += world[i - 1][j]
            if not (i == 0 or j == n - 1):
                state += world[i - 1][j + 1]
            if not j == 0:
                state += world[i][j - 1]
            if not j == n - 1:
                state += world[i][j + 1]
            if not (i == m - 1 or j == 0):
                state += world[i + 1][j - 1]
            if not i == m - 1:
                state += world[i + 1][j]
            if not (i == m - 1 or j == n - 1):
                state += world[i + 1][j + 1]

            if state < 2 or state > 3:
                    _world[i][j] = 0
            elif state == 3:
                    _world[i][j] = 1
            print(('\033[47m  \033[0m' if _world[i][j] else '  '), end='')
        print()
    world = copy.deepcopy(_world)
    input()
Comments
登录后评论
Sign In
·

以后学习新语言第一件事:做个生命游戏瞧瞧

·

这个有什么说法吗,跟之前看的不太一样,没怎么看懂 joy