Feel free to leave the class at any time if you don’t find it interesting. You can attend a walk-in activity in Lobby 13, or change your schedule in 4-163.
This can be a slightly harder coding assignment than those offered at your high school. Don't feel frustrated if you don't get it at first. If you're stuck, just raise your hand, and I'll come over to help.
- Open up a terminal and execute the following command:
git clone https://github.com/AnimatedRNG/splash-2016-conway.git
- Execute
ls
. You should see a folder calledsplash-2016-conway
. Execute the commandcd splash-2016-conway
- In this directory are multiple files, including this very README. Execute
alias python=python3
- Now if you execute
python implementation.py
you should see a grid and three buttons. If you don't see a grid and three buttons or if you see an ugly error instead, raise your hand and I'll come over to help you. Either close the window with the grid in it, or type the key combinationCtrl+C
into the terminal to end the program. - I've been having some difficulties getting emacs and vi working on these machines; however, you can get a nice Python IDE installed clicking on the shortcut on the desktop named
idle
. Now clickCtrl+O
and open thesplash-2016-conway
folder and then openimplementation.py
. If you can't find the folder, then just close IDLE, go back to the terminal you had and executeidle3 implementation.py &
- Now that IDLE is up, you can now run your code by pressing
F5
. Happy Hacking!
While life_renderer.py has a lot of stuff in it, only four of them are publicly accessible.
From LifeWindow
in life_renderer.py
:
def create_cell(self, row = -1, col = -1, x = -1, y = -1):
"""Creates a cell at the specified row and column OR
(exclusive OR) at the position specified by (x, y).
If the cell at the specified position is already alive,
this function does nothing.
Example:
lifewindow.create_cell(row = 1, col = 2)
lifewindow.create_cell(x = 2, y = 1)
Both of the examples above create a cell at the same
location.
"""
self.__exec_cell__(lambda p: self.__render_cells__.add(p),
row, col, x, y)
def check_cell(self, row = -1, col = -1, x = -1, y = -1):
"""Determines whether the cell at the specified row and
column OR (exclusive OR) at the position specified by (x, y)
is alive.
Returns True if a cell is found at the specified position
otherwise False.
Example:
lifewindow.check_cell(row = 1, col = 2)
lifewindow.check_cell(x = 2, y = 1)
Both of the examples above check for a cell at the same
location.
"""
return self.__exec_cell__(lambda c: c in self.__render_cells__, row, col, x, y)
def kill_cell(self, row = -1, col = -1, x = -1, y = -1):
"""Kills a cell at the specified row and column OR
(exclusive OR) at the position specified by (x, y).
If there is no living cell at the specified position,
this function does nothing.
Example:
lifewindow.kill_cell(row = 1, col = 2)
lifewindow.kill_cell(x = 2, y = 1)
Both of the examples above kill a cell at the same
location.
"""
self.__exec_cell__(lambda p: self.__render_cells__.remove(p),
row, col, x, y)
def clear_grid(self):
"""Kills all the cells on the grid."""
self.__render_cells__ = set()
From implementation.py
:
def setup(lifewindow):
"""This callback runs prior to the start of the Game of Life.
Put any initialization code in here.
Any object returned from this method is ignored.
"""
pass
def loop(lifewindow):
"""This callback runs on every iteration of the Game of Life.
Implement the rules of the game here.
Any object returned from this method is ignored.
"""
pass
When I had to implement the Game of Life, I kept mixing up (Row, Column) with (x, y). So I decided to use the Pythonic convention of Explicit over Implicit and encourage named arguments:
lifewindow.create_cell(row = 0, col = 5)
This snippet of code creates a cell at row 0, column 5. I could do the same using (x, y) coordinates:
lifewindow.create_cell(x = 5, y = 0)
But it makes no sense to mix the two:
lifewindow.create_cell(y = 0, row = 5)
That's just weird. So don't do it. You'll get an exception.
The simulation starts out paused. Press the Pause button to unpause it.
Don't render cells outside the grid.
The first two parameters to the LifeWindow constructor are the number of rows and columns in the grid. You can change these numbers as you see fit
LifeWindow doesn't clear the grid after every iteration. If you created a cell during one iteration of loop(), it will remain there until you remove it.
Technically Python doesn't really have arrays, but it does have lists. A nice shortcut to creating a 2D list of False
:
array = [[False for y in range(DIM_2)] for x in range(DIM_1)]
where DIM_1
and DIM_2
are the dimensions of your desired array.
Clever you. Take a look at this.
That's okay. If you don't know what to do, take a look at this