MCPcopy Create free account
hub / github.com/bslatkin/effectivepython / count_neighbors

Function count_neighbors

example_code/item_074.py:97–111  ·  view source on GitHub ↗
(y, x, get_cell)

Source from the content-addressed store, hash-verified

95
96
97def count_neighbors(y, x, get_cell):
98 n_ = get_cell(y - 1, x + 0) # North
99 ne = get_cell(y - 1, x + 1) # Northeast
100 e_ = get_cell(y + 0, x + 1) # East
101 se = get_cell(y + 1, x + 1) # Southeast
102 s_ = get_cell(y + 1, x + 0) # South
103 sw = get_cell(y + 1, x - 1) # Southwest
104 w_ = get_cell(y + 0, x - 1) # West
105 nw = get_cell(y - 1, x - 1) # Northwest
106 neighbor_states = [n_, ne, e_, se, s_, sw, w_, nw]
107 count = 0
108 for state in neighbor_states:
109 if state == ALIVE:
110 count += 1
111 return count
112
113def game_logic(state, neighbors):
114 # Do some blocking input/output in here:

Callers 1

step_cellFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected