public int compare(State left, State right) { if (left.getX() < right.getX()) { return -1; } else if (left.getX() > right.getX()) { return 1; } else if (left.getY() < right.getY()) { return -1; } else if (left.getY() > right.getY()) { return 1; } else { return 0; } }
public boolean isEquals(State test) { if ((x == test.getX()) && (y == test.getY())) { return true; } else { return false; } }
public void drawPath() { State currentState = goalState; while (currentState.parent != null) { newImage.setRGB(currentState.getX(), currentState.getY(), 0xFFFF0000); currentState = currentState.parent; } ; }
public int findPath(Logger logger) { int x; int y; int cost = 0; Color c; State tempState; int iterations = 0; stateQueue.add(startState); beenThere.add(startState); while (stateQueue.size() > 0) { State currentState = stateQueue.remove(); if (currentState.isEquals(goalState)) { goalState.parent = currentState.parent; return currentState.getCost(); } x = currentState.getX(); y = currentState.getY(); if (iterations++ % 5000 < 1000) { newImage.setRGB(x, y, 0xff00ff00); } else { newImage.setRGB(x, y, board.getRGB(x, y)); } // look up if (x != 0) { State upState = new State(x - 1, y); c = new Color(board.getRGB(x - 1, y)); cost = c.getGreen(); if (beenThere.contains(upState)) { tempState = beenThere.floor(upState); if (currentState.getCost() + cost < tempState.getCost()) { tempState.setCost(currentState.getCost() + cost); tempState.parent = currentState; } } else { upState.setCost(currentState.getCost() + cost); upState.parent = currentState; stateQueue.add(upState); beenThere.add(upState); } } // look down if (x < (height - 1)) { State downState = new State(x + 1, y); c = new Color(board.getRGB(x + 1, y)); cost = c.getGreen(); if (beenThere.contains(downState)) { tempState = beenThere.floor(downState); if (currentState.getCost() + cost < tempState.getCost()) { tempState.setCost(currentState.getCost() + cost); tempState.parent = currentState; } } else { downState.setCost(currentState.getCost() + cost); downState.parent = currentState; stateQueue.add(downState); beenThere.add(downState); } } // look left if (y != 0) { State leftState = new State(x, y - 1); c = new Color(board.getRGB(x, y - 1)); cost = c.getGreen(); if (beenThere.contains(leftState)) { tempState = beenThere.floor(leftState); if (currentState.getCost() + cost < tempState.getCost()) { tempState.setCost(currentState.getCost() + cost); tempState.parent = currentState; } } else { leftState.setCost(currentState.getCost() + cost); leftState.parent = currentState; stateQueue.add(leftState); beenThere.add(leftState); } } if (y < (width - 1)) { State rightState = new State(x, y + 1); c = new Color(board.getRGB(x, y + 1)); cost = c.getGreen(); if (beenThere.contains(rightState)) { tempState = beenThere.floor(rightState); if (currentState.getCost() + cost < tempState.getCost()) { tempState.setCost(currentState.getCost() + cost); tempState.parent = currentState; } } else { rightState.setCost(currentState.getCost() + cost); rightState.parent = currentState; stateQueue.add(rightState); beenThere.add(rightState); } } } return -1; }