Created
May 2, 2016 16:04
-
-
Save Cartman0/5af7c5b3b80b716a571ab6bd01104284 to your computer and use it in GitHub Desktop.
LifeGame 処理のみ(classなし、可視化なし)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
CELLS_WIDTH=8 | |
cells = np.zeros((CELLS_WIDTH, CELLS_WIDTH), 'int') | |
cells[:3,:3] = 1 | |
print(cells) | |
# 条件:自分が生きている | |
def isAlive(cell_idx, cells): | |
if cells[cell_idx] == 0: | |
return False | |
return True | |
def count_alive_surrounding(cell_idx, cells): | |
''' | |
note:自分のセル分も含まれる | |
''' | |
range_idx = [(idx - 1 if idx - 1 >= 0 else 0, idx + 2 if idx + 2 <= CELLS_WIDTH else CELLS_WIDTH) for idx in cell_idx] | |
#print(range_idx) | |
return np.count_nonzero(cells[range_idx[0][0]:range_idx[0][1], range_idx[1][0]:range_idx[1][1]]) | |
## 生存: 2 | |
def rule_alive_condition_setting(min_cell_num:int, max_cell_num:int): | |
''' | |
''' | |
def rule_alive_condition(cell_idx, cells_in): | |
if isAlive(cell_idx, cells_in) and (min_cell_num <= count_alive_surrounding(cell_idx, cells_in) - 1 <= max_cell_num): | |
return True | |
return False | |
return rule_alive_condition | |
def rule_alive_apply(cell_idx, cells_out): | |
# そのcellはそのまま生存 なにもしない | |
cells_out[cell_idx] = 2 | |
## 誕生:1 | |
def rule_birth_condition_setting(birth_required_cell_num:int): | |
def rule_birth_condition(cell_idx, cells_in): | |
# 周囲8マス以内に他のセルがbirth_required_cell_numだけ生きていれば | |
if not isAlive(cell_idx, cells_in) and (count_alive_surrounding(cell_idx, cells_in) == birth_required_cell_num): | |
return True | |
return False | |
return rule_birth_condition | |
def rule_birth_apply(cell_idx, cells_out): | |
## そのcellに誕生 | |
cells_out[cell_idx] = 1 | |
# death | |
# 条件 True | |
def default_condition(cell_idx, cells_in): | |
return True | |
def rule_death_apply(cell_idx, cells_out): | |
cells_out[cell_idx] = 0 | |
# Rule | |
rules = ( | |
(rule_alive_condition_setting(2, 3), rule_alive_apply), # alive | |
(rule_birth_condition_setting(3), rule_birth_apply), # birth | |
(default_condition, rule_death_apply) # death | |
) | |
def play(cells, count=1): | |
if count <= 0: | |
return cells | |
# copy先のcell | |
cells_out = np.copy(cells) | |
# index の取得 | |
for idx in np.ndindex(cells.shape): | |
for condition, apply in rules: | |
if condition(idx, cells): | |
apply(idx, cells_out) | |
break | |
print(cells_out) | |
return play(cells_out, count-1) | |
def play_gen(cells): | |
def count(i): | |
x = [i] | |
def count_up(): | |
x[0] += 1 | |
return x[0] | |
return count_up | |
countup = count(0) | |
cells_in = np.copy(cells) | |
while True: | |
# copy先のcell | |
cells_out = np.copy(cells_in) | |
# index の取得 | |
for idx in np.ndindex(cells.shape): | |
for condition, apply in rules: | |
if condition(idx, cells_in): | |
apply(idx, cells_out) | |
break | |
yield countup(), cells_out | |
cells_in = np.copy(cells_out) | |
# 処理 | |
gen = play_gen(cells) | |
next(gen) # 1 | |
next(gen) # 2 | |
next(gen) # 3 | |
next(gen) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment