$24
For this exercise, you and a partner will create a Conway's Game of Life simulator in Ruby, starting with the template provided. You will have to modify the num_neighbors, iterate, and print_arr methods, as well as fill in additional code after the EXECUTION STARTS HERE section (think of it as the equivalent of `public static void main`).
## Grading
This is an exercise and is thus not graded.
## Conway's Game of Life
Conway's Game of Life is an n x m matrix of cells, each of which can be in one of two states, living or dead. In our example, `n == m` (our array can never be 5 x 2, for example, but could be 2 x 2 or 5 x 5) and we shall represent living cells by the character 'x' and dead cells by the character '.'. When printing, we shall also add in the line after the matrix a line of asterisks, of size n, as well as an additional blank line (see Example Runs, below, if this is confusing).
The matrix shall be a Euclidean 2-torus - that is, if you are at an edge, moving one spot over wraps you around to to the other edge. For example, assume location 0, 4 in a 5 x 5 matrix (the 'X' character below):
```
01234
0....X
1.....
2.....
3.....
4.....
```
Moving one location to the right (to the nonexistent "0, 5") would reference location 0, 0 (indicated by '*'), and moving one location up (to the nonexistent "-1, 4") would reference location 4, 4 (indicated by '%').
```
01234
0*...X
1.....
2.....
3.....
4....%
```
"Neighbors" are defined as all adjoining cells. For example, all of the cells marked 'n' are neighbors of the cell X. Every cell has eight neighbors.
```
01234
0.....
1.nnn.
2.nXn.
3.nnn.
4.....
```
Remember that we are on a Euclidean 2-torus, so neighbors may exist "wrapped around" the world. All neighbors for cell X are shown here.
```
01234
0n..nX
1n..nn
2.....
3.....
4n..nn
```
The game shall continue for the number of iterations expressed at the command line. This is true _even if_ the game is static (i.e. all cells are dead, or are stuck in a static pattern and never changing).
At each iteration, the program shall execute the Rules of Conway's Game of Life (listed below) on the current array and print out the resulting array. The resulting array shall then become the current array.
The program shall accept three arguments: size (size of the 2-D array - the number of rows and columns should always be equal, an integer 0), percentage to be randomly assigned to be living (an integer between 0 and 100 inclusive), and the number of iterations to run (an integer = 0).
If a different number of arguments has been passed in, an error message should be displayed indicating this and the program exits.
For each of the arguments passed in does not meet the requirements listed above (e.g., the size of the array is negative, or the percentage is entered as 'foo' which cannot be converted to an integer), then an error message shall be displayed indicating this and the program will exit. The program shall indicate which argument was the problem, but it does not have to check every single argument, nor explain what the problem actually is (for example, assume a user enters an invalid size and an invalid percentage - the program only needs to indicate that there was an error with one of them).
The number of iterations shall be 0-based. That is, one iteration will mean that the rules of the game have been applied one time. The "0th" iteration shall be considered the initial matrix after it has been pseudorandomly populated.
### Rules of Conway's Game of Life
1. If a cell is dead, and has exactly three neighbors, it will become alive in the next iteration.
2. If a cell is dead, and has any other number of neighbors, it will remain dead in the next iteration.
3. If a cell is alive, and has either two or three neighbors, it will be alive in the next iteration.
3. If a cell is alive, and has any other number of neighbors, it will become dead in the next iteration.
## Why Do We Care?
Conway's Game of Life shows the power of cellular automata - it can even be used to make a Turing Machine and thus compute any known function. It's also just fun to play with. I recommend you check out https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life for some examples.
If you would like further challenges, you can extend your program to allow you to enter certain patterns, save/load, refresh at the same spot, or detect static or oscillating worlds.
## Example Runs
_Note: Due to the pseudorandom number generator determining whether or not a cell is alive or dead initially, you will not always get the EXACT same output as seen here (except in cases where percentage == 0 or percentage == 100). It should be similar, however, and since the rules of Conway's Game of Life are deterministic, if you have the same pattern at any particular iteration, all further iterations shall always be exactly the same._
```
(20308) $ ruby life.rb
Traceback (most recent call last):
life.rb:56:in `<main': Enter integers for size, percentage (1..100), and number of iterations at command line (RuntimeError)
(20309) $ ruby life.rb -1 20 10
Traceback (most recent call last):
life.rb:70:in `<main': Enter valid size (0) for size (RuntimeError)
(20310) $ ruby life.rb -1 -1 -1
Traceback (most recent call last):
life.rb:70:in `<main': Enter valid size (0) for size (RuntimeError)
(20311) $ ruby life.rb 10 0 0
...........
...........
...........
...........
...........
...........
...........
...........
...........
...........
...........
***********
(20312) $ ruby life.rb 10 10 0
x........x.
...........
......x....
.x...x.....
.x.........
.......x...
.xx........
...........
...x.......
.x.......x.
......x....
***********
(20313) $ ruby life.rb 10 10 1
x.........x
......x....
...........
....x..x...
..x........
......x....
..x........
.x.....x...
.....x.....
...........
....x......
***********
...........
...........
...........
...........
...........
...........
...........
...........
...........
...........
...........
***********
(20314) $ ruby life.rb 10 20 3
x..x.x....x
.......xx..
...........
...........
.x.......x.
..xx...x...
......x....
.....x....x
.x.....x...
...........
xx.......x.
***********
xx......xxx
...........
...........
...........
..x........
..x........
......x....
......x....
...........
xx.........
xx.........
***********
.x.......xx
x........xx
...........
...........
...........
...........
...........
...........
...........
xx.........
..x......x.
***********
.x......x..
x........x.
..........x
...........
...........
...........
...........
...........
...........
.x.........
..x......x.
***********
(20318) $ ruby life.rb 10 100 1
xxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxx
***********
...........
...........
...........
...........
...........
...........
...........
...........
...........
...........
...........
***********
(20319) $ ruby life.rb 10 20 10
........x..
x......x...
.x...x.....
..........x
..x....x.xx
......x..x.
.......x.xx
...........
xxxx....x..
...........
.x.......xx
***********
x.......xxx
...........
x..........
x........xx
........xxx
......xx...
........xxx
xxx.....xxx
.xx........
.........xx
.........x.
***********
........xxx
x........x.
x..........
x.......x..
x......xx..
.......x...
.x.........
..x.....x..
..x.....x..
.........xx
x..........
***********
x.......xx.
x.......xx.
xx.........
xx.....xx.x
.......xx..
.......xx..
...........
.xx........
........x..
.........xx
x.......x..
***********
xx.....x...
x.......xx.
.......x...
.x.....xxxx
x.....x....
.......xx..
...........
...........
.........x.
........xxx
x.......x..
***********
xx.....x.x.
xx.....xx.x
x......x...
x.....xxxxx
x.....x...x
.......x...
...........
...........
........xxx
........x.x
xx.....xx..
***********
..x...x..x.
......xx.x.
...........
.x....x.xx.
x.....x....
...........
...........
.........x.
........x.x
..........x
.x.....x...
***********
......x....
......xxx..
......x..x.
.......x...
.......x...
...........
...........
.........x.
..........x
x........x.
...........
***********
......x....
.....xx.x..
......x....
......xxx..
...........
...........
...........
...........
.........xx
..........x
...........
***********
.....xxx...
.....xx....
........x..
......xx...
.......x...
...........
...........
...........
.........xx
.........xx
...........
***********
.....x.x...
.....x.....
.....x.....
......xxx..
......xx...
...........
...........
...........
.........xx
.........xx
......x....
***********
```