Debian Codegraph @

demo/kitchen-sink/docs/gobstones.gbs

raw
1 /*
2 * A Gobstones implementation of Conwey's Game of Life
3 * that tries to use most of Gobstones syntax.
4 *
5 * Check out more about Gobstones at http://gobstones.org
6 */
7
8 -----------------------------------------------------------
9 -- HELPERS TO MOVE AROUND ALL THE BOARD --
10 -----------------------------------------------------------
11
12 // This helpers allow the user to move around the board
13 // one cell at a time
14 procedure GoToStartOfBoard(firstDir, secondDir) {
15 IrAlBorde(opuesto(firstDir))
16 IrAlBorde(opuesto(secondDir))
17 }
18
19 function isEndOfBoard(firstDir, secondDir) {
20 return (not puedeMover(firstDir) && not puedeMover(secondDir))
21 }
22
23 procedure NextCellOfTheBoard(firstDir, secondDir) {
24 if (puedeMover(firstDir)) {
25 Mover(firstDir)
26 } else {
27 IrAlBorde(opuesto(firstDir))
28 Mover(secondDir)
29 }
30 }
31
32 -----------------------------------------------------------
33 -- TYPE DECLARATIONS --
34 -----------------------------------------------------------
35 // The cell type declarations
36 type CellStatus is variant {
37 case Alive {}
38 case Dead {}
39 }
40
41 type Cell is record {
42 field status # CellStatus
43 field color # Color
44 }
45
46
47 -----------------------------------------------------------
48 -- PROGRAM DEFINITION --
49 -----------------------------------------------------------
50 interactive program {
51 # Map key press to actions
52 K_UP -> { PrepareNextTick() }
53 K_RIGHT -> { CompleteNextTick() }
54 K_RETURN -> { SimulateNextTick() }
55 }
56
57 /*
58 program {
59 return ( numberOfNeighbors())
60 }
61 */
62 -----------------------------------------------------------
63 -- FUNCTIONS --
64 -----------------------------------------------------------
65 function isCellAlive() {
66 return (hayBolitas(Verde))
67 }
68
69 function cellStatus() {
70 return (
71 choose
72 Alive when (isCellAlive())
73 Dead otherwise
74 )
75 }
76
77 function readCell() {
78 return (
79 Cell(color <- Verde, status <- cellStatus())
80 )
81 }
82
83 function numberOfNeighbors() {
84 amountOfNeighbors := 0
85 foreach dir in [minDir() .. maxDir()] {
86 amountOfNeighbors := amountOfNeighbors + aliveCellsAt(dir)
87 }
88 return (amountOfNeighbors)
89 }
90
91 function aliveCellsAt(dir) {
92 firstCompare := puedeMover(dir) && hasAliveCellAt(dir)
93 secondCompare := puedeMover(dir) && puedeMover(siguiente(dir)) && hasAliveCellAtD(dir)
94 value :=
95 matching (firstCompare) select
96 1 on True
97 0 otherwise
98 +
99 matching (secondCompare) select
100 1 on True
101 0 otherwise
102 return (value)
103 }
104
105 function hasAliveCellAt(dir) {
106 Mover(dir)
107 return (isCellAlive())
108 }
109
110 function hasAliveCellAtD(dir) {
111 Mover(dir)
112 Mover(siguiente(dir))
113 return (isCellAlive())
114 }
115
116 -----------------------------------------------------------
117 -- ROCEDURES DECLARATIONS --
118 -----------------------------------------------------------
119 procedure TagAsReanimated() {
120 Poner(Azul)
121 }
122
123 procedure TagAsDead() {
124 Poner(Rojo)
125 }
126
127 procedure ProcessAction() {
128 switch (hayBolitas(Rojo)) {
129 True -> { Sacar(Verde); Sacar(Rojo) }
130 _ -> {}
131 }
132 switch (hayBolitas(Azul)) {
133 True -> { Poner(Verde); Sacar(Azul) }
134 _ -> {}
135 }
136 }
137
138 procedure TagCell(numNeighbors) {
139 if ( isCellAlive() && numNeighbors < 2) { TagAsDead() }
140 elseif ( isCellAlive() && numNeighbors > 3) { TagAsDead() }
141 elseif ((not isCellAlive()) && numNeighbors == 3) { TagAsReanimated() }
142 }
143
144 procedure SimulateTicks(amount) {
145 repeat(amount) {
146 SimulateNextTick()
147 }
148 }
149
150 procedure SimulateNextTick() {
151 PrepareNextTick()
152 CompleteNextTick()
153 }
154
155 procedure PrepareNextTick() {
156 GoToStartOfBoard(Norte, Este)
157 while (not isEndOfBoard(Norte, Este)) {
158 TagCell(numberOfNeighbors())
159 NextCellOfTheBoard(Norte, Este)
160 }
161 TagCell(numberOfNeighbors())
162 }
163
164 procedure CompleteNextTick() {
165 GoToStartOfBoard(Norte, Este)
166 while (not isEndOfBoard(Norte, Este)) {
167 ProcessAction()
168 NextCellOfTheBoard(Norte, Este)
169 }
170 ProcessAction()
171 }