Skip to content

Commit 957a33c

Browse files
committed
Need to work on A*
0 parents  commit 957a33c

9 files changed

+343
-0
lines changed

.classpath

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Ass2</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

.settings/org.eclipse.jdt.core.prefs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#Sun Jun 05 20:32:52 EST 2011
2+
eclipse.preferences.version=1
3+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
5+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6+
org.eclipse.jdt.core.compiler.compliance=1.6
7+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
9+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.source=1.6

src/ass02/Board.java

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package ass02;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.PriorityQueue;
6+
7+
public class Board implements SlidingBlock {
8+
9+
LinkedList<Integer> path = new LinkedList<Integer>();
10+
int n;
11+
int[] board;
12+
Node[] node;
13+
int startPosition;
14+
15+
16+
//PriorityQueue<Node> openSet;
17+
//ArrayList<Node> closedSet;
18+
19+
public Board(int n){
20+
this.n = n;
21+
board = new int[n*n];
22+
}
23+
24+
@Override
25+
public int[] solve(int[] start, int[] goal, int maxMoves) {
26+
// TODO Auto-generated method stub
27+
return null;
28+
}
29+
30+
@Override
31+
public int[] shortestPath(int startPosition) {
32+
this.startPosition = startPosition;
33+
initialiseSPNodes(startPosition);
34+
this.aStar(node[startPosition], node[0], Integer.MAX_VALUE);
35+
return null;
36+
}
37+
38+
@Override
39+
public void addWall(int positionI, int positionJ) {
40+
// TODO Auto-generated method stub
41+
42+
}
43+
44+
public void initialiseBoard(int startPos){
45+
for(int i = 0; i < this.board.length; i++){
46+
if(i == startPos){
47+
this.board[i] = 0;
48+
} else{
49+
this.board[i] = 1;
50+
}
51+
}
52+
}
53+
54+
public void initialiseSPNodes(int startPos){
55+
node = new SPNode[n*n];
56+
for(int i = 0; i < this.node.length; i++){
57+
this.node[i] = new SPNode(n);
58+
if(i == startPos){
59+
this.node[i].f = 0;
60+
this.node[i].g = 0;
61+
this.node[i].h = 0;
62+
this.node[i].location = i;
63+
} else{
64+
this.node[i].g = 0;
65+
this.node[i].h = 0;
66+
this.node[i].f = Integer.MAX_VALUE;
67+
this.node[i].location = i;
68+
}
69+
}
70+
71+
for(int i = 0; i < this.node.length; i++){
72+
node[i].setIndexesToOtherNodes();
73+
}
74+
node[0].setN(n);
75+
}
76+
77+
public void printBoardToConsole(){
78+
for(int i = 0; i < this.node.length; i++){
79+
System.out.print(this.node[i].location+" ");
80+
if((i+1) % n == 0){
81+
System.out.print("\n");
82+
}
83+
}
84+
}
85+
86+
public Node up(Node node){
87+
if(node.indexOfUp() < 0) return null;
88+
return this.node[node.indexOfUp()];
89+
}
90+
public Node down(Node node){
91+
if(node.indexOfDown() < 0) return null;
92+
return this.node[node.indexOfDown()];
93+
}
94+
public Node left(Node node){
95+
if(node.indexOfLeft() < 0) return null;
96+
return this.node[node.indexOfLeft()];
97+
}
98+
public Node right(Node node){
99+
if(node.indexOfRight() < 0) return null;
100+
return this.node[node.indexOfRight()];
101+
}
102+
103+
public Node getNode(int pos){
104+
return node[pos];
105+
}
106+
107+
public boolean reachedGoal(Node node){
108+
return node.location < n;
109+
}
110+
111+
public ArrayList<Node> getDirectlyAttachedNodes(Node n){
112+
ArrayList<Node> list = new ArrayList<Node>();
113+
if(this.up(n) != null) list.add(this.up(n));
114+
if(this.down(n) != null) list.add(this.down(n));
115+
if(this.left(n) != null) list.add(this.left(n));
116+
if(this.right(n) != null) list.add(this.right(n));
117+
return list;
118+
}
119+
120+
121+
private int[] aStar(Node startNode, Node goalNode, int maxLength) {
122+
ArrayList<Node> closedSet = new ArrayList<Node>();
123+
PriorityQueue<Node> openSet = new PriorityQueue<Node>();
124+
startNode.h = this.heuristicDistance(startNode, goalNode);
125+
int tempG;
126+
Node current;
127+
boolean yInOpenSet;
128+
129+
while (!openSet.isEmpty()) {
130+
current = openSet.poll();
131+
if(this.reachedGoal(current)){
132+
System.out.print("done");
133+
}
134+
closedSet.add(current);
135+
if (current.g >= maxLength) continue;
136+
this.printBoardToConsole();
137+
for (Node attached : getDirectlyAttachedNodes(current)) {
138+
if (closedSet.contains(attached)) continue;
139+
tempG = current.g + 1;
140+
141+
yInOpenSet = false;
142+
for (Node thisState : openSet) {
143+
if (attached.equals(thisState)) {
144+
attached = thisState;
145+
yInOpenSet = true;
146+
}
147+
}
148+
if (!yInOpenSet || tempG < attached.g) {
149+
attached.parent = current;
150+
attached.g = tempG;
151+
attached.h = heuristicDistance(attached, goalNode);
152+
attached.f = attached.g + attached.h;
153+
}
154+
if (!yInOpenSet && attached.f <= maxLength) openSet.add(attached);
155+
}
156+
}
157+
158+
return null;
159+
}
160+
161+
private int heuristicDistance(Node from, Node to){
162+
return(Math.abs(from.col() - to.col()) + Math.abs(from.row() - to.row()));
163+
}
164+
165+
public void reconstructPath(Node node){
166+
if(node.parent != null){
167+
reconstructPath(node.parent);
168+
} else{
169+
path.add(node.location);
170+
}
171+
}
172+
}

