This repository has been archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp_jeu.adb
186 lines (167 loc) · 5 KB
/
p_jeu.adb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
with p_combinaisons, Ada.Characters.Handling, Ada.Calendar;
use p_combinaisons, Ada.Characters.Handling, Ada.Calendar;
package body p_jeu is
task body T_Chrono is
actif, fin: boolean := false;
prochaine_maj, datefin : Ada.Calendar.Time;
begin
while not fin loop
if not actif then
select
accept start(temps: in duration) do
actif := true;
datefin := Ada.Calendar.Clock + temps;
prochaine_maj := Ada.Calendar.Clock + FREQUENCE_MAJ;
tempsRestant := datefin - Ada.Calendar.Clock;
end start;
or
accept fermer do
fin := true;
end fermer;
end select;
else
select
accept stop do
actif := false;
end stop;
or
delay until prochaine_maj;
end select;
if actif then
if datefin < Ada.Calendar.Clock then
actif := false;
tempsRestant := 0.0;
finJeu(false);
else
tempsRestant := datefin - Ada.Calendar.Clock;
prochaine_maj := prochaine_maj + FREQUENCE_MAJ;
delay until prochaine_maj;
end if;
end if;
end if;
end loop;
end T_Chrono;
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"));
jeuEnCours := true;
chrono.start(30.0);
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
open(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));
else chrono.stop;
end if;
delete(fichierJeu);
close(fichierSolution);
jeuEnCours := false;
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 := to_upper(solution);
begin
if combinaison'length > 0 and combinaison'length <= 14 and jeuEnCours 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;
function Nbscores(f : in p_score_io.file_type) return integer is
--{f ouvert et f- = <>} => {copmpte de le Nb de score du fichier}
tmp : TR_Score;
i: integer := 0;
begin
while not end_of_file(f) loop
read(f,Tmp);
i := i+1;
end loop;
return i;
end Nbscores;
procedure CopieFicherScore(f : in out p_score_io.file_type ; V : out TV_Score) is
-- {f ouvert, V de taille suffisante} => {Copie les elements vers v}
tmp : TR_Score;
i: integer;
begin
i := v'first;
reset(f,IN_FILE);
while not end_of_file(f) loop
read(f,tmp);
v(i) := tmp;
i := i+1;
end loop;
close(f);
end CopieFicherScore;
procedure permut(a, b: in out TR_Score) is -- type des valeurs du vecteur
-- {} => {les valeurs de a et b ont été échangées}
temp: TR_Score ;
begin
temp := a;
a := b;
b := temp;
end permut;
procedure triBullesScores(V : in out TV_Score) is
-- {} => {V trié par ordre croissant}
i : integer := V'first;
permutation: boolean := true;
begin
while permutation loop
permutation := false;
for j in reverse i+1..V'last loop
if V(j).score > V(j-1).score then
permut(V(j), V(j-1));
permutation := true;
end if;
end loop;
i := i+1;
end loop;
end triBullesScores;
end p_jeu;