Just got to the point at which I can click buttons and it works for a bit and started playing with it, lol. In fact I'm pretty sure it's got a couple bugs. Probably not the best written thing in the world, but I don't care. You can only toggle populations and change speed when paused. Square brackets to change delay between updates (limited between 0.0125s and 1.6s). Os.Here's ~2 hours of mucking around making the game of life in python with tkinter. Return "\n".join() for row in self.cells])Īnd a (simplified version of the) main loop would then be table = Table(width, height) New_cells = for row in range(self.height)] Sum += self.cell_state(row + row_shift, col + col_shift) If row in range(self.height) and col in range(self.width): Self.cells = ) for _ in range(width)] for _ in range(height)] This is what it would look like (without changing your logic otherwise): class Table:ĭef _init_(self, width, height, rand_seed=time.time()): Method, and then simply call print(table). The Python way of printing an object is to implement the Make the global functions instance methods of that class. In other words: make it a class with attributes and consequently, Height from the table would be one way to remove this inconsistency.Īnother way would make the table “know” its width and height. Takes it as parameters, but get_cell_state(table, row, col)ĭetermines it from the table itself. The table width/height is not treated consistently: update_table(table, height, width) Return ) for _ in range(width)] for _ in range(height)] Initial table: def generate_table(height, width, rand_seed=time.time()): The same list comprehension can be used when generating the This makesĪlso the deep copy (and thus the import copy) unnecessary. Instead of nested for loops and an if/elif/elif chain. (nested) list comprehension def update_table(table, height, width): Then computing the next-generation table can be done with Return num_neighbors = 2 or num_neighbors = 3 Num_neighbors = get_neighboring_cells(table, row, col) If you compute the new cell state in a separate function def new_state(table, row, col): Otherwise the program will crash for non-square tables. In the “main loop” should be table = update_table(table, width, height) There is one bug: table = update_table(table, height, width) check why, this is a very important lesson.ĭeepcopy - you don't need a copy, you want an empty table. gives a ValueError currently)? What happens if someone imports your module and tries to generate random boards without seed? you will generate identical boards. you gave the user the chance to reproduce a board - nicely done. Seeding - usually shall be done once at program start only. there are also patterns to throw import errors on wrong OS to give correct and immediate information. functions used by an importer should not depent on that. Some non-portable OS calls - while acceptable for a test program you should think of a better solution. parameters to run() should be an initial board and the visualisation callback. split it into a run() function with parameters and a interactive main(). It has got some game logic (the loop), it has got some UI (input and printing). Main() - while you have the guard and also an extra printing function there is a little mess in main(). Table = update_table(table, height, width) Table = generate_table(width, height, float(rand_seed)) Rand_seed = input('Enter seed to generate the table(leave blank if dont want to specify') Width = int(input('Enter table height: ')) Height = int(input('Enter table width: ')) Os.system('color f0') # making the background white and text black Print(black_square_symbol if elem else ' ', end='') Os.system('cls') # clearing up the screen to print new table If neighboring_cells 3 and table:Įlif neighboring_cells = 3 and not table: Neighboring_cells = get_neighboring_cells(table, row, col) New_table = epcopy(table) # deep copy to avoid mutability issues Table = * width for _ in range(height)] # generating the table frame ed(rand_seed) # giving a seed if user specifies Sum += get_cell_state(table, row + row_shift, col + col_shift)ĭef generate_table(height, width, rand_seed=time.time()): # checking for funcion to not check the state of the cell itself # little hack to return 0 if row and col are out of rangĭef get_neighboring_cells(table, row, col): If row in range(len(table)) and col in range(len(table)): import timeīlack_square_symbol = chr(int('25A0', 16)) # a nice unicode black square symbol Would be glad to see any improvements or critique. Not sure that get_cell_state and update_table functions are good as they might be. One of my first serious Python programs, realized Conway's Game of Life in Python.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |