康威生命游戏(英语:Conway's Game of Life),又称康威生命棋,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机。
它最初于1970年10月在《科学美国人》杂志上马丁·葛登能的“数学游戏”专栏出现。
简单版
import tkinter as tk
import copy
# 设置格子大小
SIZE = 50
WIDTH = 10
HEIGHT = 10
def count_neighbours(board, row, col):
count = 0
for i in range(max(0, row - 1), min(HEIGHT, row + 2)):
for j in range(max(0, col - 1), min(WIDTH, col + 2)):
if (i, j) != (row, col) and board[i][j] == 1:
count += 1
return count
def update_board(board):
new_board = copy.deepcopy(board)
for i in range(HEIGHT):
for j in range(WIDTH):
neighbours = count_neighbours(board, i, j)
if board[i][j] == 1 and not 2 <= neighbours <= 3:
new_board[i][j] = 0
elif neighbours == 3:
new_board[i][j] = 1
return new_board
def draw_board(board):
for i in range(HEIGHT):
for j in range(WIDTH):
cell = cells[i][j]
if board[i][j] == 1:
cell.config(bg="black")
else:
cell.config(bg="white")
def game_loop():
global board
board = update_board(board)
draw_board(board)
root.after(500, game_loop)
# 初始化游戏
board = [[0 for _ in range(WIDTH)] for _ in range(HEIGHT)]
root = tk.Tk()
cells = [[tk.Label(root, width=2, height=1) for _ in range(WIDTH)] for _ in range(HEIGHT)]
for i in range(HEIGHT):
for j in range(WIDTH):
cells[i][j].grid(row=i, column=j)
# 随机生成一些细胞
for _ in range(30):
i, j = random.randint(0, HEIGHT - 1), random.randint(0, WIDTH - 1)
board[i][j] = 1
draw_board(board)
root.after(500, game_loop)
root.mainloop()
进阶版
import tkinter as tk
import random
import copy
# 设置格子大小
SIZE = 50
WIDTH = 10
HEIGHT = 10
def count_neighbours(board, row, col):
count = 0
for i in range(max(0, row - 1), min(HEIGHT, row + 2)):
for j in range(max(0, col - 1), min(WIDTH, col + 2)):
if (i, j) != (row, col) and board[i][j] == 1:
count += 1
return count
def update_board(board):
new_board = copy.deepcopy(board)
for i in range(HEIGHT):
for j in range(WIDTH):
neighbours = count_neighbours(board, i, j)
if board[i][j] == 1 and not 2 <= neighbours <= 3:
new_board[i][j] = 0
elif neighbours == 3:
new_board[i][j] = 1
return new_board
def draw_board(board):
for i in range(HEIGHT):
for j in range(WIDTH):
cell = cells[i][j]
if board[i][j] == 1:
cell.config(bg="black")
else:
cell.config(bg="white")
def game_loop():
global board, score
if not is_playing:
return
board = update_board(board)
draw_board(board)
score += sum(map(sum, board)) # 统计存活的细胞数量
score_label.config(text=f"Score: {score}")
root.after(500, game_loop)
def start_game():
global is_playing
is_playing = True
game_loop()
def stop_game():
global is_playing
is_playing = False
# 初始化游戏
board = [[0 for _ in range(WIDTH)] for _ in range(HEIGHT)]
root = tk.Tk()
cells = [[tk.Label(root, width=2, height=1) for _ in range(WIDTH)] for _ in range(HEIGHT)]
for i in range(HEIGHT):
for j in range(WIDTH):
cells[i][j].grid(row=i, column=j)
# 随机生成一些细胞
for _ in range(30):
i, j = random.randint(0, HEIGHT - 1), random.randint(0, WIDTH - 1)
board[i][j] = 1
score = 0
score_label = tk.Label(root, text=f"Score: {score}")
score_label.grid(row=HEIGHT, column=0, columnspan=WIDTH)
is_playing = False
start_button = tk.Button(root, text="Start", command=start_game)
start_button.grid(row=HEIGHT+1, column=0, columnspan=WIDTH//2)
stop_button = tk.Button(root, text="Stop", command=stop_game)
stop_button.grid(row=HEIGHT+1, column=WIDTH//2, columnspan=WIDTH//2)
draw_board(board)
root.mainloop()
终极版
import tkinter as tk
import random
import copy
# 设置格子大小
SIZE = 50
WIDTH = 10
HEIGHT = 10
def count_neighbours(board, row, col):
count = 0
for i in range(max(0, row - 1), min(HEIGHT, row + 2)):
for j in range(max(0, col - 1), min(WIDTH, col + 2)):
if (i, j) != (row, col) and board[i][j] == 1:
count += 1
return count
def update_board(board):
new_board = copy.deepcopy(board)
for i in range(HEIGHT):
for j in range(WIDTH):
neighbours = count_neighbours(board, i, j)
if board[i][j] == 1 and not 2 <= neighbours <= 3:
new_board[i][j] = 0
elif neighbours == 3:
new_board[i][j] = 1
return new_board
def draw_board(board):
for i in range(HEIGHT):
for j in range(WIDTH):
cell = cells[i][j]
if board[i][j] == 1:
cell.config(bg="black")
else:
cell.config(bg="white")
def game_loop():
global board, state_label
if not is_playing:
return
board = update_board(board)
draw_board(board)
living_cells = sum(map(sum, board))
state_label.config(text=f"Living Cells: {living_cells}")
root.after(500, game_loop)
def start_game():
global is_playing
is_playing = True
game_loop()
def stop_game():
global is_playing, board
is_playing = False
board = [[0 for _ in range(WIDTH)] for _ in range(HEIGHT)]
draw_board(board)
def initialize_game():
global board
for _ in range(30):
i, j = random.randint(0, HEIGHT - 1), random.randint(0, WIDTH - 1)
board[i][j] = 1
draw_board(board)
def click_cell(event):
cell = event.widget
i, j = cell.grid_info()["row"], cell.grid_info()["column"]
board[i][j] = 1 if board[i][j] == 0 else 0
draw_board(board)
# 初始化游戏
board = [[0 for _ in range(WIDTH)] for _ in range(HEIGHT)]
root = tk.Tk()
cells = [[tk.Label(root, width=2, height=1) for _ in range(WIDTH)] for _ in range(HEIGHT)]
for i in range(HEIGHT):
for j in range(WIDTH):
cells[i][j].grid(row=i, column=j)
cells[i][j].bind("<Button-1>", click_cell)
state_label = tk.Label(root, text=f"Living Cells: {0}")
state_label.grid(row=HEIGHT, column=0, columnspan=WIDTH)
is_playing = False
start_button = tk.Button(root, text="Start", command=start_game)
start_button.grid(row=HEIGHT+1, column=0, columnspan=WIDTH//3)
stop_button = tk.Button(root, text="Stop", command=stop_game)
stop_button.grid(row=HEIGHT+1, column=WIDTH//3, columnspan=WIDTH//3)
init_button = tk.Button(root, text="Initialize", command=initialize_game)
init_button.grid(row=HEIGHT+1, column=(2*WIDTH)//3, columnspan=WIDTH//3)
draw_board(board)
root.mainloop()