Skip to content
Snippets Groups Projects
Commit 6fabb793 authored by Bariatti Francesco's avatar Bariatti Francesco
Browse files

added solve method to Solver

parent b15005e4
No related branches found
No related tags found
No related merge requests found
...@@ -34,4 +34,14 @@ public class Board { ...@@ -34,4 +34,14 @@ public class Board {
public int getCarsNumber() { public int getCarsNumber() {
return cars.length; return cars.length;
} }
public int[][] getBoard()
{
return board;
}
public void setBoard(int[][] board)
{
this.board = board;
}
} }
package model; package model;
import java.util.List; import java.util.*;
import java.util.PriorityQueue;
import java.util.Queue;
/**
* Solve the problem using A*
*/
public class Solver { public class Solver {
private Queue<State> states; private Queue<State> states;
private State currentState; private State currentState;
private Board board; private Board board;
/**
* Create a new Solver. call the solve method to start solving the problem
*/
public Solver(Board board) public Solver(Board board)
{ {
this.states = new PriorityQueue<>(); this.states = new PriorityQueue<>();
...@@ -16,14 +20,42 @@ public class Solver { ...@@ -16,14 +20,42 @@ public class Solver {
this.currentState = null; this.currentState = null;
} }
public List<Move> solve() /**
* Solve the problem
* @param resetBoard If the board should be in its initial state at the end of the algorithm
* @return A Queue of moves so that the first move in the list is the first move you should play at the beginning of the game. Or null if the game has no solutions.
*/
public Deque<Move> solve(boolean resetBoard)
{ {
this.currentState = null;
this.states.add(new State(board, null, null));
// Executing A* until we find a solution or we explored all of the tree
while((!board.isSolved()) && (!states.isEmpty()))
aStar();
// Construction of the resulting move list
if(board.isSolved())
{
State s = currentState;
Deque<Move> result = new ArrayDeque<>();
while(s != null)
{
if (s.getMove() != null)
{
result.addFirst(s.getMove());
if(resetBoard)
s.getMove().getOpposite().play(board.getBoard());
}
s = s.getParent();
}
return result;
}
//TODO: 4/27/16
return null; return null;
} }
private void AStar() private void aStar()
{ {
} }
......
...@@ -6,12 +6,12 @@ public class State implements Comparable<State> ...@@ -6,12 +6,12 @@ public class State implements Comparable<State>
private Move move; private Move move;
private State parent; private State parent;
public State(int[][] board, Move move, State parent) public State(Board board, Move move, State parent)
{ {
this.move = move; this.move = move;
this.parent = parent; this.parent = parent;
this.cost = parent.cost + 1; this.cost = parent.cost + 1;
this.heuristic = naiveHeuristic(board); this.heuristic = naiveHeuristic(board.getBoard());
} }
public State getParent() public State getParent()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment