Commits

Anonymous committed f2fd08c

load from string, start & goal

  • Participants
  • Parent commits 2fbf286

Comments (0)

Files changed (2)

File maze/maze.ml

     in
     List.rev !list
   
+  let from_string str =
+    let start = ref (0, 0) and goal = ref (0, 0) in
+    let lines = split '\n' str in
+    let height, width = List.length lines, String.length (List.hd lines) in
+    let maze = Array.make_matrix width height ' ' in
+    for x = 0 to width -1 do
+      for y = 0 to height -1 do
+        maze.(x).(y) <- (List.nth lines y).[x];
+        if maze.(x).(y) = 'S' then start := x, y
+        else if maze.(x).(y) = 'G' then goal := x, y
+      done
+    done;
+    maze, !start, !goal
+  
   let from_file filename =
     let channel = open_in_bin filename in
     let size = in_channel_length channel in
     let buffer = Buffer.create size in
     Buffer.add_channel buffer channel size;
-    let s = Buffer.contents buffer in
-    let lines = split '\n' s in
-    let height, width = List.length lines, String.length (List.hd lines) in
-    let maze = Array.make_matrix width height ' ' in
-    for x = 0 to width -1 do
-      for y = 0 to height -1 do
-        maze.(x).(y) <- (List.nth lines y).[x]
-      done
-    done;
-    maze
+    from_string (Buffer.contents buffer)
 end
 
 module Maze = struct
   loop ()
 
 let () =
-  let maze = MazeParse.from_file "maze/maze.txt" in
+  let maze, start, goal = MazeParse.from_file "maze/maze.txt" in
   let before = Unix.gettimeofday () in
-  let path = find_path maze (0, 0) (78, 29) in
+  let path = find_path maze start goal in
   
   for i = 0 to 1000 do
-    ignore (find_path maze (0, 0) (78, 29))
+    ignore (find_path maze start goal)
   done;
   
   let after = Unix.gettimeofday () in

File maze/maze.txt

-   ############################################################################
+S  ############################################################################
 ##  ###########################################################################
 ## ############################################################################
 ## ###########        ###################                ######################
 ######################################### #####################################
 #########################################                                  ####
 ########################################################################## ####
-##########################################################################     
+##########################################################################    G