Beispiel #1
0
 def __getitem__(self, item):
     row, col = item[0], item[1]
     if row < 0 or row > len(self.prev_board) - 1:
         return Cell(None)
     if col < 0 or col > len(self.prev_board) - 1:
         return Cell(None)
     return self.prev_board[row][col]
Beispiel #2
0
    def __init__(self, height, width):
        '''
        Create a new playing area of (height, width) cells.
        '''

        # Set up the grid and schedule.

        # Use SimultaneousActivation which simulates all the cells
        # computing their next state simultaneously.  This needs to
        # be done because each cell's next state depends on the current
        # state of all its neighbors -- before they've changed.
        self.schedule = SimultaneousActivation(self)

        # Use a simple grid, where edges wrap around.
        self.grid = Grid(height, width, torus=True)

        # Place a cell at each location, with some initialized to
        # ALIVE and some to DEAD.
        for (contents, x, y) in self.grid.coord_iter():
            cell = Cell((x, y), self)
            if random() < .1:
                cell.state = cell.ALIVE
            self.grid.place_agent(cell, (x, y))
            self.schedule.add(cell)

        self.running = True
Beispiel #3
0
    def __init__(self, height, width):
        '''
        Create a new playing area of (height, width) cells.
        '''

        # Set up the grid and schedule.

        # Use SimultaneousActivation which simulates all the cells
        # computing their next state simultaneously.  This needs to
        # be done because each cell's next state depends on the current
        # state of all its neighbors -- before they've changed.
        self.schedule = SimultaneousActivation(self)

        # Use a simple grid, where edges wrap around.
        self.grid = Grid(height, width, torus=True)

        # Place a cell at each location, with some initialized to
        # ALIVE and some to DEAD.
        for (contents, x, y) in self.grid.coord_iter():
            cell = Cell((x, y), self)
            if random() < .1:
                cell.state = cell.ALIVE
            self.grid.place_agent(cell, (x, y))
            self.schedule.add(cell)
        self.running = True
Beispiel #4
0
 def __toggle(self):
     if self['bg'] == "white":
         self['bg'] = "black"
         self.__cell = Cell(True)
     else:
         self['bg'] = "white"
         self.__cell = Cell(False)
Beispiel #5
0
    def update(self):
        for row in range(len(self.board)):
            for col in range(len(self.board)):
                cell_neighbours = self.get_neighbour_states(row, col)
                if self[row, col].will_survive(cell_neighbours):
                    self[row, col] = Cell(ALIVE)
                else:
                    self[row, col] = Cell(DEAD)

        self.prev_board = deepcopy(self.board)
        return self
Beispiel #6
0
def register_cell(sid):
    try:
        print('new cell')
        new_cell = Cell(game=game_of_life, client_id=sid)
        spirits.append(new_cell)
    except:
        print("error registering cell")
Beispiel #7
0
    def get_new_state(self, grid):
        new_grid = [[Cell(False) for _ in range(len(grid[0]))]
                    for _ in range(len(grid))]

        for i in range(len(grid)):
            for j in range(len(grid[0])):
                nb_neighbours = self.count_neighbours(grid, i, j)
                if nb_neighbours == 2:
                    new_grid[i][j].is_alive = grid[i][j].is_alive
                elif nb_neighbours == 3:
                    new_grid[i][j].is_alive = True

        return new_grid
Beispiel #8
0
    def read_from_file(self, filename: str):
        """
            Reads the board from an input file
        """
        if self.__check_file_status(filename):
            with open(filename, 'r') as file_handler:
                lines = file_handler.readlines()
                self.__num_rows = int(lines[0])
                self.__num_cols = int(lines[1])
                for line in lines[2:]:
                    row = []
                    tokens = list(line)[:-1]
                    for token in tokens:
                        cell = None
                        if token == '*':
                            cell = Cell(False)
                        elif token == 'o':
                            cell = Cell(True)
                        else:
                            raise RuntimeError(f'Token {token} not allowed')

                        row.append(cell)
                    self.__board.append(row)
Beispiel #9
0
    def update_board(self):
        """
            Updates the board with a new iteration of the game
        """
        new_board = []
        for idx in range(0, self.__num_rows):
            new_row = []
            for jdx in range(0, self.__num_cols):
                is_alive = self.__is_going_to_be_alive(idx, jdx)
                new_row.append(Cell(is_alive))
            new_board.append(new_row)

        self.__board.clear()
        self.__board = new_board
Beispiel #10
0
 def test_cell_is_dead_next_generation_if_lt_stable_count(self):
     cell = Cell()
     min_stable = Cell.STABLE_NEIGHBOR_RANGE[0]
     is_alive_next_gen = cell.is_alive_next_generation(min_stable - 1)
     self.assertFalse(is_alive_next_gen)
Beispiel #11
0
 def test_cell_is_still_alive_next_gen_if_in_stable_neighborhood(self):
     for i in Cell.STABLE_NEIGHBOR_RANGE:
         with self.subTest(stable_neighbor_count=i):
             cell = Cell()
             cell.is_alive_next_generation(i)
             self.assertTrue(cell.is_alive_next_generation)
Beispiel #12
0
 def test_get_str_death(self):
     death_cell = Cell(False)
     self.assertEqual(str(death_cell), '*')
Beispiel #13
0
 def test_create_death_cell(self):
     death_cell = Cell(False)
     self.assertFalse(death_cell.get_alive_status())
Beispiel #14
0
 def test_dead_cell_is_dead_next_gen_if_gt_fertile_neighbor_count(self):
     cell = Cell(alive=False)
     gt_fertile_count = Cell.FERTILE_NEIGHBOR_COUNT + 1
     is_alive_next_gen = cell.is_alive_next_generation(gt_fertile_count)
     self.assertFalse(is_alive_next_gen)
Beispiel #15
0
 def test_stay_alive(self):
     cell = Cell(ALIVE)
     neighbours2 = [1, 1, 0, 0, 0, 0, None, None]
     self.assertTrue(cell.will_survive(neighbours2))
     neighbours3 = [1, 1, 1, 0, 0, 0, None, None]
     self.assertTrue(cell.will_survive(neighbours3))
Beispiel #16
0
 def test_will_not_be_born(self):
     cell = Cell(DEAD)
     neighbours = [1, 1, 0, 0, 0, 0, 0,]
     self.assertFalse(cell.will_survive(neighbours))
     neighbours = [1, 1, 1, 1, 0, 0, 0]
     self.assertFalse(cell.will_survive(neighbours))
Beispiel #17
0
 def test_create_life(self):
     cell = Cell(DEAD)
     neighbours = [1, 1, 1, 0, 0, 0, 0, None, None]
     self.assertTrue(cell.will_survive(neighbours))
Beispiel #18
0
    def test_is_alive(self):
        alive_cell = Cell(ALIVE)
        self.assertTrue(alive_cell.is_alive)

        dead_cell = Cell(DEAD)
        self.assertFalse(dead_cell.is_alive)
Beispiel #19
0
def initialize_grid(h, w):
    return [[Cell(False) for _ in range(w)] for _ in range(h)]
Beispiel #20
0
def test_should_not_change_cells_if_all_cells_are_dead():
    grid_manager = GridManager()
    grid = [[Cell(False) for _ in range(1)] for _ in range(1)]
    new_grid = grid_manager.get_new_state(grid)

    assert new_grid[0][0].is_alive is False
Beispiel #21
0
 def test_cell_is_dead_from_overcrowding_next_gen_if_gt_stable_count(self):
     cell = Cell()
     max_stable = Cell.STABLE_NEIGHBOR_RANGE[-1]
     is_alive_next_gen = cell.is_alive_next_generation(max_stable + 1)
     self.assertFalse(is_alive_next_gen)
Beispiel #22
0
 def test_dead_cell_in_fertile_neighboorhood_is_alive_next_generation(self):
     cell = Cell(alive=False)
     is_alive_next_generation = cell.is_alive_next_generation(
         Cell.FERTILE_NEIGHBOR_COUNT)
     self.assertTrue(is_alive_next_generation)
Beispiel #23
0
 def test_will_not_survive_underpopulation(self):
     cell = Cell(ALIVE)
     neighbours = [1, 0, 0, 0, 0, 0, 0]
     self.assertFalse(cell.will_survive(neighbours))
Beispiel #24
0
 def test_a_new_cell_is_alive(self):
     cell = Cell()
     self.assertTrue(cell.is_alive)
Beispiel #25
0
 def set_alive_cells(self, coords: list):
     for row, col in coords:
         self.board[row][col] = Cell(ALIVE)
         self.prev_board[row][col] = Cell(ALIVE)
     return self
Beispiel #26
0
 def __init__(self, size):
     self.board = []
     for i in range(size):
         self.board.append([Cell(DEAD) for j in range(size)])
     self.prev_board = deepcopy(self.board)
Beispiel #27
0
 def __init__(self, *args, **kargs):
     Button.__init__(self, *args, **kargs)
     self.__cell = Cell(False)
     self['command'] = self.__toggle
Beispiel #28
0
 def test_get_str_alive(self):
     alive_cell = Cell(True)
     self.assertEqual(str(alive_cell), 'o')
Beispiel #29
0
 def test_a_dead_cell_is_not_alive(self):
     cell = Cell()
     cell.die()
     self.assertFalse(cell.is_alive)
Beispiel #30
0
 def test_create_a_cell(self):
     alive_cell = Cell(True)
     self.assertTrue(alive_cell.get_alive_status())
Beispiel #31
0
 def test_a_cell_can_be_set_dead(self):
     cell = Cell(alive=False)
     self.assertFalse(cell.is_alive)