Pouknouki

Démineur

Apr 5th, 2017
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 3.42 KB | None | 0 0
  1.  
  2. type champ == bool vect vect;;
  3. type sondage = Explosion | Voisins of int;;
  4.  
  5. let sondage x y c =
  6.     if not (x < 0 || y < 0 || x >= vect_length c || x >= (vect_length c.(0) - 1)) then
  7.     begin
  8.     let r = ref(Explosion) in
  9.     if c.(x).(y) = false then
  10.         begin
  11.             let total = ref(0) in
  12.             if x <> 0 then
  13.                 if c.(x - 1).(y) then total := !total + 1;
  14.             if x <> vect_length c - 1 then
  15.                 if c.(x + 1).(y) then total := !total + 1;
  16.             if y <> 0 then
  17.                 if c.(x).(y - 1) then total := !total + 1;
  18.             if y <> vect_length (c.(0)) - 1 then
  19.                 if c.(x).(y + 1) then total := !total + 1;
  20.             r := Voisins(!total);
  21.         end;
  22.     !r
  23.     end
  24.     else
  25.         Explosion;;
  26.  
  27. let dans_vecteur_pas_dynamique v e =
  28.     let trouve = ref(false) in
  29.     for i = 0 to vect_length v - 1 do
  30.         if v.(i) = e then trouve := true;
  31.     done;
  32.     !trouve;;
  33.  
  34. let creer x y m =
  35.     let resultat = make_matrix x y false in
  36.     let mines = make_vect m (0, 0) in
  37.    
  38.     for i = 0 to m - 1 do
  39.         let rX = ref(random__int (x - 1)) in
  40.         let rY = ref(random__int (y - 1)) in
  41.        
  42.         while dans_vecteur_pas_dynamique mines (!rX, !rY) do
  43.             rX := random__int (x - 1);
  44.             rY := random__int (y - 1);
  45.         done;
  46.        
  47.         mines.(i) <- (!rX, !rY);
  48.         resultat.(!rX).(!rY) <- true;
  49.     done;
  50.     resultat;;
  51.  
  52. let convertir s = match s with
  53.     | Explosion -> `X`
  54.     | Voisins n -> char_of_int (48 + n);;
  55.  
  56. let affiche c =
  57.     open_graph "";
  58.     let x = vect_length c and y = vect_length c.(0) in
  59.     let sx = size_x() and sy = size_y() in
  60.     let dimx = sx / x and dimy = sy / y in
  61.     for i = 0 to x - 1 do
  62.         for j = 0 to y - 1 do
  63.             moveto (i * dimx) (j * dimy);
  64.             lineto ((i + 1) * dimx) (j * dimy);
  65.             lineto ((i + 1) * dimx) ((j + 1) * dimy);
  66.             lineto (i * dimx) ((j + 1) * dimy);
  67.             lineto (i * dimx) (j * dimy);
  68.            
  69.             draw_char c.(i).(j);
  70.         done;
  71.     done;
  72.     dimx, dimy;;
  73.  
  74. let rien a = ();;
  75.  
  76. let rec decouverte r c x y =
  77.     let xMax = vect_length r - 1 and yMax = (vect_length r.(0)) - 1 in
  78.     match (sondage x y c) with
  79.         | Explosion -> r.(x).(y) <- `X`;
  80.         | Voisins n ->
  81.             r.(x).(y) <- `0`;
  82.             let arreter = ref(false) in
  83.             for i = (max (x - 1) 0) to (min (x + 1) xMax) do
  84.                 for j = (max (y - 1) 0) to (min (y + 1) yMax) do
  85.                     if x <> i || y <> j then
  86.                         if sondage i j c = Explosion then
  87.                             arreter := true;
  88.                 done;
  89.             done;
  90.             if not !arreter then
  91.             (
  92.                 for i = (max (x - 1) 0) to (min (x + 1) xMax) do
  93.                     for j = (max (y - 1) 0) to (min (y + 1) yMax) do
  94.                         if x <> i || y <> j then
  95.                             decouverte r c i j
  96.                     done;
  97.                 done;
  98.             )
  99.             else
  100.             (
  101.                 for i = (max (x - 1) 0) to (min (x + 1) xMax) do
  102.                     for j = (max (y - 1) 0) to (min (y + 1) yMax) do
  103.                         if x <> i || y <> j then
  104.                             r.(i).(j) <- convertir (sondage i j c);
  105.                     done;
  106.                 done
  107.             );;
  108.            
  109. let interaction c =
  110.     let sondes = make_matrix (vect_length c) (vect_length c.(0)) false in
  111.     let resultats = make_matrix (vect_length c) (vect_length c.(0)) ` ` in
  112.     let dimX, dimY = affiche resultats in
  113.    
  114.     while true do
  115.         while not button_down() do
  116.             ()
  117.         done;
  118.    
  119.         let x, y = mouse_pos() in
  120.         let caseX, caseY = (x / dimX), (y / dimY) in
  121.         sondes.(caseX).(caseY) <- true;
  122.                
  123.         let s = sondage caseX caseY c in
  124.             if s = Explosion then
  125.                 failwith "Perdu"
  126.             else
  127.                 begin
  128.                     resultats.(caseX).(caseY) <- (convertir s);
  129.                     decouverte resultats c caseX caseY;
  130.                 end;
  131.         let a, b = affiche resultats in rien a;
  132.     done;
  133. ();;
  134.  
  135. #open "graphics";;
  136. let c = creer 20 20 (20) in interaction c;;
Advertisement
Add Comment
Please, Sign In to add comment