Beispiel #1
0
def main():
    # Set the size of the grid.
    GRID_WIDTH, GRID_HEIGHT = None, None
    while not GRID_WIDTH and not GRID_HEIGHT:
        try:
            GRID_WIDTH, GRID_HEIGHT = [
                int(x)
                for x in input("Enter width and height of the grid: ").split()
            ]
        except:
            print("Something went wrong - make sure you entered\
                  two positive numbers")

    # Indicate the number of generations.
    NUM_GENS = None
    while not NUM_GENS:
        try:
            NUM_GENS = int(input("Enter the number of generations: "))
        except:
            print("Something went wrong - make sure you entered\
                  one positive number")

    # Constructs the game grid and configure it.
    grid = LifeGrid(GRID_WIDTH, GRID_HEIGHT)
    grid.configure(INIT_CONFIG, list())

    # Plays the game.
    draw(grid)
    for i in range(NUM_GENS):
        evolve(grid)
        draw(grid)
Beispiel #2
0
    def __init__(self, master):
        master.wm_title('Game of life')
        self.frame = Frame(master)
        self.frame.pack()

        self.top_frame = Frame(self.frame)
        self.grid_frame = Frame(self.frame)

        self.top_frame.pack(side="top", fill="x")
        self.grid_frame.pack(side="bottom", fill="both", expand=True)

        self.start_button = Button(self.top_frame,
                                   text="Start",
                                   command=self.toggle)
        self.start_button.grid(column=0, row=0)
        self.reset_button = Button(self.top_frame,
                                   text="Reset",
                                   command=self.reset)
        self.reset_button.grid(column=1, row=0)

        self.generation_string = StringVar()
        self.generation_counter = Label(self.top_frame,
                                        textvariable=self.generation_string)
        self.generation_counter.grid(column=2, row=0)
        self.update_generation()

        self.canvas = Canvas(self.grid_frame,
                             width=App.CELL_SIZE * App.GRID_WIDTH,
                             height=App.CELL_SIZE * App.GRID_HEIGHT)
        self.canvas.grid(column=0, row=1)
        self.canvas.bind('<Button-1>', self.on_canvas_click)

        self.grid = LifeGrid(self.GRID_WIDTH, self.GRID_HEIGHT)
        self.draw_grid()
def main():
    GRID_WIDTH = int(input("Input width: "))
    GRID_HEIGHT = int(input("Input height: "))
    NUM_GENS = int(input("Input number of generations: "))
    # Constructs the game grid and configure it.
    grid = LifeGrid(GRID_WIDTH, GRID_HEIGHT)
    grid.configure(INIT_CONFIG)

    # Plays the game.
    draw(grid)
    for i in range(NUM_GENS):
        evolve(grid)
        draw(grid)
Beispiel #4
0
  def __init__(self, master):
    master.wm_title('Game of life')
    self.frame = Frame(master)
    self.frame.pack()

    self.top_frame = Frame(self.frame)
    self.grid_frame = Frame(self.frame)

    self.top_frame.pack(side="top", fill="x")
    self.grid_frame.pack(side="bottom", fill="both", expand=True)

    self.start_button = Button(self.top_frame, text="Start", command=self.toggle)
    self.start_button.grid(column=0, row=0)
    self.reset_button = Button(self.top_frame, text="Reset", command=self.reset)
    self.reset_button.grid(column=1, row=0)

    self.generation_string = StringVar()
    self.generation_counter = Label(self.top_frame, textvariable=self.generation_string)
    self.generation_counter.grid(column=2, row=0)
    self.update_generation()
    
    self.canvas = Canvas(self.grid_frame, width=App.CELL_SIZE * App.GRID_WIDTH, height=App.CELL_SIZE * App.GRID_HEIGHT)
    self.canvas.grid(column=0, row=1)
    self.canvas.bind('<Button-1>', self.on_canvas_click)

    self.grid = LifeGrid(self.GRID_WIDTH, self.GRID_HEIGHT)
    self.draw_grid()
