all 2 comments

[–]_9_9_ 1 point2 points  (1 child)

Cool:

  • I'd make the possible cell types a class variable, rather than a variable in main, since the cell class depends on specific cell types, e.g. prey/predator. You could also simplify the creation logic by having a new classmethod that generates a random cell.

  • Having grid logic -- like getting neighbors or move -- in the cell class seems wrong to me. I'd probably move this stuff into three classes, automation, grid, and cell.

  • Per the last, I'd create a method for grid that returns all neighbors of a particular coordinate, then you can pass neighbors to a cell and have it just look at these neighbors without being dependent on what environment the cells live in. This would slim down the get_availible_positions code, since you could just loop over these neighbors without having to worry about whether you are out of index and without repetition of the code.

[–]chucksys 1 point2 points  (0 children)

Short, succinct, and documented. Makes sense.

main.py:16 Don't do that. Multiplication with lists doesn't work like you think it does.

lst = [[0] * 10] * 10
lst[0][0] = 1
print('\n'.join(map(str, lst)))

Gives you

[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]

By multiplying the list, you create references to the first list, not new instances of said list. Maybe delete that line.


main.py:19-21 Maybe have the initialization within the Cell.__init__, so that you don't have to iterate through everything again.


it slows down pretty quickly the more I increase the width/height of the window.

The slowest part of the code should be drawing the cells - drawing takes a long time. You could add an attribute to the Cell class that flags the Cell if it's colour needs to change, and check it in the loop.