![]() ![]() 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 |