Beispiel #5
0
	def test_peek_horizontal_boundaries(self):
		testgridleft = LifeGrid(10,10)
		# (0, 0) + (-1, 0) == (9, 0)
		testgridleft.set_xy(9, 0, True)
		self.assertTrue(testgridleft.peek(0,0,-1,0))
		
		testgridright = LifeGrid(10,10)
		# (9, 0) + (1, 0) == (0, 0)
		testgridright.set_xy(0, 0, True)
		self.assertTrue(testgridright.peek(9,0,1,0))
Beispiel #6
0
	def test_peek_vertical_boundaries(self):
		testgridbottom = LifeGrid(10,10)
		# (0, 0) + (0, -1) == (0, 9)
		testgridbottom.set_xy(0,9, True)
		self.assertTrue(testgridbottom.peek(0,0,0,-1))
		
		testgridtop = LifeGrid(10,10)
		# (0, 9) + (0, 1) == (0, 0)
		testgridtop.set_xy(0, 0, True)
		self.assertTrue(testgridtop.peek(0,9,0,1))
def main():
    '''
     Constructs the game grid and configure it.
    '''
    grid_width = int(input("Enter the width:"))
    grid_height = int(input("Enter the height:"))
    num_gens = int(input("Enter the number of generations"))
    grid = LifeGrid(grid_width, grid_height)
    try:
        grid.configure(INIT_CONFIG)
        # Plays the game.
        print(draw(grid))
        # print("neighbour:",grid.num_live_neighbors(0, 0))
        # print("neighbours:",grid.num_live_neighbors(1, 1))
        for i in range(num_gens):
            evolve(grid)
            time.sleep(1)
            print(draw(grid))
    except IndexError:
        print(
            "Please change INIT_CONFIG in correspondance to the chosen width and height"
        )
Beispiel #8
0
def main():
    init_config = []
    m = 1
    n = 4
    # Constructs the game grid and configure it.
    grid_widz = int(input("Enter width: "))
    grid_height = int(input("Enter height: "))
    num_gens = int(input("Enter number of gens: "))
    i = input(
        "Enter list of coords in tuples (ex: [(1, 1), (1, 2), (2, 2), (3, 2)]: "
    )[1:-1]
    while n < len(i):
        t = (int(i[m]), int(i[n]))
        init_config.append(t)
        m += 8
        n += 8
    grid = LifeGrid(grid_widz, grid_height)
    grid.configure(init_config)

    # Plays the game.
    draw(grid)
    for i in range(num_gens):
        evolve(grid)
        draw(grid)
Beispiel #9
0
class App:

    CELL_SIZE = 10
    GRID_WIDTH = 50
    GRID_HEIGHT = 50

    running = False
    generation = 0

    def __init__(self, master):
        master.wm_title('Game of life')
        self.frame = Frame(master)
        self.frame.pack()

        self.top_frame = Frame(self.frame)
        self.grid_frame = Frame(self.frame)

        self.top_frame.pack(side="top", fill="x")
        self.grid_frame.pack(side="bottom", fill="both", expand=True)

        self.start_button = Button(self.top_frame,
                                   text="Start",
                                   command=self.toggle)
        self.start_button.grid(column=0, row=0)
        self.reset_button = Button(self.top_frame,
                                   text="Reset",
                                   command=self.reset)
        self.reset_button.grid(column=1, row=0)

        self.generation_string = StringVar()
        self.generation_counter = Label(self.top_frame,
                                        textvariable=self.generation_string)
        self.generation_counter.grid(column=2, row=0)
        self.update_generation()

        self.canvas = Canvas(self.grid_frame,
                             width=App.CELL_SIZE * App.GRID_WIDTH,
                             height=App.CELL_SIZE * App.GRID_HEIGHT)
        self.canvas.grid(column=0, row=1)
        self.canvas.bind('<Button-1>', self.on_canvas_click)

        self.grid = LifeGrid(self.GRID_WIDTH, self.GRID_HEIGHT)
        self.draw_grid()

    def draw_grid(self):
        self.canvas.delete('all')
        self.canvas.create_rectangle((1, 1, self.CELL_SIZE * self.GRID_WIDTH,
                                      self.CELL_SIZE * self.GRID_HEIGHT))

        for y, row in enumerate(self.grid.matrix):
            for x, cell in enumerate(row):
                if self.grid.matrix[y][x] == 1:
                    self.canvas.create_rectangle(
                        (x * self.CELL_SIZE, y * self.CELL_SIZE,
                         x * self.CELL_SIZE + self.CELL_SIZE,
                         y * self.CELL_SIZE + self.CELL_SIZE),
                        fill="black")

    def toggle(self):
        if self.running:
            self.running = False
            self.start_button.config(text="Start")
        else:
            self.running = True
            self.start_button.config(text="Stop")
            self.do_step()

    def reset(self):
        self.grid.reset()
        self.generation = 0
        self.draw_grid()
        self.update_generation()

    def do_step(self):
        if self.running:
            self.grid.step()
            self.generation += 1
            self.draw_grid()
            self.update_generation()
            self.frame.after(1000, self.do_step)

    def on_canvas_click(self, event):
        if not self.running:
            x = event.x / self.CELL_SIZE
            y = event.y / self.CELL_SIZE

            self.grid.matrix[y][x] = not self.grid.matrix[y][x]
            self.draw_grid()

    def update_generation(self):
        self.generation_string.set("Generation: {0}".format(self.generation))
