Пример #1
0
 def test_step__line_of_sight_policy(self):
     grid = ['L.L.#']
     next_grid = ['#.L.#']
     g = GameOfLife(grid,
                    extinction_threshold=4,
                    neighbour_policy='line-of-sight')
     self.assertEqual(g.step(), next_grid)
Пример #2
0
 def test_step__new_life(self):
     grid = ['L.#', 'L.#', '#..']
     next_grid = ['#.#', 'L.#', '#..']
     g = GameOfLife(grid,
                    extinction_threshold=4,
                    neighbour_policy='adjacent')
     self.assertEqual(g.step(), next_grid)
Пример #3
0
 def test_step__some_die(self):
     grid = ['.#.', '###', '.#.']
     next_grid = ['.#.', '#L#', '.#.']
     g = GameOfLife(grid,
                    extinction_threshold=4,
                    neighbour_policy='adjacent')
     self.assertListEqual(g.step(), next_grid)
Пример #4
0
    def test_doesnt_crash_on_edges2(self):
        game = GameOfLife()
        game.grid = [
                [False, False, False],
                [False, True, True],
                [False, False, False]
               ]

        self.assertEqual(
            game.number_of_neighbours(0, 2, game.grid),
            2
        )
Пример #5
0
    def test_gives_correct_result_1(self):
        game = GameOfLife()
        game.grid = [
                [True, True, True],
                [True, True, True],
                [True, True, True]
               ]

        self.assertEqual(
            game.number_of_neighbours(1, 1, game.grid),
            8
        )
Пример #6
0
    def test_gives_correct_result_2(self):
        game = GameOfLife()
        game.grid = [
                [False, False, False],
                [False, False, False],
                [False, False, False]
               ]

        self.assertEqual(
            game.number_of_neighbours(1, 1, game.grid),
            0
        )
Пример #7
0
 def setUp(self):
     self.game_options = {
       'rows' : 25,
       'cols' : 35,
       'alive_cells' : 100
     }
     self.game = GameOfLife(self.game_options)
Пример #8
0
class TestGameOfLife(unittest.TestCase):

    def setUp(self):
        self.game_options = {
          'rows' : 25,
          'cols' : 35,
          'alive_cells' : 100
        }
        self.game = GameOfLife(self.game_options)

    def tearDown(self):
        self.game = None
        self.game_options = None
        
  
    def test_generation_number_is_set(self):
        """When the game is initialized. Generation count is set to 1."""
        self.assertEqual(self.game.generation, 1)

    def test_cells_are_initalized_with_correct_ammount(self):
        """"The user provides the amount of starting live cells in the game options. Check that is accurately conveyed to the real game."""
        alive_cells = 0
        for i, line in enumerate(self.game.current_cells):
            for j, cell in enumerate(line):
                if cell:
                    alive_cells += 1

        self.assertEqual(alive_cells, self.game_options['alive_cells'])

    def test_alive_neighbour_count(self):
        """Test that the amount of alive neighbours is being calculated properly."""
        self.game.current_cells = self.game.blank_cells.copy()
        self.game.current_cells[0][0] = 1
        self.game.current_cells[0][1] = 1

        self.assertEqual(self.game.alive_neighbours(0,0), 1)
        self.assertEqual(self.game.alive_neighbours(0,1), 1)
        self.assertEqual(self.game.alive_neighbours(0,2), 1)
        self.assertEqual(self.game.alive_neighbours(0,3), 0)
        self.assertEqual(self.game.alive_neighbours(1,0), 2)
        self.assertEqual(self.game.alive_neighbours(1,1), 2)
        self.assertEqual(self.game.alive_neighbours(1,2), 1)
        self.assertEqual(self.game.alive_neighbours(1,3), 0)
Пример #9
0
 def setUp(self):
     self.game = GameOfLife()
Пример #10
0
class TickTest(unittest.TestCase):
    def setUp(self):
        self.game = GameOfLife()

    def test_live_cells_with_two_neighbours_survives(self):
        self.game.grid = [
            [False, True, False],
            [False, True, False],
            [False, True, False],
        ]
        self.game.tick()
        self.assertEqual(
            self.game.grid[1][1],
            True
        )

    def test_live_cells_with_three_neighbours_survives(self):
        self.game.grid = [
            [False, True, False],
            [False, True, True],
            [False, True, False],
        ]
        self.game.tick()
        self.assertEqual(
            self.game.grid[1][1],
            True
        )

    def test_dead_cells_with_three_neighbours_become_alive(self):
        self.game.grid = [
            [False, True, False],
            [False, False, True],
            [False, True, False],
        ]
        self.game.tick()
        self.assertEqual(
            self.game.grid[1][1],
            True
        )

    def test_live_cells_with_no_neighbours_dies(self):
        self.game.grid = [
            [False, False, False],
            [False, True, False],
            [False, False, False],
        ]
        self.game.tick()
        self.assertEqual(
            self.game.grid[1][1],
            False
        )

    def test_live_cells_with_single_neighbour_dies(self):
        self.game.grid = [
            [False, False, False],
            [True, True, False],
            [False, False, False],
        ]
        self.game.tick()
        self.assertEqual(
            self.game.grid[1][1],
            False
        )

    def test_live_cells_with_four_neighbours_dies(self):
        self.game.grid = [
            [True, True, True],
            [True, True, False],
            [False, False, False],
        ]
        self.game.tick()
        self.assertEqual(
            self.game.grid[1][1],
            False
        )
Пример #11
0
 def test_grid_init(self):
     game = GameOfLife(3, 5)
     self.assertEqual(len(game.grid), 3)
     self.assertEqual(len(game.grid[0]), 5)
Пример #12
0
def test_game_of_life_beacon():
    game = GameOfLife(beacon[0])

    for state in beacon[1:]:
        game.step()
        assert (game.board == state)
Пример #13
0
def test_game_of_life_toad():
    game = GameOfLife(toad[0])

    for state in toad[1:]:
        game.step()
        assert (game.board == state)
Пример #14
0
def test_game_of_life_blinker():
    game = GameOfLife(blinker[0])

    for state in blinker[1:]:
        game.step()
        assert (game.board == state)
Пример #15
0
    pygame.init()

    GAME_RES = WIDTH, HEIGHT = 1024, 1024
    FPS = 60
    GAME_TITLE = 'Blank'

    window = pygame.display.set_mode(GAME_RES, HWACCEL | HWSURFACE | DOUBLEBUF)
    pygame.display.set_caption(GAME_TITLE)
    clock = pygame.time.Clock()

    # Game Values

    background_color = (150, 150, 150)
    count = 1

    gol = GameOfLife(16, 16, multithreaded=True)
    cell_scale = 4

    # End of Game Values

    # Game loop
    game_ended = False
    while not game_ended:

        # Event handling
        for event in pygame.event.get():
            if event.type == QUIT:
                game_ended = True
                break
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
Пример #16
0
 def test_count_occupied__empty(self):
     g = GameOfLife(['.L.'],
                    extinction_threshold=1,
                    neighbour_policy='adjacent')
     self.assertEqual(g.count_occupied(), 0)