Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type color = W | B | R | G;;
- type clock = color array;;
- let color_at clock i =
- if i >= 1 && i <= 5
- then clock.(i-1)
- else raise (Invalid_argument "color_at")
- ;;
- let all_white = Array.make 5 W;;
- type time = {
- hh: int;
- mm:int
- };;
- let valid_time t =
- t.hh >= 0 && t.hh < 24 && t.mm >= 0 && t.mm<60
- ;;
- let normal_time t =
- t.hh >= 0 && t.hh < 12 && t.mm >= 0 && t.mm<60
- ;;
- let rounded_time t =
- {t with mm = 5 * (t.mm / 5)}
- ;;
- let valid_to_normal t =
- if valid_time t
- then {t with hh = t.hh mod 12 }
- else invalid_arg "valid_to_normal"
- ;;
- let rec fib = function
- | 0 -> 0
- | 1 -> 1
- | n -> fib (n-1) + fib (n-2)
- ;;
- let time (h,m) =
- let
- t = {hh=h;mm=m}
- in
- if valid_time t
- then t
- else invalid_arg "time"
- ;;
- let time_of_clock c =
- let h = ref 0 (* horas *)
- and m = ref 0 in (* minutos *)
- for i = 1 to 5 do
- let v =
- fib i
- in match color_at c i with
- | W -> ()
- | B -> h := !h + v; m := !m + v
- | R -> h := !h + v
- | G -> m := !m + v
- done;
- { hh = !h; mm = 5 * !m }
- ;;
- let rec combinations elms s =
- if s <= 0
- then [[||]]
- else List.concat (List.map (fun x -> List.map (fun y -> Array.append [|y|] x) elms ) (combinations elms (s - 1)))
- ;;
- let colors = [R;G;B;W];;
- let possibleClocks = combinations colors 5;;
- let all_clocks_for_time tt =
- List.filter (fun c -> time_of_clock c = rounded_time (valid_to_normal tt) ) possibleClocks
- ;;
- let a_clock_for_time tt =
- List.find (fun c -> time_of_clock c = rounded_time (valid_to_normal tt) ) possibleClocks
- ;;
- let random_clock_for_time t =
- let all =
- all_clocks_for_time t
- in
- let n =
- List.length all
- in
- List.nth all (Random.int n)
- ;;
- open Graphics;;
- (*open Unix;;*)
- let color = function
- | W -> rgb 245 247 250
- | B -> rgb 0 122 255
- | R -> rgb 255 59 48
- | G -> rgb 76 217 100
- ;;
- type sq = {
- x: int;
- y: int;
- s: int
- };;
- let sq = Array.make 5 { x = 0; y = 0; s = 0 };;
- sq.(0) <- { x = 2; y = 3; s = 1 };
- sq.(1) <- { x = 2; y = 4; s = 1 };
- sq.(2) <- { x = 0; y = 3; s = 2 };
- sq.(3) <- { x = 0; y = 0; s = 3 };
- sq.(4) <- { x = 3; y = 0; s = 5 };;
- let drawSquareS s i c =
- set_color c;
- let sq =
- sq.(i)
- in
- fill_rect (sq.x * s) (sq.y * s) (sq.s * s) (sq.s * s)
- ;;
- let drawlineS s () =
- let k =
- s
- in
- set_color (rgb 60 59 61);
- set_line_width 2;
- for i = 0 to 4 do
- let sq = sq.(i) in
- draw_rect (sq.x * k) (sq.y * k) (sq.s * k) (sq.s * k)
- done
- ;;
- let draw_clockS s (c:clock) =
- for i = 0 to 4 do
- drawSquareS s i (color c.(i))
- done;
- drawlineS s ()
- ;;
- let print_current_time s =
- let current_tm =
- Unix.localtime (Unix.time ())
- in
- let tm =
- time (current_tm.tm_hour mod 12, current_tm.tm_min)
- in draw_clockS s (a_clock_for_time tm )
- ;;
- let open_clock size =
- let st =
- " " ^ string_of_int (8* size) ^ "x" ^ string_of_int (5 * size)
- in
- open_graph st;
- set_window_title "Fibonacci Clock";
- drawlineS size ();
- draw_clockS size
- ;;
- let close_clock () = close_graph ();;
- let run_clock size =
- try
- open_clock size
- with
- | Graphic_failure ("fatal I/O error") ->
- print_string "Hello!";
- print_current_time size
- ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement