import java.util.*;

public class Node extends BoardItem {
  private static final int OCCUPIED_SCORE = 3;
  private static final int EMPTY_SCORE = 1;
  
  private int score;
  private int cost;
  private Node parent;
  private Environment env;
  
  public Node(int x, int y, Node parent, Environment env) {
    super(x, y);
    this.parent = parent;
    this.env = env;
    score = EMPTY_SCORE;
    if (env.getCondition(x, y) == Environment.OCCUPIED)
      score = OCCUPIED_SCORE;
    if (parent != null)
      score += parent.getScore();
  }

  public Node(int x, int y, Environment env) {
    this(x, y, null, env);
  }

  public ArrayList getSuccessors() {
    ArrayList result = new ArrayList();
    getNorthernNode().addIfPassable(result);
    getEasternNode().addIfPassable(result);
    getSouthernNode().addIfPassable(result);
    getWesternNode().addIfPassable(result);
    return result;
  }

  public int getScore() {
    return score;
  }

  public Node getParent() {
    return parent;
  }

  public boolean hasParent() {
    return parent != null;
  }

  public Node getNorthernNode() {
    return new Node(getX(), getY() + 1, this, env);
  }

  public Node getEasternNode() {
    return new Node(getX() + 1, getY(), this, env);
  }

  public Node getSouthernNode() {
    return new Node(getX(), getY() - 1, this, env);
  }

  public Node getWesternNode() {
    return new Node(getX() - 1, getY(), this, env);
  }

  public void addIfPassable(ArrayList list) {
    char c = env.getCondition(x, y);
    if (c == Environment.EMPTY || c == Environment.BASE)
      list.add(this);
  }
}
