[Commit] RRClient RRBoard.java,1.7,1.8 RRBoardPanel.java,1.7,1.8 RRClient.java,1.5,1.6 RRNetIn.java,1.2,1.3 RRNetOut.java,1.2,1.3

Bart Massey commit at keithp.com
Sun Jun 8 19:54:47 PDT 2003


Committed by: bart

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

Modified Files:
	RRBoard.java RRBoardPanel.java RRClient.java RRNetIn.java 
	RRNetOut.java 
Log Message:
Watches successfully now!  The rest is easy...



Index: RRBoard.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRBoard.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- RRBoard.java	8 Jun 2003 10:31:23 -0000	1.7
+++ RRBoard.java	9 Jun 2003 01:54:44 -0000	1.8
@@ -14,10 +14,13 @@
     public static final int dim = 16;
     volatile RRSquare[][] squares = new RRSquare[dim][dim];
     volatile RRBoardCoord goal = null;
-    RRNetOut netout;
+    RRController ctl;
+    boolean active = false;
+    public String playerName = null;
 
-    public RRBoard(RRNetOut netout) {
-	this.netout = netout;
+    public RRBoard(RRController ctl, String player_name) {
+	this.ctl = ctl;
+	this.playerName = player_name;
 	for (int row = 0; row < dim; row++)
 	    for (int col = 0; col < dim; col++)
 		squares[row][col] = new RRSquare();
@@ -29,16 +32,16 @@
 	}
     }
 
-    public RRBoard(BufferedReader in, RRNetOut netout)
+    public RRBoard(BufferedReader in, RRController ctl)
       throws IOException {
-	this(netout);
+	this(ctl, null);
 	become(in);
     }
 
 
-    public RRBoard(String board, RRNetOut netout)
+    public RRBoard(String board, RRController ctl)
       throws IOException {
-	this(netout);
+	this(ctl, null);
 	become(board);
     }
 
@@ -282,7 +285,27 @@
 	    return;
 	RRSquare se = squares[cur.row][cur.col];
 	se.setRobot(robot);
-	netout.move(robot, dir);
+	ctl.netout.move(robot, dir);
     }
 
+    synchronized public void moveRobot(int color, RRBoardCoord end) {
+	RRBoardCoord start = null;
+	for (int i = 0; i < dim; i++) {
+	    for (int j = 0; j < dim; j++) {
+		if (squares[i][j].getRobot() == color) {
+		    start = new RRBoardCoord(i, j);
+		    break;
+		}
+	    }
+	}
+	moveRobot(start, end);
+    }
+
+    public void setActive(boolean active) {
+	this.active = active;
+    }
+
+    public boolean getActive() {
+	return active;
+    }
 }

Index: RRBoardPanel.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRBoardPanel.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- RRBoardPanel.java	8 Jun 2003 09:02:57 -0000	1.7
+++ RRBoardPanel.java	9 Jun 2003 01:54:44 -0000	1.8
@@ -18,51 +18,52 @@
     static final int IDLE = 0;
     static final int DRAGGING = 1;
     int state = IDLE;
-    RRBoard board;
-    RRBoardPanel panel;
+    RRController ctl;
     public RRBoardCoord start = null;
     public RRBoardCoord stop = null;
     public RRBoardCoord cur = null;
 
-    public RobotMover(RRBoard board, RRBoardPanel panel) {
+    public RobotMover(RRController ctl) {
 	super();
-	this.board = board;
-	this.panel = panel;
+	this.ctl = ctl;
     }
 
     public void mousePressed(MouseEvent e) {
+	if (!ctl.board.getActive())
+	    return;
 	if (e.getButton() != e.BUTTON1)
 	    return;
-	start = panel.boardCoord(e.getPoint());
+	start = ctl.boardPanel.boardCoord(e.getPoint());
 	stop = null;
 	cur = start;
 	state = DRAGGING;
     }
 
     public void mouseReleased(MouseEvent e) {
+	if (state != DRAGGING)
+	    return;
 	if (e.getButton() != e.BUTTON1)
 	    return;
-	stop = panel.boardCoord(e.getPoint());
+	stop = ctl.boardPanel.boardCoord(e.getPoint());
 	cur = stop;
 	state = IDLE;
 	if (!start.equals(stop))
-	    board.moveRobot(start, stop);
-	panel.clearHilite();
+	    ctl.board.moveRobot(start, stop);
+	ctl.boardPanel.clearHilite();
     }
 
     public void mouseDragged(MouseEvent e) {
 	if (state != DRAGGING)
 	    return;
-	cur = panel.boardCoord(e.getPoint());
-	panel.setHilite(cur);
+	cur = ctl.boardPanel.boardCoord(e.getPoint());
+	ctl.boardPanel.setHilite(cur);
     }
 }
 
 public class RRBoardPanel extends JPanel {
     static final double wallwidth = 0.1;
 
-    RRBoard board;
-    RRImages images;
+    RRController ctl;
     RobotMover mover;
 
     int dim;
@@ -75,16 +76,15 @@
     }
 
     void update_dims() {
-	dim = board.dim;
+	dim = ctl.board.dim;
         Dimension d = getSize();
 	dcol = d.width / (double)dim;
 	drow = d.height / (double)dim;
     }
 
-    public RRBoardPanel(RRBoard board, RRImages images) {
-        this.board = board;
-        this.images = images;
-	mover = new RobotMover(board, this);
+    public RRBoardPanel(RRController ctl) {
+        this.ctl = ctl;
+	mover = new RobotMover(ctl);
 	this.addMouseMotionListener(mover);
 	this.addMouseListener(mover);
     }
@@ -96,7 +96,7 @@
 	int idrow = floor(drow + 1);
 
 	/* draw floor */
-	Image blank_img = images.getBlankImage();
+	Image blank_img = ctl.images.getBlankImage();
         for (int row = 0; row < dim; row++) {
             for (int col = 0; col < dim; col++) {
 		int xdcol = floor(col * dcol);
@@ -107,11 +107,11 @@
         /* draw targets */
         for (int row = 0; row < dim; row++) {
             for (int col = 0; col < dim; col++) {
-                RRSquare s = board.getSquare(row, col);
+                RRSquare s = ctl.board.getSquare(row, col);
 		if (s.getTarget() == RRSquare.NO_TARGET)
 		    continue;
 		boolean primary = s.isPrimaryTarget();
-		Image img = images.getTargetImage(s.getTarget(), primary);
+		Image img = ctl.images.getTargetImage(s.getTarget(), primary);
 		int xdcol = floor(col * dcol);
 		int ydrow = floor(row * drow);
 		g.drawImage(img, xdcol, ydrow, idcol, idrow, this);
@@ -120,7 +120,7 @@
         /* draw walls */
         for (int row = 0; row < dim; row++) {
             for (int col = 0; col < dim; col++) {
-                RRSquare s = board.getSquare(row, col);
+                RRSquare s = ctl.board.getSquare(row, col);
                 for (int w = 0; w < 4; w++) {
                     if (!s.getWall(w))
                         continue;
@@ -150,11 +150,11 @@
         /* draw robots */
         for (int row = 0; row < dim; row++) {
             for (int col = 0; col < dim; col++) {
-                RRSquare s = board.getSquare(row, col);
+                RRSquare s = ctl.board.getSquare(row, col);
                 int r = s.getRobot();
                 if (r == RRSquare.NO_ROBOT)
                     continue;
-                Image img = images.getRobotImage(r);
+                Image img = ctl.images.getRobotImage(r);
 		int col_dcol = floor(col * dcol);
 		int row_drow = 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.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- RRClient.java	8 Jun 2003 10:31:23 -0000	1.5
+++ RRClient.java	9 Jun 2003 01:54:45 -0000	1.6
@@ -28,31 +28,30 @@
 	    System.err.println("connection error");
 	    return;
 	}
-        RRBoard board;
-	RRNetIn netin;
-	JPanel bp;
+	RRController ctl = new RRController();
 	try {
-	    RRNetOut netout = new RRNetOut(s);
-	    board = new RRBoard(netout);
-	    RRImages images = new RRImages(this);
-	    bp = new RRBoardPanel(board, images);
-	    netin = new RRNetIn(s, board, bp);
+	    ctl.netout = new RRNetOut(s, ctl);
+	    ctl.board = new RRBoard(ctl, null);
+	    ctl.images = new RRImages(this);
+	    ctl.boardPanel = new RRBoardPanel(ctl);
+	    ctl.netin = new RRNetIn(s, ctl);
 	} catch (IOException e) {
 	    System.err.println("could not connect to host");
 	    return;
 	}
-        getContentPane().add(bp, BorderLayout.CENTER);
-	netin.start();
+        getContentPane().add(ctl.boardPanel, BorderLayout.CENTER);
+	ctl.netin.start();
     }
 
     public static void main(String[] args)
       throws IOException {
     	Socket s = new Socket(args[0], 5252);
-	RRNetOut netout = new RRNetOut(s);
-        RRBoard board = new RRBoard(netout);
-        RRImages images = new RRImages(Toolkit.getDefaultToolkit());
-        JPanel bp = new RRBoardPanel(board, images);
-	RRNetIn netin = new RRNetIn(s, board, bp);
+	RRController ctl = new RRController();
+	ctl.netout = new RRNetOut(s, ctl);
+	ctl.board = new RRBoard(ctl, args[1]);
+	ctl.images = new RRImages(Toolkit.getDefaultToolkit());
+	ctl.boardPanel = new RRBoardPanel(ctl);
+	ctl.netin = new RRNetIn(s, ctl);
         JFrame f = new JFrame("Ricochet Robots");
         f.addWindowListener(new WindowAdapter() {
                 public void windowClosing(WindowEvent e) {
@@ -60,14 +59,14 @@
                 }
             });
 
-        f.getContentPane().add(bp, BorderLayout.CENTER);
+        f.getContentPane().add(ctl.boardPanel, BorderLayout.CENTER);
         f.setSize(new Dimension(496,496));
         f.setVisible(true);
 
-	netin.start();
-	netout.hello(args[1]);
-	netout.watch(args[2]);
-	netout.show();
+	ctl.netin.start();
+	ctl.netout.hello(args[1]);
+	ctl.netout.watch(args[2]);
+	ctl.netout.show();
     }
 
 }

Index: RRNetIn.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRNetIn.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- RRNetIn.java	8 Jun 2003 10:31:23 -0000	1.2
+++ RRNetIn.java	9 Jun 2003 01:54:45 -0000	1.3
@@ -66,13 +66,11 @@
 public class RRNetIn
   extends Thread {
     Lexer in;
-    RRBoard board;
-    JPanel panel;
+    RRController ctl;
     
-    public RRNetIn(Socket s, RRBoard board, JPanel panel)
+    public RRNetIn(Socket s, RRController ctl)
       throws IOException {
-	this.board = board;
-	this.panel = panel;
+	this.ctl = ctl;
 	InputStream si = s.getInputStream();
 	InputStreamReader sr = new InputStreamReader(si);
 	BufferedReader sb = new BufferedReader(sr);
@@ -88,8 +86,78 @@
 	  throws IOException {
 	    if (notice.length != 2 || !notice[0].equals("SHOW"))
 		return false;
-	    board.become(notice[1]);
-	    panel.repaint();
+	    ctl.board.become(notice[1]);
+	    ctl.boardPanel.repaint();
+	    return true;
+	}
+    }
+
+    class TurnHandler implements NoticeHandler {
+	public boolean match(String[] notice) {
+	    if (notice.length != 4 ||
+		!notice[0].equals("NOTICE") ||
+		!notice[1].equals("TURN"))
+		return false;
+	    ctl.netout.show();
+	    return true;
+	}
+    }
+
+    class ActiveHandler implements NoticeHandler {
+	public boolean match(String[] notice) {
+	    if (notice.length != 4 ||
+		!notice[0].equals("NOTICE") ||
+		!notice[1].equals("ACTIVE"))
+		return false;
+	    if (!notice[2].equals(ctl.board.playerName)) {
+		ctl.board.setActive(false);
+		return true;
+	    }
+	    ctl.board.setActive(true);
+	    return true;
+	}
+    }
+
+    class DoneHandler implements NoticeHandler {
+	public boolean match(String[] notice) {
+	    if (notice.length != 3 ||
+		!notice[0].equals("NOTICE") ||
+		!notice[1].equals("GAMESTATE") ||
+		!notice[2].equals("DONE"))
+		return false;
+	    ctl.board.setActive(false);
+	    return true;
+	}
+    }
+
+    class ResetHandler implements NoticeHandler {
+	public boolean match(String[] notice) {
+	    if (notice.length != 1 ||
+		!notice[0].equals("RESET"))
+		return false;
+	    ctl.netout.show();
+	    return true;
+	}
+    }
+
+    static  int lookup_color(String name) {
+	for (int i = 0; i < RRSquare.colorname.length; i++)
+	    if (RRSquare.colorname[i].equals(name))
+		return i;
+	return -1;
+    }
+
+    class PositionHandler implements NoticeHandler {
+	public boolean match(String[] notice) {
+	    if (notice.length != 5 ||
+		!notice[0].equals("NOTICE") ||
+		!notice[1].equals("POSITION"))
+		return false;
+	    RRBoardCoord coord =
+		new RRBoardCoord(Integer.parseInt(notice[4]),
+				 Integer.parseInt(notice[3]));
+	    ctl.board.moveRobot(lookup_color(notice[2]), coord);
+	    ctl.boardPanel.repaint();
 	    return true;
 	}
     }
@@ -107,11 +175,14 @@
 	}
     }
 
+
     NoticeHandler handlers[] = {
 	new ShowHandler(),
-	// new TurnHandler(),
-	// new ActiveHandler(),
-	// new PositionHandler(),
+	new TurnHandler(),
+	new PositionHandler(),
+	new ActiveHandler(),
+	new DoneHandler(),
+	new ResetHandler(),
 	new DefaultHandler()
     };
 

Index: RRNetOut.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRNetOut.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- RRNetOut.java	8 Jun 2003 10:31:23 -0000	1.2
+++ RRNetOut.java	9 Jun 2003 01:54:45 -0000	1.3
@@ -12,6 +12,7 @@
 
 public class RRNetOut {
     PrintStream out;
+    RRController ctl;
     
     /* XXX Doesn't belong here */
     public String[] dirname = {
@@ -40,8 +41,9 @@
 	out.print(s);
     }
 
-    public RRNetOut(Socket s)
+    public RRNetOut(Socket s, RRController ctl)
       throws IOException, UnsupportedEncodingException {
+	this.ctl = ctl;
 	OutputStream so = s.getOutputStream();
 	BufferedOutputStream sb = new BufferedOutputStream(so);
 	out = new PrintStream(sb, true, "UTF-8");




More information about the Commit mailing list