[Commit] RRClient RRBoard.java,1.1.1.1,1.2 RRBoardPanel.java,1.1.1.1,1.2 RRClient.java,1.1.1.1,1.2 RRImages.java,1.1.1.1,1.2 RRSquare.java,1.1.1.1,1.2

Bart Massey commit at keithp.com
Sat Jun 7 00:05:08 PDT 2003


Committed by: bart

Update of /local/src/CVS/RRClient
In directory home.keithp.com:/tmp/cvs-serv13913

Modified Files:
	RRBoard.java RRBoardPanel.java RRClient.java RRImages.java 
	RRSquare.java 
Log Message:
As-yet-untested board parsing code based on cworth's
example.  Consistently use [row][col] indexing and work in
row-major order to keep straight what's happening.



Index: RRBoard.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRBoard.java,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- RRBoard.java	1 Jun 2003 06:03:11 -0000	1.1.1.1
+++ RRBoard.java	7 Jun 2003 06:05:06 -0000	1.2
@@ -4,6 +4,8 @@
  *
  */
 
+import java.io.*;
+
 /**
  * @author Bart Massey <bart at cs.pdx.edu>
  *
@@ -12,32 +14,220 @@
 public class RRBoard {
     public static final int dim = 16;
     RRSquare[][] squares = new RRSquare[dim][dim];
+    int row_goal = -1;
+    int col_goal = -1;
 
     public RRBoard() {
-	for (int x = 0; x < dim; x++)
-	    for (int y = 0; y < dim; y++)
-		squares[x][y] = new RRSquare();
+	for (int row = 0; row < dim; row++)
+	    for (int col = 0; col < dim; col++)
+		squares[row][col] = new RRSquare();
 	for (int i = 0; i < dim; i++) {
-	    squares[i][0].setWall(0);
-	    squares[dim - 1][i].setWall(1);
-	    squares[i][dim - 1].setWall(2);
-	    squares[0][i].setWall(3);
+	    squares[0][i].setWall(0);
+	    squares[i][dim - 1].setWall(1);
+	    squares[dim - 1][i].setWall(2);
+	    squares[i][0].setWall(3);
 	}
 	for (int i = 6; i < 10; i++) {
-	    squares[i][10].setWall(0);
-	    squares[5][i].setWall(1);
-	    squares[i][5].setWall(2);
-	    squares[10][i].setWall(3);
+	    squares[10][i].setWall(0);
+	    squares[i][5].setWall(1);
+	    squares[5][i].setWall(2);
+	    squares[i][10].setWall(3);
 	}
-	RRSquare s = squares[1][1];
+    }
+
+    static void expect(int c, int ch)
+      throws IOException {
+	if (c != ch)
+	    throw new IOException("expected " + ch + ", got " + c);
+    }
+
+    static String next_line(BufferedReader in, int off)
+      throws IOException {
+	String s = in.readLine();
+	int n = s.length();
+	if (n != 4 * dim + off)
+	    throw new IOException("wrong line width");
+	return s;
+    }
+
+    static void check_thetarget(boolean t)
+      throws IOException {
+	if (!t)
+	    throw new IOException("target priority mismatch");
+    }
+
+    void wall_line(BufferedReader in, int i)
+      throws IOException {
+	int off = 1;
+	if (i == dim)
+	    off = 2;
+	String s = next_line(in, off);
+	int ci = 0;
+	for (int j = 0; j < dim; j++) {
+	    int c = s.charAt(ci++);
+	    expect(c, ' ');
+	    c = s.charAt(ci++);
+	    if (c == '=') {
+		if (i - 1 > 0)
+		    squares[i-1][j].setWall(3);
+		if (i < dim)
+		    squares[i][j].setWall(0);
+	    } else {
+		expect(c, ' ');
+	    }
+	    int cx = s.charAt(ci++);
+	    expect(cx, c);
+	    cx = s.charAt(ci++);
+	    expect(cx, c);
+	}
+	int c = s.charAt(ci++);
+	expect(c, ' ');
+	if (i == dim) {
+	    c = s.charAt(ci++);
+	    expect(c, '"');
+	}
+    }
+
+    void target_line(BufferedReader in, int i)
+      throws IOException {
+	String s = next_line(in, 1);
+	int ci = 0;
+	for (int j = 0; j <= dim; j++) {
+	    int c = s.charAt(ci++);
+	    if (c == '|') {
+		if (j < dim)
+		    squares[i][j].setWall(3);
+		if (j - 1 >= 0)
+		    squares[i][j - 1].setWall(1);
+	    } else {
+		expect(c, ' ');
+	    }
+	    if (j == dim)
+		break;
+	    c = s.charAt(ci++);
+	    switch(c) {
+	    case 'r':
+	    case 'R':
+		squares[i][j].setRobot(RRSquare.RED);
+		    break;
+	    case 'y':
+	    case 'Y':
+		squares[i][j].setRobot(RRSquare.YELLOW);
+		break;
+	    case 'g':
+	    case 'G':
+		squares[i][j].setRobot(RRSquare.GREEN);
+		break;
+	    case 'b':
+	    case 'B':
+		squares[i][j].setRobot(RRSquare.BLUE);
+		break;
+	    default:
+		expect(c, ' ');
+	    }
+	    int c1 = s.charAt(ci++);
+	    int c2 = s.charAt(ci++);
+	    int color = -1;
+	    boolean thetarget = false;
+	    switch (c1) {
+	    case 'R':
+		thetarget = true;
+	    case 'r':
+		color = RRSquare.RED;
+		break;
+	    case 'Y':
+		thetarget = true;
+	    case 'y':
+		color = RRSquare.YELLOW;
+		break;
+	    case 'g':
+		thetarget = true;
+	    case 'G':
+		color = RRSquare.GREEN;
+		break;
+	    case 'b':
+		thetarget = true;
+	    case 'B':
+		color = RRSquare.BLUE;
+		break;
+	    case 'W':
+		thetarget = true;
+	    case 'w':
+		color = RRSquare.WHIRLPOOL;
+		break;
+	    default:
+		expect(c1, ' ');
+	    }
+	    int shape = -1;
+	    switch(c2) {
+	    case 'C':
+		check_thetarget(thetarget);
+	    case 'c':
+		shape = RRImages.CIRCLE;
+		break;
+	    case 'S':
+		check_thetarget(thetarget);
+	    case 's':
+		shape = RRImages.SQUARE;
+		break;
+	    case 'T':
+		check_thetarget(thetarget);
+	    case 't':
+		shape = RRImages.TRIANGLE;
+		break;
+	    case 'O':
+		check_thetarget(thetarget);
+	    case 'o':
+		shape = RRImages.STAR;
+		break;
+	    case 'W':
+		check_thetarget(thetarget);
+	    case 'w':
+		if (color != RRSquare.WHIRLPOOL)
+		    throw new IOException("target whirlpool mismatch");
+		shape = 0;
+		break;
+	    default:
+		expect(c2, ' ');
+		if (c1 != -1)
+		    throw new IOException("target blank mismatch");
+	    }
+	    if (color >= 0) {
+		int coord = 4 * color + shape;
+		squares[i][j].setTarget(coord, thetarget);
+		if (thetarget) {
+		    row_goal = i;
+		    col_goal = j;
+		}
+	    }
+	}
+    }
+
+
+    public RRBoard(BufferedReader in)
+      throws IOException {
+	this();
+	for (int i = 0; i < dim; i++) {
+	    wall_line(in, i);
+	    target_line(in, i);
+	}
+	wall_line(in, dim);
+    }
+
+
+    public static RRBoard testBoard() {
+	RRBoard b = new RRBoard();
+	RRSquare s = b.squares[1][1];
 	s.setWall(1);
 	s.setWall(2);
-	s.setTarget(RRSquare.WHIRLPOOL_TARGET);
-	squares[2][2].setTarget(7);
-	squares[14][13].setRobot(RRSquare.YELLOW);
+	s.setTarget(RRSquare.WHIRLPOOL * 4, true);
+	b.squares[2][2].setTarget(RRSquare.YELLOW * 4 + RRImages.STAR);
+	b.squares[3][2].setTarget(RRSquare.YELLOW * 4 + RRImages.TRIANGLE);
+	b.squares[13][14].setRobot(RRSquare.YELLOW);
+	return b;
     }
 
-    public RRSquare getSquare(int x, int y) {
-        return squares[x][y];
+    public RRSquare getSquare(int row, int col) {
+        return squares[row][col];
     }
 }

Index: RRBoardPanel.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRBoardPanel.java,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- RRBoardPanel.java	1 Jun 2003 06:03:11 -0000	1.1.1.1
+++ RRBoardPanel.java	7 Jun 2003 06:05:06 -0000	1.2
@@ -26,62 +26,63 @@
     public void paintComponent(Graphics g) {
 	int dim = board.dim;
         Dimension d = getSize();
-        double dx = d.width / (double)dim;
-        double dy = d.height / (double)dim;
-	int idx = (int)Math.floor(dx + 1);
-	int idy = (int)Math.floor(dy + 1);
+        double dcol = d.width / (double)dim;
+        double drow = d.height / (double)dim;
+	int idcol = (int)Math.floor(dcol + 1);
+	int idrow = (int)Math.floor(drow + 1);
 
         /* draw targets */
