1 package ru.bosony.solvers;
3 import java.util.ArrayList;
4 import java.util.HashSet;
8 import ru.bosony.graph.CellsLink;
9 import ru.bosony.graph.CellsLinkTransformer;
10 import ru.bosony.model.cellscontents.CellContent;
11 import ru.bosony.model.game.Game;
12 import ru.bosony.model.game.GameState;
13 import ru.bosony.model.mine.Cell;
14 import ru.bosony.model.mine.Mine;
15 import ru.bosony.model.moving.Movement;
16 import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
17 import edu.uci.ics.jung.graph.DirectedSparseGraph;
18 import edu.uci.ics.jung.graph.Graph;
21 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
25 public class DijkstraSolver extends AbstractSolver {
27 public DijkstraSolver(Mine mine, SolverListener listener) {
28 super(mine, listener);
32 protected void solve() {
34 Game game = new Game(mine);
36 while (game.getState() == GameState.Game) {
38 Set<Cell> targets = mine.findCells(CellContent.Lambda);
39 targets.addAll(mine.findCells(CellContent.HighOrderRock));
40 if (targets.size() == 0)
41 targets = mine.findCells(CellContent.OpenLambdaLift);
42 if (targets.size() == 0)
44 Graph<Cell, CellsLink> graph = getDirectedGraph();
45 DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph,
46 new CellsLinkTransformer<CellsLink, Double>());
47 Cell nextTarget = null;
48 List<CellsLink> routeToNextTarget = null;
49 double shortestDist = Double.MAX_VALUE;
50 for (Cell target : targets) {
51 List<CellsLink> route = alg.getPath(mine.getRobotCell(), target);
52 Number dist = alg.getDistance(mine.getRobotCell(), target);
53 if (dist != null && dist.doubleValue() < shortestDist) {
54 shortestDist = dist.doubleValue();
56 routeToNextTarget = route;
59 if (nextTarget != null) {
60 for (CellsLink link : routeToNextTarget) {
61 Movement mov = link.getSource().getCoordinate()
62 .getNecessaryMovement(link.getTarget().getCoordinate());
67 } catch (IllegalArgumentException e) {
71 if (game.getState() == GameState.Win) {
72 addNewRoute(game.getStringRoute(), game.getScore());
78 protected Graph<Cell, CellsLink> getDirectedGraph() {
79 Graph<Cell, CellsLink> graph = new DirectedSparseGraph<Cell, CellsLink>();
80 for (CellsLink link : getCellLinks()) {
81 graph.addEdge(link, link.getSource(), link.getTarget());
86 protected List<CellsLink> getCellLinks() {
87 List<CellsLink> links = new ArrayList<CellsLink>();
88 Cell robotCell = mine.getRobotCell();
89 findAllCellLinks(robotCell, links, new HashSet<Cell>());
93 protected void findAllCellLinks(Cell cell, List<CellsLink> links, Set<Cell> processedCells) {
94 if (processedCells.contains(cell))
97 processedCells.add(cell);
98 for (Cell neighboringCell : mine.getAdjacentCells(cell)) {
99 if (neighboringCell == null)
101 CellContent neighboringContent = neighboringCell.getContent();
102 if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty
103 || neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda
104 || neighboringContent == CellContent.OpenLambdaLift) {
105 CellsLink link = new CellsLink(mine, cell, neighboringCell);
107 findAllCellLinks(neighboringCell, links, processedCells);
108 } else if (!CellContent.getTargets().contains(cell.getContent())
109 && CellContent.getTargets().contains(neighboringContent)) {
110 findAllCellLinks(neighboringCell, links, processedCells);
113 if (CellContent.getTrampolines().contains(cell.getContent())) {
114 for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) {
117 CellsLink link = new CellsLink(mine, cell, target);
119 findAllCellLinks(target, links, processedCells);