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

Function count_neighbors

example_code/item_072.py:100–114  ·  view source on GitHub ↗
(y, x, get_cell)

Source from the content-addressed store, hash-verified

98from threading import Thread
99
100def count_neighbors(y, x, get_cell):
101 n_ = get_cell(y - 1, x + 0) # North
102 ne = get_cell(y - 1, x + 1) # Northeast
103 e_ = get_cell(y + 0, x + 1) # East
104 se = get_cell(y + 1, x + 1) # Southeast
105 s_ = get_cell(y + 1, x + 0) # South
106 sw = get_cell(y + 1, x - 1) # Southwest
107 w_ = get_cell(y + 0, x - 1) # West
108 nw = get_cell(y - 1, x - 1) # Northwest
109 neighbor_states = [n_, ne, e_, se, s_, sw, w_, nw]
110 count = 0
111 for state in neighbor_states:
112 if state == ALIVE:
113 count += 1
114 return count
115
116def game_logic(state, neighbors):
117 # This version of the function is just to illustrate the point

Callers 1

step_cellFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected