Guest User

Untitled

a guest
Jul 15th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. import random
  2. import time
  3.  
  4. DEAD = 0
  5. LIVE = 1
  6.  
  7. def dead_state(width, height):
  8. """Constuct an empty state with all cells set to DEAD.
  9.  
  10. Parameters
  11. ----------
  12. width: the width of the state, in cells
  13. height: the height of the state, in cells
  14.  
  15. Returns
  16. -------
  17. A state of dimensions width x height, with all cells set to DEAD
  18. """
  19. return [[DEAD for _ in range(height)] for _ in range(width)]
  20.  
  21. def random_state(width, height):
  22. """Construct a random state with all cells randomly set.
  23.  
  24. Parameters
  25. ----------
  26. width: the width of the state, in cells
  27. height: the height of the state, in cells
  28.  
  29. Returns
  30. -------
  31. A state of dimensions width x height, with all cells randomly set to either
  32. DEAD or LIVE with equal probability.
  33. """
  34. state = dead_state(width, height)
  35. for x in range(0, state_width(state)):
  36. for y in range(0, state_height(state)):
  37. random_number = random.random()
  38. if random_number > 0.85:
  39. cell_state = LIVE
  40. else:
  41. cell_state = DEAD
  42. state[x][y] = cell_state
  43.  
  44. return state
  45.  
  46. def state_width(state):
  47. """Get the width of a state.
  48.  
  49. Parameters
  50. ----------
  51. state: a Game state
  52.  
  53. Returns
  54. -------
  55. The width of the input state
  56. """
  57. return len(state)
  58.  
  59. def state_height(state):
  60. """Get the height of a state.
  61.  
  62. Parameters
  63. ----------
  64. state: a Game state
  65.  
  66. Returns
  67. -------
  68. The height of the input state
  69. """
  70. return len(state[0])
  71.  
  72. def next_cell_value(cell_coords, state):
  73. """Get the next value of a single cell in a state.
  74.  
  75. Parameters
  76. ----------
  77. cell_coords: an (x, y) tuple of the co-ordinates of a cell
  78. state: the current state of the Game board
  79.  
  80. Returns
  81. -------
  82. The new state of the given cell - either DEAD or LIVE.
  83. """
  84. width = state_width(state)
  85. height = state_height(state)
  86. x = cell_coords[0]
  87. y = cell_coords[1]
  88. n_live_neighbors = 0
  89.  
  90. # Iterate around this cell's neighbors
  91. for x1 in range((x-1), (x+1)+1):
  92. # Make sure we don't go off the edge of the board
  93. if x1 < 0 or x1 >= width: continue
  94.  
  95. for y1 in range((y-1), (y+1)+1):
  96. # Make sure we don't go off the edge of the board
  97. if y1 < 0 or y1 >= height: continue
  98. # Make sure we don't count the cell as a neighbor of itself!
  99. if x1 == x and y1 == y: continue
  100.  
  101. if state[x1][y1] == LIVE:
  102. n_live_neighbors += 1
  103.  
  104. if state[x][y] == LIVE:
  105. if n_live_neighbors <= 1:
  106. return DEAD
  107. elif n_live_neighbors <= 3:
  108. return LIVE
  109. else:
  110. return DEAD
  111. else:
  112. if n_live_neighbors == 3:
  113. return LIVE
  114. else:
  115. return DEAD
  116.  
  117. def next_board_state(init_state):
  118. """Take a single step in the Game of Life.
  119.  
  120. Parameters
  121. ----------
  122. init_state: the initial state of the Game board
  123.  
  124. Returns
  125. -------
  126. The next state of the Game board, after taking one step for every cell in
  127. the previous state.
  128. """
  129. width = state_width(init_state)
  130. height = state_height(init_state)
  131. next_state = dead_state(width, height)
  132.  
  133. for x in range(0, width):
  134. for y in range(0, height):
  135. next_state[x][y] = next_cell_value((x, y), init_state)
  136.  
  137. return next_state
  138.  
  139. def render(state):
  140. """Displays a state by printing it to the terminal.
  141.  
  142. Parameters
  143. ----------
  144. state: a Game state
  145.  
  146. Returns
  147. -------
  148. Nothing - this is purely a display function.
  149. """
  150. display_as = {
  151. DEAD: ' ',
  152. # This is "unicode" for a filled-in square. You can also just use a thick
  153. # "ASCII" character like a '$' or '#'.
  154. LIVE: u"\u2588"
  155. }
  156. lines = []
  157. for y in range(0, state_height(state)):
  158. line = ''
  159. for x in range(0, state_width(state)):
  160. line += display_as[state[x][y]] * 2
  161. lines.append(line)
  162. print "\n".join(lines)
  163.  
  164. def load_board_state(filepath):
  165. """Loads a board state from the given filepath
  166.  
  167. Parameters
  168. ----------
  169. filepath: the filepath to load the state from. Dead cells should be
  170. represented by 0s, live cells by 1s
  171.  
  172. Returns
  173. -------
  174. The board state loaded from the given filepath
  175. """
  176. with open(filepath, 'r') as f:
  177. lines = [l.rstrip() for l in f.readlines()]
  178.  
  179. height = len(lines)
  180. width = len(lines[0])
  181. board = dead_state(height, width)
  182.  
  183. for x, line in enumerate(lines):
  184. for y, char in enumerate(line):
  185. board[x][y] = int(char)
  186. return board
  187.  
  188.  
  189. def run_forever(init_state):
  190. """Runs the Game of Life forever, starting from the given initial state.
  191.  
  192. Parameters
  193. ----------
  194. init_state: the Game state to start at
  195.  
  196. Returns
  197. -------
  198. This function never returns - the program must be forcibly exited!
  199. """
  200. next_state = init_state
  201. while True:
  202. render(next_state)
  203. next_state = next_board_state(next_state)
  204. time.sleep(0.03)
  205.  
  206. init_state = random_state(100, 50)
  207. # init_state = load_board_state('./toad.txt')
  208. run_forever(init_state)
Add Comment
Please, Sign In to add comment