Example #1
0
  @Override
  public Board.Path requestPlayerMove(
      Player player, Board board, Set<Location<Integer>> blockedLocations, int distance) {
    this.out.printf("Enter a move of length %d.\n", distance);
    this.out.printf(
        "Moves are in the format UDLR, where such a move would mean go up, then down, then left, then right.\n\n");

    List<Direction> moveSequence = new ArrayList<>();

    String charSequence = this.scanner.next();

    if (charSequence.length() != distance) {
      return this.requestPlayerMove(player, board, blockedLocations, distance);
    }

    for (int i = 0; i < charSequence.length(); i++) {
      char c = charSequence.charAt(i);
      Direction dir = Direction.fromCharacter(c);
      if (dir == null) {
        return this.requestPlayerMove(player, board, blockedLocations, distance);
      }
      moveSequence.add(dir);
    }

    Optional<Board.Path> path =
        board.directionsToPath(player.location(), moveSequence, blockedLocations);

    return path.isPresent()
        ? path.get()
        : this.requestPlayerMove(player, board, blockedLocations, distance);
  }
Example #2
0
  @Override
  public void showGame(
      Game game, Set<Location<Integer>> blockedLocations, Map<Room, Weapon> weaponLocations) {
    Board board = game.board;

    char[][] buffer = new char[2 * (board.width + 1)][2 * (board.height + 1)];
    for (char[] line : buffer) {
      Arrays.fill(line, ' ');
    }

    for (int x = 0; x < board.width; x++) {
      for (int y = 0; y < board.height; y++) {
        // The actual tile is at buffer[2x + 1][2y + 1]
        // Walls: above is [2x + 1][2y], left is [2x][2y + 1]

        if (board.hasWallBetween(
            new Location<>(x, y), new Location<>(x, y - 1))) { // if there's a wall above the tile.
          buffer[2 * x + 1][2 * y] = '-';
        }
        if (board.hasWallBetween(
            new Location<>(x, y),
            new Location<>(x - 1, y))) { // if there's a wall to the left of the tile.
          buffer[2 * x][2 * y + 1] = '|';
        }
      }
    }
    for (int x = 0; x < board.width; x++) {
      buffer[2 * x + 1][2 * board.height] = '-';
    }
    for (int y = 0; y < board.height; y++) {
      buffer[2 * board.width][2 * y + 1] = '|';
    }

    for (Room room : Room.values()) {
      Location<Float> centre = board.centreLocationForRoom(room);
      String name = room.shortName();

      int startX = 2 * centre.x.intValue() + 1 - name.length() / 2;
      int y = 2 * centre.y.intValue() + 1;

      for (int i = 0; i < +name.length(); i++) {
        buffer[startX + i][y] = name.charAt(i);
      }
    }

    for (Player player : game.allPlayers) {
      CluedoCharacter character = player.character;
      Location<Integer> location = player.location();
      buffer[2 * location.x + 1][2 * location.y + 1] = this.asciiIconForCharacter(character);
    }

    for (int y = 0; y < 2 * (board.height + 1); y++) {
      for (int x = 0; x < 2 * (board.width + 1); x++) {
        this.out.print(buffer[x][y]);
      }
      this.out.print('\n');
    }
  }