git @ Cat's Eye Technologies yoob / master src / lang / WorbState.java
master

Tree @master (Download .tar.gz)

WorbState.java @masterraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * tc.catseye.yoob.WorbState -- noit o'mnain worb for yoob
 * The source code in this file has been placed into the public domain.
 */

/*
 * A tc.catseye.yoob.WorbState implements the semantics of the
 * noit o'mnain worb automaton under the yoob framework.
 */

package tc.catseye.yoob.worb;

import tc.catseye.yoob.Error;
import tc.catseye.yoob.*;

import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
import java.util.Map;
import java.util.HashMap;
import java.util.Random;
import java.util.Iterator;


class Worb implements Language {
    public String getName() {
        return "noit o' mnain worb";
    }

    public int numPlayfields() {
        return 1;
    }

    public int numTapes() {
        return 0;
    }

    public boolean hasProgramText() {
        return false;
    }

    public boolean hasInput() {
        return false;
    }

    public boolean hasOutput() {
        return false;
    }

    public List<String> exampleProgramNames() {
        ArrayList<String> names = new ArrayList<String>();
        names.add("freefill");
        names.add("magnetic field");
        names.add("theory of time");
        names.add("pressure");
        names.add("slow loop");
        names.add("fast loop");
        names.add("transistor");
        names.add("or gate");
        names.add("subtraction");
        names.add("division");
        return names;
    }

    public WorbState loadExampleProgram(int index) {
        String[][] program = {
          {
            /* eg/freefill.worb */
            "######################",
            "#                    #",
            "#                    #",
            "#                    #",
            "#                    #",
            "#                    #",
            "#         +          #",
            "#                    #",
            "#                    #",
            "#                    #",
            "#                    #",
            "######################",
          },
          {
            /* eg/magnetic-field.worb */
            "######################",
            "#                    #",
            "#                    #",
            "#                    #",
            "#                    #",
            "#         -          #",
            "#         +          #",
            "#                    #",
            "#                    #",
            "#                    #",
            "#                    #",
            "######################",
          },
          {
            /* eg/theory-of-time.worb */
            "######################",
            "#..........          #",
            "#..........          #",
            "#..........          #",
            "#..........          #",
            "#..........          #",
            "#..........          #",
            "#..........          #",
            "#..........          #",
            "#..........          #",
            "#..........          #",
            "######################",
          },
          {
            /* eg/pressure.worb */
            "#######################",
            "#..........>          #",
            "#######################",
          },
          {
            /* eg/slow-loop.worb */
            "#####################",
            "#        <          #",
            "# ################# #",
            "# #               # #",
            "# #               #.#",
            "# #               # #",
            "# ################# #",
            "#         >         #",
            "#####################",
          },
          {
            /* eg/fast-loop.worb */
            "#######",
            "# < < #",
            "# ### #",
            "# >.> #",
            "#######",
          },
          {
            /* eg/transistor.worb */
            "    ###",
            "### #+#",
            "#+# # #",
            "# ###v#",
            "#   < #",
            "### < #",
            "  # < #",
            "  ###v#",
            "    # #",
            "    #!#",
            "    #-#",
            "    ###",
            "",
          },
          {
            /* eg/or-gate.worb */
            "#####         #####",
            "#   ###########   #",
            "# . >         < . #",
            "#   #####v#####   #",
            "#####   #  ########",
            "        #       >!#",
            "        #v#########",
            "        # #",
            "        ###",
          },
          {
            /* eg/subtraction.worb */
            "###############",
            "#.............#",
            "#######v#######",
            "      #       #",
            "      #########",
            "",
          },
          {
            /* eg/division.worb */
            "############",
            "#..........#",
            "#######v####",
            "      #    #",
            "      #v####",
            "      #    #",
            "      #v####",
            "      #    #",
            "      #v####",
            "      #    #",
            "      ######",
          },
        };
        WorbState s = new WorbState();
        s.playfield.load(program[index]);
        return s;
    }

    public List<String> getAvailableOptionNames() {
        ArrayList<String> names = new ArrayList<String>();
        return names;
    }

    public WorbState importFromText(String text) {
        WorbState s = new WorbState();
        s.playfield.load(text.split("\\r?\\n"));
        return s;
    }
  
    private static final String[][] properties = {
        {"Author", "Chris Pressey"},
        {"Implementer", "Chris Pressey"},
        {"Implementation notes",
         "None yet."}
    };

    public String[][] getProperties() {
        return properties;
    }
}

class Bobule implements Element {
    int pressure;
    
    public Bobule() {
        pressure = 1;
    }

    public String getName() {
        if (pressure == 1)
            return ".";
        if (pressure >= 2 && pressure <= 3)
            return "o";
        if (pressure >= 4 && pressure <= 6)
            return "O";
        return "@";
    }

    public boolean equals(Element e) {
        return e instanceof Bobule; // a bobule is a bobule is a bobule!
    }

    public Bobule fromChar(char c) {
        return new Bobule();
    }
}

class WorbPlayfield extends BasicPlayfield<Element> {
    protected HashMap<Position, Bobule> bobuleMap;
    private Random rand;

    public WorbPlayfield() {
        super(new CharacterElement(' '));
        bobuleMap = new HashMap<Position, Bobule>();
        rand = new Random();
    }

    public WorbPlayfield clone() {
        WorbPlayfield c = new WorbPlayfield();
        c.copyBackingStoreFrom(this);
        c.bobuleMap = new HashMap<Position, Bobule>(
          (Map<Position, Bobule>)this.bobuleMap
        );
        return c;
    }

    public Element get(IntegerElement x, IntegerElement y) {
        Bobule bobule = bobuleMap.get(new Position(x, y));
        return (bobule == null) ? getBackground(x, y) : bobule;
    }

    public CharacterElement getBackground(IntegerElement x, IntegerElement y) {
        return (CharacterElement)super.get(x, y);
    }

    public void step() {
        Set<Position> bobulePositions = new HashSet<Position>(bobuleMap.keySet());
        Iterator<Position> it = bobulePositions.iterator();

        while (it.hasNext()) {
            Position p = it.next();
            Bobule b = bobuleMap.get(p);

            b.pressure++;
            IntegerElement new_x = p.getX().add(new IntegerElement(rand.nextInt(3) - 1));
            IntegerElement new_y = p.getY().add(new IntegerElement(rand.nextInt(3) - 1));
            Element e = get(new_x, new_y);
            if (e instanceof Bobule) {
                continue;
            } else if (e instanceof CharacterElement) {
                char c = ((CharacterElement)e).getChar();
                if (c == '#')
                    continue;
                if (c == '<' && p.getX().compareTo(new_x) < 0)
                    continue;
                if (c == '>' && p.getX().compareTo(new_x) > 0)
                    continue;
                if (c == '^' && p.getY().compareTo(new_y) < 0)
                    continue;
                if (c == 'v' && p.getY().compareTo(new_y) > 0)
                    continue;
                // print chr(7) if $playfield[$new_x][$new_y] eq '!';
            } else {
                // No other possibilities
            }
            bobuleMap.remove(p);
            b.pressure = 1;
            bobuleMap.put(new Position(new_x, new_y), b);
        }

        Iterator<Map.Entry<Position, Element>> cit = store.entrySet().iterator();
        while (cit.hasNext()) {
            Map.Entry<Position, Element> entry = cit.next();
            Position p = entry.getKey();
            char c = ((CharacterElement)entry.getValue()).getChar();
          
            if (c == '+') {
                Bobule b = bobuleMap.get(p);
                if (b == null && rand.nextInt(10) == 0) {
                    b = new Bobule();
                    bobuleMap.put(p, b);
                }
            } else if (c == '-') {
                Bobule b = bobuleMap.get(p);
                if (b != null && rand.nextInt(10) == 0) {
                    bobuleMap.remove(p);
                }
            }
        }
    }

    public void loadChar(int x, int y, char c) {
        if (c == '.') {
            bobuleMap.put(new Position(x, y), new Bobule());
        } else {
            set(x, y, new CharacterElement(c));
        }
    }

}

public class WorbState implements tc.catseye.yoob.State {
    protected WorbPlayfield playfield;
    protected BasicPlayfieldView view;
    private static final Worb language = new Worb();
    
    public WorbState() {
        playfield = new WorbPlayfield();
        view = new BasicPlayfieldView();
    }
    
    public WorbState clone() {
        WorbState c = new WorbState();
        c.playfield = this.playfield.clone();
        return c;
    }

    public Language getLanguage() {
	return language;
    }

    public List<Error> step(World world) {
        ArrayList<Error> errors = new ArrayList<Error>();      
        playfield.step();
        return errors;
    }

    public Playfield getPlayfield(int index) {
        return playfield;
    }

    public Tape getTape(int index) {
        return null;
    }

    public String getProgramText() {
        return "";
    }

    public int getProgramPosition() {
        return 0;
    }

    public List<Error> setProgramText(String text) {
        ArrayList<Error> errors = new ArrayList<Error>();
        return errors;
    }

    public View getPlayfieldView(int index) {
        return view;
    }

    public View getTapeView(int index) {
        return null;
    }

    public String exportToText() {
        return playfield.dump();
    }

    public void setOption(String name, boolean value) {
    }

    public boolean needsInput() {
        return false;
    }

    public boolean hasHalted() {
        return false;
    }
}