1 | package com.takenoko.bot.utils.pathfinding.irrigation; | |
2 | ||
3 | import com.takenoko.asset.IrrigationDeck; | |
4 | import com.takenoko.engine.Board; | |
5 | import com.takenoko.layers.irrigation.EdgePosition; | |
6 | import com.takenoko.ui.ConsoleUserInterface; | |
7 | import com.takenoko.vector.PositionVector; | |
8 | import java.util.*; | |
9 | ||
10 | public class IrrigationPathFinding { | |
11 | ||
12 | static final int ITERATION_LIMIT = 1000; | |
13 | static ConsoleUserInterface consoleUserInterface = new ConsoleUserInterface(); | |
14 | ||
15 | private IrrigationPathFinding() {} | |
16 | ||
17 | private static int calculateHCost( | |
18 | Board board, List<PositionVector> tilesToIrrigate, EdgePosition placedEdgePosition) { | |
19 |
1
1. calculateHCost : negated conditional → KILLED |
if (placedEdgePosition == null) { |
20 |
1
1. calculateHCost : replaced int return with 0 for com/takenoko/bot/utils/pathfinding/irrigation/IrrigationPathFinding::calculateHCost → KILLED |
return 1000000; |
21 | } | |
22 | // get the sum of the distances between the tiles to irrigate and the placed edge position | |
23 |
1
1. calculateHCost : replaced int return with 0 for com/takenoko/bot/utils/pathfinding/irrigation/IrrigationPathFinding::calculateHCost → KILLED |
return tilesToIrrigate.stream() |
24 |
2
1. lambda$calculateHCost$0 : negated conditional → KILLED 2. lambda$calculateHCost$0 : replaced boolean return with true for com/takenoko/bot/utils/pathfinding/irrigation/IrrigationPathFinding::lambda$calculateHCost$0 → KILLED |
.filter(tile -> !board.isIrrigatedAt(tile)) |
25 | .mapToInt( | |
26 | tile -> | |
27 |
1
1. lambda$calculateHCost$1 : replaced int return with 0 for com/takenoko/bot/utils/pathfinding/irrigation/IrrigationPathFinding::lambda$calculateHCost$1 → KILLED |
(int) |
28 | (placedEdgePosition.getLeftTilePosition().distance(tile) | |
29 | + placedEdgePosition | |
30 | .getRightTilePosition() | |
31 |
1
1. lambda$calculateHCost$1 : Replaced double addition with subtraction → KILLED |
.distance(tile))) |
32 | .sum(); | |
33 | } | |
34 | ||
35 | public static List<EdgePosition> getShortestIrrigationPath( | |
36 | List<PositionVector> tilesToIrrigate, Board board) { | |
37 | Queue<Node> open = new PriorityQueue<>(Comparator.comparingInt(Node::fCost)); | |
38 | Set<Board> closed = new HashSet<>(); | |
39 | open.add(new Node(board, 0, calculateHCost(board, tilesToIrrigate, null), null, null)); | |
40 |
1
1. getShortestIrrigationPath : negated conditional → KILLED |
while (!open.isEmpty()) { |
41 |
2
1. getShortestIrrigationPath : changed conditional boundary → TIMED_OUT 2. getShortestIrrigationPath : negated conditional → KILLED |
if (open.size() > ITERATION_LIMIT) { |
42 |
1
1. getShortestIrrigationPath : removed call to com/takenoko/ui/ConsoleUserInterface::displayError → TIMED_OUT |
consoleUserInterface.displayError( |
43 | "Irrigation path finding iteration limit reached"); | |
44 | return List.of(); | |
45 | } | |
46 | ||
47 | Node current = open.poll(); | |
48 |
1
1. getShortestIrrigationPath : negated conditional → KILLED |
if (calculateHCost(current.board(), tilesToIrrigate, current.edgePosition()) == 0) { |
49 |
1
1. getShortestIrrigationPath : replaced return value with Collections.emptyList for com/takenoko/bot/utils/pathfinding/irrigation/IrrigationPathFinding::getShortestIrrigationPath → KILLED |
return rebuildPath(current); |
50 | } | |
51 |
2
1. getShortestIrrigationPath : changed conditional boundary → TIMED_OUT 2. getShortestIrrigationPath : negated conditional → KILLED |
if (current.board().getPlacedIrrigations().size() > IrrigationDeck.DEFAULT_SIZE) { |
52 | closed.add(current.board()); | |
53 | continue; | |
54 | } | |
55 | ||
56 | closed.add(current.board()); | |
57 | for (EdgePosition edgePosition : current.board().getAvailableIrrigationPositions()) { | |
58 | Board copy = current.board().copy(); | |
59 |
1
1. getShortestIrrigationPath : removed call to com/takenoko/engine/Board::placeIrrigation → KILLED |
copy.placeIrrigation(edgePosition); |
60 | int g = 0; | |
61 | int h = calculateHCost(copy, tilesToIrrigate, edgePosition); | |
62 | Node child = new Node(copy, g, h, current, edgePosition); | |
63 |
2
1. getShortestIrrigationPath : negated conditional → KILLED 2. getShortestIrrigationPath : negated conditional → KILLED |
if (!closed.contains(copy) && !open.contains(child)) { |
64 | open.add(child); | |
65 | } | |
66 | } | |
67 | } | |
68 | return List.of(); | |
69 | } | |
70 | ||
71 | private static List<EdgePosition> rebuildPath(Node node) { | |
72 | List<EdgePosition> path = new ArrayList<>(); | |
73 |
1
1. rebuildPath : negated conditional → KILLED |
while (node != null) { |
74 |
1
1. rebuildPath : negated conditional → KILLED |
if (node.edgePosition() != null) { |
75 |
1
1. rebuildPath : removed call to java/util/List::add → KILLED |
path.add(0, node.edgePosition()); |
76 | } | |
77 | node = node.parent(); | |
78 | } | |
79 |
1
1. rebuildPath : replaced return value with Collections.emptyList for com/takenoko/bot/utils/pathfinding/irrigation/IrrigationPathFinding::rebuildPath → KILLED |
return path; |
80 | } | |
81 | } | |
Mutations | ||
19 |
1.1 |
|
20 |
1.1 |
|
23 |
1.1 |
|
24 |
1.1 2.2 |
|
27 |
1.1 |
|
31 |
1.1 |
|
40 |
1.1 |
|
41 |
1.1 2.2 |
|
42 |
1.1 |
|
48 |
1.1 |
|
49 |
1.1 |
|
51 |
1.1 2.2 |
|
59 |
1.1 |
|
63 |
1.1 2.2 |
|
73 |
1.1 |
|
74 |
1.1 |
|
75 |
1.1 |
|
79 |
1.1 |