![]() ![]() ![]() Another advantage also is we get to model infinite boards this way. This way we don't have to scan the whole board each time we want to compute an iteration of the game. The main idea behind this solution is to keep track of only live cells. If count = 3 or (count = 2 and cell in board):īoard = To address all your questions in one go, you could consider a very simple implementation using only a couple of functions and the set data structure: def neighbors(cell):Ĭandidates = board.union(set(n for cell in board for n in neighbors(cell)))Ĭount = sum((n in board) for n in neighbors(cell)) Does this simplify things, or does it only make it more complicated? I could have left out the display() function of the Game class and just write print my_() on the last line. I used several nested for-loops, but maybe I could replace some with list comprehensions. I know it's good practice to write a lot of comments, but I found most of this code self-explanatory (though that could be because I've just written it). For instance, I could have implemented the neighbours() function in the State class, rather than in the Game class. I'm not sure how to divide the tasks between the two classes. However, I thought it would make sense to let the board be an array of rows rather than columns. It feels a bit unnatural to write, instead of. My_game = Game(State(glider, x = 2, y = 3, width = 10, height = 10)) If not hor = ver = 0 and (self.infinite_board = True or (0 <= x + hor < self.width and 0 <= y + ver < self.height)):Ĭount += ĭef _init_(self, positions, x, y, width, height):įor y, row in enumerate(positions.splitlines()):īoard = * width for row in range(height)] Should_live = neighbours = 3 or (neighbours = 2 and previous_state = True) New_board = * self.width for row in range(self.height)]įor y, row in enumerate(): ![]() Here's my Python implementation of Conway's Game of Life: class Game(object):ĭef _init_(self, state, infinite_board = True): ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |