Commits

Anonymous committed e36bfeb

line length

  • Participants
  • Parent commits 72cedf3

Comments (0)

Files changed (4)

File maze/astar.ml

         | Unknown -> invalid_arg "unknown node"
       
       let set_exploring node steps camefrom =
-        matrix.(fst node).(snd node) <- Exploring { steps = steps; estimation = steps + (heuristic node); camefrom = camefrom }
+        matrix.(fst node).(snd node) <- Exploring { 
+          steps = steps; 
+          estimation = steps + (heuristic node); 
+          camefrom = camefrom
+        }
       
       let set_explored node = 
         let explored = match matrix.(fst node).(snd node) with
           | Exploring cost -> Explored cost
-          | _ -> invalid_arg ("not in exploring state: " ^ string_of_int (fst node) ^ ", " ^ string_of_int (snd node))
+          | _ -> invalid_arg ("not in exploring state: " ^ 
+            string_of_int (fst node) ^ ", " ^ string_of_int (snd node))
         in 
         matrix.(fst node).(snd node) <- explored
 
                 openset := OpenSet.add node !openset;
               end
               else if tentative_steps < (Exploration.get_steps node) then begin 
-                (* remove & add are needed, as the score changes, the place in the tree changes too *)
+                (* remove & add are needed, *)
+                (* as the score changes, the place in the tree changes too *)
                 openset := OpenSet.remove node !openset;
                 Exploration.set_exploring node tentative_steps current;
                 openset := OpenSet.add node !openset

File maze/generator.ml

   Maze.set maze goal 'X';
   maze, start, goal    
 
-(*Start at a particular cell and call it the "exit."                           *)
-(*Mark the current cell as visited, and get a list of its neighbors.           *)
-(*For each neighbor, starting with a randomly selected neighbor:               *)
-(*  If that neighbor hasn't been visited, remove the wall between this cell    *)
-(*  and that neighbor, and then recurse with that neighbor as the current cell.*)
+(*Start at a particular cell and call it the "exit."                          *)
+(*Mark the current cell as visited, and get a list of its neighbors.          *)
+(*For each neighbor, starting with a randomly selected neighbor:              *)
+(* If that neighbor hasn't been visited, remove the wall between this cell    *)
+(* and that neighbor, and then recurse with that neighbor as the current cell.*)
   
 let gen_backtracker width height =
   let maze = Array.make_matrix width height '#' in
   (* start with a random point, with even coords *)
   let start = select_point maze 0 0 (width-1) (height-1) '#' in
-  let start = if (fst start) mod 2 <> 0 then (fst start)-1, snd start else start in
-  let start = if (snd start) mod 2 <> 0 then fst start, (snd start)-1 else start in
+  let start = 
+    if (fst start) mod 2 <> 0 then (fst start)-1, snd start else start in
+  let start = 
+    if (snd start) mod 2 <> 0 then fst start, (snd start)-1 else start in
   let remove_wall (x1, y1) (x2, y2) =
     if x1 <> x2 then Maze.set maze ((max x1 x2)-1, y1) ' '
     else if y1 <> y2 then Maze.set maze (x1, (max y1 y2)-1) ' '

File maze/maze.ml

           list := (String.sub s !start (index - !start)) :: !list;
           start := index + 1
         done
-      with Not_found -> list := (String.sub s !start ((String.length s) - !start)) :: !list
+      with Not_found -> 
+        list := (String.sub s !start ((String.length s) - !start)) :: !list
     in
     List.rev !list
   
 
 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
 
+let check maze (x, y) = 
+  x >= 0 && y >= 0 && x < (width maze) && y < (height maze)
+
 let walk maze path =
   let walk_once coords = if get maze coords = ' ' then set maze coords '.' in
   List.iter walk_once path

File maze/sdlmaze.ml

     flag := Some (load_image ("data/images/flag/flag_" ^ color ^ ".png"));
     star_bullet := Some (load_image "data/images/bullet/bullet_star.png");
     dragon_bullet := Some (load_image ("data/images/bullet/bullet_dragon.png"));
-    player_bullet := Some (load_image ("data/images/bullet/bullet_" ^ color ^ ".png"))
+    player_bullet :=
+    Some (load_image ("data/images/bullet/bullet_" ^ color ^ ".png"))
   
   let blit dst dst_rect imgref = match !imgref with
     | None -> ()
-    | Some image -> Sdlvideo.blit_surface ~dst_rect: dst_rect ~src: image ~dst: dst ()
+    | Some image ->
+        Sdlvideo.blit_surface ~dst_rect: dst_rect ~src: image ~dst: dst ()
   
   let draw_background screen x y color =
     let open Sdlvideo in
     let fullrect = rect (x * tilesize) (y * tilesize) tilesize tilesize in
-    let insiderect = rect (fullrect.r_x +1) (fullrect.r_y +1) (fullrect.r_w -1) (fullrect.r_h -1) in
+    let insiderect = rect (fullrect.r_x +1) (fullrect.r_y +1)
+        (fullrect.r_w -1) (fullrect.r_h -1) in
     let gray = map_RGB screen (150, 150, 150) in
     fill_rect ~rect: fullrect screen gray;
     fill_rect ~rect: insiderect screen color;
     player_position = start; dragon_position = start; steps = 0
   }
   
-  let move_dragon state = 
-    let path = Astar.Pathfinding.find_path state.maze state.dragon_position state.player_position in
+  let move_dragon state =
+    let path = Astar.Pathfinding.find_path
+        state.maze state.dragon_position state.player_position in
     match path with
-      | current::next::more -> {state with dragon_position = next}
-      | _ -> state
+    | current:: next:: more -> { state with dragon_position = next }
+    | _ -> state
   
   let solve screen state =
     let open Sdlevent in
     let open Sdlkey in
     let path = Astar.Pathfinding.find_path state.maze state.start state.goal in
-    let walk path = List.iter (Image.draw_bullet screen state.maze Image.star_bullet) path in
+    let walk path =
+      List.iter (Image.draw_bullet screen state.maze Image.star_bullet) path in
     walk path;
     let rec loop () =
       match wait_event () with
       | _ -> position
     in
     if next = position then interact screen state
-    else if not (Maze.check state.maze next) || Maze.get state.maze next = '#' then begin
+    else if not (Maze.check state.maze next)
+    || Maze.get state.maze next = '#' then begin
       Sfx.play Sfx.error;
       interact screen state
     end
     else begin
       Image.draw_bullet screen state.maze Image.player_bullet position;
       if next = state.dragon_position then (Sfx.play Sfx.failure; raise Break)
-      else if next = state.goal then (Sfx.play Sfx.success; raise Break)      
-      else begin 
-        Sfx.play Sfx.step; 
-        let state = { state with player_position = next; steps = succ state.steps } in
-        if state.steps > state.dragon_delay then begin 
-          Image.draw_bullet screen state.maze Image.dragon_bullet state.dragon_position;
+      else if next = state.goal then (Sfx.play Sfx.success; raise Break)
+      else begin
+        Sfx.play Sfx.step;
+        let state =
+          { state with player_position = next; steps = succ state.steps } in
+        if state.steps > state.dragon_delay then begin
+          Image.draw_bullet screen state.maze
+            Image.dragon_bullet state.dragon_position;
           let state' = move_dragon state in
           Image.draw_dragon screen state.maze state'.dragon_position;
-          if state'.dragon_position = state'.player_position then (Sfx.play Sfx.failure; raise Break)
+          if state'.dragon_position = state'.player_position then
+            (Sfx.play Sfx.failure; raise Break)
           else interact screen state'
         end
         else interact screen state
   in
   let args =
     let open Arg in
-    ("-size", Tuple [Set_int width; Set_int height], "set width and height") ::
-    ("-generator", Symbol (["RANDOM";"BACKTRACKER";"RECDIV"], set_generator), "sets a custom generator") ::
-    ("-dragon-delay", Set_int dragon_delay, "steps before the dragon appears") ::
+    ("-size", Tuple [Set_int width; Set_int height],
+      "set width and height") ::
+    ("-generator", Symbol (["RANDOM";"BACKTRACKER";"RECDIV"], set_generator),
+      "sets a custom generator") ::
+    ("-dragon-delay", Set_int dragon_delay,
+      "steps before the dragon appears") ::
     []
   in
   Arg.parse args ignore "usage";