Commits

Anonymous committed 026dd04

graphic solver

  • Participants
  • Parent commits f2fa3fd

Comments (0)

Files changed (3)

File maze/astar.ml

   "#                #######    X\n" ^
   "############## ########  ####\n" ^
   "##############          #####"
-
+  
+(*
 let () =
   let maze, goal, ants = Maze.Parser.from_file "maze/maze.txt" in
   let start = List.hd ants in
   Maze.draw maze;
   Printf.printf "%d steps\n" (List.length path);
   let time = (after -. before) *. 1000. in
-  Printf.printf "%f ms\n" time
+  Printf.printf "%f ms\n" time
+*)
+let pixel = 16
+
+let load_maze maze name = 
+  Graphics.set_window_title ("Maze solver - " ^ name);
+  let width = Maze.width maze and height = Maze.height maze in
+  Graphics.resize_window (width*pixel) (height*pixel);
+  let gray = Graphics.rgb 150 150 150 in
+  for x = 0 to width -1 do
+    for y = 0 to height - 1 do
+      let tile = Maze.get maze (x, y) in
+      let color = match tile with
+        | '@' -> Graphics.red
+        | 'X' -> Graphics.green
+        | '#' -> gray
+        | _ -> Graphics.white
+      in
+      Graphics.set_color color;
+      Graphics.fill_rect (x*pixel) ((height-1-y)*pixel) pixel pixel;
+      Graphics.set_color gray;
+      Graphics.draw_rect (x*pixel) ((height-1-y)*pixel) pixel pixel;
+    done
+  done 
+
+let draw_dot maze (x, y) = 
+  let shift = pixel / 2 in
+  let half = shift / 2 in
+  let height = Maze.height maze in
+  if Maze.get maze (x, y) <> '#' then Graphics.fill_rect 
+    (x*pixel + half) ((height-y-1)*pixel + half) shift shift
+
+let walk maze path =
+  Graphics.set_color Graphics.blue;
+  List.iter (draw_dot maze) path
+
+let solve maze goal start = 
+  let path = Astar.Pathfinding.find_path maze start goal in
+  walk maze path;
+  while Graphics.read_key() != ' ' do
+    ()
+  done
+
+let rec interact maze goal start current =
+  Graphics.set_color Graphics.black;
+  draw_dot maze current; 
+  
+  let key = Graphics.read_key() in
+  if key = ' ' then solve maze goal start
+  else begin
+    let next = match key with
+      | '6' -> (fst current) +1, snd current
+      | '4' -> (fst current) -1, snd current
+      | '8' -> fst current, (snd current) -1
+      | '2' -> fst current, (snd current) +1
+      | _ -> current
+    in
+    if not (Maze.check maze next) || Maze.get maze next = '#' then begin 
+      interact maze goal start current
+    end
+    else begin
+      Graphics.set_color 0xffc600;
+      draw_dot maze current;
+      if next = goal then solve maze goal start
+      else interact maze goal start next
+    end       
+  end
+         
+                   
+let _ =
+  let filename = "maze/maze.txt" in
+  let maze, goal, ants = Maze.Parser.from_file filename in
+  let start = List.hd ants in
+  Graphics.open_graph "";
+  load_maze maze filename;
+  interact maze goal start start

File maze/maze.ml

 
 let width maze = Array.length maze
 let height maze = Array.length maze.(0)
+let check maze (x, y) = x >= 0 && y >= 0 && x < (width maze) && y < (height maze)
 let get maze (x, y) = maze.(x).(y)
 let set maze (x, y) value = maze.(x).(y) <- value