Add tags

Add tags to the following comment

壁かどうかはヴァリアントにすべきでした… 自己満足ですが修正版を。

 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
type field = Space | Wall

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 Space

let make_filled_line () = make_line Wall

let make_checked_line () =
  let line = make_empty_line () in
  line_loop line (
    fun l p -> if (p mod 2) = 1 then l.(p) <- Wall
  );
  line

let print_line line =
  print_string "■";
  line_loop line (
    fun l p -> if l.(p) = Wall then print_string "■" else print_string " "
  );
  print_endline "■"

_build  maze.ml
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.(y).(x) <- Wall 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 ()

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...