Advertisement
Guest User

Untitled

a guest
May 5th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. public boolean checkSwarm() {
  2. List<Coordinate> needToCheck = new ArrayList<>();
  3. Set<Coordinate> checkedPieces = new HashSet<>();
  4. for (Piece piece : boardPieces) {
  5. needToCheck.add(piece.getCoordinate());
  6. }
  7.  
  8. int numberofpieces = needToCheck.size();
  9. checkedNeigbours(checkedPieces, needToCheck, needToCheck.get(0));
  10. return numberofpieces == checkedPieces.size() || numberofpieces == 1;
  11. }
  12.  
  13. private void checkedNeigbours(Set<Coordinate> checkedPieces, List<Coordinate> needToCheck, Coordinate coordinate) {
  14. for (Coordinate surrounding : coordinate.getNeighbours()) {
  15. if (needToCheck.remove(surrounding)) {
  16. checkedPieces.add(surrounding);
  17. checkedNeigbours(checkedPieces, needToCheck, surrounding);
  18. }
  19. }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement