1- import java .awt .Dimension ;
2-
31/**
42 * 2D Maze Search
53 *
@@ -23,8 +21,8 @@ private void doSearchOn2DGrid() {
2321 int height = maze .getHeight ();
2422 boolean alReadyVisitedFlag [][] = new boolean [width ][height ];
2523 //float distanceToNode[][] = new float[width][height];
26- Dimension predecessor [][] = new Dimension [width ][height ];
27- DimensionQueue queue = new DimensionQueue ();
24+ Location predecessor [][] = new Location [width ][height ];
25+ LocationQueue queue = new LocationQueue ();
2826
2927 for (int i =0 ; i <width ; i ++) {
3028 for (int j =0 ; j <height ; j ++) {
@@ -34,19 +32,19 @@ private void doSearchOn2DGrid() {
3432 }
3533 }
3634
37- alReadyVisitedFlag [startLoc .width ][startLoc .height ] = true ;
35+ alReadyVisitedFlag [startLoc .x ][startLoc .y ] = true ;
3836 //distanceToNode[startLoc.width][startLoc.height] = 0.0f;
3937 queue .addToBackOfQueue (startLoc );
4038 boolean success = false ;
4139 outer :
4240 while (queue .isEmpty () == false ) {
43- Dimension head = queue .peekAtFrontOfQueue ();
41+ Location head = queue .peekAtFrontOfQueue ();
4442 if (head == null ) break ; // ??
45- Dimension [] connected = getPossibleMoves (head );
43+ Location [] connected = getPossibleMoves (head );
4644 for (int i =0 ; i <4 ; i ++) {
4745 if (connected [i ] == null ) break ;
48- int w = connected [i ].width ;
49- int h = connected [i ].height ;
46+ int w = connected [i ].x ;
47+ int h = connected [i ].y ;
5048 if (alReadyVisitedFlag [w ][h ] == false ) {
5149 //distanceToNode[w][h] = distanceToNode[w][h] + 1.0f;
5250 alReadyVisitedFlag [w ][h ] = true ;
@@ -65,31 +63,31 @@ private void doSearchOn2DGrid() {
6563 if (success ) {
6664 searchPath [maxDepth ++] = goalLoc ;
6765 for (int i =0 ; i <100 ; i ++) {
68- searchPath [maxDepth ] = predecessor [searchPath [maxDepth - 1 ].width ][searchPath [maxDepth - 1 ].height ];
66+ searchPath [maxDepth ] = predecessor [searchPath [maxDepth - 1 ].x ][searchPath [maxDepth - 1 ].y ];
6967 maxDepth ++;
7068 if (equals (searchPath [maxDepth - 1 ], startLoc )) break ; // back to starting node
7169 }
7270 }
7371 }
74- protected class DimensionQueue {
75- public DimensionQueue (int num ) {
76- queue = new Dimension [num ];
72+ protected class LocationQueue {
73+ public LocationQueue (int num ) {
74+ queue = new Location [num ];
7775 head = tail = 0 ;
7876 len = num ;
7977 }
80- public DimensionQueue () {
78+ public LocationQueue () {
8179 this (400 );
8280 }
83- public void addToBackOfQueue (Dimension n ) {
81+ public void addToBackOfQueue (Location n ) {
8482 queue [tail ] = n ;
8583 if (tail >= (len - 1 )) {
8684 tail = 0 ;
8785 } else {
8886 tail ++;
8987 }
9088 }
91- public Dimension removeFromFrontOfQueue () {
92- Dimension ret = queue [head ];
89+ public Location removeFromFrontOfQueue () {
90+ Location ret = queue [head ];
9391 if (head >= (len - 1 )) {
9492 head = 0 ;
9593 } else {
@@ -100,10 +98,10 @@ public Dimension removeFromFrontOfQueue() {
10098 public boolean isEmpty () {
10199 return head == (tail + 1 );
102100 }
103- public Dimension peekAtFrontOfQueue () {
101+ public Location peekAtFrontOfQueue () {
104102 return queue [head ];
105103 }
106- private Dimension [] queue ;
104+ private Location [] queue ;
107105 private int tail , head , len ;
108106 }
109107}
0 commit comments