Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. package nl.groep4.kvc.server.util;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Set;
  7.  
  8. import nl.groep4.kvc.common.interfaces.Player;
  9. import nl.groep4.kvc.common.map.Street;
  10. import nl.groep4.kvc.common.util.CollectionUtil;
  11.  
  12. /**
  13. * A road object, used only for {@link RoadFinder}
  14. *
  15. * @author MakerTim
  16. * @version 1.0
  17. *
  18. */
  19. class Road {
  20.  
  21. private Set<Street> streets;
  22.  
  23. /**
  24. * Constructs a road
  25. *
  26. * @param streets
  27. * a set of streets
  28. */
  29. public Road(Set<Street> streets) {
  30. this.streets = streets;
  31. }
  32.  
  33. /**
  34. * Gets the owner of this road
  35. *
  36. * @return the Player from who the road is
  37. */
  38. public Player getOwner() {
  39. return CollectionUtil.first(streets).getOwner();
  40. }
  41.  
  42. /**
  43. * Check if the given street is inside this road
  44. *
  45. * @param street
  46. * the street to check
  47. * @return returns true if the street is resent
  48. */
  49. public boolean hasStreet(Street street) {
  50. return streets.contains(street);
  51. }
  52.  
  53. /**
  54. * Gets all roads on the outside of a path<br>
  55. * <b>note that branches of size 1 are not counted as corner</b>
  56. *
  57. * @return a set of streets that are at the end of a path inside the road
  58. */
  59. public Set<Street> getCorners() {
  60. Set<Street> corners = new HashSet<>();
  61. for (Street street : streets) {
  62. if (neighbours(street).size() < 2) {
  63. corners.add(street);
  64. }
  65. }
  66. if (corners.isEmpty()) {
  67. corners = new HashSet<>(streets);
  68. }
  69. return corners;
  70. }
  71.  
  72. /**
  73. * Calculates the longest path inside the road
  74. *
  75. * @return the longest road
  76. */
  77. public int getLongestPath() {
  78. int lenght = 0;
  79. for (Street corner : getCorners()) {
  80. lenght = Math.max(roadsFind(corner, new HashSet<>(), 0), lenght);
  81. }
  82. return lenght;
  83. }
  84.  
  85. private int roadsFind(Street toFind, Set<Street> visited, int length) {
  86. visited.add(toFind);
  87. for (Street neighbour : neighbours(toFind)) {
  88. if (!visited.contains(neighbour)) {
  89. length = Math.max(roadsFind(neighbour, /* new HashSet<> */(visited), length + 1), length);
  90. }
  91. }
  92. return length;
  93. }
  94.  
  95. private List<Street> neighbours(Street street) {
  96. List<Street> neighbours = new ArrayList<>();
  97. for (Street connected : street.getConnectedStreets()) {
  98. if (street.getOwner().equals(connected.getOwner())) {
  99. neighbours.add(connected);
  100. }
  101. }
  102. return neighbours;
  103. }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement