コード例 #1
0
ファイル: Unicorn.java プロジェクト: jpverkamp/chesslike
  /**
   * Check if a capture would be valid if there's something there to capture.
   *
   * @param x The x of the location to capture at.
   * @param y The y of the location to capture at.
   * @return If we can capture it.
   */
  @Override
  public boolean validCapture(int x, int y) {
    // Not even possible.
    if (!World.getTile(x, y).IsWalkable) return false;

    // Cannot move onto anyone
    Actor a = World.getActorAt(x, y);
    if (a != this && a.Team == Team) return false;

    // Self valid.
    if (x == Location.x && y == Location.y) return true;

    // Any multiple of 2/1 leaps
    int xdiff = Math.abs(x - Location.x);
    int ydiff = Math.abs(y - Location.y);
    if (!(xdiff == ydiff * 2 || xdiff * 2 == ydiff)) return false;

    // Make sure we didn't hit anything
    int xd = (x > Location.x ? 1 : -1) * (xdiff > ydiff ? 2 : 1);
    int yd = (y > Location.y ? 1 : -1) * (xdiff > ydiff ? 1 : 2);
    for (int xi = Location.x, yi = Location.y; xi != x && yi != y; xi += xd, yi += yd) {
      Actor a2 = World.getActorAt(xi, yi);
      if (!World.getTile(xi, yi).IsWalkable || (a2 != this && a2 != null)) return false;
    }

    // Passed all tests!
    return true;
  }
コード例 #2
0
ファイル: Rook.java プロジェクト: jpverkamp/chesslike
  /**
   * Check if a capture would be valid if there's something there to capture.
   *
   * @param x The x of the location to capture at.
   * @param y The y of the location to capture at.
   * @return If we can capture it.
   */
  @Override
  public boolean validCapture(int x, int y) {
    // Not even possible.
    if (!World.getTile(x, y).IsWalkable || !(x == Location.x || y == Location.y)) return false;

    // Team capture.
    Actor that = World.getActorAt(x, y);
    if (that == null || this.Team == that.Team) return false;

    // Self valid.
    if (x == Location.x && y == Location.y) return true;

    // Maximum range
    if (Math.abs(x - Location.x) > 4 || Math.abs(y - Location.y) > 4) return false;

    // Check the path.
    int xd = x == Location.x ? 0 : x > Location.x ? 1 : -1;
    int yd = y == Location.y ? 0 : y > Location.y ? 1 : -1;
    for (int xi = Location.x + xd, yi = Location.y + yd;
        !(xi == x && yi == y);
        xi += xd, yi += yd) {
      if (xi == x && yi == y) continue;

      Tile t = World.getTile(xi, yi);
      Actor a = World.getActorAt(xi, yi);
      if (!t.IsWalkable || a != null) return false;
    }

    return true;
  }