1: package siege.core;
2:
3: import java.util.Collection;
4: import java.util.HashSet;
5: import java.util.LinkedList;
6:
7: 8: 9: 10:
11: public class TerritoryDetector
12: {
13: private Playboard playboard;
14: private PlayboardPoint startPoint;
15: private Territory foundTerritory;
16: private LinkedList<Crossroad> unexplored;
17:
18: public TerritoryDetector(Playboard playboard)
19: {
20: super();
21: this.playboard = playboard;
22: }
23:
24: public void setStartPoint(PlayboardPoint startPoint)
25: {
26: if(startPoint.hasOwner() == false) {
27: throw new IllegalArgumentException("Starting point in territory detector must have set owner");
28: }
29: this.startPoint = startPoint;
30: }
31:
32: public PlayboardPoint getStartPoint()
33: {
34: return startPoint;
35: }
36:
37: public void search()
38: {
39: 40: unexplored = new LinkedList<Crossroad>();
41: HashSet<Territory> territories = new HashSet<Territory>();
42: HashSet<PlayboardPoint> exploredPoints = new HashSet<PlayboardPoint>();
43:
44: Path firstPath = new Path();
45: firstPath.add(startPoint);
46: unexplored.add(new Crossroad(firstPath, startPoint));
47:
48: 49: Path uninterruptedPath = null;
50:
51: 52: foundTerritory = null;
53:
54: 55: exploringCrossroads:
56: while(unexplored.isEmpty() == false) {
57: Crossroad crossroad = unexplored.pollLast();
58:
59: 60: LinkedList<PlayboardPoint> surroundingPoints = new LinkedList<PlayboardPoint>();
61: Player player = crossroad.direction.getOwner();
62: PlayboardPoint exploredPoint = crossroad.direction;
63:
64: if(exploredPoints.contains(exploredPoint)) {
65: continue;
66: } else {
67: exploredPoints.add(exploredPoint);
68: }
69:
70: 71: surroundingPointsSearch:
72: for (int y = -1; y <= 1; y++) {
73: for (int x = -1; x <= 1; x++) {
74: 75: if(y == 0 && x == 0) {
76: continue;
77: }
78:
79: 80: if(playboard.isValidCoord(exploredPoint.getX()+x, exploredPoint.getY()+y)) {
81: PlayboardPoint point = playboard.getPoint(exploredPoint.getX()+x, exploredPoint.getY()+y);
82:
83: 84: if(point.hasOwner(player) == false) {
85: continue;
86: }
87:
88: 89: if(point.hasParentTerritory(PointStatus.INNER)) {
90: continue;
91: }
92:
93: 94: if(crossroad.path.contains(point)) {
95: 96: if(point.equals(startPoint) && crossroad.path.size()>2) {
97: uninterruptedPath = crossroad.path;
98: territories.add(new Territory(uninterruptedPath));
99: 100: continue;
101: } else {
102: continue;
103: }
104: }
105:
106: 107: surroundingPoints.add(point);
108: }
109: }
110: } 111:
112: 113: if(surroundingPoints.size() == 1) {
114: 115: PlayboardPoint point = surroundingPoints.getFirst();
116: 117: crossroad.path.add(point);
118: 119: crossroad.direction = point;
120: 121: unexplored.add(crossroad);
122: } else if(surroundingPoints.size() > 1) {
123: 124: for (PlayboardPoint point : surroundingPoints) {
125: Path currentPath = new Path(crossroad.path);
126: currentPath.add(point);
127: 128: unexplored.add(new Crossroad(currentPath, point));
129: }
130: }
131:
132: } 133:
134:
135: for (Territory territory : territories) {
136: if(foundTerritory == null) {
137: foundTerritory = territory;
138: continue;
139: }
140: if(territory.getArea() > foundTerritory.getArea()) {
141: foundTerritory = territory;
142: }
143: }
144:
145: }
146:
147: public Territory getFoundTerritory()
148: {
149: return foundTerritory;
150: }
151:
152: public class Crossroad
153: {
154: public Path path;
155: public PlayboardPoint direction;
156:
157: public Crossroad(Path path, PlayboardPoint direction)
158: {
159: this.path = path;
160: this.direction = direction;
161: }
162: }
163:
164: public class Path extends LinkedList<PlayboardPoint>
165: {
166: public Path()
167: {
168: super();
169: }
170:
171: public Path(Collection<PlayboardPoint> path)
172: {
173: super(path);
174: }
175: }
176:
177: }
178: