/* Copyright (C) 2002 J. M. Spivey */ public class Machine { public static final int NSTATES = 8, NCOLORS = 4; public static final int field = 0, red = 1, green = 2, yellow = 3; public static final int left = 0, right = 1, ahead = 2; private int state = 0; private int drawtab[][] = new int[NSTATES][NCOLORS]; private int movetab[][] = new int[NSTATES][NCOLORS]; private int trantab[][] = new int[NSTATES][NCOLORS]; private void clear() { for (int i = 0; i < NSTATES; i++) { for (int j = 0; j < NCOLORS; j++) { drawtab[i][j] = field; movetab[i][j] = left; trantab[i][j] = 0; } } } public void setup(String desc) { int p = 0; char c; clear(); for (int i = 0;; i++) { for (int j = 0; j < NCOLORS; j++) { if (p+3 > desc.length()) return; c = desc.charAt(p); drawtab[i][j] = (c == 'r' ? red : c == 'g' ? green : c == 'y' ? yellow : field); c = desc.charAt(p+1); movetab[i][j] = (c == 'L' ? left : c == 'R' ? right : ahead); c = desc.charAt(p+2); trantab[i][j] = (int) c - '1'; p += 3; } } } public String descriptor() { int N = NSTATES; String colors = "brgy"; String dirs = "LRA"; // Find the last non-trivial state while (N > 0) { int i = N-1; boolean found = false; for (int j = 0; j < NCOLORS && !found; j++) { if (drawtab[i][j] != field || movetab[i][j] != left || trantab[i][j] != 0) found = true; } if (found) break; N--; } StringBuffer buf = new StringBuffer(100); for (int i = 0; i < N; i++) { for (int j = 0; j < NCOLORS; j++) { buf.append(colors.charAt(drawtab[i][j])); buf.append(dirs.charAt(movetab[i][j])); buf.append(trantab[i][j]+1); } } return buf.toString(); } public int getState() { return state; }; public void reset() { state = 0; } public void setDraw(int i, int j, int color) { drawtab[i][j] = color; } public int getDraw(int i, int j) { return drawtab[i][j]; } public void setMove(int i, int j, int n) { movetab[i][j] = n; } public int getMove(int i, int j) { return movetab[i][j]; } public void setTran(int i, int j, int n) { trantab[i][j] = n; } public int getTran(int i, int j) { return trantab[i][j]; } public int draw(int color) { return drawtab[state][color]; } public int move(int color) { return movetab[state][color]; } public void next(int color) { state = trantab[state][color]; } }