![]() (There’s a whole field of such simulations called “artificial life” - do a search and you’ll find lots of stuff online, including many applets. This has been used in films such as “Batman Returns” to depict a large group of creatures without having to animate the motion of each one separately. Though Life and other “cellular automata” (as they’re called) usually take place on some sort of a grid (whether 1D, 2D or 3D) for simplicity, not like the general 3D of blender.įor something similar in spirit to Life, you might want to look at Craig Reynolds’s “Boids”, which creates a simulation of the motion of a group of birds or similar flocking creatures by giving each “boid” a simple group of rules to follow. At this moment I have yet to write any code for the borders of the program so I am just asking for help with what I have right now. Dewdney’s columns (he ran a successor to Gardner’s columns in Scientific American focused on computer-related math). I took a liking to Conway's Game of Life and began to try and write it in python. I think I’ve seen it in Martin Gardner’s Scientific American columns, or in A. This is a bad sign indicating that a more descriptive name should be adopted.I don’t remember exactly where I’ve read about it, but I have heard that there have been 3D variants on Conway’s Life (which is 2D) that preserve some of the complexity of the original (you need exactly the right set of rules to have interesting stuff happen, so you can’t just automatically translate 2D to 3D). I cannot understand what the purpose of the ngb field is even after reading the code. Minor: use ALL_CAPS for constantsĪs a widely accepted convention constants are written ALL CAPS to quickly discern them from variable variables. The 3 options differ only in the colour of the drawing so you could also cut down code duplication by using such a function. The same can be said for this block of code calculates the next state of a given cell: if cellv2.state=1 and 2<=cellv2.ngb<=3:ĭecide_colour(cell) if cells.state=1: This 8 lines of code provide the program with wrap-around functionality (for example: going too far to the right leaves you all the way back to the left), it would ideal if you wrote a function for this. Write functions to encapsulate logical units of action wrap_around(dx, dy, x_size, y_size) if dy 49: You may instead create such list and return it. You need a 2d list called board for this function to work. I would just delete these printing statements after the debugging is complete. If printing out is not desired the user will not be able to use this function. Your function prints to std-out in addition to building the board and this behaviour cannot be turned off. This way you gain more re-usability for your function (and you can test it simpler by giving small sizes). I suggest asking x and y sizes as parameters: def generate(x_size, y_size): Your function can only generate boards of size \$50 * 50\$, may I be interested in other sizes I would need to modify the definition accordingly. ![]() """ Generates a random game of life board. ![]() It also usually is a complete phrase, I would write it a little different: def generate(): (mainsrf, red, (x*10, y*10, 10, 10))įunctions good practices: Documentation, Parametrization, Single Purpose, Outside State Independence Documentation #random field generationĭocumentation is usually written as a docstring in triple quotes under the function definition (this allows programmatic access via help: def generate(): In my opinion, I believe your code is pretty straight forward and doesnt need much extra comments (Perhaps to explain your steps in the countNeighbors method). import copyĬells = for i in range(50)]Ĭells = cell(0, random.randint(0, 1)) I'm quite new to Python and would like to know how could I improve that code, especially in terms of performance, compactness and readability. Here's my implementation of Game of Life.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |