[Commit] RRClient RRNetIn.java,NONE,1.1 RRNetOut.java,NONE,1.1
RRBoard.java,1.5,1.6 RRBoardCoord.java,1.1,1.2
RRBoardPanel.java,1.6,1.7 RRClient.java,1.3,1.4
RRSquare.java,1.3,1.4
Bart Massey
commit at keithp.com
Sun Jun 8 03:02:59 PDT 2003
- Previous message: [Commit] RRClient RRBoardCoord.java,NONE,1.1 RRBoard.java,1.4,1.5
RRBoardPanel.java,1.5,1.6 RRSquare.java,1.2,1.3
- Next message: [Commit] RRClient RRBoard.java,1.6,1.7 RRClient.java,1.4,1.5
RRNetIn.java,1.1,1.2 RRNetOut.java,1.1,1.2
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
Committed by: bart
Update of /local/src/CVS/RRClient
In directory home.keithp.com:/tmp/cvs-serv27419
Modified Files:
RRBoard.java RRBoardCoord.java RRBoardPanel.java RRClient.java
RRSquare.java
Added Files:
RRNetIn.java RRNetOut.java
Log Message:
Started to work the networking into the board.
--- NEW FILE: RRNetIn.java ---
/*
* Ricochet Robots Server Monitor Connection
*/
/**
* @author Bart Massey <bart at cs.pdx.edu>
*
*/
import java.io.*;
import java.net.*;
import java.util.*;
public class RRNetIn
extends Thread {
StreamTokenizer in;
RRBoard board;
public RRNetIn(Socket s, RRBoard board)
throws IOException {
this.board = board;
InputStream si = s.getInputStream();
InputStreamReader sr = new InputStreamReader(si);
BufferedReader sb = new BufferedReader(sr);
in = new StreamTokenizer(sb);
in.slashSlashComments(false);
in.slashStarComments(false);
in.quoteChar('"');
in.lowerCaseMode(true);
in.eolIsSignificant(true);
}
interface NoticeHandler {
public boolean match(String[] notice) throws IOException;
}
class ShowHandler implements NoticeHandler {
public boolean match(String[] notice)
throws IOException {
if (notice[0] != "show")
return false;
board.become(notice[1]);
return true;
}
}
class DefaultHandler implements NoticeHandler {
public boolean match(String[] notice) {
System.out.println(notice);
return true;
}
}
NoticeHandler handlers[] = {
new ShowHandler(),
// new TurnHandler(),
// new ActiveHandler(),
// new PositionHandler(),
new DefaultHandler()
};
public void run() {
try {
while(true) {
Vector v = new Vector();
int n = 0;
while (true) {
int t = in.nextToken();
if (t == in.TT_EOF) {
if (n != 0)
throw new IOException("notice line ended in middle");
return;
}
if (t == in.TT_EOL)
break;
if (t == in.TT_NUMBER)
throw new Error("stream tokenizer returned number");
if (t == in.TT_WORD) {
switch (in.ttype) {
case '"':
case StreamTokenizer.TT_WORD:
v.add(in.sval);
break;
default:
throw new Error("unexpected token type");
}
}
n++;
}
String[] notice = new String[v.size()];
for (int i = 0; i < n; i++)
notice[i] = (String)v.elementAt(i);
for (int i = 0; i < handlers.length; i++)
if (handlers[i].match(notice))
break;
}
} catch (IOException e) {
System.err.println("I/O Exception in RRNetIn");
e.printStackTrace(System.err);
}
}
}
--- NEW FILE: RRNetOut.java ---
/*
* Ricochet Robots Output Server Connection
*/
/**
* @author Bart Massey <bart at cs.pdx.edu>
*
*/
import java.io.*;
import java.net.*;
public class RRNetOut {
PrintStream out;
/* XXX Doesn't belong here */
public String[] dirname = {
"north", "east", "south", "west"
};
void uniprint(String s) {
boolean isodd = false;
for (int i = 0; i < s.length(); i++) {
int c = s.charAt(i);
if (c >= 0x7f || c == '"') {
isodd = true;
break;
}
}
if (isodd) {
out.print('"');
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '"')
out.print('\\');
out.print(s.charAt(i));
}
out.print('"');
return;
}
out.print(s);
}
public RRNetOut(Socket s)
throws IOException, UnsupportedEncodingException {
OutputStream so = s.getOutputStream();
BufferedOutputStream sb = new BufferedOutputStream(so);
out = new PrintStream(sb, true, "UTF-8");
}
public void hello(String name) {
out.print("helo ");
uniprint(name);
out.println();
}
public void newGame(String game) {
out.print("new ");
uniprint(game);
out.println();
}
public void join(String game) {
out.print("join ");
uniprint(game);
out.println();
}
public void show() {
out.println("show");
}
public void bid(int b) {
out.print("bid ");
out.print(b);
out.println();
}
public void move(int color, int dir) {
out.println("move " +
RRSquare.colorname[color] +
dirname[dir]);
}
public void undo() {
out.println("undo");
}
public void reset() {
out.println("reset");
}
}
Index: RRBoard.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRBoard.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- RRBoard.java 8 Jun 2003 03:53:29 -0000 1.5
+++ RRBoard.java 8 Jun 2003 09:02:57 -0000 1.6
@@ -1,23 +1,23 @@
/*
* Ricochet Robots Board
- * Created on May 31, 2003
- *
*/
-import java.io.*;
-
/**
* @author Bart Massey <bart at cs.pdx.edu>
*
*/
+import java.io.*;
+import java.util.*;
+
public class RRBoard {
public static final int dim = 16;
- RRSquare[][] squares = new RRSquare[dim][dim];
- int row_goal = -1;
- int col_goal = -1;
+ volatile RRSquare[][] squares = new RRSquare[dim][dim];
+ volatile RRBoardCoord goal = null;
+ RRNetOut netout;
- public RRBoard() {
+ public RRBoard(RRNetOut netout) {
+ this.netout = netout;
for (int row = 0; row < dim; row++)
for (int col = 0; col < dim; col++)
squares[row][col] = new RRSquare();
@@ -29,6 +29,20 @@
}
}
+ public RRBoard(BufferedReader in, RRNetOut netout)
+ throws IOException {
+ this(netout);
+ become(in);
+ }
+
+
+ public RRBoard(String board, RRNetOut netout)
+ throws IOException {
+ this(netout);
+ become(board);
+ }
+
+
static void expect(int c, int ch)
throws IOException {
if (c != ch)
@@ -44,12 +58,8 @@
return s;
}
- void wall_line(BufferedReader in, int i)
+ void wall_line(String s, 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++);
@@ -76,9 +86,8 @@
}
}
- void target_line(BufferedReader in, int i)
+ void target_line(String s, 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++);
@@ -186,69 +195,68 @@
if (color >= 0) {
int symbol = 4 * color + shape;
squares[i][j].setTarget(symbol, goal_target);
- if (goal_target) {
- row_goal = i;
- col_goal = j;
- }
+ if (goal_target)
+ goal = new RRBoardCoord(i, j);
}
}
}
- public RRBoard(BufferedReader in)
+ synchronized public void become(BufferedReader in)
throws IOException {
- this();
+ goal = null;
for (int i = 0; i < dim; i++) {
- wall_line(in, i);
- target_line(in, i);
+ String s = next_line(in, 1);
+ wall_line(s, i);
+ s = next_line(in, 1);
+ target_line(s, i);
}
- wall_line(in, dim);
+ String s = next_line(in, 2);
+ wall_line(s, dim);
}
-
- public static RRBoard testBoard() {
- RRBoard b = new RRBoard();
- RRSquare s = b.squares[1][1];
- /* XXX is this still right? */
- for (int i = 7; i < 9; i++) {
- b.squares[9][i].setWall(0);
- b.squares[i][6].setWall(1);
- b.squares[6][i].setWall(2);
- b.squares[i][9].setWall(3);
+ synchronized public void become(String board)
+ throws IOException {
+ goal = null;
+ StringTokenizer st = new StringTokenizer(board, "\n");
+ for (int i = 0; i < dim; i++) {
+ String s = st.nextToken();
+ wall_line(s, i);
+ s = st.nextToken();
+ target_line(s, i);
}
- s.setWall(1);
- s.setWall(2);
- 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;
+ String s = st.nextToken();
+ wall_line(s, dim);
}
- public RRSquare getSquare(int row, int col) {
+
+ synchronized public RRSquare getSquare(int row, int col) {
return squares[row][col];
}
- public void moveRobot(RRBoardCoord start, RRBoardCoord end) {
+ synchronized public void moveRobot(RRBoardCoord start, RRBoardCoord end) {
/* anything to move? */
RRSquare ss = squares[start.row][start.col];
int robot = ss.getRobot();
if (robot == ss.NO_ROBOT)
return;
+ if (start.equals(end))
+ return;
/* figure out where it's going */
int dr = 0;
int dc = 0;
int dir = -1;
- if (start.row == end.row) {
- if (start.col < end.col) {
+ RRBoardCoord cur = new RRBoardCoord(start);
+ if (cur.row == end.row) {
+ if (cur.col < end.col) {
dir = 1;
dc = 1;
} else {
dir = 3;
dc = -1;
}
- } else if (start.col == end.col) {
- if (start.row < end.row) {
+ } else if (cur.col == end.col) {
+ if (cur.row < end.row) {
dir = 2;
dr = 1;
} else {
@@ -260,19 +268,24 @@
}
/* move it */
ss.clearRobot();
- while (!start.equals(end)) {
- RRSquare scur = squares[start.row][start.col];
+ while (!cur.equals(end)) {
+ RRSquare scur = squares[cur.row][cur.col];
if (scur.getRobot() != scur.NO_ROBOT) {
- start.row -= dr;
- start.col -= dc;
+ cur.row -= dr;
+ cur.col -= dc;
break;
}
if (scur.getWall(dir))
break;
- start.row += dr;
- start.col += dc;
+ cur.row += dr;
+ cur.col += dc;
}
- RRSquare se = squares[start.row][start.col];
+ /* deal with it */
+ if (cur.equals(start))
+ return;
+ RRSquare se = squares[cur.row][cur.col];
se.setRobot(robot);
+ netout.move(robot, dir);
}
+
}
Index: RRBoardCoord.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRBoardCoord.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- RRBoardCoord.java 8 Jun 2003 03:53:29 -0000 1.1
+++ RRBoardCoord.java 8 Jun 2003 09:02:57 -0000 1.2
@@ -12,6 +12,16 @@
public class RRBoardCoord {
public int row, col;
+ public RRBoardCoord(int row, int col) {
+ this.row = row;
+ this.col = col;
+ }
+
+ public RRBoardCoord(RRBoardCoord c) {
+ this.row = c.row;
+ this.col = c.col;
+ }
+
public boolean equals(BoardCoord b) {
if (b.row != row)
return false;
Index: RRBoardPanel.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRBoardPanel.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- RRBoardPanel.java 8 Jun 2003 03:53:29 -0000 1.6
+++ RRBoardPanel.java 8 Jun 2003 09:02:57 -0000 1.7
@@ -173,11 +173,7 @@
public RRBoardCoord boardCoord(Point p) {
update_dims();
-
- RRBoardCoord c = new RRBoardCoord();
- c.row = floor(p.y / drow);
- c.col = floor(p.x / dcol);
- return c;
+ return new RRBoardCoord(floor(p.y / drow), floor(p.x / dcol));
}
public void setHilite(RRBoardCoord c) {
Index: RRClient.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRClient.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- RRClient.java 7 Jun 2003 07:03:10 -0000 1.3
+++ RRClient.java 8 Jun 2003 09:02:57 -0000 1.4
@@ -13,11 +13,29 @@
import java.awt.*;
import java.awt.event.*;
import java.io.*;
+import java.net.*;
public class RRClient extends JApplet {
public void init() {
- RRBoard board = new RRBoard();
+ Socket s;
+ try {
+ s = new Socket("localhost", 5252);
+ } catch (UnknownHostException e) {
+ System.err.println("unknown host");
+ return;
+ } catch (IOException e) {
+ System.err.println("connection error");
+ return;
+ }
+ RRNetOut netout;
+ try {
+ netout = new RRNetOut(s);
+ } catch (IOException e) {
+ System.err.println("could not connect to host");
+ return;
+ }
+ RRBoard board = new RRBoard(netout);
RRImages images = new RRImages(this);
JPanel bp = new RRBoardPanel(board, images);
getContentPane().add(bp, BorderLayout.CENTER);
@@ -25,9 +43,11 @@
public static void main(String[] args)
throws IOException {
+ Socket s = new Socket("localhost", 5252);
+ RRNetOut netout = new RRNetOut(s);
FileReader fin = new FileReader(args[0]);
BufferedReader in = new BufferedReader(fin);
- RRBoard board = new RRBoard(in);
+ RRBoard board = new RRBoard(in, netout);
RRImages images = new RRImages(Toolkit.getDefaultToolkit());
JPanel bp = new RRBoardPanel(board, images);
JFrame f = new JFrame("Ricochet Robots");
Index: RRSquare.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRSquare.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- RRSquare.java 8 Jun 2003 03:53:29 -0000 1.3
+++ RRSquare.java 8 Jun 2003 09:02:57 -0000 1.4
@@ -18,6 +18,10 @@
public static final int WHIRLPOOL = 4;
public static final int NO_ROBOT = -1;
+ public static final String[] colorname = {
+ "red", "yellow", "green", "blue", "whirlpool"
+ };
+
/* see above */
int target = NO_TARGET;
/* R, Y, G, B */
- Previous message: [Commit] RRClient RRBoardCoord.java,NONE,1.1 RRBoard.java,1.4,1.5
RRBoardPanel.java,1.5,1.6 RRSquare.java,1.2,1.3
- Next message: [Commit] RRClient RRBoard.java,1.6,1.7 RRClient.java,1.4,1.5
RRNetIn.java,1.1,1.2 RRNetOut.java,1.1,1.2
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
More information about the Commit
mailing list