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.Coordinate;
18 import ru.bosony.model.moving.Movement;
19 import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
20 import edu.uci.ics.jung.graph.DirectedSparseGraph;
21 import edu.uci.ics.jung.graph.Graph;
24 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
28 public class DijkstraSolver extends AbstractSolver {
30 protected Map<Mine, Set<Cell>> testedTargetCells = new HashMap<Mine, Set<Cell>>();
32 public DijkstraSolver(Mine mine, SolverListener listener) {
33 super(mine, listener);
41 testMine = mine.clone();
42 } catch (CloneNotSupportedException e1) {
43 throw new RuntimeException(e1);
45 Game game = new Game(testMine);
47 while (game.getState() == GameState.Game) {
48 if (!testedTargetCells.containsKey(testMine)) {
49 testedTargetCells.put(testMine, new HashSet<Cell>());
51 Set<Cell> targets = testMine.findCells(CellContent.Lambda);
52 targets.addAll(testMine.findCells(CellContent.HighOrderRock));
53 if (targets.size() == 0)
54 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);
62 if (testedTargetCells.get(testMine).containsAll(targets)) {
63 // Fail. All cases are tried.
64 game.move(Movement.ABORT);
68 Graph<Cell, CellsLink> graph = getDirectedGraph(testMine);
69 DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph,
70 new CellsLinkTransformer<CellsLink, Double>());
71 Cell nextTarget = null;
72 List<CellsLink> routeToNextTarget = null;
73 double shortestDist = Double.MAX_VALUE;
74 for (Cell target : targets) {
77 List<CellsLink> route = null;
80 route = alg.getPath(testMine.getRobotCell(), target);
81 dist = alg.getDistance(testMine.getRobotCell(), target);
82 } catch (IllegalArgumentException e) {
84 testedTargetCells.get(testMine).add(target);
87 if (dist != null && route != null && dist.doubleValue() < shortestDist
88 && !testedTargetCells.get(testMine).contains(target)) {
89 shortestDist = dist.doubleValue();
91 routeToNextTarget = route;
92 testedTargetCells.get(testMine).add(target);
95 if (nextTarget != null) {
96 for (CellsLink link : routeToNextTarget) {
97 Movement mov = link.getSource().getCoordinate()
98 .getNecessaryMovement(link.getTarget().getCoordinate());
104 if (game.getState() == GameState.Win) {
105 addNewRoute(game.getStringRoute(), game.getScore());
111 protected Graph<Cell, CellsLink> getDirectedGraph(Mine mine) {
112 Graph<Cell, CellsLink> graph = new DirectedSparseGraph<Cell, CellsLink>();
113 for (CellsLink link : getCellLinks(mine)) {
114 graph.addEdge(link, link.getSource(), link.getTarget());
119 protected List<CellsLink> getCellLinks(Mine mine) {
120 List<CellsLink> links = new ArrayList<CellsLink>();
121 Cell robotCell = mine.getRobotCell();
122 findAllCellLinks(mine, robotCell, links, new HashSet<Cell>());
126 protected void findAllCellLinks(Mine mine, Cell cell, List<CellsLink> links, Set<Cell> processedCells) {
127 if (processedCells.contains(cell))
130 processedCells.add(cell);
131 for (Cell neighboringCell : mine.getAdjacentCells(cell)) {
132 if (neighboringCell == null)
134 CellContent neighboringContent = neighboringCell.getContent();
135 if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty
136 || neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda
137 || neighboringContent == CellContent.OpenLambdaLift) {
138 CellsLink link = new CellsLink(mine, cell, neighboringCell);
140 findAllCellLinks(mine, neighboringCell, links, processedCells);
141 } else if (!CellContent.getTargets().contains(cell.getContent())
142 && CellContent.getTargets().contains(neighboringContent)) {
143 findAllCellLinks(mine, neighboringCell, links, processedCells);
146 if (CellContent.getTrampolines().contains(cell.getContent())) {
147 for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) {
150 CellsLink link = new CellsLink(mine, cell, target);
152 findAllCellLinks(mine, target, links, processedCells);