1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
type field = Space | Wall

let x = int_of_string (Sys.argv.(1))
let y = int_of_string (Sys.argv.(2))

let rec repeate f x i n =
  if i < n then repeate f (f i x) (i + 1) n else x

let make_line v = Array.make (x * 2 - 1) v

let make_empty_line () = make_line Space

let make_filled_line () = make_line Wall

let print_line line =
  print_string "■";
  Array.iter (
    fun a -> if a = Wall then print_string "■" else print_string " "
  ) line;
  print_endline "■"

type mode = Top | Normal

let make_maze mode lines =
  let way = match mode with Top -> 4 | Normal -> 3 in
  let rec make_wall pos =
    let (y, x) =
      match (Random.int way) with
      | 0 -> (1, pos + 1)
      | 1 -> (2, pos)
      | 2 -> (1, pos - 1)
      | 3 -> (0, pos)
      | _ -> failwith "invalid value" in
    if lines.(y).(x) = Wall
      then make_wall pos
      else (
        lines.(1).(pos) <- Wall;
        lines.(y).(x) <- Wall
      ) in
  Array.iteri (fun i a -> if i mod 2 = 1 then make_wall i) lines.(0)

let main () =
  let _ = Random.init (int_of_float (Unix.time ())) in
  let draw_maze pos last_line =
    let lines = [|last_line; make_empty_line (); make_empty_line ()|] in
    make_maze (if pos = 0 then Top else Normal) lines;
    print_line lines.(0);
    print_line lines.(1);
    lines.(2) in
  let filled_line = make_filled_line () in
  print_line filled_line;
  print_line (repeate draw_maze (make_empty_line ()) 0 y);
  print_line filled_line

let _ = main ()