From efe3b3a02feaa134125972ffe6fd621200f87899 Mon Sep 17 00:00:00 2001 From: adash Date: Sat, 7 Mar 2009 03:49:52 +0000 Subject: [PATCH] Working and stable version of Java --- .../Distributed/RainForest/java/Player.java | 9 +- .../RainForest/java/RainForestClient.java | 145 +++++++++++------- .../java/RainForestServerExample.java | 7 +- 3 files changed, 95 insertions(+), 66 deletions(-) diff --git a/Robust/src/Benchmarks/Distributed/RainForest/java/Player.java b/Robust/src/Benchmarks/Distributed/RainForest/java/Player.java index 66704b14..1931b38c 100644 --- a/Robust/src/Benchmarks/Distributed/RainForest/java/Player.java +++ b/Robust/src/Benchmarks/Distributed/RainForest/java/Player.java @@ -19,7 +19,6 @@ public class Player { private int state; private int goalx, goaly; private int rows, cols; - private Random rand; private char action; public Player(int type, int x, int y) { @@ -50,11 +49,10 @@ public class Player { this.state = 0; //0 => INIT state this.goalx = 0; this.goaly = 0; - rand = new Random(30); //seed to generate random numbers this.action = 'M'; //initalized to moving } - public void reset(GameMap[][] land, int row, int col, int bounds) { + public void reset(GameMap[][] land, int row, int col, int bounds, Random rand) { //Teleport to new location if(type == 1) { //PLANTER x = (rand.nextInt(Math.abs(row - 2) + 1)) + 1; @@ -118,9 +116,7 @@ public class Player { this.y = y; } - //public void setNewPosition(int x, int y, int row, int col, int bounds) { public void setNewPosition(int row, int col, int bounds) { - //setPosition(x, y); setBoundary(bounds, row, col); goalx = -1; goaly = -1; @@ -148,14 +144,13 @@ public class Player { /** Randomly finds a goal in a given boundary for player ** @return 0 on success and -1 when you cannot find any new goal **/ - public int findGoal(GameMap[][] land) { + public int findGoal(GameMap[][] land, Random rand) { /* Try setting the goal for try count times * if not possible, then select a completely new goal */ int trycount = (highx - lowx) + (highy - lowy); int i; - Random rand = new Random(0); for (i = 0; i < trycount; i++) { int row = (rand.nextInt(Math.abs(highx - lowx)) + 1) + lowx; int col = (rand.nextInt(Math.abs(highy - lowy)) + 1) + lowy; diff --git a/Robust/src/Benchmarks/Distributed/RainForest/java/RainForestClient.java b/Robust/src/Benchmarks/Distributed/RainForest/java/RainForestClient.java index 2d76e454..888e150f 100644 --- a/Robust/src/Benchmarks/Distributed/RainForest/java/RainForestClient.java +++ b/Robust/src/Benchmarks/Distributed/RainForest/java/RainForestClient.java @@ -1,5 +1,5 @@ -#define ROW 200 /* columns in the map */ -#define COLUMN 200 /* rows of in the map */ +#define ROW 100 /* columns in the map */ +#define COLUMN 100 /* rows of in the map */ #define ROUNDS 200 /* Number of moves by each player */ #define PLAYERS 20 /* Number of Players when num Players != num of client machines */ #define RATI0 0.5 /* Number of lumberjacks to number of planters */ @@ -27,7 +27,7 @@ public class RainForestClient { Random rand = new Random(seed); RainForestClient rfc = new RainForestClient(); - Socket sock = new Socket("dw-8.eecs.uci.edu",9002); + Socket sock = new Socket("dc-1.calit2.uci.edu",9002); SocketInputStream si=new SocketInputStream(sock); /* Read player type from Server */ @@ -44,7 +44,7 @@ public class RainForestClient { // Barriers for syncronization Barrier barr; - barr = new Barrier("128.195.175.79"); + barr = new Barrier("128.195.136.162"); //Generate a random x and y coordinate to start with int maxValue = ROW - 1; @@ -55,9 +55,9 @@ public class RainForestClient { Player gamer = new Player(person, row, col, ROW, COLUMN, BLOCK); // - //Debug + // Debug + // System.println("Person= "+person+" LocX= "+row+" LocY= "+col); // - /* System.println("Person= "+person+" LocX= "+row+" LocY= "+col); */ GameMap[][] land = new GameMap[ROW][COLUMN]; for(int i = 0; i= ROW && posY >= COLUMN) { - System.println("b[0] = "+(char)b[0]+" b[1]= "+(char)b[1]+" b[2]= "+(char)b[2]+" b[3]= "+(char)b[3]+" b[4]= "+(char)b[4]+" b[5]= "+(char)b[5]+" b[6]= "+(char)b[6]+" b[7]= "+(char)b[7]+ " b[8]= " +(char)b[8]); + System.println("Error: Trying to access elements out of bounds in the array"); } if(b[0] == (byte) 'T') { @@ -176,7 +209,7 @@ public class RainForestClient { ** @param gamer The player making the move ** @return 0 is success , -1 otherwise **/ - public int doOneMove(GameMap[][] land, Player gamer, Socket sock) { + public int doOneMove(GameMap[][] land, Player gamer, Socket sock, Random rand) { // 1. Get start(x, y) position of the player int currx = gamer.getX(); int curry = gamer.getY(); @@ -194,8 +227,8 @@ public class RainForestClient { //gamer.debugPlayer(); - if (gamer.findGoal(land) < 0) { - gamer.reset(land, ROW, COLUMN, BLOCK); + if (gamer.findGoal(land, rand) < 0) { + gamer.reset(land, ROW, COLUMN, BLOCK, rand); gamer.action = 'M'; sock.write(fillBytes(SHIFT, 0, 0)); return 0; @@ -222,7 +255,7 @@ public class RainForestClient { // System.println("Path from ("+currx+","+curry+") to ("+gamer.getGoalX()+","+gamer.getGoalY()+") is null"); // - gamer.reset(land, ROW, COLUMN, BLOCK); + gamer.reset(land, ROW, COLUMN, BLOCK, rand); gamer.setState(INIT); gamer.action = 'M'; sock.write(fillBytes(SHIFT, 0, 0)); diff --git a/Robust/src/Benchmarks/Distributed/RainForest/java/RainForestServerExample.java b/Robust/src/Benchmarks/Distributed/RainForest/java/RainForestServerExample.java index 094852d7..df1a5400 100644 --- a/Robust/src/Benchmarks/Distributed/RainForest/java/RainForestServerExample.java +++ b/Robust/src/Benchmarks/Distributed/RainForest/java/RainForestServerExample.java @@ -1,5 +1,5 @@ -#define ROW 200 /* columns in the map */ -#define COLUMN 200 /* rows of in the map */ +#define ROW 100 /* columns in the map */ +#define COLUMN 100 /* rows of in the map */ public class RainForestServerExample { private int numThreads; @@ -69,8 +69,9 @@ public class RainForestServerExample { rft[i].join(); } + ss.close(); System.printString("Finished\n"); - //System.exit(0); + System.exit(0); } /** -- 2.34.1