Mastermind v1.0 :sol:
[cpp]
exception BAD_INPUT;;
type pion = Blanc | Noir | Rouge | Vert | Bleu | Jaune | Orange | Vide;;
(* type enregistrement representant une “main” *)
type main = {mutable prem: pion; mutable deux: pion; mutable trois: pion; mutable quat: pion};;
(* main a trouver *)
let main_a_trouver = {prem=Blanc; deux=Rouge; trois=Vert; quat=Noir};;
let main_gagnante = [Blanc; Rouge; Orange; Noir];;
let ref main_joueur = [Blanc; Orange; Vert; Bleu];;
let rec size liste n =
match liste with
[]->n
|tete::queue-> size queue (n+1);;
let rec est_dans_main pion main =
match main with
[]->false
|tete::queue-> if tete=pion then true else est_dans_main pion queue;;
est_dans_main Vert main_gagnante;;
est_dans_main Noir main_gagnante;;
let indice_pion pion main =
let rec indice_pion_bis pion main n =
match main with
[]-> -1
| tete::queue -> if tete=pion then n else indice_pion_bis pion queue (n+1)
in indice_pion_bis pion main 0;;
let a = indice_pion Vert main_gagnante;;
let b = indice_pion Noir main_gagnante;;
let c = (string_of_int a);;
let d = (string_of_int b);;
let () = print_string c in (print_newline (); print_newline (); );;
let () = print_string d in (print_newline (); print_newline (); );;
let reponse_pion pion pion_gagnant main_gagnante =
if pion = pion_gagnant then [Rouge]
else
if indice_pion pion main_gagnante > -1 then [Blanc] else [Vide];;
let reponse_main main_joueur main_gagnante =
let rec reponse_main_bis main_joueur main_gagnante n =
match main_joueur with
[] -> []
| tete :: queue -> (reponse_pion tete (List.nth main_gagnante n) main_gagnante) @ reponse_main_bis queue main_gagnante (n+1)
in
reponse_main_bis main_joueur main_gagnante 0;;
let f = reponse_main [Vert; Bleu; Rouge; Jaune] [Vert; Rouge; Noir; Jaune];;
let g = reponse_main [Vert; Bleu; Rouge; Jaune] [Vert; Bleu; Rouge; Jaune];;
let efface_ecran =
let () = print_newline () in
let () = print_newline () in
let () = print_newline () in
let () = print_newline () in
let () = print_newline () in
let () = print_newline () in
let () = print_newline () in
let () = print_newline () in
let () = print_newline () in
let () = print_newline () in
let () = print_newline () in
let () = print_newline () in
let () = print_newline () in
let () = print_newline () in
let () = print_newline () in
let () = print_newline () in
let () = print_newline () in
let () = print_newline () in
let () = print_newline () in
let () = print_newline ()
in print_newline () ;;
let print_header =
let () = print_newline () in
let () = print_string “############################################################” in
let () = print_newline () in
let () = print_string “##### #####” in
let () = print_newline () in
let () = print_string “##### Mastermind V1.0 #####” in
let () = print_newline () in
let () = print_string “##### #####” in
let () = print_newline () in
let () = print_string “##### Le_syndicat 13/01/04 #####” in
let () = print_newline () in
let () = print_string “##### #####” in
let () = print_newline () in
let () = print_string “############################################################” in
print_newline ();;
let string_of_pion pion=
match pion with
Blanc -> "Blanc "
| Noir -> "Noir "
| Bleu -> "Bleu "
| Rouge -> "Rouge "
| Vert -> "Vert "
| Jaune -> "Jaune "
| Orange -> "Orange "
| Vide -> "Vide ";;
let rec string_of_main main=
match main with
[]-> “”
|tete::queue -> (string_of_pion tete)^(string_of_main queue);;
let pion_of_int i =
match i with
1 -> Blanc
| 2 -> Noir
| 3 -> Rouge
| 4 -> Vert
| 5 -> Bleu
| 6 -> Jaune
| 7 -> Orange
|_-> raise BAD_INPUT;;
let rec get_pion() =
let () = print_newline() in
let () = print_string "1 - Blanc " in
let () = print_newline() in
let () = print_string "2 - Noir " in
let () = print_newline() in
let () = print_string "3 - Rouge " in
let () = print_newline() in
let () = print_string "4 - Vert " in
let () = print_newline() in
let () = print_string "5 - Bleu " in
let () = print_newline() in
let () = print_string "6 - Jaune " in
let () = print_newline() in
let () = print_string "7 - Orange " in
let () = print_newline() in
let () = print_string “Entrer un nombre entre 1 et 7 svp” in
let () = print_newline() in
let readpion = read_int () in
try pion_of_int readpion with
| BAD_INPUT -> get_pion ();;
let print_int_choix n =
match n with
1 -> print_string “1 - Blanc”
| 2 -> print_string “2 - Noir”
| 3 -> print_string “3 - Rouge”
| 4 -> print_string “4 - Vert”
| 5 -> print_string “5 - Bleu”
| 6 -> print_string “6 - Jaune”
| 7 -> print_string “7 - Orange”
|_-> print_string “Appel fct print_choix illogique !!”;;
let rec print_liste_choix liste =
match liste with
[]-> print_newline ()
| tete::queue -> let () = print_newline ()
in let () = print_int_choix tete
in print_liste_choix queue;;
let readpion = read_int () in
try pion_of_int readpion with
| BAD_INPUT -> get_pion ();;
let listrm a liste =
let rec listrm_bis a liste accu =
match liste with
[]-> accu
| tete::queue -> if a=tete then listrm_bis a queue accu else listrm_bis a queue accu@[tete]
in List.rev (listrm_bis a liste []);;
let rec get_one_pion liste =
let () = print_liste_choix liste
in let () = print_string “Entrez un nombre svp”
in let () = print_newline ()
in let pion_int_tmp = read_int ()
in try pion_of_int pion_int_tmp with
| BAD_INPUT -> get_one_pion liste;;
let rec get_good_int liste =
let () = print_liste_choix liste
in let () = print_string “Entrez un nombre svp”
in let () = print_newline ()
in let pion_int_tmp = read_int ()
in if not (est_dans_main pion_int_tmp liste)
then let () = efface_ecran
in get_good_int liste
else pion_int_tmp;;
let manche () =
let () = efface_ecran
in let liste_all = [1;2;3;4;5;6;7]
in let () = efface_ecran
in let good_pion_lu_1 = get_good_int liste_all
in let liste_all_2 = listrm good_pion_lu_1 liste_all
in let () = efface_ecran
in let good_pion_lu_2 = get_good_int liste_all_2
in let liste_all_3 = listrm good_pion_lu_2 liste_all_2
in let () = efface_ecran
in let good_pion_lu_3 = get_good_int liste_all_3
in let liste_all_4 = listrm good_pion_lu_3 liste_all_3
in let () = efface_ecran
in let good_pion_lu_4 = get_good_int liste_all_4
in let () = efface_ecran
in let correcte_main_lu =
[ pion_of_int good_pion_lu_1;
pion_of_int good_pion_lu_2;
pion_of_int good_pion_lu_3;
pion_of_int good_pion_lu_4]
in let () = efface_ecran
in let () = print_string "Vous Proposez : "
in let () = print_string (string_of_main correcte_main_lu)
in let () = print_newline ()
in let reponse_main = reponse_main correcte_main_lu main_gagnante
in let () = print_string "Reponse : "
in let () = print_string (string_of_main reponse_main)
in reponse_main;;
let print_win n =
let () = print_newline ()
in let phrase = “Vous avez gagner en “^(string_of_int n)^” coup(s)”
in let () = print_string phrase
in print_newline ();;
let partie =
let rec partie n =
let res_tmp = manche ()
in if res_tmp = [Rouge;Rouge;Rouge;Rouge]
then print_win n
else partie (n+1)
in partie 1;;
[/cpp]
Le code marche mais je dois retoucher 2-3 truc d’affichage, donc rien de
grave.
Tu peux deja y jouer en attendant ce soir, je reposterais la version finale
tu dois trouver : (click sur le carre pour voir la reponse !)
[spoiler]
Blanc - Rouge - Orange - Noir
[/spoiler]
Mais fait moi le plaisir de me dire Ta (enfin ma) note
[:acrylic]