1 package ru.bosony.solvers;
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
10 import ru.bosony.graph.CellsLink;
11 import ru.bosony.graph.CellsLinkTransformer;
12 import ru.bosony.model.cellscontents.CellContent;
13 import ru.bosony.model.game.Game;
14 import ru.bosony.model.game.GameState;
15 import ru.bosony.model.mine.Cell;
16 import ru.bosony.model.mine.Mine;
17 import ru.bosony.model.moving.Movement;
18 import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
19 import edu.uci.ics.jung.graph.DirectedSparseGraph;
20 import edu.uci.ics.jung.graph.Graph;
23 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
27 public class DijkstraSolver extends AbstractSolver {
29 protected Map<String, Set<Movement>> loseMovs = new HashMap<String, Set<Movement>>();
30 protected Set<CellsLink> loseLinks = new HashSet<CellsLink>();
32 public DijkstraSolver(Mine mine, SolverListener listener) {
33 super(mine, listener);
40 testMine = mine.clone();
41 Game game = new Game(testMine);
43 while (game.getState() == GameState.Game) {
44 String map = testMine.toText();
45 if (!loseMovs.containsKey(map)) {
46 loseMovs.put(map, new HashSet<Movement>());
48 Set<Cell> targets = testMine.findCells(CellContent.Lambda);
49 targets.addAll(testMine.findCells(CellContent.HighOrderRock));
50 if (targets.size() == 0)
51 targets = testMine.findCells(CellContent.OpenLambdaLift);
52 Graph<Cell, CellsLink> graph = getDirectedGraph(testMine);
53 DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph,
54 new CellsLinkTransformer<CellsLink, Double>());
55 Cell nextTarget = null;
56 List<CellsLink> routeToNextTarget = null;
57 double shortestDist = Double.MAX_VALUE;
58 for (Cell target : targets) {
61 List<CellsLink> route = null;
64 route = alg.getPath(testMine.getRobotCell(), target);
65 dist = alg.getDistance(testMine.getRobotCell(), target);
66 } catch (IllegalArgumentException e) {
70 if (dist != null && route != null && route.size() > 0 && dist.doubleValue() < shortestDist) {
71 CellsLink firstLink = route.get(0);
72 Movement mov = firstLink.getSource().getCoordinate()
73 .getNecessaryMovement(firstLink.getTarget().getCoordinate());
74 if (!loseMovs.get(map).contains(mov)) {
75 shortestDist = dist.doubleValue();
77 routeToNextTarget = route;
82 if (nextTarget != null) {
83 for (CellsLink link : routeToNextTarget) {
84 Cell source = link.getSource();
85 Cell target = link.getTarget();
86 if (isSafeMovement(testMine, source, target)) {
87 Movement mov = source.getCoordinate().getNecessaryMovement(target.getCoordinate());
88 GameState state = game.move(mov);
89 if (state == GameState.Lose || state == GameState.Abort) {
90 loseMovs.get(map).add(mov);
97 for (Movement mov : Movement.values()) {
98 if (!loseMovs.get(map).contains(mov)) {
99 GameState state = game.move(mov);
100 if (state == GameState.Game && map.equals(testMine.toText())) {
101 if (mov != Movement.WAIT)
102 loseMovs.get(map).add(mov);
103 game.move(Movement.ABORT);
115 protected boolean isSafeMovement(Mine mine, Cell source, Cell target) {
116 if (source == null || target == null)
118 if (source.getContent().getTrampolineTarget() == target.getContent())
120 Cell upCell = mine.getCell(target.getCoordinate().up());
123 Cell upUpCell = mine.getCell(target.getCoordinate().up().up());
124 Cell upUpLeftCell = mine.getCell(target.getCoordinate().up().up().left());
125 Cell upUpRightCell = mine.getCell(target.getCoordinate().up().up().right());
126 if (upCell.getContent() == CellContent.Empty
128 && (upUpCell.getContent() == CellContent.Rock || upUpCell.getContent() == CellContent.HighOrderRock)
129 || upUpLeftCell != null
130 && (upUpLeftCell.getContent() == CellContent.Rock || upUpLeftCell.getContent() == CellContent.HighOrderRock) || upUpRightCell != null
131 && (upUpRightCell.getContent() == CellContent.Rock || upUpRightCell.getContent() == CellContent.HighOrderRock))) {
137 protected Graph<Cell, CellsLink> getDirectedGraph(Mine mine) {
138 Graph<Cell, CellsLink> graph = new DirectedSparseGraph<Cell, CellsLink>();
139 for (CellsLink link : getCellLinks(mine)) {
140 graph.addEdge(link, link.getSource(), link.getTarget());
145 protected List<CellsLink> getCellLinks(Mine mine) {
146 List<CellsLink> links = new ArrayList<CellsLink>();
147 Cell robotCell = mine.getRobotCell();
148 findAllCellLinks(mine, robotCell, links, new HashSet<Cell>());
152 protected void findAllCellLinks(Mine mine, Cell cell, List<CellsLink> links, Set<Cell> processedCells) {
153 if (processedCells.contains(cell))
156 processedCells.add(cell);
157 for (Cell neighboringCell : mine.getAdjacentCells(cell)) {
158 if (neighboringCell == null)
160 CellContent neighboringContent = neighboringCell.getContent();
161 if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty
162 || neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda
163 || neighboringContent == CellContent.OpenLambdaLift) {
164 if (isSafeMovement(mine, cell, neighboringCell)) {
165 CellsLink link = new CellsLink(mine, cell, neighboringCell);
166 if (!loseLinks.contains(link)) {
168 findAllCellLinks(mine, neighboringCell, links, processedCells);
171 } else if (!CellContent.getTargets().contains(cell.getContent())
172 && CellContent.getTargets().contains(neighboringContent)) {
173 findAllCellLinks(mine, neighboringCell, links, processedCells);
176 if (CellContent.getTrampolines().contains(cell.getContent())) {
177 for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) {
180 if (isSafeMovement(mine, cell, target)) {
181 CellsLink link = new CellsLink(mine, cell, target);
182 if (!loseLinks.contains(link)) {
184 findAllCellLinks(mine, target, links, processedCells);