A library for solving mazes.

Revision:
1:80b73beb7742
Parent:
0:dddf6e50f1e7
Child:
2:5343f19381c8
diff -r dddf6e50f1e7 -r 80b73beb7742 Lees_Algorithm.cpp
--- a/Lees_Algorithm.cpp	Fri Jun 24 22:37:49 2011 +0000
+++ b/Lees_Algorithm.cpp	Sat Jun 25 07:22:36 2011 +0000
@@ -104,8 +104,50 @@
     return;
 }
 
-char Lees_Algorithm::getDirection(_location position) {
+char Lees_Algorithm::getDirection(_location position, char facing) {
     char buffer;
+    unsigned char minimumWeight = 255;
+    bool direction[4] = {false, false, false, false};
+    
+    if(!map[position.y][position.x].north && map[(position.y - 1) % mazeSize][position.x].weight < minimumWeight) minimumWeight = map[(position.y - 1) % mazeSize][position.x].weight;
+    if(!map[position.y][position.x].west && map[position.y][(position.x - 1) % mazeSize].weight < minimumWeight) minimumWeight = map[position.y][(position.x - 1) % mazeSize].weight;
+    if(!map[(position.y + 1) % mazeSize][position.x].north && map[(position.y + 1) % mazeSize][position.x].weight < minimumWeight) minimumWeight = map[(position.y + 1) % mazeSize][position.x].weight;
+    if(!map[position.y][(position.x + 1) % mazeSize].west && map[position.y][(position.x + 1) % mazeSize].weight < minimumWeight) minimumWeight = map[position.y][(position.x + 1) % mazeSize].weight;
+    
+    if(!map[position.y][position.x].north && map[(position.y - 1) % mazeSize][position.x].weight == minimumWeight) direction[0] = true;
+        if(!map[position.y][position.x].west && map[position.y][(position.x - 1) % mazeSize].weight == minimumWeight) direction[1] = true;
+        if(!map[(position.y + 1) % mazeSize][position.x].north && map[(position.y + 1) % mazeSize][position.x].weight == minimumWeight) direction[2] = true;
+        if(!map[position.y][(position.x + 1) % mazeSize].west && map[position.y][(position.x + 1) % mazeSize].weight == minimumWeight) direction[3] = true;
+    
+    switch(facing) {
+        case 'n':
+            if(direction[3] == true) buffer = 's'; 
+            if(direction[2] == true) buffer = 'e'; 
+            if(direction[1] == true) buffer = 'w';
+            if(direction[0] == true) buffer = 'n';
+            break;
+        case 'w':
+            if(direction[2] == true) buffer = 'e';
+            if(direction[3] == true) buffer = 's'; 
+            if(direction[0] == true) buffer = 'n';
+            if(direction[1] == true) buffer = 'w';
+            break;
+        case 's':
+            if(direction[0] == true) buffer = 'n';
+            if(direction[2] == true) buffer = 'e';
+            if(direction[1] == true) buffer = 'w';
+            if(direction[3] == true) buffer = 's';
+            break;
+        case 'e':
+            if(direction[1] == true) buffer = 'w';
+            if(direction[0] == true) buffer = 'n';
+            if(direction[3] == true) buffer = 's';
+            if(direction[2] == true) buffer = 'e';
+            break;
+        default:
+            break;
+    }
+    
     return buffer;
 }