📄 problemsolver.java
字号:
/*
* Created on 9-okt-2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package logic;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.LinkedList;
/**
* Each problem solving method needs to implement the ProblemSolver.
* It contains all methods to navigate through the solution.
*/
public abstract class ProblemSolver {
protected LinkedList list = new LinkedList();
public abstract PuzzleState getSolution(PuzzleState start, PuzzleState target);
protected int intPositionInList = 0;
protected long lngTimeInMillis = 0;
protected long lngStartTimeInMillis = 0;
protected int intNumberOfNodesExpanded = 0;
//private SpeelBordToestand huidigeKnoop;
/**
*
* When the search algorithm found the solution, you have to construct the path from source to target.
* This happens in this method. Run back through the root of the tree and add each node of the tree to a linked list.
*
* @param solution Is de oplossing waarvan alle parents in een list moeten geplaatst worden
*/
protected void createSolutionList(PuzzleState solution)
{
solutionFound();
list.clear();
PuzzleState huidigeKnoop = solution;
list.addFirst(solution);
while(huidigeKnoop != null)
{
list.addFirst(huidigeKnoop);
huidigeKnoop = huidigeKnoop.getParent();
}
}
/**
*
* Get first puzzle state
*/
public PuzzleState getFirst()
{
intPositionInList = 0;
return (PuzzleState)list.get(intPositionInList);
}
/**
*
* Get next puzzle state
*/
public PuzzleState getNext()
{
PuzzleState volgende = null;
if(intPositionInList + 1 < list.size())
intPositionInList++;
return (PuzzleState)list.get(intPositionInList);
}
/**
*
* Get previous puzzle state
*/
public PuzzleState getPrevious()
{
PuzzleState vorige = null;
if(intPositionInList > 0)
intPositionInList--;
return (PuzzleState)list.get(intPositionInList);
}
public int getNumberOfNodesExpanded()
{
return intNumberOfNodesExpanded;
}
public int getNumberOfMovesToSolution()
{
return list.size() - 2;//minus two because we don't cant in the 'source' and the 'target' state.
}
/*
* Get the time it took to solve the puzzle.
*/
public long getTimeElapsed()
{
return lngTimeInMillis;
}
/*
* Get a HTML representation of the result of the search.
* */
public String getSummaryInHtml()
{
StringBuffer html = new StringBuffer("<html><body><table><tr><td>");
html.append("Number of moves: </td><td>" + this.getNumberOfMovesToSolution());
html.append("</td></tr><tr><td>Number of nodes expanded: </td><td>" + this.getNumberOfNodesExpanded());
html.append("</td></tr><tr><td>Time elapsed (in millisec.): </td><td>" + this.getTimeElapsed());
html.append("</table></body></html>");
return html.toString();
}
/*
* Method to be called when the search is started
* */
protected void solutionFinderStarted()
{
Calendar cal = new GregorianCalendar();
lngStartTimeInMillis = cal.getTimeInMillis();
}
/*
* Method to be called after the search is finished
* */
private void solutionFound()
{
Calendar cal = new GregorianCalendar();
lngTimeInMillis = cal.getTimeInMillis() - lngStartTimeInMillis;
}
protected void clearLists()
{
list.clear();
intPositionInList = 0;
intNumberOfNodesExpanded = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -