#open "graphics" ;; type sondage = Explosion | Voisins of int;; let cree nl nc nm = if nl<1 or nc<1 or nm>nl*nc then failwith "arguments incorrects" else let m = make_matrix nl nc false in let rec aux = function | 0 -> m | k -> match (random__int nl,random__int nc) with | (i,j) when m.(i).(j) -> aux k | (i,j) -> m.(i).(j) <- true ; aux (k-1) in aux nm ;; let nb_voisins m i j = let l_voisins = [(-1,-1);(-1,0);(-1,1);(0,-1);(0,1);(1,-1);(1,0);(1,1)] in let aux (dx,dy) = try match m.(i+dx).(j+dy) with | true -> 1 | false -> 0 with _ -> 0 in it_list (prefix +) 0 (map aux l_voisins) ;; let sondage m i j = try match m.(i).(j) with | true -> Explosion | false -> Voisins (nb_voisins m i j) with _ -> failwith "arguments incorrects pour sondage" ;; let demineur nl nc nm = open_graph "" ; let m = cree nl nc nm in let larg = (size_x() - 10) / nc and haut = (size_y() - 10) / nl in let cote = min larg haut in let xmin = (size_x() - nc * cote) / 2 and ymin = (size_y() - nl * cote) / 2 in let xmax = xmin + nc * cote and ymax = ymin + nl * cote in let affiche m = for i = 0 to nl do let y = ymin + i * cote in moveto xmin y; lineto xmax y done ; for j = 0 to nc do let x = xmin + j * cote in moveto x ymin ; lineto x ymax done in let coords_of x y = ((y-ymin) / cote,(x-xmin)/cote) in let pose_texte i j s = let x = xmin + j * cote + cote/2 and y = ymin + i * cote + cote/2 in let (dx,dy) = text_size s in moveto (x - dx / 2) (y - dy / 2) ; draw_string s in let rec aux() = let s = wait_next_event [Button_down] in let (i,j) = coords_of s.mouse_x s.mouse_y in match sondage m i j with | Explosion -> close_graph() ; print_string "boum" | Voisins nv -> pose_texte i j (string_of_int(nv)) ; aux() in clear_graph(); affiche m ; aux() ;;