1
(***********************************************************************)
5
(* Pierre Weis, projet Cristal, INRIA Rocquencourt *)
7
(* Copyright 2001 Institut National de Recherche en Informatique et *)
8
(* en Automatique. All rights reserved. This file is distributed *)
9
(* only by permission. *)
11
(***********************************************************************)
15
let d�coupe_image img nx ny =
16
let l = Imagephoto.width img
17
and h = Imagephoto.height img in
18
let tx = l / nx and ty = h / ny in
19
let pi�ces = ref [] in
20
for x = 0 to nx - 1 do
21
for y = 0 to ny - 1 do
23
Imagephoto.create [Width (Pixels tx); Height (Pixels ty)] in
24
Imagephoto.copy pi�ce img
25
[ImgFrom(x * tx, y * ty, (x + 1) * tx, (y + 1) * ty)];
26
pi�ces := pi�ce :: !pi�ces
29
(tx, ty, List.tl !pi�ces);;
31
let remplir_taquin c nx ny tx ty pi�ces =
32
let trou_x = ref (nx - 1)
33
and trou_y = ref (ny - 1) in
35
Canvas.create_rectangle c
36
(Pixels (!trou_x * tx)) (Pixels (!trou_y * ty))
37
(Pixels tx) (Pixels ty) [] in
38
let taquin = Array.make_matrix nx ny trou in
40
for x = 0 to nx - 1 do
41
for y = 0 to ny - 1 do
47
(Pixels (x * tx)) (Pixels (y * ty))
48
[ImagePhoto pi�ce; Anchor NW; Tags [Tag "pi�ce"]];
53
let pi�ce = taquin.(x).(y) in
54
Canvas.coords_set c pi�ce
55
[Pixels (!trou_x * tx); Pixels(!trou_y * ty)];
56
Canvas.coords_set c trou
57
[Pixels (x * tx); Pixels(y * ty); Pixels tx; Pixels ty];
58
taquin.(!trou_x).(!trou_y) <- pi�ce;
59
taquin.(x).(y) <- trou;
60
trou_x := x; trou_y := y in
62
let x = ei.ev_MouseX / tx and y = ei.ev_MouseY / ty in
63
if x = !trou_x && (y = !trou_y - 1 || y = !trou_y + 1)
64
|| y = !trou_y && (x = !trou_x - 1 || x = !trou_x + 1)
66
Canvas.bind c (Tag "pi�ce") [[], ButtonPress]
67
(BindSet ([Ev_MouseX; Ev_MouseY], jouer));;
69
let rec permutation = function
71
| l -> let n = Random.int (List.length l) in
72
let (�l�ment, reste) = partage l n in
73
�l�ment :: permutation reste
77
| [] -> failwith "partage"
79
if n = 0 then (t�te, reste) else
80
let (�l�ment, reste') = partage reste (n - 1) in
81
(�l�ment, t�te :: reste');;
83
let create_filled_text parent lines =
84
let lnum = List.length lines
88
let l = String.length line in
89
if l > max then l else max)
91
let txtw = Text.create parent [TextWidth lwidth; TextHeight lnum] in
94
Text.insert txtw (TextIndex (End, [])) line [];
95
Text.insert txtw (TextIndex (End, [])) "\n" [])
99
let give_help parent lines () =
100
let help_window = Toplevel.create parent [] in
101
Wm.title_set help_window "Help";
103
let help_frame = Frame.create help_window [] in
105
let help_txtw = create_filled_text help_frame lines in
107
let quit_help () = destroy help_window in
108
let ok_button = Button.create help_frame [Text "Ok"; Command quit_help] in
110
pack [help_txtw; ok_button ] [Side Side_Bottom];
111
pack [help_frame] [];;
113
let taquin nom_fichier nx ny =
114
let fp = openTk () in
115
Wm.title_set fp "Taquin";
116
let img = Imagephoto.create [File nom_fichier] in
119
[Width(Pixels(Imagephoto.width img));
120
Height(Pixels(Imagephoto.height img))] in
121
let (tx, ty, pi�ces) = d�coupe_image img nx ny in
122
remplir_taquin c nx ny tx ty (permutation pi�ces);
125
let quit = Button.create fp [Text "Quit"; Command closeTk] in
127
["Pour jouer, cliquer sur une des pi�ces";
130
"To play, click on a part around the hole"] in
132
Button.create fp [Text "Help"; Command (give_help fp help_lines)] in
133
pack [quit; help] [Side Side_Left; Fill Fill_X];
136
if !Sys.interactive then () else begin taquin "joconde.gif" 3 5; exit 0 end;;