Debian Codegraph @

demo/kitchen-sink/docs/kotlin.kt

raw
1 #!/usr/bin/env kotlinc -script
2
3 /*Taken from http://try.kotlinlang.org/#/Examples/Longer%20examples/Life/Life.kt*/
4 /**
5 * This is a straightforward implementation of The Game of Life
6 * See http://en.wikipedia.org/wiki/Conway's_Game_of_Life
7 */
8 package life
9
10 /*
11 * A field where cells live. Effectively immutable
12 */
13 class Field(
14 val width: Int,
15 val height: Int,
16 // This function tells the constructor which cells are alive
17 // if init(i, j) is true, the cell (i, j) is alive
18 init: (Int, Int) -> Boolean
19 ) {
20 private val live: Array<Array<Boolean>> = Array(height) { i -> Array(width) { j -> init(i, j) } }
21
22 private fun liveCount(i: Int, j: Int)
23 = if (i in 0..height - 1 &&
24 j in 0..width - 1 &&
25 live[i][j]) 1 else 0
26
27 // How many neighbors of (i, j) are alive?
28 fun liveNeighbors(i: Int, j: Int) =
29 liveCount(i - 1, j - 1) +
30 liveCount(i - 1, j) +
31 liveCount(i - 1, j + 1) +
32 liveCount(i, j - 1) +
33 liveCount(i, j + 1) +
34 liveCount(i + 1, j - 1) +
35 liveCount(i + 1, j) +
36 liveCount(i + 1, j + 1)
37
38 // You can say field[i, j], and this function gets called
39 operator fun get(i: Int, j: Int) = live[i][j]
40 }
41
42 class Test public constructor {
43 }
44 var test = "test"
45
46 /**
47 * This function takes the present state of the field
48 * and returns a new field representing the next moment of time
49 */
50 fun next(field: Field): Field {
51 return Field(field.width, field.height) { i, j ->
52 val n = field.liveNeighbors(i, j)
53 if (field[i, j])
54 // (i, j) is alive
55 n in 2..3 // It remains alive iff it has 2 or 3 neighbors
56 else
57 // (i, j) is dead
58 n == 3 // A new cell is born if there are 3 neighbors alive
59 }
60 }
61
62 /** A few colony examples here */
63 fun main(args: Array<String>) {
64 // Simplistic demo
65 runGameOfLife("***", 3)
66 // "Star burst"
67 runGameOfLife("""
68 _______
69 ___*___
70 __***__
71 ___*___
72 _______
73 """, 10)
74 // Stable colony
75 runGameOfLife("""
76 _____
77 __*__
78 _*_*_
79 __*__
80 _____
81 """, 3)
82 // Stable from the step 2
83 runGameOfLife("""
84 __**__
85 __**__
86 __**__
87 """, 3)
88 // Oscillating colony
89 runGameOfLife("""
90 __**____
91 __**____
92 ____**__
93 ____**__
94 """, 6)
95 // A fancier oscillating colony
96 runGameOfLife("""
97 -------------------
98 -------***---***---
99 -------------------
100 -----*----*-*----*-
101 -----*----*-*----*-
102 -----*----*-*----*-
103 -------***---***---
104 -------------------
105 -------***---***---
106 -----*----*-*----*-
107 -----*----*-*----*-
108 -----*----*-*----*-
109 -------------------
110 -------***---***---
111 -------------------
112 """, 10)
113 }
114
115 // UTILITIES
116
117 fun runGameOfLife(fieldText: String, steps: Int) {
118 var field = makeField(fieldText)
119 for (step in 1..steps) {
120 println("Step: $step")
121 for (i in 0..field.height - 1) {
122 for (j in 0..field.width - 1) {
123 print(if (field[i, j]) "*" else " ")
124 }
125 println("")
126 }
127 field = next(field)
128 }
129 }
130
131 fun makeField(s: String): Field {
132 val lines = s.replace(" ", "").split('\n').filter({ it.isNotEmpty() })
133 val longestLine = lines.toList().maxBy { it.length } ?: ""
134
135 return Field(longestLine.length, lines.size) { i, j -> lines[i][j] == '*' }
136 }