|
0 |
/*
|
|
1 |
* tc.catseye.worb.WorbState -- noit o'mnain worb for yoob
|
|
2 |
*/
|
|
3 |
|
|
4 |
/*
|
|
5 |
* Copyright (c)2011 Cat's Eye Technologies. All rights reserved.
|
|
6 |
*
|
|
7 |
* Redistribution and use in source and binary forms, with or without
|
|
8 |
* modification, are permitted provided that the following conditions
|
|
9 |
* are met:
|
|
10 |
*
|
|
11 |
* Redistributions of source code must retain the above copyright
|
|
12 |
* notice, this list of conditions and the following disclaimer.
|
|
13 |
*
|
|
14 |
* Redistributions in binary form must reproduce the above copyright
|
|
15 |
* notice, this list of conditions and the following disclaimer in
|
|
16 |
* the documentation and/or other materials provided with the
|
|
17 |
* distribution.
|
|
18 |
*
|
|
19 |
* Neither the name of Cat's Eye Technologies nor the names of its
|
|
20 |
* contributors may be used to endorse or promote products derived
|
|
21 |
* from this software without specific prior written permission.
|
|
22 |
*
|
|
23 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
24 |
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
25 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
26 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
27 |
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
28 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
29 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
30 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
31 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
32 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
33 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
34 |
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
35 |
*/
|
|
36 |
|
|
37 |
/*
|
|
38 |
* A tc.catseye.worb.WorbState implements the semantics of the
|
|
39 |
* noit o'mnain worb automaton under the yoob framework.
|
|
40 |
*
|
|
41 |
* This class targets version 0.2 of the yoob framework's interface,
|
|
42 |
* which isn't released yet, so knowing this doesn't help you much.
|
|
43 |
*/
|
|
44 |
|
|
45 |
package tc.catseye.worb;
|
|
46 |
|
|
47 |
import tc.catseye.yoob.Error;
|
|
48 |
import tc.catseye.yoob.*;
|
|
49 |
|
|
50 |
import java.util.List;
|
|
51 |
import java.util.ArrayList;
|
|
52 |
import java.util.Set;
|
|
53 |
import java.util.HashSet;
|
|
54 |
import java.util.Map;
|
|
55 |
import java.util.HashMap;
|
|
56 |
import java.util.Random;
|
|
57 |
import java.util.Iterator;
|
|
58 |
|
|
59 |
|
|
60 |
class Worb implements Language {
|
|
61 |
public String getName() {
|
|
62 |
return "noit o' mnain worb";
|
|
63 |
}
|
|
64 |
|
|
65 |
public int numPlayfields() {
|
|
66 |
return 1;
|
|
67 |
}
|
|
68 |
|
|
69 |
public int numTapes() {
|
|
70 |
return 0;
|
|
71 |
}
|
|
72 |
|
|
73 |
public boolean hasProgramText() {
|
|
74 |
return false;
|
|
75 |
}
|
|
76 |
|
|
77 |
public boolean hasInput() {
|
|
78 |
return false;
|
|
79 |
}
|
|
80 |
|
|
81 |
public boolean hasOutput() {
|
|
82 |
return false;
|
|
83 |
}
|
|
84 |
|
|
85 |
public List<String> exampleProgramNames() {
|
|
86 |
ArrayList<String> names = new ArrayList<String>();
|
|
87 |
names.add("freefill");
|
|
88 |
names.add("magnetic field");
|
|
89 |
names.add("theory of time");
|
|
90 |
names.add("pressure");
|
|
91 |
names.add("slow loop");
|
|
92 |
names.add("fast loop");
|
|
93 |
names.add("transistor");
|
|
94 |
names.add("or gate");
|
|
95 |
names.add("subtraction");
|
|
96 |
names.add("division");
|
|
97 |
return names;
|
|
98 |
}
|
|
99 |
|
|
100 |
public WorbState loadExampleProgram(int index) {
|
|
101 |
String[][] program = {
|
|
102 |
{
|
|
103 |
/* eg/freefill.worb */
|
|
104 |
"######################",
|
|
105 |
"# #",
|
|
106 |
"# #",
|
|
107 |
"# #",
|
|
108 |
"# #",
|
|
109 |
"# #",
|
|
110 |
"# + #",
|
|
111 |
"# #",
|
|
112 |
"# #",
|
|
113 |
"# #",
|
|
114 |
"# #",
|
|
115 |
"######################",
|
|
116 |
},
|
|
117 |
{
|
|
118 |
/* eg/magnetic-field.worb */
|
|
119 |
"######################",
|
|
120 |
"# #",
|
|
121 |
"# #",
|
|
122 |
"# #",
|
|
123 |
"# #",
|
|
124 |
"# - #",
|
|
125 |
"# + #",
|
|
126 |
"# #",
|
|
127 |
"# #",
|
|
128 |
"# #",
|
|
129 |
"# #",
|
|
130 |
"######################",
|
|
131 |
},
|
|
132 |
{
|
|
133 |
/* eg/theory-of-time.worb */
|
|
134 |
"######################",
|
|
135 |
"#.......... #",
|
|
136 |
"#.......... #",
|
|
137 |
"#.......... #",
|
|
138 |
"#.......... #",
|
|
139 |
"#.......... #",
|
|
140 |
"#.......... #",
|
|
141 |
"#.......... #",
|
|
142 |
"#.......... #",
|
|
143 |
"#.......... #",
|
|
144 |
"#.......... #",
|
|
145 |
"######################",
|
|
146 |
},
|
|
147 |
{
|
|
148 |
/* eg/pressure.worb */
|
|
149 |
"#######################",
|
|
150 |
"#..........> #",
|
|
151 |
"#######################",
|
|
152 |
},
|
|
153 |
{
|
|
154 |
/* eg/slow-loop.worb */
|
|
155 |
"#####################",
|
|
156 |
"# < #",
|
|
157 |
"# ################# #",
|
|
158 |
"# # # #",
|
|
159 |
"# # #.#",
|
|
160 |
"# # # #",
|
|
161 |
"# ################# #",
|
|
162 |
"# > #",
|
|
163 |
"#####################",
|
|
164 |
},
|
|
165 |
{
|
|
166 |
/* eg/fast-loop.worb */
|
|
167 |
"#######",
|
|
168 |
"# < < #",
|
|
169 |
"# ### #",
|
|
170 |
"# >.> #",
|
|
171 |
"#######",
|
|
172 |
},
|
|
173 |
{
|
|
174 |
/* eg/transistor.worb */
|
|
175 |
" ###",
|
|
176 |
"### #+#",
|
|
177 |
"#+# # #",
|
|
178 |
"# ###v#",
|
|
179 |
"# < #",
|
|
180 |
"### < #",
|
|
181 |
" # < #",
|
|
182 |
" ###v#",
|
|
183 |
" # #",
|
|
184 |
" #!#",
|
|
185 |
" #-#",
|
|
186 |
" ###",
|
|
187 |
"",
|
|
188 |
},
|
|
189 |
{
|
|
190 |
/* eg/or-gate.worb */
|
|
191 |
"##### #####",
|
|
192 |
"# ########### #",
|
|
193 |
"# . > < . #",
|
|
194 |
"# #####v##### #",
|
|
195 |
"##### # ########",
|
|
196 |
" # >!#",
|
|
197 |
" #v#########",
|
|
198 |
" # #",
|
|
199 |
" ###",
|
|
200 |
},
|
|
201 |
{
|
|
202 |
/* eg/subtraction.worb */
|
|
203 |
"###############",
|
|
204 |
"#.............#",
|
|
205 |
"#######v#######",
|
|
206 |
" # #",
|
|
207 |
" #########",
|
|
208 |
"",
|
|
209 |
},
|
|
210 |
{
|
|
211 |
/* eg/division.worb */
|
|
212 |
"############",
|
|
213 |
"#..........#",
|
|
214 |
"#######v####",
|
|
215 |
" # #",
|
|
216 |
" #v####",
|
|
217 |
" # #",
|
|
218 |
" #v####",
|
|
219 |
" # #",
|
|
220 |
" #v####",
|
|
221 |
" # #",
|
|
222 |
" ######",
|
|
223 |
},
|
|
224 |
};
|
|
225 |
WorbState s = new WorbState();
|
|
226 |
s.playfield.load(program[index]);
|
|
227 |
return s;
|
|
228 |
}
|
|
229 |
|
|
230 |
public List<String> getAvailableOptionNames() {
|
|
231 |
ArrayList<String> names = new ArrayList<String>();
|
|
232 |
return names;
|
|
233 |
}
|
|
234 |
|
|
235 |
public WorbState importFromText(String text) {
|
|
236 |
WorbState s = new WorbState();
|
|
237 |
s.playfield.load(text.split("\\r?\\n"));
|
|
238 |
return s;
|
|
239 |
}
|
|
240 |
|
|
241 |
private static final String[][] properties = {
|
|
242 |
{"Author", "Chris Pressey"},
|
|
243 |
{"Implementer", "Chris Pressey"},
|
|
244 |
{"Implementation notes",
|
|
245 |
"None yet."}
|
|
246 |
};
|
|
247 |
|
|
248 |
public String[][] getProperties() {
|
|
249 |
return properties;
|
|
250 |
}
|
|
251 |
}
|
|
252 |
|
|
253 |
class Bobule implements Element {
|
|
254 |
int pressure;
|
|
255 |
|
|
256 |
public Bobule() {
|
|
257 |
pressure = 1;
|
|
258 |
}
|
|
259 |
|
|
260 |
public String getName() {
|
|
261 |
if (pressure == 1)
|
|
262 |
return ".";
|
|
263 |
if (pressure >= 2 && pressure <= 3)
|
|
264 |
return "o";
|
|
265 |
if (pressure >= 4 && pressure <= 6)
|
|
266 |
return "O";
|
|
267 |
return "@";
|
|
268 |
}
|
|
269 |
|
|
270 |
public boolean equals(Element e) {
|
|
271 |
return e instanceof Bobule; // a bobule is a bobule is a bobule!
|
|
272 |
}
|
|
273 |
|
|
274 |
public Bobule fromChar(char c) {
|
|
275 |
return new Bobule();
|
|
276 |
}
|
|
277 |
}
|
|
278 |
|
|
279 |
class WorbPlayfield extends BasicPlayfield<Element> {
|
|
280 |
protected HashMap<Position, Bobule> bobuleMap;
|
|
281 |
private Random rand;
|
|
282 |
|
|
283 |
public WorbPlayfield() {
|
|
284 |
super(new CharacterElement(' '));
|
|
285 |
bobuleMap = new HashMap<Position, Bobule>();
|
|
286 |
rand = new Random();
|
|
287 |
}
|
|
288 |
|
|
289 |
public WorbPlayfield clone() {
|
|
290 |
WorbPlayfield c = new WorbPlayfield();
|
|
291 |
c.copyBackingStoreFrom(this);
|
|
292 |
c.bobuleMap = new HashMap<Position, Bobule>(
|
|
293 |
(Map<Position, Bobule>)this.bobuleMap
|
|
294 |
);
|
|
295 |
return c;
|
|
296 |
}
|
|
297 |
|
|
298 |
public Element get(IntegerElement x, IntegerElement y) {
|
|
299 |
Bobule bobule = bobuleMap.get(new Position(x, y));
|
|
300 |
return (bobule == null) ? getBackground(x, y) : bobule;
|
|
301 |
}
|
|
302 |
|
|
303 |
public CharacterElement getBackground(IntegerElement x, IntegerElement y) {
|
|
304 |
return (CharacterElement)super.get(x, y);
|
|
305 |
}
|
|
306 |
|
|
307 |
public void step() {
|
|
308 |
Set<Position> bobulePositions = new HashSet<Position>(bobuleMap.keySet());
|
|
309 |
Iterator<Position> it = bobulePositions.iterator();
|
|
310 |
|
|
311 |
while (it.hasNext()) {
|
|
312 |
Position p = it.next();
|
|
313 |
Bobule b = bobuleMap.get(p);
|
|
314 |
|
|
315 |
b.pressure++;
|
|
316 |
IntegerElement new_x = p.getX().add(new IntegerElement(rand.nextInt(3) - 1));
|
|
317 |
IntegerElement new_y = p.getY().add(new IntegerElement(rand.nextInt(3) - 1));
|
|
318 |
Element e = get(new_x, new_y);
|
|
319 |
if (e instanceof Bobule) {
|
|
320 |
continue;
|
|
321 |
} else if (e instanceof CharacterElement) {
|
|
322 |
char c = ((CharacterElement)e).getChar();
|
|
323 |
if (c == '#')
|
|
324 |
continue;
|
|
325 |
if (c == '<' && p.getX().compareTo(new_x) < 0)
|
|
326 |
continue;
|
|
327 |
if (c == '>' && p.getX().compareTo(new_x) > 0)
|
|
328 |
continue;
|
|
329 |
if (c == '^' && p.getY().compareTo(new_y) < 0)
|
|
330 |
continue;
|
|
331 |
if (c == 'v' && p.getY().compareTo(new_y) > 0)
|
|
332 |
continue;
|
|
333 |
// print chr(7) if $playfield[$new_x][$new_y] eq '!';
|
|
334 |
} else {
|
|
335 |
// No other possibilities
|
|
336 |
}
|
|
337 |
bobuleMap.remove(p);
|
|
338 |
b.pressure = 1;
|
|
339 |
bobuleMap.put(new Position(new_x, new_y), b);
|
|
340 |
}
|
|
341 |
|
|
342 |
Iterator<Map.Entry<Position, Element>> cit = store.entrySet().iterator();
|
|
343 |
while (cit.hasNext()) {
|
|
344 |
Map.Entry<Position, Element> entry = cit.next();
|
|
345 |
Position p = entry.getKey();
|
|
346 |
char c = ((CharacterElement)entry.getValue()).getChar();
|
|
347 |
|
|
348 |
if (c == '+') {
|
|
349 |
Bobule b = bobuleMap.get(p);
|
|
350 |
if (b == null && rand.nextInt(10) == 0) {
|
|
351 |
b = new Bobule();
|
|
352 |
bobuleMap.put(p, b);
|
|
353 |
}
|
|
354 |
} else if (c == '-') {
|
|
355 |
Bobule b = bobuleMap.get(p);
|
|
356 |
if (b != null && rand.nextInt(10) == 0) {
|
|
357 |
bobuleMap.remove(p);
|
|
358 |
}
|
|
359 |
}
|
|
360 |
}
|
|
361 |
}
|
|
362 |
|
|
363 |
public void loadChar(int x, int y, char c) {
|
|
364 |
if (c == '.') {
|
|
365 |
bobuleMap.put(new Position(x, y), new Bobule());
|
|
366 |
} else {
|
|
367 |
set(x, y, new CharacterElement(c));
|
|
368 |
}
|
|
369 |
}
|
|
370 |
|
|
371 |
}
|
|
372 |
|
|
373 |
public class WorbState implements tc.catseye.yoob.State {
|
|
374 |
protected WorbPlayfield playfield;
|
|
375 |
protected BasicPlayfieldView view;
|
|
376 |
private static final Worb language = new Worb();
|
|
377 |
|
|
378 |
public WorbState() {
|
|
379 |
playfield = new WorbPlayfield();
|
|
380 |
view = new BasicPlayfieldView();
|
|
381 |
}
|
|
382 |
|
|
383 |
public WorbState clone() {
|
|
384 |
WorbState c = new WorbState();
|
|
385 |
c.playfield = this.playfield.clone();
|
|
386 |
return c;
|
|
387 |
}
|
|
388 |
|
|
389 |
public Language getLanguage() {
|
|
390 |
return language;
|
|
391 |
}
|
|
392 |
|
|
393 |
public List<Error> step(World world) {
|
|
394 |
ArrayList<Error> errors = new ArrayList<Error>();
|
|
395 |
playfield.step();
|
|
396 |
return errors;
|
|
397 |
}
|
|
398 |
|
|
399 |
public Playfield getPlayfield(int index) {
|
|
400 |
return playfield;
|
|
401 |
}
|
|
402 |
|
|
403 |
public Tape getTape(int index) {
|
|
404 |
return null;
|
|
405 |
}
|
|
406 |
|
|
407 |
public String getProgramText() {
|
|
408 |
return "";
|
|
409 |
}
|
|
410 |
|
|
411 |
public int getProgramPosition() {
|
|
412 |
return 0;
|
|
413 |
}
|
|
414 |
|
|
415 |
public List<Error> setProgramText(String text) {
|
|
416 |
ArrayList<Error> errors = new ArrayList<Error>();
|
|
417 |
return errors;
|
|
418 |
}
|
|
419 |
|
|
420 |
public View getPlayfieldView(int index) {
|
|
421 |
return view;
|
|
422 |
}
|
|
423 |
|
|
424 |
public View getTapeView(int index) {
|
|
425 |
return null;
|
|
426 |
}
|
|
427 |
|
|
428 |
public String exportToText() {
|
|
429 |
return playfield.dump();
|
|
430 |
}
|
|
431 |
|
|
432 |
public void setOption(String name, boolean value) {
|
|
433 |
}
|
|
434 |
|
|
435 |
public boolean needsInput() {
|
|
436 |
return false;
|
|
437 |
}
|
|
438 |
|
|
439 |
public boolean hasHalted() {
|
|
440 |
return false;
|
|
441 |
}
|
|
442 |
}
|