12 import ru.bosony.model.cellscontents.CellContent; |
12 import ru.bosony.model.cellscontents.CellContent; |
13 import ru.bosony.model.game.Game; |
13 import ru.bosony.model.game.Game; |
14 import ru.bosony.model.game.GameState; |
14 import ru.bosony.model.game.GameState; |
15 import ru.bosony.model.mine.Cell; |
15 import ru.bosony.model.mine.Cell; |
16 import ru.bosony.model.mine.Mine; |
16 import ru.bosony.model.mine.Mine; |
17 import ru.bosony.model.moving.Coordinate; |
|
18 import ru.bosony.model.moving.Movement; |
17 import ru.bosony.model.moving.Movement; |
19 import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath; |
18 import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath; |
20 import edu.uci.ics.jung.graph.DirectedSparseGraph; |
19 import edu.uci.ics.jung.graph.DirectedSparseGraph; |
21 import edu.uci.ics.jung.graph.Graph; |
20 import edu.uci.ics.jung.graph.Graph; |
22 |
21 |
43 throw new RuntimeException(e1); |
43 throw new RuntimeException(e1); |
44 } |
44 } |
45 Game game = new Game(testMine); |
45 Game game = new Game(testMine); |
46 |
46 |
47 while (game.getState() == GameState.Game) { |
47 while (game.getState() == GameState.Game) { |
48 if (!testedTargetCells.containsKey(testMine)) { |
48 String map = testMine.toText(); |
49 testedTargetCells.put(testMine, new HashSet<Cell>()); |
49 if (!loseMovs.containsKey(map)) { |
|
50 loseMovs.put(map, new HashSet<Movement>()); |
50 } |
51 } |
51 Set<Cell> targets = testMine.findCells(CellContent.Lambda); |
52 Set<Cell> targets = testMine.findCells(CellContent.Lambda); |
52 targets.addAll(testMine.findCells(CellContent.HighOrderRock)); |
53 targets.addAll(testMine.findCells(CellContent.HighOrderRock)); |
53 if (targets.size() == 0) |
54 if (targets.size() == 0) |
54 targets = testMine.findCells(CellContent.OpenLambdaLift); |
55 targets = testMine.findCells(CellContent.OpenLambdaLift); |
55 if (targets.size() == 0 || testedTargetCells.get(testMine).containsAll(targets)) { |
|
56 // No targets. Walking around |
|
57 Cell robotCell = testMine.getRobotCell(); |
|
58 for (Coordinate nextCoordinate : robotCell.getCoordinate().getAdjacentCoordinates()) { |
|
59 Cell nextCell = testMine.getCell(nextCoordinate); |
|
60 targets.add(nextCell); |
|
61 } |
|
62 if (testedTargetCells.get(testMine).containsAll(targets)) { |
|
63 // Fail. All cases are tried. |
|
64 game.move(Movement.ABORT); |
|
65 break; |
|
66 } |
|
67 } |
|
68 Graph<Cell, CellsLink> graph = getDirectedGraph(testMine); |
56 Graph<Cell, CellsLink> graph = getDirectedGraph(testMine); |
69 DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph, |
57 DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph, |
70 new CellsLinkTransformer<CellsLink, Double>()); |
58 new CellsLinkTransformer<CellsLink, Double>()); |
71 Cell nextTarget = null; |
59 Cell nextTarget = null; |
72 List<CellsLink> routeToNextTarget = null; |
60 List<CellsLink> routeToNextTarget = null; |
79 try { |
67 try { |
80 route = alg.getPath(testMine.getRobotCell(), target); |
68 route = alg.getPath(testMine.getRobotCell(), target); |
81 dist = alg.getDistance(testMine.getRobotCell(), target); |
69 dist = alg.getDistance(testMine.getRobotCell(), target); |
82 } catch (IllegalArgumentException e) { |
70 } catch (IllegalArgumentException e) { |
83 // hmmm... |
71 // hmmm... |
84 testedTargetCells.get(testMine).add(target); |
|
85 continue; |
72 continue; |
86 } |
73 } |
87 if (dist != null && route != null && dist.doubleValue() < shortestDist |
74 if (dist != null && route != null && route.size() > 0 && dist.doubleValue() < shortestDist) { |
88 && !testedTargetCells.get(testMine).contains(target)) { |
75 CellsLink firstLink = route.get(0); |
89 shortestDist = dist.doubleValue(); |
76 Movement mov = firstLink.getSource().getCoordinate() |
90 nextTarget = target; |
77 .getNecessaryMovement(firstLink.getTarget().getCoordinate()); |
91 routeToNextTarget = route; |
78 if (!loseMovs.get(map).contains(mov)) { |
92 testedTargetCells.get(testMine).add(target); |
79 shortestDist = dist.doubleValue(); |
|
80 nextTarget = target; |
|
81 routeToNextTarget = route; |
|
82 break; |
|
83 } |
93 } |
84 } |
94 } |
85 } |
95 if (nextTarget != null) { |
86 if (nextTarget != null) { |
96 for (CellsLink link : routeToNextTarget) { |
87 for (CellsLink link : routeToNextTarget) { |
97 Movement mov = link.getSource().getCoordinate() |
88 Cell source = link.getSource(); |
98 .getNecessaryMovement(link.getTarget().getCoordinate()); |
89 Cell target = link.getTarget(); |
99 game.move(mov); |
90 if (isSafeMovement(testMine, source, target)) { |
|
91 Movement mov = source.getCoordinate().getNecessaryMovement(target.getCoordinate()); |
|
92 GameState state = game.move(mov); |
|
93 if (state == GameState.Lose || state == GameState.Abort || game.hasViciousCircle()) { |
|
94 loseMovs.get(map).add(mov); |
|
95 loseLinks.add(link); |
|
96 } |
|
97 } |
100 break; |
98 break; |
|
99 } |
|
100 } else { |
|
101 for (Movement mov : Movement.values()) { |
|
102 if (!loseMovs.get(map).contains(mov)) { |
|
103 GameState state = game.move(mov); |
|
104 if (state == GameState.Lose || state == GameState.Abort || map.equals(testMine.toText()) |
|
105 || game.hasViciousCircle()) |
|
106 loseMovs.get(map).add(mov); |
|
107 break; |
|
108 } |
101 } |
109 } |
102 } |
110 } |
103 } |
111 } |
104 if (game.getState() == GameState.Win) { |
112 if (game.getState() == GameState.Win) { |
105 addNewRoute(game.getStringRoute(), game.getScore()); |
113 addNewRoute(game.getStringRoute(), game.getScore()); |
106 } |
114 } |
107 attemptsCount++; |
115 attemptsCount++; |
108 } |
116 } |
|
117 } |
|
118 |
|
119 protected boolean isSafeMovement(Mine mine, Cell source, Cell target) { |
|
120 if (source == null || target == null) |
|
121 return false; |
|
122 if (source.getContent().getTrampolineTarget() == target.getContent()) |
|
123 return true; |
|
124 Cell upCell = mine.getCell(target.getCoordinate().up()); |
|
125 if (upCell == null) |
|
126 return true; |
|
127 Cell upUpCell = mine.getCell(target.getCoordinate().up().up()); |
|
128 Cell upUpLeftCell = mine.getCell(target.getCoordinate().up().up().left()); |
|
129 Cell upUpRightCell = mine.getCell(target.getCoordinate().up().up().right()); |
|
130 if (upCell.getContent() == CellContent.Empty |
|
131 && (upUpCell != null |
|
132 && (upUpCell.getContent() == CellContent.Rock || upUpCell.getContent() == CellContent.HighOrderRock) |
|
133 || upUpLeftCell != null |
|
134 && (upUpLeftCell.getContent() == CellContent.Rock || upUpLeftCell.getContent() == CellContent.HighOrderRock) || upUpRightCell != null |
|
135 && (upUpRightCell.getContent() == CellContent.Rock || upUpRightCell.getContent() == CellContent.HighOrderRock))) { |
|
136 return false; |
|
137 } |
|
138 return true; |
109 } |
139 } |
110 |
140 |
111 protected Graph<Cell, CellsLink> getDirectedGraph(Mine mine) { |
141 protected Graph<Cell, CellsLink> getDirectedGraph(Mine mine) { |
112 Graph<Cell, CellsLink> graph = new DirectedSparseGraph<Cell, CellsLink>(); |
142 Graph<Cell, CellsLink> graph = new DirectedSparseGraph<Cell, CellsLink>(); |
113 for (CellsLink link : getCellLinks(mine)) { |
143 for (CellsLink link : getCellLinks(mine)) { |
133 continue; |
163 continue; |
134 CellContent neighboringContent = neighboringCell.getContent(); |
164 CellContent neighboringContent = neighboringCell.getContent(); |
135 if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty |
165 if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty |
136 || neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda |
166 || neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda |
137 || neighboringContent == CellContent.OpenLambdaLift) { |
167 || neighboringContent == CellContent.OpenLambdaLift) { |
138 CellsLink link = new CellsLink(mine, cell, neighboringCell); |
168 if (isSafeMovement(mine, cell, neighboringCell)) { |
139 links.add(link); |
169 CellsLink link = new CellsLink(mine, cell, neighboringCell); |
140 findAllCellLinks(mine, neighboringCell, links, processedCells); |
170 if (!loseLinks.contains(link)) { |
|
171 links.add(link); |
|
172 findAllCellLinks(mine, neighboringCell, links, processedCells); |
|
173 } |
|
174 } |
141 } else if (!CellContent.getTargets().contains(cell.getContent()) |
175 } else if (!CellContent.getTargets().contains(cell.getContent()) |
142 && CellContent.getTargets().contains(neighboringContent)) { |
176 && CellContent.getTargets().contains(neighboringContent)) { |
143 findAllCellLinks(mine, neighboringCell, links, processedCells); |
177 findAllCellLinks(mine, neighboringCell, links, processedCells); |
144 } |
178 } |
145 } |
179 } |
146 if (CellContent.getTrampolines().contains(cell.getContent())) { |
180 if (CellContent.getTrampolines().contains(cell.getContent())) { |
147 for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) { |
181 for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) { |
148 if (target == null) |
182 if (target == null) |
149 continue; |
183 continue; |
150 CellsLink link = new CellsLink(mine, cell, target); |
184 if (isSafeMovement(mine, cell, target)) { |
151 links.add(link); |
185 CellsLink link = new CellsLink(mine, cell, target); |
152 findAllCellLinks(mine, target, links, processedCells); |
186 if (!loseLinks.contains(link)) { |
|
187 links.add(link); |
|
188 findAllCellLinks(mine, target, links, processedCells); |
|
189 } |
|
190 } |
153 } |
191 } |
154 } |
192 } |
155 } |
193 } |
156 } |
194 } |