let x = int_of_string (Sys.argv.(1))
let y = int_of_string (Sys.argv.(2))

let line_loop line func =
  let rec loop pos =
    if pos < x * 2 - 1 then (
      func line pos;
      loop (pos + 1)
    ) in
  loop 0

let make_line v = Array.make (x * 2 - 1) v

let make_empty_line () = make_line 0

let make_filled_line () = make_line 1

let make_checked_line () =
  let line = make_empty_line () in
  line_loop line (
    fun l p -> if (p mod 2) = 1 then l.(p) <- 1
  );
  line

let print_line line =
  print_string "■";
  line_loop line (
    fun l p -> if l.(p) = 1 then print_string "■" else print_string "　"
  );
  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) = 1 then make_wall pos else lines.(y).(x) <- 1 in
  let rec loop pos =
    if pos < x * 2 - 1 then (
      if pos mod 2 = 1 then make_wall pos; loop (pos + 1)
    ) in
  loop 0

let main () =
  let _ = Random.init (int_of_float (Unix.time ())) in
  let filled_line = make_filled_line () in
  print_line filled_line;
  let rec loop cnt last_line =
    if cnt < y
      then (
        let lines = [|last_line; make_checked_line (); make_empty_line ()|] in
        make_maze (if cnt = 0 then Top else Normal) lines;
        print_line lines.(0);
        print_line lines.(1);
        loop (cnt + 1) lines.(2)
      )
      else print_line last_line in
  loop 0 (make_empty_line ());
  print_line filled_line

let _ = main ()