Visualizing Conway's Game of Life with Matplotlib
While working through practice problems on leetcode, I encountered a problem I hadn’t seen for a while, Conway’s Game of Life. After implementing the solution, I decided it would be interesting to take it a step further and generate graphics to display the game.
Updating the Game Board
Even though leetcode only requires the finalized state of a move for its solution, I decided it would be visually more interesting to add a visualization for the spawning / decay cycle between each state move state. To accomplish this I split each move into a starting state and an ending state.
Adding Output Visualization with matplotlib
With transition and ending game boards now being generated for each round of play, we can generate interesting visualizations with matplotlib easily.
First, we need to import matplotlib. I also suggest turning off the axis labels for this type of visualization.
Second, we need to add a visually interesting color map. I always spend way too much time agonizing over the right colors.
Finally, we can use the code below to output a visualization of the state of the game board to an image file.
Consolidating Output Images Together with ImageMagick
It wouldn’t be as exciting if we just had a few hundred .png images sitting around that we had to look through one by one. As a finishing touch, we can use ImageMagick to consolidate all of our output images into a single consolidated animated gif using the included bash script.