Ejemplo n.º 1
0
def game_setup():
    grid_x = int(entr1.get())
    grid_y = int(entr2.get())
    selected_pattern = comboBox.current()
    init_state = [[0 for x in range(0, grid_x)] for y in range(0, grid_y)]
    if selected_pattern == 0:  #glider
        init_state[2][2] = 1
        init_state[3][3] = 1
        init_state[4][3] = 1
        init_state[4][2] = 1
        init_state[4][1] = 1

    if selected_pattern == 1:  #oscillator
        init_state[3][4] = 1
        init_state[2][4] = 1
        init_state[1][4] = 1

    if selected_pattern == 2:  #still life
        init_state[1][4] = 1
        init_state[1][5] = 1
        init_state[3][4] = 1
        init_state[3][5] = 1
        init_state[2][3] = 1
        init_state[2][6] = 1

    if selected_pattern == 3:  #random
        init_state = [[np.random.randint(2) for x in range(0, grid_x)]
                      for y in range(0, grid_y)]

    gol = GameOfLife.GameOfLife(grid_x, grid_y, init_state)
    gol.game_of_life()
Ejemplo n.º 2
0
#!/usr/bin/python3

import json
import GameOfLife as gol

if __name__ == '__main__':
    with open('config.json', encoding='utf-8') as config_file:
        config = json.loads(config_file.read())
    config['is_test'] = 1
    config['test_pattern'] = "Pulsar"
    gol.GameOfLife(config)
Ejemplo n.º 3
0
from Game of Life import GameOfLife

game = GameOfLife()
game.run()
Ejemplo n.º 4
0
def main():
    GameOfLife.GameOfLife()
Ejemplo n.º 5
0
class GuiLogic(object):
    GOL = GameOfLife.GameOfLife()  # Set up game logic
    DONE = False  # Loop until the user clicks the close button.
    ADVANCE = False
    BLACK = (0, 0, 0)  # Define some colors
    WHITE = (255, 255, 255)
    GREEN = (0, 255, 0)
    RED = (255, 0, 0)
    WIDTH = 15  # This sets the WIDTH and HEIGHT of each grid location
    HEIGHT = 15
    MARGIN = 2  # This sets the margin between each cell
    WINDOW_SIZE = []  # Set the HEIGHT and WIDTH of the SCREEN
    SCREEN = None
    CLOCK = pygame.time.Clock()  # Used to manage how fast the SCREEN updates

    def examineEvents(self):
        for event in pygame.event.get():  # User did something
            if event.type == pygame.QUIT:  # If user clicked close
                self.DONE = True  # Flag that we are DONE so we exit this loop

            elif event.type == pygame.MOUSEBUTTONDOWN:
                # User clicks the mouse. Get the position
                pos = pygame.mouse.get_pos()

                # Change the x/y SCREEN coordinates to grid coordinates
                column = pos[0] // (self.WIDTH + self.MARGIN)
                row = pos[1] // (self.HEIGHT + self.MARGIN)

                # Set that location to one
                self.GOL.board[column][row] = not (self.GOL.board[column][row])
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    self.ADVANCE = not (self.ADVANCE
                                        )  #Stop iterating generations
                if event.key == pygame.K_c:
                    self.clearBoard()

    def clearBoard(self):
        self.GOL.reset()

    def drawGrid(self):
        for row in range(self.GOL.B_HEIGHT):
            for column in range(self.GOL.B_WIDTH):
                color = self.WHITE
                if self.GOL.board[column][row] == 1:
                    color = self.BLACK
                pygame.draw.rect(
                    self.SCREEN, color,
                    [(self.MARGIN + self.WIDTH) * column + self.MARGIN,
                     (self.MARGIN + self.HEIGHT) * row + self.MARGIN,
                     self.WIDTH, self.HEIGHT])

    def mainLoop(self):
        time_since_last_gen = 0

        while not self.DONE:
            self.examineEvents()

            self.SCREEN.fill(self.BLACK)  # Set the SCREEN background
            self.drawGrid()  # Draw the grid

            dt = self.CLOCK.tick(60)  # Limit to 60 frames per second
            time_since_last_gen += dt  # Calculate millis seconds passed since last loop

            # If one second has passed move forward one generation
            if (time_since_last_gen > 500):
                if (self.ADVANCE):
                    self.GOL.nextGeneration()
                # Reset
                time_since_last_gen = 0

            # Go ahead and update the SCREEN with what we've drawn.
            pygame.display.flip()

        pygame.quit()

    def __init__(self):
        pygame.init()  # Initialize pygame
        pygame.display.set_caption(
            "Conways Game Of Life")  # Set title of SCREEN
        self.WINDOW_SIZE = [((self.WIDTH * self.GOL.B_WIDTH) +
                             (self.MARGIN * (self.GOL.B_WIDTH + 1))),
                            ((self.HEIGHT * self.GOL.B_HEIGHT) +
                             (self.MARGIN * (self.GOL.B_HEIGHT + 1)))]
        self.SCREEN = pygame.display.set_mode(self.WINDOW_SIZE)
Ejemplo n.º 6
0
from GameOfLife import *

game = GameOfLife(100, 100, 1000)
game.read_state_from_file("Ronaldo", red = True)
game.read_state_from_file("Messi", red = False)
game.update_display()
input("Press enter to continue")

while True:
    for _ in range(3):
        game.evolve()
        if game.red_has_won():
            game.update_display()
            game.display_result("red")
            input("Press enter to continue")
            exit()
        if game.black_has_won():
            game.update_display()
            game.display_result("black")
            input("Press enter to continue")
            exit()
        if game.tiebreaker_needed():
            game.update_display()
            game.display_result("tie")
            input("Press enter to continue")
            break
    game.update_display()

game.t = game.T
while True:
    game.seed_random_cells()