-        for (int x = 0; x < dim; x++) {
-            for (int y = 0; y < dim; y++) {
-                RRSquare s = board.getSquare(x, y);
-                Image img = images.getTargetImage(s.getTarget());
-		int xdx = (int)Math.floor(x * dx);
-		int ydy = (int)Math.floor(y * dy);
-                g.drawImage(img, xdx, ydy, idx, idy, this);
+        for (int row = 0; row < dim; row++) {
+            for (int col = 0; col < dim; col++) {
+                RRSquare s = board.getSquare(row, col);
+                Image img = images.getTargetImage(s.getTarget(),
+						  s.isPrimaryTarget());
+		int xdcol = (int)Math.floor(col * dcol);
+		int ydrow = (int)Math.floor(row * drow);
+                g.drawImage(img, xdcol, ydrow, idcol, idrow, this);
             }
         }
         /* draw walls */
-        for (int x = 0; x < dim; x++) {
-            for (int y = 0; y < dim; y++) {
-                RRSquare s = board.getSquare(x, y);
+        for (int row = 0; row < dim; row++) {
+            for (int col = 0; col < dim; col++) {
+                RRSquare s = board.getSquare(row, col);
                 for (int w = 0; w < 4; w++) {
                     if (!s.getWall(w))
                         continue;
-                    double wdx = wallwidth * dx ;
-                    double wdy = wallwidth * dy ;
-                    double wx = x * dx - wdx;
-                    double wy = y * dy - wdy;
-		    double ww = 2 * wdx + 1;
-		    double wh = 2 * wdy + 1;
+                    double wdcol = wallwidth * dcol ;
+                    double wdrow = wallwidth * drow ;
+                    double wc = col * dcol - wdcol;
+                    double wr = row * drow - wdrow;
+		    double ww = 2 * wdcol + 1;
+		    double wh = 2 * wdrow + 1;
                     if (w == 1 || w == 3)
-			wh += dy - 1;
+			wh += drow - 1;
 		    else
-			ww += dx - 1;
+			ww += dcol - 1;
 		    if (w == 1)
-			wx += dx;
+			wc += dcol;
 		    if (w == 2)
-			wy += dy;
-		    int iwx = (int)Math.floor(wx);
-		    int iwy = (int)Math.floor(wy);
+			wr += drow;
+		    int iwc = (int)Math.floor(wc);
+		    int iwr = (int)Math.floor(wr);
 		    int iww = (int)Math.floor(ww);
 		    int iwh = (int)Math.floor(wh);
                     g.setColor(Color.BLACK);
-                    g.fillRect(iwx, iwy, iww, iwh);
+                    g.fillRect(iwc, iwr, iww, iwh);
                 }
             }
         }
         /* draw robots */
-        for (int x = 0; x < dim; x++) {
-            for (int y = 0; y < dim; y++) {
-                RRSquare s = board.getSquare(x, y);
+        for (int row = 0; row < dim; row++) {
+            for (int col = 0; col < dim; col++) {
+                RRSquare s = board.getSquare(row, col);
                 int r = s.getRobot();
                 if (r == RRSquare.NO_ROBOT)
                     continue;
                 Image img = images.getRobotImage(r);
-		int xdx = (int)Math.floor(x * dx);
-		int ydy = (int)Math.floor(y * dy);
-                g.drawImage(img, xdx, ydy, idx, idy, this);
+		int col_dcol = (int)Math.floor(col * dcol);
+		int row_drow = (int)Math.floor(row * drow);
+                g.drawImage(img, col_dcol, row_drow, idcol, idrow, this);
             }
         }
     }

Index: RRClient.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRClient.java,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- RRClient.java	1 Jun 2003 06:03:11 -0000	1.1.1.1
+++ RRClient.java	7 Jun 2003 06:05:06 -0000	1.2
@@ -23,7 +23,7 @@
     }
 
     public static void main(String[] args) {
-        RRBoard board = new RRBoard();
+        RRBoard board = RRBoard.testBoard();
         RRImages images = new RRImages(Toolkit.getDefaultToolkit());
         JPanel bp = new RRBoardPanel(board, images);
         JFrame f = new JFrame("Ricochet Robots");

Index: RRImages.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRImages.java,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- RRImages.java	1 Jun 2003 06:03:11 -0000	1.1.1.1
+++ RRImages.java	7 Jun 2003 06:05:06 -0000	1.2
@@ -34,6 +34,11 @@
 }
 
 public class RRImages {
+    public static final int CIRCLE = 0;
+    public static final int SQUARE = 1;
+    public static final int TRIANGLE = 2;
+    public static final int STAR = 3;
+
     Image[] target_images = new Image[18];
     Image[] robot_images = new Image[4];
 
@@ -70,7 +75,9 @@
         this(new GetImage(applet));
     }
 
-    public Image getTargetImage(int index) {
+    public Image getTargetImage(int index, boolean primary) {
+	if (index > 0 && !primary)
+	    return target_images[0];
         return target_images[index + 1];
     }
 

Index: RRSquare.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRSquare.java,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- RRSquare.java	1 Jun 2003 06:03:11 -0000	1.1.1.1
+++ RRSquare.java	7 Jun 2003 06:05:06 -0000	1.2
@@ -15,14 +15,7 @@
     public static final int YELLOW = 1;
     public static final int GREEN = 2;
     public static final int BLUE = 3;
-    public static final int WHIRLPOOL_TARGET = 16;
-    static final int[] target_colors = {
-          RED * 4,
-          YELLOW * 4,
-          GREEN * 4,
-          BLUE * 4,
-          WHIRLPOOL_TARGET
-    };
+    public static final int WHIRLPOOL = 4;
     public static final int NO_ROBOT = -1;
 
     /* see above */
@@ -31,13 +24,24 @@
     int robot = NO_ROBOT;
     /* N, E, S, W */
     boolean[] wall = {false, false, false, false};
+    /* Is this the goal square? */
+    boolean primary_target = false;
     
     public void setTarget(int t) {
         target = t;
     }
 
+    public void setTarget(int t, boolean primary) {
+        target = t;
+	primary_target = primary;
+    }
+
     public int getTarget() {
         return target;
+    }
+
+    public boolean isPrimaryTarget() {
+        return primary_target;
     }
 
     public int getRobot() {




More information about the Commit mailing list