public boolean isLegalMove(Position position) {
    boolean add = true;
    if (isInside(position) && get(position) != '%'){
        for (Position ghostPosition: this.findGhosts()) {
            if (position.equals(ghostPosition)) add = false;
        }
    } else {
        add = false;
    }
    return add;
}

public PacmanState move(Position to){
    char[][] newBoard = copyBoard();
    PacmanState newState = new PacmanState(newBoard, to, this.ghostPositions);
    newState.set(to, ' ');
    newState.score = this.score + 1;
    return newState;
}

public boolean isFinalState(){
    return this.findFoods().size() == 0 || overlapWithGhost();
}

private boolean overlapWithGhost(){
    for (Position position: this.ghostPositions) {
        if (position.equals(this.pacmanPosition)) return true;
    }
    return false;
}

public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    PacmanState that = (PacmanState) o;
    return Arrays.deepEquals(board, that.board) && pacmanPosition.equals(that.pacmanPosition) &&
            Arrays.deepEquals(this.ghostPositions, that.ghostPositions);
}

public int hashCode() {
    int result = pacmanPosition.hashCode();
    result = 31 * result + Arrays.deepHashCode(board) + Arrays.deepHashCode(this.ghostPositions);
    return result;
}