diff --git a/p_jeu.adb b/p_jeu.adb new file mode 100644 index 0000000..395e1d5 --- /dev/null +++ b/p_jeu.adb @@ -0,0 +1,81 @@ +with p_combinaisons ; +use p_combinaisons ; + +package body p_jeu is + procedure debutJeu(contigue: in boolean) is + -- {} => {Lance le jeu} + begin + tailleSolution := 0; + create(fichierJeu, IN_FILE, "solutionsTrouvees"); + open(fichierSolution, IN_FILE, (if contigue then "foutcont.txt" else "fout.txt")); + end debutJeu; + + function compterPoints return integer is + -- {fichierJeu ouvert} => {résultat = nombre de points du joueur} + sol: string(1..15); + nb: integer; + score : integer := 0; + begin + reset(fichierJeu, IN_FILE); + while not end_of_file(fichierJeu) loop + get_line(fichierJeu, sol, nb); + case nb/2 is + when 3 => score := score + 2; + when 4 => score := score + 1; + when 5 => score := score + 2; + when 6 => score := score + 3; + when 7 => score := score + 5; + when others => null; + end case; + end loop; + + return score; + end compterPoints; + + procedure enregistrerScore(score: in TR_Score) is + -- {} => {le score a été enregistré dans le fichier de scores} + f: p_score_io.file_type; + begin + create(f, APPEND_FILE, "score"); + write(f, score); + close(f); + end enregistrerScore; + + procedure finJeu(abandon: in boolean) is + -- {} => {Finit le jeu} + begin + if not abandon then enregistrerScore((pseudo, compterPoints)); end if; + delete(fichierJeu); + close(fichierSolution); + end finJeu; + + procedure verifSol(solution: in string; result: out integer) is + -- {} => {result contient le statut de la solution (correcte, doublon, incorrecte)} + estValide, dejaTrouve: boolean; + combinaison: string := solution; + begin + if combinaison'length > 0 and combinaison'length <= 14 then + ordonne(combinaison); + + dernier := (others => ' '); + dernier(combinaison'range) := combinaison; + tailleSolution := combinaison'length; + + resultatExiste(fichierSolution, combinaison, estValide); + if estValide then -- la solution existe + resultatExiste(fichierJeu, combinaison, dejaTrouve); + if not dejaTrouve then -- la solution n'a pas encore été découverte + result := SOLUTION_CORRECTE; + reset(fichierJeu, APPEND_FILE); + put_line(fichierJeu, combinaison); + else + result := SOLUTION_DOUBLON; + end if; + else + result := SOLUTION_INCORRECTE; + end if; + else + result := SOLUTION_INVALIDE; + end if; + end verifSol; +end p_jeu; diff --git a/p_jeu.ads b/p_jeu.ads new file mode 100644 index 0000000..be5edb0 --- /dev/null +++ b/p_jeu.ads @@ -0,0 +1,38 @@ +with text_io, sequential_IO; +use text_io; + +package p_jeu is + type TR_Score is record + pseudo: string(1..20); + score: integer; + end record; + + package p_score_io is new sequential_IO(TR_Score); use p_score_io; + + SOLUTION_CORRECTE : constant integer := 0; + SOLUTION_DOUBLON : constant integer := 1; + SOLUTION_INCORRECTE : constant integer := 2; + SOLUTION_INVALIDE : constant integer := 3; + + tailleSolution : integer; + fichierSolution : text_io.file_type; + dernier : string(1..14); + fichierJeu : text_io.file_type; + pseudo: string(1..20); + + procedure debutJeu(contigue: in boolean); + -- {} => {Lance le jeu} + + function compterPoints return integer; + -- {fichierJeu ouvert} => {résultat = nombre de points du joueur} + + procedure enregistrerScore(score: in TR_Score); + -- {} => {le score a été enregistré dans le fichier de scores} + + procedure finJeu(abandon: in boolean); + -- {} => {Finit le jeu} + + procedure verifSol(solution: in string; result: out integer); + -- {} => {Vérifie si la solution est correcte} + +end p_jeu; diff --git a/p_vue_graph.adb b/p_vue_graph.adb index f6a19c7..5500f96 100644 --- a/p_vue_graph.adb +++ b/p_vue_graph.adb @@ -1,5 +1,5 @@ -with p_fenbase, Forms, p_combinaisons, Ada.Strings, Ada.Strings.Fixed, text_io, X.Strings; -use p_fenbase, Forms, p_combinaisons, Ada.Strings, Ada.Strings.Fixed, text_io, p_combinaisons.p_cases_io; +with p_fenbase, Forms, p_combinaisons, p_jeu, Ada.Strings, Ada.Strings.Fixed, text_io, X.Strings; +use p_fenbase, Forms, p_combinaisons, p_jeu, Ada.Strings, Ada.Strings.Fixed, text_io, p_combinaisons.p_cases_io; package body p_vue_graph is @@ -229,7 +229,10 @@ package body p_vue_graph is end if; elsif Elem = "jeu" then cacherFenetre(fenetre); - debutJeu; + debutJeu(contigue); + casesClic := (others => ' '); + fenetreRegles; + fenetreJeu; elsif Elem = "Fermer" then CacherFenetre(fenetre); else @@ -320,16 +323,18 @@ package body p_vue_graph is end ajoutCase; procedure appuiBoutonJeu (Elem : in string; fenetre : in out TR_Fenetre) is - pts : integer; + solutionCorrecte : integer; caseClic : string(1..2); begin -- if Elem = "SolutionProp" or Elem ="valider" then - verifSol(fenetre, consulterContenu(fenetre, "SolutionProp") & trim(casesClic, BOTH)); - pts := compterPoints; - changerTexte(fenetre, "Score", trim(Integer'image(pts), BOTH) & (if pts >= 2 then " Points" else " Point")); + effacerGrille(fenetre); + verifSol(consulterContenu(fenetre, "SolutionProp") & trim(casesClic, BOTH), solutionCorrecte); + actualisationEssai(fenetre, consulterContenu(fenetre, "SolutionProp") & trim(casesClic, BOTH), solutionCorrecte); appuiBoutonJeu(attendreBouton(fenetre),fenetre); elsif Elem = "abandon" then - finJeu(fenetre, true); + finJeu(true); + cacherFenetre(fenetre); + fenetreAccueil; elsif Elem'length = 3 and Elem(Elem'first) = 'B' and Elem(Elem'first + 1) in T_Col'range @@ -365,97 +370,46 @@ package body p_vue_graph is end loop; end affichageSol; - procedure debutJeu is - -- {} => {Lance le jeu} + procedure effacerGrille(fen: in out TR_Fenetre) is + -- {} => {Les solutions affichées sur la grille sont effacées} begin - nbCasesSolution := 0; - create(fichierJeu, IN_FILE, "solutionsTrouvees"); - open(fichierSolution, IN_FILE, (if contigue then "foutcont.txt" else "fout.txt")); - pseudo := (others => ' '); - casesClic := (others => ' '); - fenetreRegles; - fenetreJeu; - end debutJeu; - - function compterPoints return integer is - -- {fichierJeu ouvert} => {résultat = nombre de points du joueur} - sol: string(1..15); - nb: integer; - score : integer := 0; - begin - reset(fichierJeu, IN_FILE); - while not end_of_file(fichierJeu) loop - get_line(fichierJeu, sol, nb); - case nb/2 is - when 3 => score := score + 2; - when 4 => score := score + 1; - when 5 => score := score + 2; - when 6 => score := score + 3; - when 7 => score := score + 5; - when others => null; - end case; - end loop; - - return score; - end compterPoints; + if tailleSolution > 0 then + affichageSol(fen, dernier(1..tailleSolution), FL_COL1); + end if; - procedure enregistrerScore(score: in TR_Score) is - -- {} => {le score a été enregistré dans le fichier de scores} - f: p_score_io.file_type; - begin - create(f, APPEND_FILE, "score"); - write(f, score); - close(f); - end enregistrerScore; + if casesClic(1) /= ' ' then + affichageSol(fen, trim(casesClic, BOTH), FL_COL1); + end if; + end effacerGrille; - procedure finJeu(fen: in out TR_Fenetre; abandon: in boolean) is - -- {} => {Finit le jeu} + procedure actualisationEssai(fen: in out TR_Fenetre; solution: in string; resultat: in integer) is + -- {} => {Met à jour la grille avec la solution colorée après un essai dans le jeu} + coul: FL_Color; + pts: integer; + combinaison : string(1..14) := (others => ' '); begin - if not abandon then enregistrerScore((pseudo, compterPoints)); end if; - delete(fichierJeu); - close(fichierSolution); - cacherFenetre(fen); - fenetreAccueil; - end finJeu; - - procedure verifSol(fen: in out TR_Fenetre; solution: in string) is - -- {} => {Vérifie si la solution est correcte} - estValide, dejaTrouve: boolean; - combinaison: string := solution; - begin - if combinaison'length > 0 then - ordonne(combinaison); - if nbCasesSolution > 0 then - affichageSol(fen, dernier(1..nbCasesSolution*2), FL_COL1); - end if; + if solution'length <= combinaison'length then + combinaison(solution'range) := solution; + end if; - if casesClic(1) /= ' ' then - affichageSol(fen, trim(casesClic, BOTH), FL_COL1); - casesClic := (others => ' '); - end if; + case resultat is + when SOLUTION_CORRECTE => coul := FL_PALEGREEN; + when SOLUTION_DOUBLON => coul := FL_WHEAT; + when SOLUTION_INCORRECTE => coul := FL_INDIANRED; + when SOLUTION_INVALIDE => + coul := ancienneCoul; + combinaison := (others => ' '); + combinaison(1..tailleSolution) := dernier(1..tailleSolution); + when others => null; + end case; + ancienneCoul := coul; + + affichageSol(fen, trim(combinaison, BOTH), coul); + changerContenu(fen, "SolutionProp", ""); + pts := compterPoints; + changerTexte(fen, "Score", trim(Integer'image(pts), BOTH) & (if pts >= 2 then " Points" else " Point")); - dernier := (others => ' '); - dernier(combinaison'range) := combinaison; - nbCasesSolution := combinaison'length / 2; - changerContenu(fen, "SolutionProp", ""); - - resultatExiste(fichierSolution, combinaison, estValide); - if estValide then -- la solution existe - resultatExiste(fichierJeu, combinaison, dejaTrouve); - if not dejaTrouve then -- la solution n'a pas encore été découverte - affichageSol(fen, combinaison, FL_PALEGREEN); - reset(fichierJeu, APPEND_FILE); - put_line(fichierJeu, combinaison); - else - affichageSol(fen, combinaison, FL_WHEAT); - end if; - else - affichageSol(fen, combinaison, FL_INDIANRED); - end if; - end if; - exception - when others => - appuiBoutonJeu(attendreBouton(fen),fen); - end verifSol; + casesClic := (others => ' '); + end actualisationEssai; end p_vue_graph; diff --git a/p_vue_graph.ads b/p_vue_graph.ads index a794fe1..e252f41 100644 --- a/p_vue_graph.ads +++ b/p_vue_graph.ads @@ -1,13 +1,7 @@ -with p_fenbase, p_combinaisons, text_io, Forms, sequential_IO; +with p_fenbase, p_combinaisons, text_io, Forms; use p_fenbase, p_combinaisons, text_io, Forms; package p_vue_graph is - type TR_Score is record - pseudo: string(1..20); - score: integer; - end record; - - package p_score_io is new sequential_IO(TR_Score); use p_score_io; nbCasesSolution : integer; nbCombinaisons : integer; @@ -15,10 +9,8 @@ package p_vue_graph is combinaisonAct : integer; contigue : boolean; - dernier : string(1..14); - fichierJeu : text_io.file_type; - pseudo: string(1..20); casesClic: string(1..14); + ancienneCoul: FL_Color := FL_COL1; procedure AjouterBoutonInvisible (F : in out TR_Fenetre; NomElement : in String; X, Y : in Natural; Largeur, Hauteur : in Positive); @@ -43,22 +35,13 @@ package p_vue_graph is procedure actualisationInfos(fen: in out TR_Fenetre; combinaisonOld: integer); -- {} => {Actualisation des informations pour la solution nbSol} - procedure debutJeu; - -- {} => {Lance le jeu} - - function compterPoints return integer; - -- {fichierJeu ouvert} => {résultat = nombre de points du joueur} - - procedure enregistrerScore(score: in TR_Score); - -- {} => {le score a été enregistré dans le fichier de scores} - - procedure finJeu(fen: in out TR_Fenetre; abandon: in boolean); - -- {} => {Finit le jeu} - procedure affichageSol(fen: in out TR_Fenetre; combinaison: in string; coul: in FL_Color); -- {} => {Actualisation de la grille avec la solution de couleur coul} - procedure verifSol(fen: in out TR_Fenetre; solution: in string); - -- {} => {Vérifie si la solution est correcte} + procedure effacerGrille(fen: in out TR_Fenetre); + -- {} => {Les solutions affichées sur la grille sont effacées} + + procedure actualisationEssai(fen: in out TR_Fenetre; solution: in string; resultat: in integer); + -- {} => {Met à jour la grille avec la solution colorée après un essai dans le jeu} end p_vue_graph;