Exemplo n.º 1
0
    def set_up_game(self, init_input):
        """
        This method overrides the inherited set_up_game method, setting up the Game of Life with an initial pattern to
        be given to the game engine.

        @param init_input The initial input to give to the GoL game engine.
        """
        GameController.set_up_game(self)

        # Create the initial input
        cell_pattern = []
        init_input = init_input.split("\n")

        for row in range(0, len(init_input)):
            cell_pattern.append([])
            for i in init_input[row]:
                if i == "*":
                    # If the cell is a *, it is meant to be alive, and so set a GoL cell with an alive state.
                    cell_pattern[row].append(GolCell(Alive()))
                else:
                    # Any other character represents death, and so set a GoL cell with a dead state.
                    cell_pattern[row].append(GolCell())

        initial_input = GolGrid()
        initial_input.set_cells(cell_pattern)

        # Create the Rule Set
        rule_set = RuleSetStandard()

        # Set up game
        self._game = GameOfLife(rule_set, initial_input)
Exemplo n.º 2
0
    def test_calculate_next_generation(self):
        """
        This method tests the ability of the game engine to calculate the next generation of the Game of Life. The
        expected result of this test is for the next generation to be correctly calculated.
        """
        # Create a grid and give it a pattern of cells:
        # ddad
        # aaad
        # dddd
        # adad
        # Represents the 'current generation'
        cur_gen = GolGrid(set_current_generation())

        # Create a grid and give it a pattern of cells:
        # adad
        # daaa
        # adad
        # dada
        # Represents the correct 'next generation'
        nex_gen = GolGrid(set_next_generation())

        # Give the Game of Life the first grid as the current generation
        gol = game_of_life.GameOfLife(rule_sets.RuleSetStandard(), cur_gen)

        res = gol._calculate_next_generation(cur_gen)
        # Assert that the game engine has calculated a new pattern.
        assert res

        for x, row in enumerate(res.get_cells()):
            # For each row of cells.
            for y, c in enumerate(row):
                # Assert that a cell has the correct state.
                assert c.get_state() == nex_gen.get_cells()[x][y].get_state()
Exemplo n.º 3
0
    def test_next_turn(self):
        """
        This method tests the ability of the game engine to play the next turn of the Game of Life. The expected
        result of this test is for the next turn to be played correctly, with the next generation of cells to be
        correct.
        """
        # Create a grid and give it a pattern of cells:
        # ddad
        # aaad
        # dddd
        # adad
        # Represents the 'current generation'
        cur_gen = GolGrid(set_current_generation())

        # Create a grid and give it a pattern of cells:
        # adad
        # daaa
        # adad
        # dada
        # Represents the correct 'next' generation
        nex_gen = GolGrid(set_next_generation())

        # Give the Game of life the first table as the current generation
        gol = game_of_life.GameOfLife(rule_sets.RuleSetStandard(), cur_gen)

        # Run the next turn of the game.
        gol.next_turn()

        retrieved_pattern = gol.get_current_generation()
        for x, row in enumerate(retrieved_pattern.get_cells()):
            # For each row of cells.
            for y, c in enumerate(row):
                # Assert that a cell has the right state.
                assert c.get_state() == nex_gen.get_cells()[x][y].get_state()
Exemplo n.º 4
0
    def test_set_cells(self):
        """
        This method tests the ability of the GoLGrid to store a collection of GoLCells. The expected result of this
        tests is that a collection of GoLCells can be correctly set.
        """
        test_grid = GolGrid()

        # Create a collection of GolCell objects
        # Hard coding the expectation of a 10 x 10 2D array as it will fail
        # if someone changes the keyword parameters
        s = 10
        cells = []
        for x in range(0, s):
            cells.append([])
            for y in range(0, s):
                c = GolCell()
                if y % 2 == 0:
                    c.set_state(Alive())
                cells[x].append(c)

        test_grid.set_cells(cells)
        # Assert that the collection of GoLCells has been set correctly.
        assert test_grid.get_cells()

        recieved_cells = test_grid.get_cells()
        for x, row in enumerate(recieved_cells):
            # For each row of cells.
            for y, _column in enumerate(row):
                # Assert that the GoLCell at these 'coordinates' is the correct GoLCell.
                assert recieved_cells[x][y] == cells[x][y]
