Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
(**TP2 : Morpion**) type marque = { c : bool; p: int * int};; type grille = marque list;; (*Question 1*) let grille1 = [{ c = true; p = (0, 0)}; { c = true; p = (0, 1)}; { c = true; p = (0, 2)}; { c = false; p = (1, 1)}; { c = false; p = (2, 0)}; { c = false; p = (2, 2)} ];; let grille2 = [{ c = true; p = (0, 1)}; { c = false; p = (0, 2)}; { c = true; p = (1, 0)}; { c = false; p = (1, 1)}; { c = true; p = (1, 2)}; { c = false; p = (2, 0)}; ];; let grille3 = [{ c = false; p = (0, 0)}; { c = true; p = (0, 1)}; { c = false; p = (0, 2)}; { c = true; p = (1, 0)}; { c = false; p = (1, 1)}; { c = true; p = (1, 2)}; { c = true; p = (2, 0)}; { c = false; p = (2, 1)}; { c = false; p = (2, 2)} ];; (*Question 2*) let dans_les_bornes = List.for_all (fun {c;p =(x,y)} -> 0<=x && x<=2 && 0<=y && y<=2);; (*Question 3*) let existe_symbole g i j = List.exists (fun {c;p =(x,y)} -> x==i&&y==j) g;; (*Question 4*) let rec sans_doublons g = match g with |[]|[_] -> true |hd::tl -> if existe_symbole tl (fst hd.p) (snd hd.p) then false else sans_doublons tl ;; (*Question 5*) let compter = List.fold_left (fun (x,y) z -> if z.c then (x+1,y) else (x,y+1)) (0,0) ;; (*Question 6*) let bonne_grille g = let nb_c,nb_r = compter g in dans_les_bornes g && sans_doublons g && (nb_c = nb_r || nb_c = nb_r + 1) ;; dans_les_bornes grille1;; (*Question 7*) exception Grille_invalide;; let bonne_grille_exn g = if bonne_grille g then () else raise Grille_invalide;; (*Question 8*) let aux_col g i = List.fold_left (fun b j -> b && (existe_symbole g i j)) true [0;1;2] ;; let aux_line g j = List.fold_left (fun b i -> b && existe_symbole g i j) true [0;1;2] ;; let diag g = (List.fold_left (fun b (i,j) -> b && existe_symbole g i j) true [(0,0);(1,1);(2,2)]) || (List.fold_left (fun b (i,j) -> b && existe_symbole g i j) true [(0,2);(1,1);(2,0)]) ;; exception Vrai;; let gagne g = try for i = 0 to 2 do if (aux_col g i)||(aux_line g i) then raise Vrai done; diag g with Vrai -> true ;; (*Question 9*) let extraire g b = List.filter (fun x-> x.c == b) g;; (*Question 10*) let qui_gagne g = try bonne_grille_exn g; let g1 = extraire g true in let g2 = extraire g false in match (gagne g1, gagne g2) with |true, false -> Printf.printf "Les croix ont gagnées" |false, true -> Printf.printf "Les ronds ont gagnées" |_,_-> Printf.printf "Match nul" with Grille_invalide -> Printf.printf "La grille n'est pas valide" ;; qui_gagne grille1;; qui_gagne grille2;; qui_gagne grille3;;