Merge pull request #679 from devLotto/puzzlesolver-improvements
Improve the puzzle solver
This commit is contained in:
@@ -1,346 +0,0 @@
|
|||||||
/*
|
|
||||||
* This code file is a derivative of code by Jaap Scherphuis <https://www.jaapsch.net/puzzles/fifteen.htm>.
|
|
||||||
* The original code can be found in the page source of https://www.jaapsch.net/puzzles/javascript/fifteenj.htm.
|
|
||||||
*
|
|
||||||
* Quote copied verbatim from the creator's grant to use this code in RuneLite:
|
|
||||||
*
|
|
||||||
* "You have my permission to incorporate, adapt, and distribute any part
|
|
||||||
* of my 15-puzzle javascript code as part of your free, open source project"
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* The adaptions made to Jaap Scherphuis' code is copyrighted and licensed according to:
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* Copyright (c) 2018, Lotto <https://github.com/devLotto>
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
||||||
* list of conditions and the following disclaimer.
|
|
||||||
*
|
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer in the documentation
|
|
||||||
* and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
||||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
||||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
||||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
||||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
||||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
package net.runelite.client.plugins.puzzlesolver;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This 5x5 sliding puzzle solver algorithm is based on https://www.jaapsch.net/puzzles/fifteen.htm.
|
|
||||||
*/
|
|
||||||
public class PuzzleSolver
|
|
||||||
{
|
|
||||||
public static final int BLANK_TILE_VALUE = -1;
|
|
||||||
public static final int DIMENSION = 5;
|
|
||||||
private static final int SIZE = DIMENSION * DIMENSION - 1;
|
|
||||||
|
|
||||||
private int[] tiles = new int[SIZE + DIMENSION];
|
|
||||||
private List<Integer> seq = new ArrayList<>();
|
|
||||||
|
|
||||||
private int blankX;
|
|
||||||
private int blankY;
|
|
||||||
|
|
||||||
private int mode;
|
|
||||||
|
|
||||||
public PuzzleSolver(int[] arr)
|
|
||||||
{
|
|
||||||
System.arraycopy(arr, 0, tiles, 0, arr.length);
|
|
||||||
|
|
||||||
for (int i = 0; i < arr.length; i++)
|
|
||||||
{
|
|
||||||
if (arr[i] == BLANK_TILE_VALUE)
|
|
||||||
{
|
|
||||||
blankX = i % DIMENSION;
|
|
||||||
blankY = i / DIMENSION;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasNext()
|
|
||||||
{
|
|
||||||
for (int i = SIZE; i >= 0; i--)
|
|
||||||
{
|
|
||||||
if (tiles[i] != i)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void next()
|
|
||||||
{
|
|
||||||
if (mode == 0)
|
|
||||||
{
|
|
||||||
mode = 3;
|
|
||||||
seq.clear();
|
|
||||||
|
|
||||||
int[] oldTiles = new int[tiles.length];
|
|
||||||
System.arraycopy(tiles, 0, oldTiles, 0, tiles.length);
|
|
||||||
int oldBlankX = blankX;
|
|
||||||
int oldBlankY = blankY;
|
|
||||||
|
|
||||||
//restore top rows
|
|
||||||
int rr = 0;
|
|
||||||
for (int r = 0; r < DIMENSION - 2; r++)
|
|
||||||
{
|
|
||||||
for (int c = 0; c < DIMENSION; c++)
|
|
||||||
{
|
|
||||||
movePiece(rr + c, r, c);
|
|
||||||
}
|
|
||||||
rr += DIMENSION;
|
|
||||||
}
|
|
||||||
|
|
||||||
//restore left columns
|
|
||||||
for (int c = 0; c < DIMENSION - 2; c++)
|
|
||||||
{
|
|
||||||
//restore top tile of column.
|
|
||||||
movePiece(rr, DIMENSION - 2, c);
|
|
||||||
//restore bottom tile of column
|
|
||||||
if (blankX == c) push(3); //fill destination spot
|
|
||||||
if (tiles[rr + DIMENSION] != rr + DIMENSION)
|
|
||||||
{
|
|
||||||
movePiece(rr + DIMENSION, DIMENSION - 1, c + 1);
|
|
||||||
if (blankY != DIMENSION - 1)
|
|
||||||
{ //0=right, 1=down, 2=up, 3=left
|
|
||||||
//A.X or AX.
|
|
||||||
//XBX XBX
|
|
||||||
if (blankX == c + 1) push(3);
|
|
||||||
push(2);
|
|
||||||
}
|
|
||||||
//AXX
|
|
||||||
//XB.
|
|
||||||
while (blankX > c + 2)
|
|
||||||
{
|
|
||||||
push(0);
|
|
||||||
}
|
|
||||||
push(0, 0, 1, 3, 2, 3, 1, 0, 0, 2, 3);
|
|
||||||
}
|
|
||||||
rr++;
|
|
||||||
}
|
|
||||||
|
|
||||||
//last 2x2 square
|
|
||||||
if (blankX < DIMENSION - 1) push(3);
|
|
||||||
if (blankY < DIMENSION - 1) push(2);
|
|
||||||
rr = SIZE - DIMENSION - 1;
|
|
||||||
if (tiles[rr] == rr + 1) push(1, 0, 2, 3);
|
|
||||||
if (tiles[rr] == rr + DIMENSION) push(0, 1, 3, 2);
|
|
||||||
|
|
||||||
//restore pieces;
|
|
||||||
System.arraycopy(oldTiles, 0, tiles, 0, tiles.length);
|
|
||||||
blankX = oldBlankX;
|
|
||||||
blankY = oldBlankY;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mode >= 3)
|
|
||||||
{
|
|
||||||
if (!seq.isEmpty())
|
|
||||||
{
|
|
||||||
Integer c = seq.get(0);
|
|
||||||
for (int i = 1; i < seq.size(); i++)
|
|
||||||
{
|
|
||||||
seq.set(i - 1, seq.get(i));
|
|
||||||
}
|
|
||||||
seq.remove(seq.size() - 1);
|
|
||||||
doMove(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (seq.isEmpty())
|
|
||||||
{
|
|
||||||
mode = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void movePiece(int p, int y, int x)
|
|
||||||
{
|
|
||||||
//moves piece p to position y,x without disturbing previously placed pieces.
|
|
||||||
int c = -1;
|
|
||||||
int i = 0;
|
|
||||||
int j = 0;
|
|
||||||
for (; i < DIMENSION; i++)
|
|
||||||
{
|
|
||||||
for (j = 0; j < DIMENSION; j++)
|
|
||||||
{
|
|
||||||
c++;
|
|
||||||
if (tiles[c] == p) break;
|
|
||||||
}
|
|
||||||
if (tiles[c] == p) break;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Move piece to same column //0=right, 1=down, 2=up, 3=left
|
|
||||||
if (j < x && blankY == y) push(2); // move blank down if it might disturb solved pieces.
|
|
||||||
while (j > x)
|
|
||||||
{
|
|
||||||
//move piece to left
|
|
||||||
//First move blank to left hand side of it
|
|
||||||
if (blankY == i && blankX > j)
|
|
||||||
{ //if blank on wrong side of piece
|
|
||||||
if (i == DIMENSION - 1) push(1);
|
|
||||||
else push(2); //then move it to other row
|
|
||||||
}
|
|
||||||
while (blankX >= j) push(0); // move blank to column left of piece
|
|
||||||
while (blankX < j - 1) push(3);
|
|
||||||
while (blankY < i) push(2); // move blank to same row as piece
|
|
||||||
while (blankY > i) push(1);
|
|
||||||
push(3); // move piece to left.
|
|
||||||
j--;
|
|
||||||
}
|
|
||||||
while (j < x)
|
|
||||||
{
|
|
||||||
//move piece to right
|
|
||||||
//First move blank to right hand side of it
|
|
||||||
if (blankY == i && blankX < j)
|
|
||||||
{
|
|
||||||
if (i == DIMENSION - 1) push(1);
|
|
||||||
else push(2);
|
|
||||||
}
|
|
||||||
while (blankX <= j) push(3);
|
|
||||||
while (blankX > j + 1) push(0);
|
|
||||||
while (blankY < i) push(2);
|
|
||||||
while (blankY > i) push(1);
|
|
||||||
push(0);
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Move piece up to same row //0=right, 1=down, 2=up, 3=left
|
|
||||||
while (i > y)
|
|
||||||
{
|
|
||||||
if (y < i - 1)
|
|
||||||
{
|
|
||||||
while (blankY < i - 1) push(2);
|
|
||||||
if (blankX == j) push(j == DIMENSION - 1 ? 0 : 3);
|
|
||||||
while (blankY > i - 1) push(1);
|
|
||||||
while (blankX < j) push(3);
|
|
||||||
while (blankX > j) push(0);
|
|
||||||
push(2);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (j != DIMENSION - 1)
|
|
||||||
{
|
|
||||||
if (blankY == i) push(2);
|
|
||||||
while (blankX < j + 1) push(3);
|
|
||||||
while (blankX > j + 1) push(0);
|
|
||||||
while (blankY > i - 1) push(1);
|
|
||||||
while (blankY < i - 1) push(2);
|
|
||||||
push(0, 2);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (blankY < i && blankX == j)
|
|
||||||
{
|
|
||||||
while (blankY < i) push(2);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
while (blankY > i + 1) push(1);
|
|
||||||
while (blankY < i + 1) push(2);
|
|
||||||
while (blankX < j) push(3);
|
|
||||||
while (blankX > j) push(0);
|
|
||||||
push(1, 1, 0, 2, 3, 2, 0, 1, 1, 3, 2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
while (i < y)
|
|
||||||
{
|
|
||||||
//move piece downwards
|
|
||||||
//First move blank below tile
|
|
||||||
if (blankX == j && blankY < i)
|
|
||||||
{
|
|
||||||
if (j == DIMENSION - 1) push(0);
|
|
||||||
else push(3);
|
|
||||||
}
|
|
||||||
while (blankY > i + 1) push(1);
|
|
||||||
while (blankY < i + 1) push(2);
|
|
||||||
while (blankX < j) push(3);
|
|
||||||
while (blankX > j) push(0);
|
|
||||||
push(1);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void push(int... moves)
|
|
||||||
{
|
|
||||||
for (int c : moves)
|
|
||||||
{
|
|
||||||
if (!seq.isEmpty() && seq.get(seq.size() - 1) + c == 3)
|
|
||||||
{
|
|
||||||
seq.remove(seq.size() - 1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
seq.add(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
doMove(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void doMove(int m)
|
|
||||||
{
|
|
||||||
int d = blankX + blankY * DIMENSION;
|
|
||||||
|
|
||||||
if (m == 0)
|
|
||||||
{
|
|
||||||
// RIGHT
|
|
||||||
tiles[d] = tiles[d - 1];
|
|
||||||
tiles[d - 1] = SIZE;
|
|
||||||
blankX--;
|
|
||||||
}
|
|
||||||
else if (m == 1)
|
|
||||||
{
|
|
||||||
// DOWN
|
|
||||||
tiles[d] = tiles[d - DIMENSION];
|
|
||||||
tiles[d - DIMENSION] = SIZE;
|
|
||||||
blankY--;
|
|
||||||
}
|
|
||||||
else if (m == 2)
|
|
||||||
{
|
|
||||||
// UP
|
|
||||||
tiles[d] = tiles[d + DIMENSION];
|
|
||||||
tiles[d + DIMENSION] = SIZE;
|
|
||||||
blankY++;
|
|
||||||
}
|
|
||||||
else if (m == 3)
|
|
||||||
{
|
|
||||||
// LEFT
|
|
||||||
tiles[d] = tiles[d + 1];
|
|
||||||
tiles[d + 1] = SIZE;
|
|
||||||
blankX++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public int[] getTiles()
|
|
||||||
{
|
|
||||||
return tiles;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getBlankX()
|
|
||||||
{
|
|
||||||
return blankX;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getBlankY()
|
|
||||||
{
|
|
||||||
return blankY;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -55,4 +55,14 @@ public interface PuzzleSolverConfig extends Config
|
|||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ConfigItem(
|
||||||
|
keyName = "drawDots",
|
||||||
|
name = "Draw dots instead of arrows",
|
||||||
|
description = "Draw dots increasing in size instead of arrows for the solution"
|
||||||
|
)
|
||||||
|
default boolean drawDots()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2018, Lotto <https://github.com/devLotto>
|
* Copyright (c) 2018, Lotto <https://github.com/devLotto>
|
||||||
|
* Copyright (c) 2018, Henke <https://github.com/henke96>
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
@@ -25,6 +26,7 @@
|
|||||||
*/
|
*/
|
||||||
package net.runelite.client.plugins.puzzlesolver;
|
package net.runelite.client.plugins.puzzlesolver;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.awt.FontMetrics;
|
import java.awt.FontMetrics;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
@@ -35,9 +37,9 @@ import java.awt.image.AffineTransformOp;
|
|||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.ArrayDeque;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Deque;
|
import java.util.concurrent.Future;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -48,7 +50,10 @@ import net.runelite.api.Item;
|
|||||||
import net.runelite.api.ItemContainer;
|
import net.runelite.api.ItemContainer;
|
||||||
import net.runelite.api.widgets.Widget;
|
import net.runelite.api.widgets.Widget;
|
||||||
import net.runelite.api.widgets.WidgetInfo;
|
import net.runelite.api.widgets.WidgetInfo;
|
||||||
import static net.runelite.client.plugins.puzzlesolver.PuzzleSolver.BLANK_TILE_VALUE;
|
import net.runelite.client.plugins.puzzlesolver.solver.PuzzleSolver;
|
||||||
|
import net.runelite.client.plugins.puzzlesolver.solver.PuzzleState;
|
||||||
|
import net.runelite.client.plugins.puzzlesolver.solver.heuristics.ManhattanDistance;
|
||||||
|
import net.runelite.client.plugins.puzzlesolver.solver.pathfinding.IDAStar;
|
||||||
import net.runelite.client.ui.overlay.Overlay;
|
import net.runelite.client.ui.overlay.Overlay;
|
||||||
import net.runelite.client.ui.overlay.OverlayLayer;
|
import net.runelite.client.ui.overlay.OverlayLayer;
|
||||||
import net.runelite.client.ui.overlay.OverlayPosition;
|
import net.runelite.client.ui.overlay.OverlayPosition;
|
||||||
@@ -60,18 +65,23 @@ import net.runelite.client.ui.overlay.components.TextComponent;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class PuzzleSolverOverlay extends Overlay
|
public class PuzzleSolverOverlay extends Overlay
|
||||||
{
|
{
|
||||||
private static final int MOVES_LEFT_WIDTH = 100;
|
private static final int INFO_BOX_WIDTH = 100;
|
||||||
private static final int MOVES_LEFT_OFFSET_Y = 50;
|
private static final int INFO_BOX_OFFSET_Y = 50;
|
||||||
private static final int MOVES_LEFT_TOP_BORDER = 2;
|
private static final int INFO_BOX_TOP_BORDER = 2;
|
||||||
private static final int MOVES_LEFT_BOTTOM_BORDER = 2;
|
private static final int INFO_BOX_BOTTOM_BORDER = 2;
|
||||||
|
|
||||||
private static final int PUZZLE_TILE_SIZE = 39;
|
private static final int PUZZLE_TILE_SIZE = 39;
|
||||||
|
private static final int DOT_MARKER_SIZE = 16;
|
||||||
|
|
||||||
|
private static final int BLANK_TILE_VALUE = -1;
|
||||||
|
private static final int DIMENSION = 5;
|
||||||
|
|
||||||
private final Client client;
|
private final Client client;
|
||||||
private final PuzzleSolverConfig config;
|
private final PuzzleSolverConfig config;
|
||||||
|
private final ScheduledExecutorService executorService;
|
||||||
|
|
||||||
private PuzzleSolver solver;
|
private PuzzleSolver solver;
|
||||||
private Deque<Integer> nextMoves;
|
private Future<?> solverFuture;
|
||||||
private int[] cachedItems;
|
private int[] cachedItems;
|
||||||
|
|
||||||
private BufferedImage downArrow;
|
private BufferedImage downArrow;
|
||||||
@@ -80,13 +90,14 @@ public class PuzzleSolverOverlay extends Overlay
|
|||||||
private BufferedImage rightArrow;
|
private BufferedImage rightArrow;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public PuzzleSolverOverlay(Client client, PuzzleSolverConfig config)
|
public PuzzleSolverOverlay(Client client, PuzzleSolverConfig config, ScheduledExecutorService executorService)
|
||||||
{
|
{
|
||||||
setPosition(OverlayPosition.DYNAMIC);
|
setPosition(OverlayPosition.DYNAMIC);
|
||||||
setPriority(OverlayPriority.HIGH);
|
setPriority(OverlayPriority.HIGH);
|
||||||
setLayer(OverlayLayer.ABOVE_WIDGETS);
|
setLayer(OverlayLayer.ABOVE_WIDGETS);
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.config = config;
|
this.config = config;
|
||||||
|
this.executorService = executorService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -112,117 +123,222 @@ public class PuzzleSolverOverlay extends Overlay
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
net.runelite.api.Point puzzleBoxLocation = puzzleBox.getCanvasLocation();
|
||||||
|
|
||||||
|
String infoString = "Solving..";
|
||||||
|
|
||||||
int[] itemIds = getItemIds(container);
|
int[] itemIds = getItemIds(container);
|
||||||
boolean shouldCache = false;
|
boolean shouldCache = false;
|
||||||
|
|
||||||
if (nextMoves != null)
|
if (solver != null)
|
||||||
{
|
{
|
||||||
Integer nextMove = nextMoves.peek();
|
if (solver.hasFailed())
|
||||||
|
|
||||||
// If this is true, nextMove is actually the current state (player has moved the empty tile),
|
|
||||||
// so let's throw away this step and try to find the actual next move.
|
|
||||||
while (nextMove != null && itemIds[nextMove] == BLANK_TILE_VALUE)
|
|
||||||
{
|
{
|
||||||
nextMoves.pop(); // pop the current move
|
infoString = "The puzzle could not be solved";
|
||||||
nextMove = nextMoves.peek();
|
|
||||||
shouldCache = true;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
// If nextMove is null, the puzzle is either finished
|
|
||||||
// or the player has not followed the instructions.
|
|
||||||
if (nextMove != null)
|
|
||||||
{
|
{
|
||||||
net.runelite.api.Point puzzleBoxLocation = puzzleBox.getCanvasLocation();
|
if (solver.hasSolution())
|
||||||
|
|
||||||
if (config.displayRemainingMoves())
|
|
||||||
{
|
{
|
||||||
int x = puzzleBoxLocation.getX() + puzzleBox.getWidth() / 2 - MOVES_LEFT_WIDTH / 2;
|
boolean foundPosition = false;
|
||||||
int y = puzzleBoxLocation.getY() - MOVES_LEFT_OFFSET_Y;
|
|
||||||
|
|
||||||
String movesLeftString = "Moves left: " + nextMoves.size();
|
// Find the current state by looking at the current step and then the next 5 steps
|
||||||
|
for (int i = 0; i < 6; i++)
|
||||||
FontMetrics fm = graphics.getFontMetrics();
|
|
||||||
int height = MOVES_LEFT_TOP_BORDER + fm.getHeight() + MOVES_LEFT_BOTTOM_BORDER;
|
|
||||||
|
|
||||||
BackgroundComponent backgroundComponent = new BackgroundComponent();
|
|
||||||
backgroundComponent.setRectangle(new Rectangle(x, y, MOVES_LEFT_WIDTH, height));
|
|
||||||
backgroundComponent.render(graphics, parent);
|
|
||||||
|
|
||||||
int textOffsetX = (MOVES_LEFT_WIDTH - fm.stringWidth(movesLeftString)) / 2;
|
|
||||||
int textOffsetY = fm.getHeight();
|
|
||||||
|
|
||||||
TextComponent textComponent = new TextComponent();
|
|
||||||
textComponent.setPosition(new Point(x + textOffsetX, y + textOffsetY));
|
|
||||||
textComponent.setText(movesLeftString);
|
|
||||||
textComponent.render(graphics, parent);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config.displaySolution())
|
|
||||||
{
|
|
||||||
int i = 0;
|
|
||||||
int lastBlankX = 0;
|
|
||||||
int lastBlankY = 0;
|
|
||||||
|
|
||||||
// First find the current blank tile position
|
|
||||||
for (int j = 0; j < itemIds.length; j++)
|
|
||||||
{
|
{
|
||||||
if (itemIds[j] == BLANK_TILE_VALUE)
|
int j = solver.getPosition() + i;
|
||||||
|
|
||||||
|
if (j == solver.getStepCount())
|
||||||
{
|
{
|
||||||
lastBlankX = j % PuzzleSolver.DIMENSION;
|
break;
|
||||||
lastBlankY = j / PuzzleSolver.DIMENSION;
|
}
|
||||||
|
|
||||||
|
PuzzleState currentState = solver.getStep(j);
|
||||||
|
|
||||||
|
// If this is false, player has moved the empty tile
|
||||||
|
if (currentState != null && currentState.hasPieces(itemIds))
|
||||||
|
{
|
||||||
|
foundPosition = true;
|
||||||
|
solver.setPosition(j);
|
||||||
|
if (i > 0)
|
||||||
|
{
|
||||||
|
shouldCache = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Integer futureMove : nextMoves)
|
// If looking at the next steps didn't find the current state,
|
||||||
|
// see if we can find the current state in the 5 previous steps
|
||||||
|
if (!foundPosition)
|
||||||
{
|
{
|
||||||
int blankX = futureMove % PuzzleSolver.DIMENSION;
|
for (int i = 1; i < 6; i++)
|
||||||
int blankY = futureMove / PuzzleSolver.DIMENSION;
|
{
|
||||||
|
int j = solver.getPosition() - i;
|
||||||
|
|
||||||
int xDelta = blankX - lastBlankX;
|
if (j < 0)
|
||||||
int yDelta = blankY - lastBlankY;
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
BufferedImage arrow;
|
PuzzleState currentState = solver.getStep(j);
|
||||||
if (xDelta > 0)
|
|
||||||
{
|
if (currentState != null && currentState.hasPieces(itemIds))
|
||||||
arrow = getRightArrow();
|
{
|
||||||
|
foundPosition = true;
|
||||||
|
shouldCache = true;
|
||||||
|
solver.setPosition(j);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (xDelta < 0)
|
}
|
||||||
|
|
||||||
|
if (foundPosition)
|
||||||
|
{
|
||||||
|
int stepsLeft = solver.getStepCount() - solver.getPosition() - 1;
|
||||||
|
|
||||||
|
if (stepsLeft == 0)
|
||||||
{
|
{
|
||||||
arrow = getLeftArrow();
|
infoString = "Solved!";
|
||||||
}
|
}
|
||||||
else if (yDelta > 0)
|
else if (config.displayRemainingMoves())
|
||||||
{
|
{
|
||||||
arrow = getDownArrow();
|
infoString = "Moves left: " + stepsLeft;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
arrow = getUpArrow();
|
infoString = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
int x = puzzleBoxLocation.getX() + blankX * PUZZLE_TILE_SIZE
|
if (config.displaySolution())
|
||||||
+ PUZZLE_TILE_SIZE / 2 - arrow.getWidth() / 2;
|
|
||||||
|
|
||||||
int y = puzzleBoxLocation.getY() + blankY * PUZZLE_TILE_SIZE
|
|
||||||
+ PUZZLE_TILE_SIZE / 2 - arrow.getHeight() / 2;
|
|
||||||
|
|
||||||
OverlayUtil.renderImageLocation(graphics, new net.runelite.api.Point(x, y), arrow);
|
|
||||||
|
|
||||||
lastBlankX = blankX;
|
|
||||||
lastBlankY = blankY;
|
|
||||||
|
|
||||||
if (++i == 3)
|
|
||||||
{
|
{
|
||||||
break;
|
if (config.drawDots())
|
||||||
|
{
|
||||||
|
graphics.setColor(Color.YELLOW);
|
||||||
|
|
||||||
|
// Display the next 4 steps
|
||||||
|
for (int i = 1; i < 5; i++)
|
||||||
|
{
|
||||||
|
int j = solver.getPosition() + i;
|
||||||
|
|
||||||
|
if (j >= solver.getStepCount())
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
PuzzleState futureMove = solver.getStep(j);
|
||||||
|
|
||||||
|
if (futureMove == null)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
int blankX = futureMove.getEmptyPiece() % DIMENSION;
|
||||||
|
int blankY = futureMove.getEmptyPiece() / DIMENSION;
|
||||||
|
|
||||||
|
int markerSize = DOT_MARKER_SIZE - i * 3;
|
||||||
|
|
||||||
|
int x = puzzleBoxLocation.getX() + blankX * PUZZLE_TILE_SIZE
|
||||||
|
+ PUZZLE_TILE_SIZE / 2 - markerSize / 2;
|
||||||
|
|
||||||
|
int y = puzzleBoxLocation.getY() + blankY * PUZZLE_TILE_SIZE
|
||||||
|
+ PUZZLE_TILE_SIZE / 2 - markerSize / 2;
|
||||||
|
|
||||||
|
graphics.fillOval(x, y, markerSize, markerSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Find the current blank tile position
|
||||||
|
PuzzleState currentMove = solver.getStep(solver.getPosition());
|
||||||
|
|
||||||
|
int lastBlankX = currentMove.getEmptyPiece() % DIMENSION;
|
||||||
|
int lastBlankY = currentMove.getEmptyPiece() / DIMENSION;
|
||||||
|
|
||||||
|
// Display the next 3 steps
|
||||||
|
for (int i = 1; i < 4; i++)
|
||||||
|
{
|
||||||
|
int j = solver.getPosition() + i;
|
||||||
|
|
||||||
|
if (j >= solver.getStepCount())
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
PuzzleState futureMove = solver.getStep(j);
|
||||||
|
|
||||||
|
if (futureMove == null)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
int blankX = futureMove.getEmptyPiece() % DIMENSION;
|
||||||
|
int blankY = futureMove.getEmptyPiece() / DIMENSION;
|
||||||
|
|
||||||
|
int xDelta = blankX - lastBlankX;
|
||||||
|
int yDelta = blankY - lastBlankY;
|
||||||
|
|
||||||
|
BufferedImage arrow;
|
||||||
|
if (xDelta > 0)
|
||||||
|
{
|
||||||
|
arrow = getRightArrow();
|
||||||
|
}
|
||||||
|
else if (xDelta < 0)
|
||||||
|
{
|
||||||
|
arrow = getLeftArrow();
|
||||||
|
}
|
||||||
|
else if (yDelta > 0)
|
||||||
|
{
|
||||||
|
arrow = getDownArrow();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
arrow = getUpArrow();
|
||||||
|
}
|
||||||
|
|
||||||
|
int x = puzzleBoxLocation.getX() + blankX * PUZZLE_TILE_SIZE
|
||||||
|
+ PUZZLE_TILE_SIZE / 2 - arrow.getWidth() / 2;
|
||||||
|
|
||||||
|
int y = puzzleBoxLocation.getY() + blankY * PUZZLE_TILE_SIZE
|
||||||
|
+ PUZZLE_TILE_SIZE / 2 - arrow.getHeight() / 2;
|
||||||
|
|
||||||
|
OverlayUtil.renderImageLocation(graphics, new net.runelite.api.Point(x, y), arrow);
|
||||||
|
|
||||||
|
lastBlankX = blankX;
|
||||||
|
lastBlankY = blankY;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Draw info box
|
||||||
|
if (infoString != null)
|
||||||
|
{
|
||||||
|
int x = puzzleBoxLocation.getX() + puzzleBox.getWidth() / 2 - INFO_BOX_WIDTH / 2;
|
||||||
|
int y = puzzleBoxLocation.getY() - INFO_BOX_OFFSET_Y;
|
||||||
|
|
||||||
|
FontMetrics fm = graphics.getFontMetrics();
|
||||||
|
int height = INFO_BOX_TOP_BORDER + fm.getHeight() + INFO_BOX_BOTTOM_BORDER;
|
||||||
|
|
||||||
|
BackgroundComponent backgroundComponent = new BackgroundComponent();
|
||||||
|
backgroundComponent.setRectangle(new Rectangle(x, y, INFO_BOX_WIDTH, height));
|
||||||
|
backgroundComponent.render(graphics, parent);
|
||||||
|
|
||||||
|
int textOffsetX = (INFO_BOX_WIDTH - fm.stringWidth(infoString)) / 2;
|
||||||
|
int textOffsetY = fm.getHeight();
|
||||||
|
|
||||||
|
TextComponent textComponent = new TextComponent();
|
||||||
|
textComponent.setPosition(new Point(x + textOffsetX, y + textOffsetY));
|
||||||
|
textComponent.setText(infoString);
|
||||||
|
textComponent.render(graphics, parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Solve the puzzle if we don't have an up to date solution
|
||||||
if (solver == null || cachedItems == null || (!shouldCache && !Arrays.equals(cachedItems, itemIds)))
|
if (solver == null || cachedItems == null || (!shouldCache && !Arrays.equals(cachedItems, itemIds)))
|
||||||
{
|
{
|
||||||
nextMoves = solve(itemIds);
|
solve(itemIds);
|
||||||
shouldCache = true;
|
shouldCache = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,31 +352,22 @@ public class PuzzleSolverOverlay extends Overlay
|
|||||||
|
|
||||||
private int[] getItemIds(ItemContainer container)
|
private int[] getItemIds(ItemContainer container)
|
||||||
{
|
{
|
||||||
return Arrays
|
int[] itemIds = new int[DIMENSION * DIMENSION];
|
||||||
.stream(container.getItems())
|
|
||||||
.mapToInt(Item::getId)
|
|
||||||
.toArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void cacheItems(int[] items)
|
Item[] items = container.getItems();
|
||||||
{
|
|
||||||
cachedItems = new int[items.length];
|
|
||||||
System.arraycopy(items, 0, cachedItems, 0, cachedItems.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Deque<Integer> solve(int[] items)
|
for (int i = 0; i < items.length; i++)
|
||||||
{
|
|
||||||
Deque<Integer> steps = new ArrayDeque<>();
|
|
||||||
|
|
||||||
solver = new PuzzleSolver(convertToSolverFormat(items));
|
|
||||||
|
|
||||||
while (solver.hasNext())
|
|
||||||
{
|
{
|
||||||
solver.next();
|
itemIds[i] = items[i].getId();
|
||||||
steps.add(solver.getBlankX() + solver.getBlankY() * PuzzleSolver.DIMENSION);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return steps;
|
// If blank is in the last position, items doesn't contain it, so let's add it manually
|
||||||
|
if (itemIds.length > items.length)
|
||||||
|
{
|
||||||
|
itemIds[items.length] = BLANK_TILE_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return convertToSolverFormat(itemIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -300,6 +407,25 @@ public class PuzzleSolverOverlay extends Overlay
|
|||||||
return convertedItems;
|
return convertedItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void cacheItems(int[] items)
|
||||||
|
{
|
||||||
|
cachedItems = new int[items.length];
|
||||||
|
System.arraycopy(items, 0, cachedItems, 0, cachedItems.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void solve(int[] items)
|
||||||
|
{
|
||||||
|
if (solverFuture != null)
|
||||||
|
{
|
||||||
|
solverFuture.cancel(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
PuzzleState puzzleState = new PuzzleState(items);
|
||||||
|
|
||||||
|
solver = new PuzzleSolver(new IDAStar(new ManhattanDistance()), puzzleState);
|
||||||
|
solverFuture = executorService.submit(solver);
|
||||||
|
}
|
||||||
|
|
||||||
private BufferedImage getDownArrow()
|
private BufferedImage getDownArrow()
|
||||||
{
|
{
|
||||||
if (downArrow == null)
|
if (downArrow == null)
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
package net.runelite.client.plugins.puzzlesolver;
|
package net.runelite.client.plugins.puzzlesolver;
|
||||||
|
|
||||||
import com.google.inject.Provides;
|
import com.google.inject.Provides;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import net.runelite.client.config.ConfigManager;
|
import net.runelite.client.config.ConfigManager;
|
||||||
import net.runelite.client.plugins.Plugin;
|
import net.runelite.client.plugins.Plugin;
|
||||||
@@ -39,6 +40,9 @@ public class PuzzleSolverPlugin extends Plugin
|
|||||||
@Inject
|
@Inject
|
||||||
private PuzzleSolverOverlay puzzleSolverOverlay;
|
private PuzzleSolverOverlay puzzleSolverOverlay;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private ScheduledExecutorService executorService;
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
PuzzleSolverConfig provideConfig(ConfigManager configManager)
|
PuzzleSolverConfig provideConfig(ConfigManager configManager)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,84 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018, Lotto <https://github.com/devLotto>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
package net.runelite.client.plugins.puzzlesolver.solver;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import net.runelite.client.plugins.puzzlesolver.solver.pathfinding.Pathfinder;
|
||||||
|
|
||||||
|
public class PuzzleSolver implements Runnable
|
||||||
|
{
|
||||||
|
public static final int DIMENSION = 5;
|
||||||
|
|
||||||
|
private Pathfinder pathfinder;
|
||||||
|
private PuzzleState startState;
|
||||||
|
|
||||||
|
private List<PuzzleState> solution;
|
||||||
|
private int position;
|
||||||
|
private boolean failed = false;
|
||||||
|
|
||||||
|
public PuzzleSolver(Pathfinder pathfinder, PuzzleState startState)
|
||||||
|
{
|
||||||
|
this.pathfinder = pathfinder;
|
||||||
|
this.startState = startState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PuzzleState getStep(int stepIdx)
|
||||||
|
{
|
||||||
|
return solution.get(stepIdx);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getStepCount()
|
||||||
|
{
|
||||||
|
return solution.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasSolution()
|
||||||
|
{
|
||||||
|
return solution != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPosition()
|
||||||
|
{
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPosition(int position)
|
||||||
|
{
|
||||||
|
this.position = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasFailed()
|
||||||
|
{
|
||||||
|
return failed;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
solution = pathfinder.computePath(startState);
|
||||||
|
failed = solution == null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,181 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018, Lotto <https://github.com/devLotto>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
package net.runelite.client.plugins.puzzlesolver.solver;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import net.runelite.client.plugins.puzzlesolver.solver.heuristics.Heuristic;
|
||||||
|
import static net.runelite.client.plugins.puzzlesolver.solver.PuzzleSolver.DIMENSION;
|
||||||
|
|
||||||
|
public class PuzzleState
|
||||||
|
{
|
||||||
|
private PuzzleState parent;
|
||||||
|
|
||||||
|
private int[] pieces;
|
||||||
|
private int emptyPiece = -1;
|
||||||
|
|
||||||
|
private int h = -1;
|
||||||
|
|
||||||
|
public PuzzleState(int[] pieces)
|
||||||
|
{
|
||||||
|
if (pieces == null)
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("Pieces cannot be null");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (DIMENSION * DIMENSION != pieces.length)
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("Piece array does not have the right dimensions");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < pieces.length; i++)
|
||||||
|
{
|
||||||
|
if (pieces[i] == -1)
|
||||||
|
{
|
||||||
|
emptyPiece = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (emptyPiece == -1)
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("Incorrect empty piece passed in!");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.pieces = pieces;
|
||||||
|
}
|
||||||
|
|
||||||
|
private PuzzleState(PuzzleState state)
|
||||||
|
{
|
||||||
|
this.pieces = Arrays.copyOf(state.pieces, state.pieces.length);
|
||||||
|
this.emptyPiece = state.emptyPiece;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<PuzzleState> computeMoves()
|
||||||
|
{
|
||||||
|
List<PuzzleState> moves = new ArrayList<>();
|
||||||
|
|
||||||
|
int emptyPieceX = emptyPiece % DIMENSION;
|
||||||
|
int emptyPieceY = emptyPiece / DIMENSION;
|
||||||
|
|
||||||
|
// Move left if there is space to the left
|
||||||
|
if (emptyPieceX > 0)
|
||||||
|
{
|
||||||
|
if (parent == null || parent.emptyPiece != emptyPiece - 1)
|
||||||
|
{
|
||||||
|
PuzzleState state = new PuzzleState(this);
|
||||||
|
state.parent = this;
|
||||||
|
|
||||||
|
state.pieces[emptyPiece - 1] = -1;
|
||||||
|
state.pieces[emptyPiece] = pieces[emptyPiece - 1];
|
||||||
|
state.emptyPiece--;
|
||||||
|
|
||||||
|
moves.add(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move right if there is space to the right
|
||||||
|
if (emptyPieceX < DIMENSION - 1)
|
||||||
|
{
|
||||||
|
if (parent == null || parent.emptyPiece != emptyPiece + 1)
|
||||||
|
{
|
||||||
|
PuzzleState state = new PuzzleState(this);
|
||||||
|
state.parent = this;
|
||||||
|
|
||||||
|
state.pieces[emptyPiece + 1] = -1;
|
||||||
|
state.pieces[emptyPiece] = pieces[emptyPiece + 1];
|
||||||
|
state.emptyPiece++;
|
||||||
|
|
||||||
|
moves.add(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move up if there is space upwards
|
||||||
|
if (emptyPieceY > 0)
|
||||||
|
{
|
||||||
|
if (parent == null || parent.emptyPiece != emptyPiece - DIMENSION)
|
||||||
|
{
|
||||||
|
PuzzleState state = new PuzzleState(this);
|
||||||
|
state.parent = this;
|
||||||
|
|
||||||
|
state.pieces[emptyPiece - DIMENSION] = -1;
|
||||||
|
state.pieces[emptyPiece] = pieces[emptyPiece - DIMENSION];
|
||||||
|
state.emptyPiece -= DIMENSION;
|
||||||
|
|
||||||
|
moves.add(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move down if there is space downwards
|
||||||
|
if (emptyPieceY < DIMENSION - 1)
|
||||||
|
{
|
||||||
|
if (parent == null || parent.emptyPiece != emptyPiece + DIMENSION)
|
||||||
|
{
|
||||||
|
PuzzleState state = new PuzzleState(this);
|
||||||
|
state.parent = this;
|
||||||
|
|
||||||
|
state.pieces[emptyPiece + DIMENSION] = -1;
|
||||||
|
state.pieces[emptyPiece] = pieces[emptyPiece + DIMENSION];
|
||||||
|
state.emptyPiece += DIMENSION;
|
||||||
|
|
||||||
|
moves.add(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return moves;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PuzzleState getParent()
|
||||||
|
{
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasPieces(int[] pieces)
|
||||||
|
{
|
||||||
|
return Arrays.equals(pieces, this.pieces);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPiece(int x, int y)
|
||||||
|
{
|
||||||
|
return pieces[y * DIMENSION + x];
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getEmptyPiece()
|
||||||
|
{
|
||||||
|
return emptyPiece;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHeuristicValue(Heuristic heuristic)
|
||||||
|
{
|
||||||
|
if (h == -1)
|
||||||
|
{
|
||||||
|
// cache the value
|
||||||
|
h = heuristic.computeValue(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018, Lotto <https://github.com/devLotto>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
package net.runelite.client.plugins.puzzlesolver.solver.heuristics;
|
||||||
|
|
||||||
|
import net.runelite.client.plugins.puzzlesolver.solver.PuzzleState;
|
||||||
|
|
||||||
|
public interface Heuristic
|
||||||
|
{
|
||||||
|
int computeValue(PuzzleState state);
|
||||||
|
}
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018, Lotto <https://github.com/devLotto>
|
||||||
|
* Copyright (c) 2018, Henke <https://github.com/henke96>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
package net.runelite.client.plugins.puzzlesolver.solver.heuristics;
|
||||||
|
|
||||||
|
import net.runelite.client.plugins.puzzlesolver.solver.PuzzleState;
|
||||||
|
import static net.runelite.client.plugins.puzzlesolver.solver.PuzzleSolver.DIMENSION;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An implementation of the manhattan distance heuristic function.
|
||||||
|
*
|
||||||
|
* https://heuristicswiki.wikispaces.com/Manhattan+Distance
|
||||||
|
*/
|
||||||
|
public class ManhattanDistance implements Heuristic
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public int computeValue(PuzzleState state)
|
||||||
|
{
|
||||||
|
int value = 0;
|
||||||
|
|
||||||
|
PuzzleState parent = state.getParent();
|
||||||
|
|
||||||
|
if (parent == null)
|
||||||
|
{
|
||||||
|
for (int x = 0; x < DIMENSION; x++)
|
||||||
|
{
|
||||||
|
for (int y = 0; y < DIMENSION; y++)
|
||||||
|
{
|
||||||
|
int piece = state.getPiece(x, y);
|
||||||
|
|
||||||
|
if (piece == -1)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int goalX = piece % DIMENSION;
|
||||||
|
int goalY = piece / DIMENSION;
|
||||||
|
|
||||||
|
value += Math.abs(x - goalX) + Math.abs(y - goalY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
If the Manhattan distance for the parent has already been
|
||||||
|
calculated, we can take advantage of that and just
|
||||||
|
add/subtract from their heuristic value.
|
||||||
|
|
||||||
|
Doing this decreases the execution time of the heuristic by about 25%.
|
||||||
|
*/
|
||||||
|
value = parent.getHeuristicValue(this);
|
||||||
|
|
||||||
|
int x = parent.getEmptyPiece() % DIMENSION;
|
||||||
|
int y = parent.getEmptyPiece() / DIMENSION;
|
||||||
|
|
||||||
|
int x2 = state.getEmptyPiece() % DIMENSION;
|
||||||
|
int y2 = state.getEmptyPiece() / DIMENSION;
|
||||||
|
|
||||||
|
int piece = state.getPiece(x, y);
|
||||||
|
|
||||||
|
if (x2 > x)
|
||||||
|
{
|
||||||
|
int targetX = piece % DIMENSION;
|
||||||
|
|
||||||
|
// right
|
||||||
|
if (targetX > x) value++;
|
||||||
|
else value--;
|
||||||
|
}
|
||||||
|
else if (x2 < x)
|
||||||
|
{
|
||||||
|
int targetX = piece % DIMENSION;
|
||||||
|
|
||||||
|
// left
|
||||||
|
if (targetX < x) value++;
|
||||||
|
else value--;
|
||||||
|
}
|
||||||
|
else if (y2 > y)
|
||||||
|
{
|
||||||
|
int targetY = piece / DIMENSION;
|
||||||
|
|
||||||
|
// down
|
||||||
|
if (targetY > y) value++;
|
||||||
|
else value--;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int targetY = piece / DIMENSION;
|
||||||
|
|
||||||
|
// up
|
||||||
|
if (targetY < y) value++;
|
||||||
|
else value--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018, Lotto <https://github.com/devLotto>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
package net.runelite.client.plugins.puzzlesolver.solver.pathfinding;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import net.runelite.client.plugins.puzzlesolver.solver.PuzzleState;
|
||||||
|
import net.runelite.client.plugins.puzzlesolver.solver.heuristics.Heuristic;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An implementation of the IDA* algorithm.
|
||||||
|
*
|
||||||
|
* https://en.wikipedia.org/wiki/Iterative_deepening_A*
|
||||||
|
*/
|
||||||
|
public class IDAStar extends Pathfinder
|
||||||
|
{
|
||||||
|
public IDAStar(Heuristic heuristic)
|
||||||
|
{
|
||||||
|
super(heuristic);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PuzzleState> computePath(PuzzleState root)
|
||||||
|
{
|
||||||
|
PuzzleState goalNode = path(root);
|
||||||
|
|
||||||
|
if (goalNode == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<PuzzleState> path = new ArrayList<>();
|
||||||
|
|
||||||
|
PuzzleState parent = goalNode;
|
||||||
|
while (parent != null)
|
||||||
|
{
|
||||||
|
path.add(0, parent);
|
||||||
|
parent = parent.getParent();
|
||||||
|
}
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
private PuzzleState path(PuzzleState root)
|
||||||
|
{
|
||||||
|
int bound = root.getHeuristicValue(getHeuristic());
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
PuzzleState t = search(root, 0, bound);
|
||||||
|
|
||||||
|
if (t != null)
|
||||||
|
{
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
bound += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private PuzzleState search(PuzzleState node, int g, int bound)
|
||||||
|
{
|
||||||
|
int h = node.getHeuristicValue(getHeuristic());
|
||||||
|
int f = g + h;
|
||||||
|
|
||||||
|
if (f > bound)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (h == 0)
|
||||||
|
{
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (PuzzleState successor : node.computeMoves())
|
||||||
|
{
|
||||||
|
PuzzleState t = search(successor, g + 1, bound);
|
||||||
|
|
||||||
|
if (t != null)
|
||||||
|
{
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018, Lotto <https://github.com/devLotto>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
package net.runelite.client.plugins.puzzlesolver.solver.pathfinding;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import net.runelite.client.plugins.puzzlesolver.solver.PuzzleState;
|
||||||
|
import net.runelite.client.plugins.puzzlesolver.solver.heuristics.Heuristic;
|
||||||
|
|
||||||
|
public abstract class Pathfinder
|
||||||
|
{
|
||||||
|
private Heuristic heuristic;
|
||||||
|
|
||||||
|
Pathfinder(Heuristic heuristic)
|
||||||
|
{
|
||||||
|
this.heuristic = heuristic;
|
||||||
|
}
|
||||||
|
|
||||||
|
Heuristic getHeuristic()
|
||||||
|
{
|
||||||
|
return heuristic;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract List<PuzzleState> computePath(PuzzleState start);
|
||||||
|
}
|
||||||
@@ -25,29 +25,77 @@
|
|||||||
*/
|
*/
|
||||||
package net.runelite.client.plugins.puzzlesolver;
|
package net.runelite.client.plugins.puzzlesolver;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import net.runelite.client.plugins.puzzlesolver.solver.PuzzleSolver;
|
||||||
|
import net.runelite.client.plugins.puzzlesolver.solver.PuzzleState;
|
||||||
|
import net.runelite.client.plugins.puzzlesolver.solver.heuristics.ManhattanDistance;
|
||||||
|
import net.runelite.client.plugins.puzzlesolver.solver.pathfinding.IDAStar;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class PuzzleSolverTest
|
public class PuzzleSolverTest
|
||||||
{
|
{
|
||||||
private static final int[] TEST_STATE = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 15, 8, 23, 22, 16, -1, 19, 12, 17, 14, 9, 11, 10, 18, 21, 13, 20};
|
private static final PuzzleState[] START_STATES =
|
||||||
private static final int[] FINISHED_STATE = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
|
{
|
||||||
|
new PuzzleState(new int[]{0, 11, 1, 3, 4, 5, 12, 2, 7, 9, 6, 20, 18, 16, 8, 15, 22, 10, 14, 13, 21, -1, 17, 23, 19}),
|
||||||
|
new PuzzleState(new int[]{0, 2, 7, 3, 4, 10, 5, 12, 1, 9, 6, 17, 8, 14, 19, -1, 16, 21, 11, 13, 15, 20, 22, 18, 23}),
|
||||||
|
new PuzzleState(new int[]{0, 1, 11, 3, 4, 12, 2, 7, 13, 9, 5, 21, 15, 17, 14, -1, 10, 6, 8, 19, 16, 20, 22, 18, 23}),
|
||||||
|
new PuzzleState(new int[]{0, 1, 2, 3, 4, 10, 5, 6, 9, 14, 15, -1, 7, 13, 17, 21, 11, 20, 23, 8, 16, 22, 12, 19, 18}),
|
||||||
|
new PuzzleState(new int[]{0, 1, 2, 3, 4, 5, 6, 8, 22, 18, 10, -1, 7, 17, 9, 20, 11, 12, 21, 14, 16, 15, 23, 13, 19}),
|
||||||
|
new PuzzleState(new int[]{0, 1, 2, 3, 4, 5, 6, 8, 12, 9, 16, 11, 17, 7, 14, 10, 20, -1, 22, 13, 15, 18, 21, 23, 19}),
|
||||||
|
new PuzzleState(new int[]{1, 6, 16, 8, 4, 0, 7, 11, 2, 9, 5, 21, 18, 3, 14, 10, 20, -1, 13, 22, 15, 23, 12, 17, 19}),
|
||||||
|
new PuzzleState(new int[]{0, 1, 2, 4, 8, 6, 16, 11, 7, 3, 5, -1, 12, 14, 9, 15, 10, 17, 13, 18, 20, 21, 22, 23, 19}),
|
||||||
|
new PuzzleState(new int[]{0, 2, 9, 14, 6, 5, 7, 11, 3, 4, 15, 10, 1, 12, 18, 16, 17, -1, 8, 13, 20, 21, 22, 23, 19}),
|
||||||
|
new PuzzleState(new int[]{0, 1, 2, 3, 4, 11, 5, 12, 7, 8, 10, 6, 15, 13, 9, 16, 21, -1, 17, 14, 20, 22, 23, 18, 19}),
|
||||||
|
new PuzzleState(new int[]{5, 0, 1, 2, 4, 10, 6, 3, 8, 9, 12, 13, 7, 14, 19, 15, 11, 16, 17, -1, 20, 21, 22, 18, 23}),
|
||||||
|
new PuzzleState(new int[]{0, 6, 1, 3, 4, 5, 8, -1, 2, 9, 16, 11, 12, 7, 14, 10, 15, 17, 13, 19, 20, 21, 22, 18, 23}),
|
||||||
|
new PuzzleState(new int[]{0, 6, 1, 2, 4, 11, 15, 8, 3, 14, 5, 7, 9, 12, 18, 16, 10, 17, 23, 13, 20, 21, 22, -1, 19}),
|
||||||
|
new PuzzleState(new int[]{0, 1, 7, 2, 4, 5, 3, 12, 8, 9, 15, 6, 18, -1, 13, 11, 10, 22, 17, 23, 16, 21, 20, 19, 14}),
|
||||||
|
new PuzzleState(new int[]{0, 1, 2, 7, 3, 5, 11, 6, 14, 4, 10, -1, 16, 12, 9, 15, 17, 18, 8, 19, 20, 21, 13, 22, 23}),
|
||||||
|
new PuzzleState(new int[]{2, 10, 5, 3, 4, -1, 0, 1, 8, 9, 15, 11, 7, 13, 23, 17, 6, 20, 14, 19, 16, 12, 18, 21, 22}),
|
||||||
|
new PuzzleState(new int[]{0, 1, 2, 8, 9, 5, 6, 7, 3, 4, 10, -1, 14, 23, 18, 21, 11, 16, 12, 19, 15, 20, 17, 13, 22}),
|
||||||
|
new PuzzleState(new int[]{0, 6, 1, 3, 4, 11, 2, 13, 9, 12, 5, 16, 7, 18, 8, 20, 15, -1, 14, 19, 21, 10, 22, 23, 17}),
|
||||||
|
new PuzzleState(new int[]{12, 1, 2, 3, 4, 0, 7, 6, 8, 9, 5, 10, 22, 13, 19, 15, 11, 21, 14, 17, 20, 16, 18, -1, 23}),
|
||||||
|
new PuzzleState(new int[]{0, 2, 11, 3, 4, 5, 1, 6, 8, 9, 15, 10, 13, 14, 19, 7, 12, -1, 17, 18, 20, 21, 16, 22, 23}),
|
||||||
|
new PuzzleState(new int[]{5, 0, 4, 2, 9, 10, 7, 3, 19, 8, 6, 1, 18, -1, 14, 15, 11, 16, 12, 13, 20, 21, 17, 22, 23}),
|
||||||
|
new PuzzleState(new int[]{0, 3, 2, 7, 4, 6, 10, 1, 8, 9, 15, 5, 12, 18, 13, -1, 20, 11, 22, 14, 16, 21, 23, 17, 19}),
|
||||||
|
new PuzzleState(new int[]{1, 2, 4, -1, 9, 0, 5, 7, 3, 14, 10, 6, 8, 13, 19, 15, 11, 18, 12, 22, 20, 16, 21, 23, 17}),
|
||||||
|
new PuzzleState(new int[]{0, 1, 2, 4, 9, 5, 11, -1, 7, 14, 10, 17, 6, 13, 8, 15, 16, 20, 3, 18, 22, 21, 12, 23, 19}),
|
||||||
|
new PuzzleState(new int[]{0, 1, 8, 2, 4, 5, 11, 17, 3, 9, 6, 16, 7, 12, 18, 15, 21, -1, 14, 13, 20, 22, 10, 23, 19}),
|
||||||
|
new PuzzleState(new int[]{5, 0, 2, 3, 4, 1, 8, 6, 7, 9, 11, 12, 16, 13, 14, -1, 22, 20, 17, 19, 21, 10, 15, 18, 23}),
|
||||||
|
new PuzzleState(new int[]{0, -1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 16, 11, 13, 14, 10, 15, 19, 22, 23, 20, 21, 17, 12, 18}),
|
||||||
|
new PuzzleState(new int[]{0, 2, 7, -1, 4, 6, 1, 9, 3, 14, 5, 12, 8, 13, 19, 15, 16, 10, 22, 23, 20, 11, 18, 21, 17}),
|
||||||
|
new PuzzleState(new int[]{7, 5, 13, 1, 12, 0, 2, 4, 3, 9, 10, 6, 8, 17, 14, 15, 11, 16, 18, -1, 20, 21, 22, 23, 19}),
|
||||||
|
new PuzzleState(new int[]{5, 0, 3, 8, 4, 10, 6, -1, 7, 9, 11, 1, 12, 2, 19, 15, 16, 17, 14, 13, 20, 21, 22, 18, 23}),
|
||||||
|
new PuzzleState(new int[]{5, 2, 3, 7, 4, 0, 6, 14, 9, 19, 1, 11, 22, 17, 12, 10, 15, -1, 13, 8, 20, 16, 21, 18, 23}),
|
||||||
|
new PuzzleState(new int[]{0, 1, 3, 4, 9, 5, 6, 2, 7, 14, 10, 13, 17, -1, 8, 15, 11, 16, 12, 18, 20, 21, 22, 23, 19}),
|
||||||
|
new PuzzleState(new int[]{0, 3, 11, 7, 4, 5, 2, 6, 12, 8, 10, 1, 17, -1, 9, 15, 16, 23, 13, 14, 20, 21, 18, 22, 19}),
|
||||||
|
new PuzzleState(new int[]{1, 5, 8, 2, 4, -1, 0, 7, 3, 9, 11, 22, 15, 12, 14, 6, 10, 18, 16, 19, 20, 21, 17, 13, 23}),
|
||||||
|
new PuzzleState(new int[]{7, 12, 11, 4, 9, -1, 0, 8, 10, 2, 6, 1, 16, 3, 14, 5, 15, 17, 13, 19, 20, 21, 22, 18, 23}),
|
||||||
|
new PuzzleState(new int[]{11, 3, 2, 12, 4, 6, 0, 7, 13, 8, 1, 5, 17, 16, 9, -1, 10, 15, 18, 14, 20, 21, 22, 23, 19}),
|
||||||
|
new PuzzleState(new int[]{0, 6, 1, 3, 4, 5, 11, 2, 10, 9, 15, 12, 8, 14, 19, 16, 21, -1, 7, 13, 20, 22, 17, 18, 23}),
|
||||||
|
new PuzzleState(new int[]{0, 1, 2, 3, 4, 6, 10, 7, 8, 9, 5, 16, 11, 14, 17, 20, 13, 18, 12, 22, 21, 15, 23, -1, 19}),
|
||||||
|
new PuzzleState(new int[]{0, 1, 2, 4, 8, 5, 6, 7, 3, -1, 10, 16, 18, 17, 9, 15, 12, 11, 14, 13, 20, 21, 22, 23, 19}),
|
||||||
|
new PuzzleState(new int[]{0, 11, 6, 1, 4, 5, 21, 8, 2, 9, 10, 3, 16, -1, 14, 15, 12, 17, 13, 19, 20, 22, 7, 18, 23}),
|
||||||
|
new PuzzleState(new int[]{0, 6, 1, 2, 4, 11, 10, 3, 13, 9, 5, 7, 8, -1, 23, 15, 16, 22, 18, 14, 20, 21, 12, 17, 19}),
|
||||||
|
new PuzzleState(new int[]{0, 6, 1, 2, 3, 10, 11, 12, 5, 18, 15, 7, 4, -1, 14, 21, 17, 13, 8, 9, 16, 20, 22, 23, 19}),
|
||||||
|
new PuzzleState(new int[]{0, 1, 3, 11, 4, 6, 10, 14, 2, 8, 5, -1, 12, 7, 9, 15, 16, 18, 13, 19, 20, 21, 22, 17, 23}),
|
||||||
|
new PuzzleState(new int[]{1, 5, 2, 3, 4, -1, 0, 7, 14, 8, 11, 6, 13, 9, 23, 10, 12, 15, 19, 17, 20, 21, 16, 22, 18}),
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final int[] FINISHED_STATE = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, -1};
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSolver()
|
public void testSolver()
|
||||||
{
|
{
|
||||||
PuzzleSolver solver = new PuzzleSolver(TEST_STATE);
|
for (PuzzleState state : START_STATES)
|
||||||
|
|
||||||
assertEquals(solver.hasNext(), true);
|
|
||||||
|
|
||||||
while (solver.hasNext())
|
|
||||||
{
|
{
|
||||||
solver.next();
|
PuzzleSolver solver = new PuzzleSolver(new IDAStar(new ManhattanDistance()), state);
|
||||||
}
|
solver.run();
|
||||||
|
|
||||||
for (int i = 0; i < FINISHED_STATE.length; i++)
|
assertTrue(solver.hasSolution());
|
||||||
{
|
assertFalse(solver.hasFailed());
|
||||||
assertEquals(solver.getTiles()[i], FINISHED_STATE[i]);
|
assertTrue(solver.getStep(solver.getStepCount() - 1).hasPieces(FINISHED_STATE));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user