Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.63 KB | None | 0 0
  1. from random import randint
  2.  
  3.  
  4. class Cell:
  5.     def __init__(self):
  6.         self._alive = False
  7.  
  8.     def set_alive(self, is_alive):
  9.         self._alive = is_alive
  10.  
  11.     def is_alive(self):
  12.         return self._alive
  13.  
  14.     def __str__(self):
  15.         return "O" if self.is_alive() else "."
  16. # end Cell
  17.    
  18.  
  19.  
  20. class Board:
  21.     def __init__(self, rows, columns):
  22.         self._rows = rows
  23.         self._cols = columns
  24.  
  25.         self._field = [None]*rows
  26.         for i in xrange(self._rows):
  27.             this_row = [None]*cols
  28.             for j in xrange(self._cols):
  29.                 this_row[j] = Cell()
  30.             self._field[i] = this_row
  31.  
  32.     # end __init__
  33.  
  34.    
  35.     def __str__(self):
  36.         join_row = lambda r: "".join(str(cell) for cell in r)
  37.         return "\n".join(join_row(r) for r in self._field)
  38.     # end __str__
  39.  
  40.  
  41.     def set_random_board(self):
  42.         for r in xrange(self._rows):
  43.             for c in xrange(self._cols):
  44.                 self._field[r][c] = randint(0, 3) == 3
  45.     # end set_random_board
  46.    
  47.  
  48.     def set_board(self, input):
  49.         """Overwrites the board with the content of s.
  50.  
  51.        @input has 3 valid characters only -- . O \n
  52.          - . means the cell is dead
  53.          - O means the cell is alive
  54.          - \n means end-of-row
  55.        """
  56.  
  57.         if not input.strip():
  58.             self._rows = self._cols = 0
  59.             self._board = []
  60.             return
  61.  
  62.         assert all(c in ('.', 'O', '\n') for c in input)
  63.         rows = [r.strip() for r in input.split("\n") if r.strip()]
  64.         num_columns = len(rows[0])
  65.         assert all(len(r) == num_columns for r in rows)
  66.         self._rows = len(rows)
  67.         self._cols = num_columns
  68.        
  69.         self._field = [None]*len(rows)
  70.         for index, r in enumerate(rows):
  71.             this_row = [None]*num_columns
  72.             for c in xrange(num_columns):
  73.                 this_row[c] = Cell()
  74.                 this_row[c].set_alive(r[c] == 'O')
  75.             self._field[index] = this_row
  76.  
  77.     # end set_board
  78.  
  79.    
  80.     def neighbours(self, row, col):
  81.         valid_index = lambda x, y: (0 <= x < self._rows) and (0 <= y < self._cols)
  82.        
  83.         result = list()
  84.         for r in (-1, 0, 1):
  85.             for c in (-1, 0, 1):
  86.                 i, j = row+r, col+c
  87.                 if (i, j) == (row, col):
  88.                     continue
  89.                 if valid_index(i, j):
  90.                     result.append(self._field[i][j])
  91.         return result
  92.     # end neighbours
  93.  
  94.  
  95.     def live_neighbours(self, row, col):
  96.         return len([x for x in self.neighbours(row, col)
  97.                       if x.is_alive()])
  98.     # end live_neighbours
  99.  
  100.  
  101.     def next_generation(self):
  102.         next_gen_alive = list()
  103.         next_gen_dead = list()
  104.        
  105.         for r in xrange(self._rows):
  106.             for c in xrange(self._cols):
  107.                 cell = self._field[r][c]
  108.                 alive_neighbours = self.live_neighbours(r, c)
  109.                 if ((cell.is_alive() and (alive_neighbours in (2, 3))) or
  110.                     (not cell.is_alive() and (alive_neighbours == 3))):
  111.                     next_gen_alive.append(cell)
  112.                 else:
  113.                     next_gen_dead.append(cell)
  114.  
  115.         for cell in next_gen_alive:
  116.             cell.set_alive(True)
  117.         for cell in next_gen_dead:
  118.             cell.set_alive(False)
  119.     # end next_generation
  120.  
  121.    
  122. def main():
  123.     s = """
  124. .OO.
  125. .O..
  126. ....
  127. """
  128.     b = Board(0, 0)
  129.     b.set_board(s)
  130.     print("Board is:\n%s" % str(b))
  131.     b.next_generation()
  132.     print("Board is:\n%s" % str(b))    
  133. # end main
  134.  
  135.  
  136. if __name__ == "__main__":
  137.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement