示例#1
0
 private void addWallToBoard(int idx, Location stPos, int len, String dir) {
   // TODO Auto-generated method stub
   Wall aw = new Wall();
   aw.wallIndex = idx;
   Location vertex1 = stPos;
   Location vertex2 = new Location();
   // aw.leftEnd = stPos;
   CardinalDirections cd = CardinalDirections.getCardinalFromString(dir);
   switch (cd) {
     case N:
       vertex2.xloc = vertex1.xloc;
       vertex2.yloc = vertex1.yloc - len;
       break;
     case S:
       vertex2.xloc = vertex1.xloc;
       vertex2.yloc = vertex1.yloc + len;
       break;
     case E:
       vertex2.xloc = vertex1.xloc + len;
       vertex2.yloc = vertex1.yloc;
       break;
     default: // W
       vertex2.xloc = vertex1.xloc - len;
       vertex2.yloc = vertex1.yloc;
       break;
   }
   aw.leftEnd = vertex1;
   aw.rightEnd = vertex2;
   board._walls.add(aw);
 }
示例#2
0
  public synchronized void parsePublisherMessage(String message) {
    message.replace("false", "\"false\"");
    message.replace("true", "\"true\"");
    try {
      JSONObject jsonObject = new JSONObject();
      try {
        Object obj = parser.parse(message);
        jsonObject = (JSONObject) obj;
      } catch (ParseException pe) {
        System.out.println(pe);
      }

      boolean gameOver = (Boolean) jsonObject.get("gameover");
      if (gameOver) {
        System.out.println("Gameover from publisher.. Exiting..");
        System.exit(0);
      }

      JSONArray preyCoordinates = (JSONArray) jsonObject.get("prey");
      System.out.println(preyCoordinates);
      if (!board._prey.pl.isEqual(jsonArrayToLocation(preyCoordinates))) {
        System.out.println(
            "What is this Hannan?? You are telling me that I am not where I am, huh?");
      }
      board._prey.pl = jsonArrayToLocation(preyCoordinates);
      // System.out.println("px: " + board._prey.pl.xloc);

      JSONArray hunterCoordinates = (JSONArray) jsonObject.get("hunter");
      // System.out.println(hunterCoordinates);
      board._hunter.hl = jsonArrayToLocation(hunterCoordinates);
      // System.out.println("hx: " + board._hunter.hl.xloc);

      String huntDirection = (String) jsonObject.get("hunterDir");
      board._hunter.hunterDirection = CardinalDirections.getCardinalFromString(huntDirection);

      long timeL = (Long) jsonObject.get("time");
      board.time = (int) timeL;

      updateWalls((JSONArray) jsonObject.get("walls"));

      //            clearBoardWalls();
      //            JSONArray walls = (JSONArray) jsonObject.get("wall");
      //            for (Object wobj : walls) {
      //            	JSONObject jobj = (JSONObject) wobj;
      //            	int idx = (int)jobj.get("id");
      //            	Location stPos = jsonArrayToLocation((JSONArray)jobj.get("position"));
      //            	int len = (int) jobj.get("length");
      //            	String dir = (String)jobj.get("direction");
      //            	addWallToBoard(idx, stPos, len, dir);
      //            }
    } catch (Exception e) {
      System.out.println(e);
      e.printStackTrace();
    }
  }