SHOW:
|
|
- or go back to the newest paste.
1 | -- Original by Vilsol in pastebin gjG6MXHq | |
2 | -- | |
3 | -- Added initializing screen with 8 gliders to form a glider gun by Forgotten_Boy. | |
4 | board = {} | |
5 | tArgs = { ... } | |
6 | generation = 0 | |
7 | sleeptime = 0.5 | |
8 | ||
9 | if(tArgs[1] == "left" or tArgs[1] == "right" or tArgs[1] == "top" or tArgs[1] == "bottom" or tArgs[1] == "front" or tArgs[1] == "back")then | |
10 | mon = peripheral.wrap(tArgs[1]) | |
11 | else | |
12 | mon = term | |
13 | end | |
14 | ||
15 | if(mon.isColor() or mon.isColor)then | |
16 | colored = true | |
17 | else | |
18 | colored = false | |
19 | end | |
20 | ||
21 | w, h = mon.getSize() | |
22 | ||
23 | for x = 1, w do | |
24 | board[x] = {} | |
25 | for y = 1, h do | |
26 | board[x][y] = 0 | |
27 | end | |
28 | end | |
29 | ||
30 | function drawScreen() | |
31 | w, h = mon.getSize() | |
32 | for x = 1, w do | |
33 | for y = 1, h do | |
34 | nei = getNeighbours(x, y) | |
35 | if(board[x][y] == 1)then | |
36 | if colored then | |
37 | if(nei < 2 or nei > 3)then | |
38 | mon.setBackgroundColor(colors.red) | |
39 | else | |
40 | mon.setBackgroundColor(colors.green) | |
41 | end | |
42 | else | |
43 | mon.setBackgroundColor(colors.white) | |
44 | end | |
45 | else | |
46 | if colored then | |
47 | if(nei == 3)then | |
48 | mon.setBackgroundColor(colors.yellow) | |
49 | else | |
50 | mon.setBackgroundColor(colors.black) | |
51 | end | |
52 | else | |
53 | mon.setBackgroundColor(colors.black) | |
54 | end | |
55 | end | |
56 | mon.setCursorPos(x, y) | |
57 | mon.write(" ") | |
58 | end | |
59 | end | |
60 | mon.setCursorPos(1,1) | |
61 | if colored then | |
62 | mon.setTextColor(colors.blue) | |
63 | end | |
64 | mon.write(generation) | |
65 | end | |
66 | ||
67 | function getNeighbours(x, y) | |
68 | w, h = mon.getSize() | |
69 | total = 0 | |
70 | if(x > 1 and y > 1)then if(board[x-1][y-1] == 1)then total = total + 1 end end | |
71 | if(y > 1)then if(board[x][y-1] == 1)then total = total + 1 end end | |
72 | if(x < w and y > 1)then if(board[x+1][y-1] == 1)then total = total + 1 end end | |
73 | if(x > 1)then if(board[x-1][y] == 1)then total = total + 1 end end | |
74 | if(x < w)then if(board[x+1][y] == 1)then total = total + 1 end end | |
75 | if(x > 1 and y < h)then if(board[x-1][y+1] == 1)then total = total + 1 end end | |
76 | if(y < h)then if(board[x][y+1] == 1)then total = total + 1 end end | |
77 | if(x < w and y < h)then if(board[x+1][y+1] == 1)then total = total + 1 end end | |
78 | return total | |
79 | end | |
80 | ||
81 | function compute() | |
82 | w, h = mon.getSize() | |
83 | while true do | |
84 | newBoard = {} | |
85 | for x = 1, w do | |
86 | newBoard[x] = {} | |
87 | for y = 1, h do | |
88 | nei = getNeighbours(x, y) | |
89 | if(board[x][y] == 1)then | |
90 | if(nei < 2)then | |
91 | newBoard[x][y] = 0 | |
92 | elseif(nei > 3)then | |
93 | newBoard[x][y] = 0 | |
94 | else | |
95 | newBoard[x][y] = 1 | |
96 | end | |
97 | else | |
98 | if(nei == 3)then | |
99 | newBoard[x][y] = 1 | |
100 | end | |
101 | end | |
102 | end | |
103 | end | |
104 | board = newBoard | |
105 | generation = generation + 1 | |
106 | sleep(sleeptime) | |
107 | end | |
108 | end | |
109 | ||
110 | function loop() | |
111 | while true do | |
112 | event, variable, xPos, yPos = os.pullEvent() | |
113 | if event == "mouse_click" or event == "monitor_touch" or event == "mouse_drag" then | |
114 | if variable == 1 then | |
115 | board[xPos][yPos] = 1 | |
116 | else | |
117 | board[xPos][yPos] = 0 | |
118 | end | |
119 | end | |
120 | if event == "key" then | |
121 | if tostring(variable) == "28" then | |
122 | return true | |
123 | elseif tostring(variable) == "57" then | |
124 | if(mon.isColor() or mon.isColor)then | |
125 | colored = not colored | |
126 | end | |
127 | elseif tostring(variable) == "200" then | |
128 | if sleeptime > 0.1 then | |
129 | sleeptime = sleeptime - 0.1 | |
130 | end | |
131 | elseif tostring(variable) == "208" then | |
132 | if sleeptime < 1 then | |
133 | sleeptime = sleeptime + 0.1 | |
134 | end | |
135 | end | |
136 | end | |
137 | drawScreen() | |
138 | end | |
139 | end | |
140 | ||
141 | function intro() | |
142 | mon.setBackgroundColor(colors.black) | |
143 | mon.clear() | |
144 | mon.setCursorPos(1,1) | |
145 | mon.write("Conway's Game Of Life") | |
146 | mon.setCursorPos(1,2) | |
147 | mon.write("It is a game which represents life.") | |
148 | mon.setCursorPos(1,3) | |
149 | mon.write("The game runs by 4 basic rules:") | |
150 | mon.setCursorPos(1,4) | |
151 | mon.write("1. If a cell has less than 2 neighbours, it dies.") | |
152 | mon.setCursorPos(1,5) | |
153 | mon.write("2. If a cell has 2 or 3 neightbours, it lives.") | |
154 | mon.setCursorPos(1,6) | |
155 | mon.write("3. If a cell has more than 3 neighbours, it dies.") | |
156 | mon.setCursorPos(1,7) | |
157 | mon.write("4. If a cell has exactly 3 neighbours it is born.") | |
158 | mon.setCursorPos(1,9) | |
159 | mon.write("At the top left is the generation count.") | |
160 | mon.setCursorPos(1,10) | |
161 | mon.write("Press spacebar to switch between color modes") | |
162 | mon.setCursorPos(1,11) | |
163 | mon.write("Press enter to start the game") | |
164 | mon.setCursorPos(1,13) | |
165 | mon.write("Colors:") | |
166 | mon.setCursorPos(1,14) | |
167 | mon.write("Red - Cell will die in next generation") | |
168 | mon.setCursorPos(1,15) | |
169 | mon.write("Green - Cell will live in next generation") | |
170 | mon.setCursorPos(1,16) | |
171 | mon.write("Yellow - Cell will be born in next generation") | |
172 | mon.setCursorPos(1,18) | |
173 | mon.write("Press any key to continue!") | |
174 | event, variable, xPos, yPos = os.pullEvent("key") | |
175 | end | |
176 | ||
177 | ||
178 | if (w >=47) then | |
179 | -- initialize the screen with 8 gliders to synthesize a glider gun | |
180 | print(" Gosper Gun initializing...") | |
181 | x = 1 | |
182 | y = 1 | |
183 | data = "16bo30b$16bobo16bo11b$16b2o17bobo9b$obo10bo21b2o10b$b2o11b2o31b$bo11b2o32b3$10b2o20b2o13b$11b2o19bobo9b3o$10bo21bo11bo2b$27bo17bob$27b2o18b$26bobo!" | |
184 | for one in string.gmatch(data, "%d*[%w$!]") do | |
185 | count = 1 | |
186 | start, fin = string.find(one, "%d*") | |
187 | token = string.sub(one, start, fin) | |
188 | kind = string.sub(string.reverse(one), 1, 1) | |
189 | if (token) then | |
190 | count = tonumber(token) | |
191 | end | |
192 | for counter = 1, count or 1 do | |
193 | if(kind == "!") then | |
194 | --eof, begin the show | |
195 | break | |
196 | elseif (kind == "$") then | |
197 | y = y + 1 | |
198 | x = 1 | |
199 | elseif (kind == "b") then | |
200 | x = x + 1 | |
201 | elseif (kind == "o") then | |
202 | board[x][y] = 1 | |
203 | x = x + 1 | |
204 | end | |
205 | end | |
206 | end | |
207 | sleep(1) | |
208 | sleeptime = 0.1 | |
209 | end | |
210 | ||
211 | intro() | |
212 | drawScreen() | |
213 | while true do | |
214 | loop() | |
215 | parallel.waitForAny(loop, compute) | |
216 | end |