Exemplo n.º 5
0
    def test_get_no_alive_cells(self):
        """
        Tests the ability of the grid to retrieve the number of living cells.

        This method tests the ability of the GoLGrid to retrieve the number of living GoLCells it currently has.
        """
        test_grid = GolGrid()
        alive_cells = test_grid.get_no_alive_cells()
        # At this point, none of the GoLCells should be alive. Assert this is true.
        assert alive_cells == 0

        # Create another set of cells, half of which are alive.
        s = 10
        cells = []
        for x in range(0, s):
            cells.append([])
            for y in range(0, s):
                c = GolCell()
                if y % 2 == 0:
                    # Make every second cell alive.
                    c.set_state(Alive())
                cells[x].append(c)

        test_grid = GolGrid(cells)
        alive_cells = test_grid.get_no_alive_cells()
        # Assert that the correct number of cells have been retrieved.
        assert alive_cells == (s * s) / 2
Exemplo n.º 6
0
    def test_grid_init(self):
        """
        This method tests the initialisation of a GoLGrid object. The expected result of this test is that the GolGrid
        should be initialised with a number of GoLCells, which are all dead.
        """
        pattern = [[GolCell(),
                    GolCell(),
                    GolCell(),
                    GolCell()],
                   [GolCell(),
                    GolCell(),
                    GolCell(),
                    GolCell()],
                   [GolCell(),
                    GolCell(),
                    GolCell(),
                    GolCell()],
                   [GolCell(),
                    GolCell(),
                    GolCell(),
                    GolCell()]]

        test_grid = GolGrid(pattern)
        # Assert that the GoLGrid has been initialised.
        assert test_grid

        cells = test_grid.get_cells()

        for row in cells:
            # For each row of cells.
            for c in row:
                # Assert that the state of each cell is dead, as it should be.
                assert isinstance(c.get_state(), Dead)
    def test_next_turn(self):
        '''
        Tests that the next turn of the game retrieves
        the correct information from storage and calculates
        the next generation of cells in the next way.
        '''
        # Create a grid and give it a pattern of cells:
        # ddad
        # aaad
        # dddd
        # adad
        # Represents the 'current generation'
        cur_gen = GolGrid(self.set_current_generation())

        # Create a grid and give it a pattern of cells:
        # adad
        # daaa
        # adad
        # dada
        # Represents the correct 'next' generation
        nex_gen = GolGrid(self.set_next_generation())

        # Give the Game of life the first table as the current generation
        gol = game_of_life.GameOfLife(rule_sets.RuleSetStandard(), cur_gen)

        # Run the next turn of the game.
        gol.next_turn()

        # Test that current generation has been correctly stored.
        retrieved_pattern = gol.get_current_generation()
        for x, row in enumerate(retrieved_pattern.get_cells()):
            for y, c in enumerate(row):
                assert c.get_state() == nex_gen.get_cells()[x][y].get_state()
    def test_calculate_next_generation(self):
        '''
        Tests that the next generation of cells has been
        correctly calculated.
        '''
        # Create a grid and give it a pattern of cells:
        # ddad
        # aaad
        # dddd
        # adad
        # Represents the 'current generation'
        cur_gen = GolGrid(self.set_current_generation())

        # Create a grid and give it a pattern of cells:
        # adad
        # daaa
        # adad
        # dada
        # Represents the correct 'next generation'
        nex_gen = GolGrid(self.set_next_generation())

        # Give the Game of Life the first grid as the current generation
        gol = game_of_life.GameOfLife(rule_sets.RuleSetStandard(), cur_gen)

        # Let the Game of Life calculate the next generation and test the
        # result is not none.
        res = gol._calculate_next_generation(cur_gen)
        assert res

        # Test the result against the correct result - if match, test passed
        for x, row in enumerate(res.get_cells()):
            for y, c in enumerate(row):
                assert c.get_state() == nex_gen.get_cells()[x][y].get_state()
Exemplo n.º 9
0
    def test_get_cells(self):
        """
        This method tests the ability of the GoLGrid to retrieve all of its GoLCells. The expected result of this
        test is that the GoLCells are correctly retrieved.
        """
        test_grid = GolGrid()

        cells = test_grid.get_cells()
        # Assert that a collection of cells has been correctly retrieved.
        assert cells
Exemplo n.º 10
0
    def __init__(self, root=None, X_MAX=10, Y_MAX=10):
        """
        Constructor

        root - reference to the wdiget containing this widget
        """
        Frame.__init__(self, root)

        cells = []
        for x in range(0, X_MAX):
            cols = []
            for y in range(0, Y_MAX):
                cell = CellWidget(master=self, width=C_SIZE, height=C_SIZE)
                cell.grid(row=x, column=y)
                cols.append(cell)
            cells.append(cols)

        GolGrid.__init__(self, cells)