Game of Life – v1.0
My class on Artificial Intelligence's first assignment was to recreate Conway's Game of Life. In his game, a grid of cells continuously evolves over the course of time.
First Task: The Interface
I wanted to create the GUI first since it would help me visualize the rest of the project. Because graphics in java are troublesome, I used a free online library from Princeton called StdDraw. Using the library, I started with a basic grid and an internal boolean[] to accompany it. Combining the two, I created my first GameScreen: a 400x400 pixel canvas with a black background and grey squares (representing cells). This eventually morphed into the final layout, which, at the time of publishing, is 1600x840. The extra 40 pixels at the bottom are used for buttons.
The yellow cells you see on the grid is the "default" life pattern.
Next: Interaction
Although interaction was already a requirement of the assignment, I decided to add an extra element in the beginning. By starting off with text output, I was able to give the user instructions, and at the same time, ask for the user's input. The input decides the height/width of the grid, and the refresh rate (in milliseconds). Then, the basic rules of my version of the game are shown:
After the grid opens, I wanted the user to be able to click individual cells in order to create a starting life pattern. I created a loop, and using StdDraw's handy methods, I was able to get the location of the mouse when it is clicked. Then, if the mouse's location is in a cell, the cell switches its state (alive/dead). If the mouse is on the play button, then the game starts.
Finally: Algorithms
Game of Life rules aren't super complicated, so designing the algorithms for this game was pretty simple. For every iteration, a cell stays in its current state, unless:
First Task: The Interface
GUI for my Game of Life |
The yellow cells you see on the grid is the "default" life pattern.
Next: Interaction
Although interaction was already a requirement of the assignment, I decided to add an extra element in the beginning. By starting off with text output, I was able to give the user instructions, and at the same time, ask for the user's input. The input decides the height/width of the grid, and the refresh rate (in milliseconds). Then, the basic rules of my version of the game are shown:
After the grid opens, I wanted the user to be able to click individual cells in order to create a starting life pattern. I created a loop, and using StdDraw's handy methods, I was able to get the location of the mouse when it is clicked. Then, if the mouse's location is in a cell, the cell switches its state (alive/dead). If the mouse is on the play button, then the game starts.
Finally: Algorithms
Game of Life rules aren't super complicated, so designing the algorithms for this game was pretty simple. For every iteration, a cell stays in its current state, unless:
- A dead cell has exactly three neighbors, then it becomes alive (reproduction).
- A live cell has one or fewer neighbors, then it dies (underpopulation).
- A live cell has more than three neighbors, then it dies (overpopulation).
I originally ran into some errors since I was including the cell itself when counting the number of neighbors, but eventually, I debugged enough that I figured out what was going on. After that, simply refreshing the screen for every iteration allows the game to continue.
Download
(coming soon)
Comments
Post a Comment