-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraphic.ml
69 lines (59 loc) · 1.52 KB
/
graphic.ml
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
open Graphics
let border = 5
let side = 120
let size = 4 * side + 5 * border
let mode = ref Addition
let get_color = function
| Blue -> blue
| Red -> red
| Green -> green
| Cyan -> cyan
| Yellow -> yellow
| Magenta -> magenta
let draw_square y x case =
let fill () =
fill_rect ((succ x) * border + x * side)
((succ y) * border + y * side)
side side
in
match case with
| Color color -> set_color (get_color color); fill ()
| White -> set_color white; fill ()
| Empty -> ()
let draw_background () =
if !mode = Addition then
begin
set_color black;
fill_rect 0 0 (size_x ()) (size_y ());
set_color (rgb 128 128 128)
end
else
begin
set_color (rgb 128 128 128);
fill_rect 0 0 (size_x ()) (size_y ());
set_color black
end;
for a = 0 to 4 do
fill_rect 0 (a * (border + side)) size border;
fill_rect (a * (border + side)) 0 border size
done
let display_grid grid =
draw_background ();
Array.iteri (fun y line -> Array.iteri (draw_square y) line) grid
let get_mode () = match !mode with
| Substract -> sub
| Addition -> add
let move key grid =
begin
match key with
|'2' -> fusion (get_mode ()) Down grid
|'4' -> fusion (get_mode ()) Left grid
|'6' -> fusion (get_mode ()) Right grid
|'8' -> fusion (get_mode ()) Up grid
end;
display_grid grid
let toto () =
open_graph (Printf.sprintf "%dx%d" size size);
let grid = init_grid () in
display_grid grid;
loop_at_exit [Key_pressed] (fun event -> move event.key grid)