Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public boolean checkSwarm() {
- List<Coordinate> needToCheck = new ArrayList<>();
- Set<Coordinate> checkedPieces = new HashSet<>();
- for (Piece piece : boardPieces) {
- needToCheck.add(piece.getCoordinate());
- }
- int numberofpieces = needToCheck.size();
- checkedNeigbours(checkedPieces, needToCheck, needToCheck.get(0));
- return numberofpieces == checkedPieces.size() || numberofpieces == 1;
- }
- private void checkedNeigbours(Set<Coordinate> checkedPieces, List<Coordinate> needToCheck, Coordinate coordinate) {
- for (Coordinate surrounding : coordinate.getNeighbours()) {
- if (needToCheck.remove(surrounding)) {
- checkedPieces.add(surrounding);
- checkedNeigbours(checkedPieces, needToCheck, surrounding);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement