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

Tree @master (Download .tar.gz)

GemooyState.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
 * A GemooyState implements the semantics of Gemooy.
 * The source code in this file has been placed into the public domain.
 */
package tc.catseye.yoob.gemooy;

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

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

import java.awt.Graphics;
import java.awt.Color;


class Gemooy implements Language {
    public String getName() {
        return "Gemooy";
    }

    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("grow indefinitely");
        names.add("toggle bounded column");
        names.add("solid - turn around - dotted");
        return names;
    }

    public GemooyState loadExampleProgram(int index) {
        // All examples from: http://www.esolangs.org/wiki/Gemooy
        // By Chris Pressey.  From the esowiki, thus in the public domain.
        String[][] program = {
          {
            " @@ %",
            "@  $",
            "@   #",
            "     #",
            "      @# @",
            "          @",
            "          @",
            "     @   @"
          },
          {
            "%   @@   @@",
            "#  @  $    @",
            "       @    ",
            "#      #    ",
            "#           ",
            "       @    ",
            "#     # #   ",
            "#    #      ",
            "    @     @ ",
            "    @     @@",
            "#  @ @    @",
            "@   @   @",
            ""
          },
          {
            "%           @",
            "",
            "",
            " @@      @@",
            "@       @  @",
            "@          @",
            " @   $    @",
            "      #",
            "       #",
            "        @  # @#  @",
            "              @   @",
            "              @   #",
            "           @ @",
            "",
            "            @@             @@",
            "           @              @  @",
            "           @                 @",
            "            @               @",
            "                 #",
            "                  @  #@#    @",
            "                 #     @     @",
            "                #         @  @",
            "               @           @@",
            "               @       @",
            "                @     @",
          }

        };
        GemooyState s = new GemooyState();
        s.playfield.load(program[index]);
        return s;
    }

    public GemooyState importFromText(String text) {
        GemooyState s = new GemooyState();
        s.playfield.load(text.split("\\r?\\n"));
        return s;
    }

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

    private static final String[][] properties = {
        {"Author", "Chris Pressey"},
        {"Implementer", "Chris Pressey"},
    };

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

class GemooyElement implements Element {
    private String symbol;
    public GemooyElement next;

    static public GemooyElement BLANK = null;
    static public GemooyElement HASH = null;
    static public GemooyElement AT = null;

    public GemooyElement(String symbol) {
        this.symbol = symbol;
    }
    
    public String getName() {
        return symbol;
    }

    public boolean equals(Element e) {
        if (e instanceof GemooyElement) {
            return this == e;
        }
        return false;
    }

    public GemooyElement fromChar(char c) {
        init();
        if (c == '#')
            return HASH;
        else if (c == '@')
            return AT;
        else
            return BLANK;
    }

    public static void init() {
        if (BLANK != null) return;
        BLANK = new GemooyElement(" ");
        HASH = new GemooyElement("#");
        AT = new GemooyElement("@");
        BLANK.next = HASH;
        HASH.next = AT;
        AT.next = BLANK;
    }
}

class GemooyPlayfield extends BasicPlayfield<GemooyElement> {
    protected BasicCursor<GemooyElement> ip = null;
    protected BasicCursor<GemooyElement> dp = null;

    // You should call GemooyElement.init() before creating one of these!
    public GemooyPlayfield() {
        super(GemooyElement.BLANK);
        clear();
    }

    public void clear() {
        super.clear();
        ip = new BasicCursor<GemooyElement>(this, IntegerElement.ZERO, IntegerElement.ZERO, IntegerElement.ONE, IntegerElement.ONE);
        dp = new BasicCursor<GemooyElement>(this);
    }

    public GemooyPlayfield clone() {
        GemooyPlayfield c = new GemooyPlayfield();
        c.copyBackingStoreFrom(this);
        c.ip = ip.clone();
        c.ip.setPlayfield(c);
        c.dp = dp.clone();
        c.dp.setPlayfield(c);
        return c;
    }

    public int numCursors() {
        return 2;
    }

    public BasicCursor<GemooyElement> getCursor(int index) {
        if (index == 0)
            return ip;
        if (index == 1)
            return dp;
        return null;
    }

    public void loadChar(int x, int y, char c) {
        GemooyElement e = GemooyElement.BLANK.fromChar(c);
        switch (c) {
            case '$':
                ip.setX(x);
                ip.setY(y);
                break;
            case '%':
                dp.setX(x);
                dp.setY(y);
                break;
        }
        set(x, y, e);
    }

    /*
     * Overrides version in BasicPlayfield.
     */
    public String dump() {
        IntegerElement min_x = getMinX();
        IntegerElement min_y = getMinY();
        IntegerElement max_x = getMaxX();
        IntegerElement max_y = getMaxY();
        IntegerElement x = min_x;
        IntegerElement y = min_y;
        StringBuffer buf = new StringBuffer();
        int blanks;

        while (y.compareTo(max_y) <= 0) {
            x = min_x;
            blanks = 0;
            while (x.compareTo(max_x) <= 0) {
                if (x.compareTo(ip.getX()) == 0 && y.compareTo(ip.getY()) == 0) {
                    while (blanks > 0) {
                        buf.append(GemooyElement.BLANK.getName());
                        blanks--;
                    }
                    buf.append('$');
                } else if (x.compareTo(dp.getX()) == 0 && y.compareTo(dp.getY()) == 0) {
                    while (blanks > 0) {
                        buf.append(GemooyElement.BLANK.getName());
                        blanks--;
                    }
                    buf.append('%');
                } else {
                    Element e = get(x, y);
                    if (e == GemooyElement.BLANK) {
                        blanks++;
                    } else {
                        while (blanks > 0) {
                            buf.append(GemooyElement.BLANK.getName());
                            blanks--;
                        }
                        buf.append(e.getName());
                    }
                }
                x = x.succ();
            }
            y = y.succ();
            buf.append("\n");
        }

        return buf.toString();
    }
}

class GemooyPlayfieldView extends BasicPlayfieldView {
    public void render(Graphics g, Cursor c, int x, int y, int w, int h) {
        Playfield p = c.getPlayfield();
        if (c == p.getCursor(0)) {
            g.setColor(Color.red);
        } else {
            g.setColor(Color.green);
        }
        g.drawRoundRect(x - 1, y - 1, w + 2, h + 2, w / 4, h / 4);
    }
}

public class GemooyState implements State {
    protected GemooyPlayfield playfield;
    protected GemooyPlayfieldView view;
    protected boolean halted = false;
    private static final Gemooy language = new Gemooy();

    public GemooyState() {
        GemooyElement.init();
        playfield = new GemooyPlayfield();
        view = new GemooyPlayfieldView();
    }

    public Language getLanguage() {
        return language;
    }
    
    public GemooyState clone() {
        GemooyState c = new GemooyState();
        c.playfield = this.playfield.clone();
        c.halted = halted;
        return c;
    }

    public List<Error> step(World world) {
        ArrayList<Error> errors = new ArrayList<Error>();
        BasicCursor<GemooyElement> ip = playfield.getCursor(0);
        BasicCursor<GemooyElement> dp = playfield.getCursor(1);

        Element instruction = ip.get();

        if (instruction == GemooyElement.BLANK) {
            /* no effect */
        } else if (instruction == GemooyElement.HASH) {
            if (ip.isHeaded(0, -1)) {
                // * North = Move data pointer one cell north, skip instruction pointer over next cell.
                dp.move(0, -1);
                ip.advance();
            } else if (ip.isHeaded(0, 1)) {
                // * South = Move data pointer one cell south, skip instruction pointer over next cell.
                dp.move(0, 1);
                ip.advance();
            } else if (ip.isHeaded(1, 0)) {
                // * East = Move data pointer one cell east, skip instruction pointer over next cell.
                dp.move(1, 0);
                ip.advance();
            } else if (ip.isHeaded(-1, 0)) {
                // * West = Move data pointer one cell west, skip instruction pointer over next cell.
                dp.move(-1, 0);
                ip.advance();
            } else if (ip.isHeaded(-1, -1) || ip.isHeaded(1, -1)) {
                // * Northeast or Northwest = Increment cell at data pointer.
                GemooyElement datum = (GemooyElement)dp.get();
                dp.set(datum.next);
            } else if (ip.isHeaded(-1, 1) || ip.isHeaded(1, 1)) {
                // * Southeast or Southwest = Decrement cell at data pointer. 
                GemooyElement datum = (GemooyElement)dp.get();
                dp.set(datum.next.next);
            } else {
                // TODO: add error to errors
            }
        } else if (instruction == GemooyElement.AT) {
            GemooyElement datum = (GemooyElement)dp.get();
            if (datum == GemooyElement.HASH) {
                ip.rotate(-45);
            } else if (datum == GemooyElement.AT) {
                /* no effect */
            } else if (datum == GemooyElement.BLANK) {
                ip.rotate(45);
            } else {
                // TODO: add error to errors
            }
        } else {
            // TODO: add error to errors
        }

        ip.advance();
        if (playfield.hasFallenOffEdge(ip)) {
            halted = true;
        }

        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 boolean hasHalted() {
        return halted;
    }

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

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