Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type champ == bool vect vect;;
- type sondage = Explosion | Voisins of int;;
- let sondage x y c =
- if not (x < 0 || y < 0 || x >= vect_length c || x >= (vect_length c.(0) - 1)) then
- begin
- let r = ref(Explosion) in
- if c.(x).(y) = false then
- begin
- let total = ref(0) in
- if x <> 0 then
- if c.(x - 1).(y) then total := !total + 1;
- if x <> vect_length c - 1 then
- if c.(x + 1).(y) then total := !total + 1;
- if y <> 0 then
- if c.(x).(y - 1) then total := !total + 1;
- if y <> vect_length (c.(0)) - 1 then
- if c.(x).(y + 1) then total := !total + 1;
- r := Voisins(!total);
- end;
- !r
- end
- else
- Explosion;;
- let dans_vecteur_pas_dynamique v e =
- let trouve = ref(false) in
- for i = 0 to vect_length v - 1 do
- if v.(i) = e then trouve := true;
- done;
- !trouve;;
- let creer x y m =
- let resultat = make_matrix x y false in
- let mines = make_vect m (0, 0) in
- for i = 0 to m - 1 do
- let rX = ref(random__int (x - 1)) in
- let rY = ref(random__int (y - 1)) in
- while dans_vecteur_pas_dynamique mines (!rX, !rY) do
- rX := random__int (x - 1);
- rY := random__int (y - 1);
- done;
- mines.(i) <- (!rX, !rY);
- resultat.(!rX).(!rY) <- true;
- done;
- resultat;;
- let convertir s = match s with
- | Explosion -> `X`
- | Voisins n -> char_of_int (48 + n);;
- let affiche c =
- open_graph "";
- let x = vect_length c and y = vect_length c.(0) in
- let sx = size_x() and sy = size_y() in
- let dimx = sx / x and dimy = sy / y in
- for i = 0 to x - 1 do
- for j = 0 to y - 1 do
- moveto (i * dimx) (j * dimy);
- lineto ((i + 1) * dimx) (j * dimy);
- lineto ((i + 1) * dimx) ((j + 1) * dimy);
- lineto (i * dimx) ((j + 1) * dimy);
- lineto (i * dimx) (j * dimy);
- draw_char c.(i).(j);
- done;
- done;
- dimx, dimy;;
- let rien a = ();;
- let rec decouverte r c x y =
- let xMax = vect_length r - 1 and yMax = (vect_length r.(0)) - 1 in
- match (sondage x y c) with
- | Explosion -> r.(x).(y) <- `X`;
- | Voisins n ->
- r.(x).(y) <- `0`;
- let arreter = ref(false) in
- for i = (max (x - 1) 0) to (min (x + 1) xMax) do
- for j = (max (y - 1) 0) to (min (y + 1) yMax) do
- if x <> i || y <> j then
- if sondage i j c = Explosion then
- arreter := true;
- done;
- done;
- if not !arreter then
- (
- for i = (max (x - 1) 0) to (min (x + 1) xMax) do
- for j = (max (y - 1) 0) to (min (y + 1) yMax) do
- if x <> i || y <> j then
- decouverte r c i j
- done;
- done;
- )
- else
- (
- for i = (max (x - 1) 0) to (min (x + 1) xMax) do
- for j = (max (y - 1) 0) to (min (y + 1) yMax) do
- if x <> i || y <> j then
- r.(i).(j) <- convertir (sondage i j c);
- done;
- done
- );;
- let interaction c =
- let sondes = make_matrix (vect_length c) (vect_length c.(0)) false in
- let resultats = make_matrix (vect_length c) (vect_length c.(0)) ` ` in
- let dimX, dimY = affiche resultats in
- while true do
- while not button_down() do
- ()
- done;
- let x, y = mouse_pos() in
- let caseX, caseY = (x / dimX), (y / dimY) in
- sondes.(caseX).(caseY) <- true;
- let s = sondage caseX caseY c in
- if s = Explosion then
- failwith "Perdu"
- else
- begin
- resultats.(caseX).(caseY) <- (convertir s);
- decouverte resultats c caseX caseY;
- end;
- let a, b = affiche resultats in rien a;
- done;
- ();;
- #open "graphics";;
- let c = creer 20 20 (20) in interaction c;;
Advertisement
Add Comment
Please, Sign In to add comment