Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 3.78 KB | None | 0 0
  1. * -----------------------------------------------------------------------
  2. SUDOKU
  3. ------------------------------------------------------------------------*)
  4.  
  5. (* ____________________version sans exception _____________________ *)
  6.  
  7.  
  8. (* la fonction correct teste si le numéro en position (i,j) crée une contradiction ie si ce numéro apparait ailleurs sur la même ligne ou colonne, ou dans le même bloc 3x3 *)
  9.  
  10. let correct (t:int vect vect) (i:int) (j:int) =
  11. let a = t.(i).(j) and x = 3 * (i / 3) and y = 3 * (j / 3) in
  12. let resultat=ref true and k=ref 0 and p=ref 0 in
  13. while (!k<9) && !resultat do
  14. if !k <> i && t.(!k).(j) = a then resultat:=false;
  15. if !k <> j && t.(i).(!k) = a then resultat:=false;
  16. incr k
  17. done;
  18. k:=x;
  19. while (!k<x+3) && !resultat do
  20. p:=y;
  21. while (!p<y+3) && !resultat do
  22. if (!k, !p) <> (i, j) && t.(!k).(!p) = a then resultat:=false;
  23. incr p
  24. done;
  25. incr k
  26. done;
  27. !resultat;;
  28.  
  29. (* la fonction cherche_vide cherche une case vide dans le tableau (contient 0 par convention ). renvoie un couple (i,j), (-1,-1) si aucune case vide n'est trouvée. *)
  30.  
  31. let cherche_vide (t:int vect vect)=
  32. let solution=ref (-1,-1) and i=ref 0 and j=ref 0 in
  33. while !solution=(-1,-1) && !i<9 do
  34. j:=0;
  35. while !solution=(-1,-1) && !j<9 do
  36. if t.(!i).(!j)=0 then solution:=(!i,!j);
  37. incr j
  38. done;
  39. incr i
  40. done;
  41. !solution;;
  42.  
  43. let rec sudoku (t:int vect vect) =
  44. match cherche_vide t with
  45. |(-1,-1)->true (* plus de case vide. On a fini *)
  46. |(i,j)-> let k=ref 1 and trouve=ref false in
  47. while !k<10 && (not !trouve) do
  48. t.(i).(j)<- !k;
  49. if correct t i j then trouve:= sudoku t;
  50. incr k;
  51. done;
  52. if not !trouve then t.(i).(j)<-0;
  53. !trouve;;
  54.  
  55. (*______________________ interface graphique ______________________*)
  56.  
  57. #open "graphics";;
  58. open_graph "";;
  59.  
  60. let sudo t =
  61.  
  62. (* tracé du quadrillage *)
  63. clear_graph();
  64. let xx = ref (- 1) and yy = ref (- 1) in
  65. let base = 30 in
  66. let refresh() =
  67. for i = 0 to 8 do
  68. for j = 0 to 8 do
  69. set_color black; fill_rect (base * i) (base * j) base base;
  70. if !xx = i && !yy = j then set_color red else set_color green;
  71. fill_rect (base * i + 1) (base * j + 1) (base - 1) (base - 1);
  72. if t.(i).(j) <> 0 then begin
  73. set_color black; moveto (base * i + base / 3) (base * j + base / 3); draw_string (string_of_int t.(i).(j))
  74. end;
  75. done;
  76. done;
  77. in
  78. refresh();
  79. set_color black; moveto base (base * 12); draw_string "Choix de case a la souris";
  80. moveto base (base * 23 / 2); draw_string "Taper sur un chiffre pour changer une valeur (0 pour effacer)";
  81. moveto base (base * 11); draw_string "touche <espace> pour résoudre";
  82.  
  83.  
  84. (* boucle interactive *)
  85. let continue = ref true in
  86. while !continue do
  87. let ev = wait_next_event [Button_down; Key_pressed] in
  88. if ev.button then begin
  89. let x = ev.mouse_x / base and y = ev.mouse_y / base in
  90. if x < 9 && y < 9 then begin
  91. xx := x; yy := y; refresh();
  92. end
  93. end
  94. else
  95. begin
  96. match ev.key with
  97. | ` ` -> continue := false
  98. | c -> let n = int_of_char c in
  99. if 48 <= n && n <= 57 then begin
  100. t.(!xx).(!yy) <- n - 48; refresh()
  101.  
  102. end;
  103.  
  104. end;
  105.  
  106. done;
  107. let ok = sudoku t in
  108. if ok then begin
  109. for i = 0 to 8 do
  110. for j = 0 to 8 do
  111. set_color black; fill_rect (base * i) (base * j) base base;
  112. set_color green; fill_rect (base * i + 1) (base * j + 1) (base - 1) (base - 1);
  113. set_color black; moveto (base * i + base / 3) (base * j + base / 3); draw_string (string_of_int t.(i).(j))
  114. done;
  115. done;
  116. end
  117. else
  118. begin
  119. set_color black; moveto base (base * 10); draw_string "PAS DE SOLUTION"
  120. end;;
  121.  
  122.  
  123. (*___________________________test____________________________*)
  124.  
  125. let grille = [|[|1; 0; 0; 0; 5; 4; 6; 7; 2|]; [|0; 0; 0; 0; 1; 0; 0; 9; 0|];
  126. [|0; 0; 9; 8; 3; 0; 0; 5; 0|]; [|0; 0; 0; 1; 0; 0; 9; 4; 0|];
  127. [|9; 2; 0; 7; 0; 5; 0; 1; 6|]; [|0; 1; 4; 0; 0; 8; 0; 0; 0|];
  128. [|0; 7; 0; 0; 2; 3; 5; 0; 0|]; [|0; 3; 0; 0; 7; 0; 0; 0; 0|];
  129. [|5; 9; 2; 6; 8; 0; 0; 0; 7|]|];;
  130. sudo grille;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement