#open "graphics" ;; let Size= ref 500;; let verifie n tab = let gagn=ref 0 in (* Alignement vertical*) for i=0 to n-1 do for j=0 to n-5 do if !gagn=0 && tab.(i).(j)=tab.(i).(j+1) && tab.(i).(j)=tab.(i).(j+2) && tab.(i).(j)=tab.(i).(j+3) && tab.(i).(j)=tab.(i).(j+4) && tab.(i).(j)<>0 then( gagn:=1 ) done; done; if !gagn=0 then( (* Alignement horizontal *) for i=0 to n-5 do for j=0 to n-1 do if !gagn=0 && tab.(i).(j)=tab.(i+1).(j) && tab.(i).(j)=tab.(i+2).(j) && tab.(i).(j)=tab.(i+3).(j) && tab.(i).(j)=tab.(i+4).(j) && tab.(i).(j)<>0 then( gagn:=1 ) done done ); if !gagn=0 then( (* Alignement Diagonale Haut Droite *) for i=0 to n-5 do for j=0 to n-5 do if !gagn=0 && tab.(i).(j)=tab.(i+1).(j+1) && tab.(i).(j)=tab.(i+2).(j+2) && tab.(i).(j)=tab.(i+3).(j+3) && tab.(i).(j)=tab.(i+4).(j+4) && tab.(i).(j)<>0 then( gagn:=1 ) done done ); if !gagn=0 then( (* Alignement Diagonale Bas Gauche*) for i=4 to n-1 do for j=0 to n-5 do if !gagn=0 && tab.(i).(j)=tab.(i-1).(j+1) && tab.(i).(j)=tab.(i-2).(j+2) && tab.(i).(j)=tab.(i-3).(j+3) && tab.(i).(j)=tab.(i-4).(j+4) && tab.(i).(j)<>0 then( gagn:=1 ) done done ); !gagn;; let ligne x1 y1 x2 y2 = moveto x1 y1; lineto x2 y2;; let rec trace_colonne max = fun 0 p-> () |n p-> ligne (n*p) p (n*p) (max*p); trace_colonne max (n-1) p;; let rec trace_ligne max = fun 0 p-> () |n p-> ligne p (n*p) (max*p) (n*p); trace_ligne max (n-1) p;; let trace_grille n = set_color(black); trace_colonne (n+1) (n+1) (!Size/(n+3)); trace_ligne (n+1) (n+1) (!Size/(n+3));; exception Stop ;; let mainloop taille = let conti = ref true in let score1 = ref 0 in let score2 = ref 0 in let cote = ref ( !Size / (taille+3) ) in let clic = ref ( wait_next_event [ Poll ] ) in try while (!conti <> false) do let joueur = ref 1 in let gagne = ref 0 in let nbCasesVides = ref (taille*taille) in let tab = make_matrix taille taille 0 in clear_graph (); trace_grille taille; set_color(red) ; fill_circle (!Size-20) 100 20 ; set_color(black) ; draw_circle (!Size-20) 100 20 ; let a , b = text_size "EXIT" in moveto ( (!Size-20) - a / 2 ) ( 100 - b / 2 ) ; draw_string "EXIT" ; while ( !gagne = 0 && !nbCasesVides >= 1 ) do if !joueur <> 1 then set_color(red) else set_color(blue); fill_rect 0 0 !Size 10; clic := wait_next_event [ Button_down ] ; let SouriX = !clic.mouse_x and SouriY = !clic.mouse_y in if (SouriX - (!Size-20)) * (SouriX - (!Size-20)) + (SouriY - 100) * (SouriY - 100) < 400 then raise Stop; let pos1 = ref 1 in pos1:=(SouriX / !cote) - 1; let pos2 = ref 1 in pos2:=(SouriY / !cote) - 1; if SouriX > ( !cote ) && SouriX < ((taille+1) * !cote ) && SouriY > ( !cote ) && SouriY < ( (taille+1) * !cote ) && tab.(!pos1).(!pos2)=0 then begin tab.(!pos1).(!pos2) <- !joueur; nbCasesVides := !nbCasesVides - 1 ; if !joueur <> 1 then set_color(red) else set_color(blue); fill_circle (!cote / 2 + !cote * (!pos1 + 1)) (!cote / 2 + !cote * ( !pos2 + 1 )) (!cote/2); set_color(black); draw_circle (!cote / 2 + !cote * (!pos1 + 1)) (!cote / 2 + !cote * ( !pos2 + 1 )) (!cote/2); joueur:= 3 - !joueur ; gagne:=verifie taille tab; if !gagne<>0 then ( moveto 100 (!Size-100); if !joueur <> 2 then ( set_color(red); score2 := !score2 + 1 ) else ( set_color(blue); score1 := !score1 + 1 ) ; draw_string "Felicitations joueur N."; draw_string (string_of_int (3 - !joueur)); draw_string ",vous avez gagne"; sound 440 5000; sound 1800 1200; sound 440 6500; ); if ( !gagne<>0 || !nbCasesVides<1 ) then ( set_color(green) ; fill_ellipse (!Size-20) 150 25 15 ; set_color(black) ; draw_ellipse (!Size-20) 150 25 15 ; let c , d = text_size "REJOUER" in (* On dessine un bouton inutile car il represente tout sauf le bouton QUIT *) moveto ( (!Size-20) - c / 2 ) ( 150 - d / 2 ) ; draw_string "REJOUER" ; clic := wait_next_event [ Button_down ] ; let SouriX = !clic.mouse_x and SouriY = !clic.mouse_y in if (SouriX - (!Size-20)) * (SouriX - (!Size-20)) + (SouriY - 100) * (SouriY - 100) < 400 then ( conti:=false ; raise Stop ); ) ; end; done; done; with Stop -> set_color (black); clear_graph (); moveto 100 ( !Size - 100 ) ; draw_string "AK Corp. -=Morpion=- Caml" ; moveto 100 ( !Size - 120 ) ; draw_string "Projet IUT 2002 Gpe 1A1" ; moveto 100 ( !Size - 140 ) ; draw_string "Sahut Romain " ; moveto 100 100; draw_string "Joueur1: "; draw_string (string_of_int !score1 ) ; draw_string " Joueur2: "; draw_string (string_of_int !score2 ) ; clic := wait_next_event [ Button_down ] ; close_graph () ;; let morpion = fun taille when taille>30 || taille<=5 -> failwith "Usage: morpion avec taille >5 et <=30"; | taille -> open_graph " !Size * !Size + 0 - 0 " ; mainloop taille;; morpion 12;;