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)
Beispiel #2
0
    def __init__(self):
        '''
        Constructor

        Initialise GameOfLife object
        Build the gui, bind events and initialise the display
        '''
        # Encasing frame
        tk = Tk()
        Frame.__init__(self, master=tk)

        # Grid frame
        self._grid = GridWidget(self, X_SIZE, Y_SIZE)
        for row in self._grid.get_cells():
            for cell in row:
                cell.bind('<Button-1>', self._cell_listener)

        # buttons
        self._next_button = NextButton(master=self)
        self._next_button.bind('<Button-1>', self._next_step_button_listener)

        # Set layout
        self._grid.grid(row=0, column=0)
        self._next_button.grid(row=0, column=1)

        # Game of life
        self._gol = GameOfLife(RuleSetStandard(), self._grid)

        self.pack()
        self._grid.pack()
        self._next_button.pack()
class GameOfLifeController(GameController):
    """
    This class represents the controller for the Game of Life. This class will act as a bridge between the GoL game
    engine and any other pieces of software that want to use it, controlling what data the software can access and
    mutate.
    """

    def __init__(self):
        """
        Ctor - Initialises the GoL Controller.
        """
        GameController.__init__(self)

    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)

    def play_next_turn(self):
        """
        This method plays a single turn of the Game of Life.
        """
        if not self.get_game().is_game_forsaken():
            # If there are still living cells on the grid, the next turn of the GoL is to be played.
            self.get_game().next_turn()

    def get_turn_count(self):
        """
        This method retrieves the number of turns the Game of Life has already been playing for.

        @return The turn count.
        """
        return self._game.get_turn_count()

    def get_current_generation(self, output=False):
        """
        Returns the current generation of the game of life, in the form of a
        Grid object

        This method retrieves the current generation, or the current cell configuration, for the Game of Life.

        @param output Returns true if this method is being called by external software. If output is false, this method
                      returns the configuration as a Grid object. If it is true, the configuration will be returned as
                      a string, because nothing outside of the game engine can know of Grid objects.
        """
        if not output:
            return self._game.get_current_generation()
        else:
            return self._to_string(self._game.get_current_generation())

    def _to_string(self, grid):
        """
        This method translates a Grid object into a string.

        @param grid The Grid object to translate into a string.

        @return string A string representation of the specified Grid object.
        """
        string = ""
        for row in range(0, len(grid.get_cells())):
            for cell in grid.get_cells()[row]:
                if cell.get_state() == Alive():
                    string += '*'
                else:
                    string += '-'

            if row != len(grid.get_cells()) - 1:
                string += '\n'

        return string
Beispiel #4
0
class Gui(Frame):
    '''
    Gui object for the game of life engine.

    Simple functionality:
        - Enter pattern into grid
        - Start simulation button
        - Stop simulation button
        - Next step button
    '''

    def __init__(self):
        '''
        Constructor

        Initialise GameOfLife object
        Build the gui, bind events and initialise the display
        '''
        # Encasing frame
        tk = Tk()
        Frame.__init__(self, master=tk)

        # Grid frame
        self._grid = GridWidget(self, X_SIZE, Y_SIZE)
        for row in self._grid.get_cells():
            for cell in row:
                cell.bind('<Button-1>', self._cell_listener)

        # buttons
        self._next_button = NextButton(master=self)
        self._next_button.bind('<Button-1>', self._next_step_button_listener)

        # Set layout
        self._grid.grid(row=0, column=0)
        self._next_button.grid(row=0, column=1)

        # Game of life
        self._gol = GameOfLife(RuleSetStandard(), self._grid)

        self.pack()
        self._grid.pack()
        self._next_button.pack()

    def _cell_listener(self, event):
        '''
        Event listener for the cells

        Event: an object created when the cell is clicked
        '''
        cell = event.widget
        a = Alive()
        if cell.get_state() == a:
            cell.set_state(Dead())
        else:
            cell.set_state(a)

        cell.repaint()

    def _next_step_button_listener(self, event):
        '''
        Event listener for the next step button

        Event: event object from the tkinter object
        '''
        self._gol.next_turn()
        self._grid.repaint()

    def _start_stop_button_listener(self, event):
        '''
        Event listener for the start button

        Event: event object from the tkinter object
        '''
        pass