|
| 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 | +} |
0 commit comments