Beispiel #10
0
	def test_peek_diagonal(self):
		testgrid_diagonal = LifeGrid(10,10)
		# (0, 0) + (-1, -1) == (9, 9)
		testgrid_diagonal.set_xy(9, 9, True)
		self.assertTrue(testgrid_diagonal.peek(0,0,-1,-1))
Beispiel #11
0
class App:

  CELL_SIZE = 10
  GRID_WIDTH = 50
  GRID_HEIGHT = 50

  running = False
  generation = 0

  def __init__(self, master):
    master.wm_title('Game of life')
    self.frame = Frame(master)
    self.frame.pack()

    self.top_frame = Frame(self.frame)
    self.grid_frame = Frame(self.frame)

    self.top_frame.pack(side="top", fill="x")
    self.grid_frame.pack(side="bottom", fill="both", expand=True)

    self.start_button = Button(self.top_frame, text="Start", command=self.toggle)
    self.start_button.grid(column=0, row=0)
    self.reset_button = Button(self.top_frame, text="Reset", command=self.reset)
    self.reset_button.grid(column=1, row=0)

    self.generation_string = StringVar()
    self.generation_counter = Label(self.top_frame, textvariable=self.generation_string)
    self.generation_counter.grid(column=2, row=0)
    self.update_generation()
    
    self.canvas = Canvas(self.grid_frame, width=App.CELL_SIZE * App.GRID_WIDTH, height=App.CELL_SIZE * App.GRID_HEIGHT)
    self.canvas.grid(column=0, row=1)
    self.canvas.bind('<Button-1>', self.on_canvas_click)

    self.grid = LifeGrid(self.GRID_WIDTH, self.GRID_HEIGHT)
    self.draw_grid()

  def draw_grid(self):
    self.canvas.delete('all')
    self.canvas.create_rectangle((1, 1, self.CELL_SIZE * self.GRID_WIDTH, self.CELL_SIZE * self.GRID_HEIGHT))    

    for y, row in enumerate(self.grid.matrix):
      for x, cell in enumerate(row):
        if self.grid.matrix[y][x] == 1:
          self.canvas.create_rectangle((x*self.CELL_SIZE, y*self.CELL_SIZE, x*self.CELL_SIZE+self.CELL_SIZE, y*self.CELL_SIZE+self.CELL_SIZE), fill="black")

  def toggle(self):
    if self.running:
      self.running = False
      self.start_button.config(text="Start")
    else:
      self.running = True
      self.start_button.config(text="Stop")
      self.do_step()

  def reset(self):
    self.grid.reset()
    self.generation = 0
    self.draw_grid()
    self.update_generation()

  def do_step(self):
    if self.running:
      self.grid.step()
      self.generation += 1
      self.draw_grid()
      self.update_generation()
      self.frame.after(1000, self.do_step)

  def on_canvas_click(self, event):
    if not self.running:
      x = event.x / self.CELL_SIZE
      y = event.y / self.CELL_SIZE

      self.grid.matrix[y][x] = not self.grid.matrix[y][x]
      self.draw_grid()

  def update_generation(self):
    self.generation_string.set("Generation: {0}".format(self.generation))