on 6d45ede help sign in
121 lines 4.0 kB history raw
1---
2navhome: /docs
3sort: 5
4comments: true
5---
7# Conway's Game of Life
9Conway's Game of Life is a zero-player game played on an `n`x`n`
10grid of spaces, each of which is either "alive" or "dead". Every
11iteration, some or all of the spaces change from alive to dead or
12from dead to alive.
14Specifically, for each iteration, each dead space becomes alive
15if and only if it is adjacent to exactly three live spaces. Each
16live remains alive if and only if it is adjacent to either two or
17three live spaces. In this context, "adjacent to" includes the
18eight spaces that share either a side or a corner with the space.
19Thus, in the following map, the center space (dead) is adjacent
20to exactly three live spaces, so it will be alive in the next
21iteration. `#` signifies a live space and `.` signifies a dead
22space.
24 ..#
25 ...
26 ##.
291. Write a function `next-generation` that takes nine loobean
30 inputs, `nw`, `nc`, `ne`, `cw`, `cc`, `ce`, `sw`, `sc`, and
31 `se`. Produce 'yes' if the center space will be alive in the
32 next generation and 'no' if it will be dead.
341. Write a function `create-board` that takes an integer `n` and
35 produces an `n` by `n` grid of loobeans, (hereafter,
36 "board"), initialized to 'no'. This grid should be
37 represented as a list of `n` lists of length `n`.
391. Write a function `toggle-space` that takes a board, a row
40 index, and a column index, and produces a new board,
41 identical to the old except that the state of the space
42 referred to by the row and column indices has been toggled.
441. Use the following function `print-board` to pretty-print the
45 board. For bonus points, try to follow what it's doing.
47 ```
48 :: XX test
49 |= board/(list (list ?))
50 ^- tang
51 %+ turn board
52 |= row/(list ?)
53 :- %leaf
54 ^- tape
55 %+ turn row
56 |= space/?
57 ?: space
58 '# '
59 '. '
60 ```
62 If you run this function with a board, you'll get a
63 pretty-printable object (a `tang`), but it won't be
64 pretty-printed. When running the generator from the dojo,
65 run `&tang +generator arguments` to mark the result as a
66 `tang`, which will tell the dojo to intelligently
67 pretty-print it.
691. Using the functions created in the previous exercises, create
70 a 10 by 10 board, and flip the states of the spaces at
71 coordinates (counting from the top left, indexed from 0)
72 `(2,1)`, `(3,2)`, `(1,3)`, `(2,3)`, and `(3,3)`. Use the
73 pretty printer to print out the board. Your output should be
74 the following:
76 ```
77 . . . . . . . . . .
78 . . # . . . . . . .
79 . . . # . . . . . .
80 . # # # . . . . . .
81 . . . . . . . . . .
82 . . . . . . . . . .
83 . . . . . . . . . .
84 . . . . . . . . . .
85 . . . . . . . . . .
86 . . . . . . . . . .
87 ```
891. Write a function `step` that iterates every space on the
90 board. Print out the new board. It should look like this:
92 ```
93 . . . . . . . . . .
94 . . . . . . . . . .
95 . # . # . . . . . .
96 . . # # . . . . . .
97 . . # . . . . . . .
98 . . . . . . . . . .
99 . . . . . . . . . .
100 . . . . . . . . . .
101 . . . . . . . . . .
102 . . . . . . . . . .
103 ```
105 > Note that the spaces on the edge don't have the full nine
106 > neighbors. For now, assume that they're always dead.
1081. Write a function `life` that runs the above function `n`
109 times on the board. Observe the board as the game
110 progresses. Congratulations, you've written Conway's Game of
111 Life in Hoon!
1131. One common way to handle the edge of the board is to pretend
114 that it "wraps around", forming a torus. In other words, say
115 that the spaces on the left edge of the map are adjacent to
116 the ones on the right edge, and the spaces on the top edge
117 are adjacent to the spaces on the bottom edge. Modify `step`
118 to act this way. Pay special attention to the corners.
1201. Modify `create-board` to use `++og` to randomly initialize
121 the board with live or dead spaces.