Exemplo n.º 1
0
 public char[] nesw(int p, int q) {
   // given position (p,q) (p across, q down), which of its possible neighbors exist? (weeds out
   // walls, corners)
   char[] neswT = new char[4];
   // boolean true/false: position 0 = can you go north, position 1 = can you go east, etc
   //
   java.util.Arrays.fill(neswT, '0');
   if (p > 0) {
     neswT[3] = '1';
     // validMoves =+ (p-1,q);
     // you can go west
   }
   if (q > 0) {
     // you can go north
     neswT[0] = '1';
     // validMoves += (p, q-1);
   }
   if (p < this.x) {
     neswT[1] = '1';
     // you can go east
     // validMoves =+ (p+1, q)
   }
   if (q < this.y) {
     neswT[2] = '1';
     // you can go south
     // valid moves include (p, q+1)
   }
   return neswT;
 }
Exemplo n.º 2
0
  private String dummyMaze() {
    // x*y grid of all 1's.
    int a = this.x * 2 + 1;
    int b = this.y * 2 + 1;
    int c = a * b;

    char[] out = new char[c];
    java.util.Arrays.fill(out, '1');
    out[3] = '0';
    return new String(out);
  }
  @RequestMapping(value = CLIENT_CONNECT_PATH, method = RequestMethod.POST)
  public @ResponseBody int connectClient(@RequestBody String allVid) {
    try {
      int reply = FAILED;
      String[] videos = allVid.split(",");
      String uName = videos[0].trim();
      videos = java.util.Arrays.copyOfRange(videos, 1, videos.length);
      // System.out.println("Client connect"+hostAdder+" "+uName+" "+ Arrays.asList(videos));
      int ans = masterService.psConnectClient(hostAdder, uName, videos);
      // System.out.println("ans =" +ans +" "+FAILED);
      while (ans == PS_NOT_CONNECTED) {
        reconnectToMS();
        ans = masterService.psConnectClient(hostAdder, uName, videos);
      }
      if (ans == FAILED) return FAILED;
      // System.out.println("Clinet "+ uName + " connected");

      if (user_vidNameMap.containsKey(uName)) {
        reply = CLIENT_ALREADY_CONNECTED;
      } else {
        reply = CLIENT_CONNECTED;
        user_vidNameMap.put(uName, new HashSet<String>());
      }
      // System.out.println("Clinet "+ uName + " connected");

      Set<String> vidSet = user_vidNameMap.get(uName);
      for (int i = 0; i < videos.length; i++) {
        String temp = videos[i].trim();
        // System.out.println("add video");

        if (!temp.equals("")) {
          vidSet.add(temp);
          addTovidName_UserMap(uName, temp);
        }
      }
      // System.out.println("Clinet "+ uName + " connected");

      userAliveMap.put(uName, new Long(System.currentTimeMillis() + TTL));
      // System.out.println("Clinet "+ uName + " connected");

      activeUsers.add(uName);
      System.out.println("Clinet " + uName + " connected");
      return reply;
    } catch (Exception e) {
      System.out.println("Error: " + e.getMessage());
      return FAILED;
    }
  }
Exemplo n.º 4
0
  private String fullMaze() {
    int a = this.x;
    int b = this.y;
    // Creates a maze with all possible walls present, of the dimensions a x b
    String out = "";

    if (a < 1 || b < 1) {
      Error e = new Error("Maze too small");
      throw e;
    }
    a = (a * 2) + 1;
    b = (b * 2) + 1;
    // A 4x4 maze has 9 coordinates by 9 coordinates, for example.

    char[] topchars = new char[a];
    java.util.Arrays.fill(topchars, '1');
    String top = new String(topchars);

    out += top;
    b--;
    // We've now created the top row;
    while (b > 0) {
      if (b % 2 == 0) {
        // if B is even, that means we are on a row when the walls look like this: |
        // alternate 1's and 0's
        char[] row = new char[a];
        for (int i = a; i > 0; i = i - 2) {
          //	System.out.println("i is" + i);
          row[i - 1] = '1';
        }
        for (int i = a - 1; i > 0; i = i - 2) {
          row[i - 1] = '0';
        }

        out += new String(row);

      } else { // we are on a row where the walls look like this: -
        out += top;
      }

      b--;
    }

    return out;
  }