src/ass02/Factory.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ass02;
2+
3+
public class Factory
4+
{
5+
6+
/** Make a sliding block puzzle of size n x n. */
7+
public static SlidingBlock make (int n)
8+
{
9+
return new Board(n);
10+
}
11+
12+
}

src/ass02/Node.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ass02;
2+
3+
import java.util.Comparator;
4+
5+
public abstract class Node implements Comparator<Node> {
6+
7+
static int n;
8+
int g;
9+
int h;
10+
int f;
11+
int location;
12+
Node parent;
13+
//boolean isHole;
14+
15+
public void setN(int nVal){
16+
n = nVal;
17+
}
18+
19+
public int getN(){
20+
return n;
21+
}
22+
23+
24+
public abstract int indexOfLeft();
25+
public abstract int indexOfRight();
26+
public abstract int indexOfUp();
27+
public abstract int indexOfDown();
28+
public abstract void setIndexesToOtherNodes();
29+
30+
public int row(){
31+
return (this.location - this.col()) / n;
32+
}
33+
34+
public int col(){
35+
return this.location % n;
36+
}
37+
}

src/ass02/SBPuzzle.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ass02;
2+
3+
public class SBPuzzle {
4+
5+
/**
6+
* @param args
7+
*/
8+
public static void main(String[] args) {
9+
Board b = new Board(3);
10+
b.initialiseBoard(8);
11+
b.shortestPath(8);
12+
b.printBoardToConsole();
13+
//int i = b.getNode(8).indexOfUp();
14+
//
15+
//System.out.print(i);
16+
}
17+
18+
}

src/ass02/SPNode.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package ass02;
2+
3+
public class SPNode extends Node {
4+
int up;
5+
int down;
6+
int left;
7+
int right;
8+
9+
public SPNode(int dimensions){
10+
n = dimensions;
11+
}
12+
13+
@Override
14+
public int compare(Node n1, Node n2) {
15+
return n1.f - n2.f;
16+
}
17+
18+
@Override
19+
public int indexOfLeft(){
20+
if(this.location % n == 0)
21+
return -1;
22+
return this.location - 1;
23+
}
24+
@Override
25+
public int indexOfRight(){
26+
if((this.location + 1) % n == 0)
27+
return -1;
28+
return this.location + 1;
29+
}
30+
@Override
31+
public int indexOfUp(){
32+
if(this.location - n < 0)
33+
return -1;
34+
return this.location - n;
35+
}
36+
@Override
37+
public int indexOfDown(){
38+
if(this.location + n >= (n*n))
39+
return -1;
40+
return this.location + n;
41+
}
42+
43+
@Override
44+
public void setIndexesToOtherNodes() {
45+
this.down = indexOfDown();
46+
this.up = indexOfUp();
47+
this.left = indexOfLeft();
48+
this.right = indexOfRight();
49+
50+
}
51+
52+
@Override
53+
public String toString(){
54+
return "#:"+this.location+" up:"+this.up+" down:"+this.down+" left:"+this.left+" right:"+this.right;
55+
}
56+
}

src/ass02/SlidingBlock.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ass02;
2+
/** A sliding block puzzle, with walls between positions. */
3+
public interface SlidingBlock
4+
{
5+
/** Solve a puzzle with a minimal number of moves, no more than some specified number. */
6+
int[] solve (int[] start, int[] goal, int maxMoves);
7+
8+
/** Find a shortest path from a given position to the first row of the puzzle. */
9+
int[] shortestPath (int startPosition);
10+
11+
/** Block direct moves between two adjacent positions. */
12+
void addWall (int positionI, int positionJ);
13+
}

0 commit comments

Comments
 (0)