-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMASTERMD.PAS
More file actions
467 lines (431 loc) · 10.2 KB
/
Copy pathMASTERMD.PAS
File metadata and controls
467 lines (431 loc) · 10.2 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
{ @author: Sylvain Maltais (support@gladir.com)
@created: 2025
@website(https://www.gladir.com/7iles)
@abstract(Target: Turbo Pascal 7, Free Pascal 3.2)
@description: Jeu MasterMind - Devinez la combinaison secrŠte de couleurs
}
Program MasterMind;
Uses
{$IFDEF FPC}
PtcCrt, PtcGraph, PtcMouse
{$ELSE}
Crt, Graph
{$ENDIF};
Const
SCREEN_WIDTH = 640;
SCREEN_HEIGHT = 480;
MAX_ATTEMPTS = 10;
CODE_LENGTH = 4;
NUM_COLORS = 6;
{ Couleurs disponibles }
COLOR_RED = 4;
COLOR_GREEN = 2;
COLOR_BLUE = 1;
COLOR_YELLOW = 14;
COLOR_MAGENTA = 5;
COLOR_CYAN = 3;
PEG_SIZE = 30;
FEEDBACK_SIZE = 15;
Type
TCode=Array[0..CODE_LENGTH-1] of Integer;
TGameState=(gsPlaying, gsWon, gsLost, gsQuit);
TGame=Record
secretCode:TCode;
attempts:Array[0..MAX_ATTEMPTS-1] of TCode;
feedback:Array[0..MAX_ATTEMPTS-1,0..CODE_LENGTH-1]of Integer;
currentAttempt:Integer;
currentPosition:Integer;
gameState:TGameState;
needRedraw:Boolean;
End;
Var
Game:TGame;
Colors:Array[0..NUM_COLORS-1] of Integer;
Procedure InitializeColors;Begin
Colors[0]:=COLOR_RED;
Colors[1]:=COLOR_GREEN;
Colors[2]:=COLOR_BLUE;
Colors[3]:=COLOR_YELLOW;
Colors[4]:=COLOR_MAGENTA;
Colors[5]:=COLOR_CYAN;
End;
Procedure GenerateSecretCode;
Var
i:Integer;
Begin
Randomize;
For i:=0 to CODE_LENGTH-1 do Game.secretCode[i]:=Random(NUM_COLORS);
End;
Procedure InitializeGame;
Var
i,j:Integer;
Begin
GenerateSecretCode;
Game.currentAttempt:=0;
Game.currentPosition:=0;
Game.gameState:=gsPlaying;
Game.needRedraw:=True;
{ Initialiser les tentatives }
For i:=0 to MAX_ATTEMPTS-1 do Begin
For j:=0 to CODE_LENGTH-1 do Begin
Game.attempts[i][j]:=-1;
Game.feedback[i][j]:=0;
End;
End;
End;
Procedure CalculateFeedback(attempt:Integer);
Var
i,j:Integer;
correctPosition,correctColor:Integer;
secretUsed,attemptUsed:Array[0..CODE_LENGTH-1] of Boolean;
Begin
correctPosition:=0;
correctColor:=0;
{ Initialiser les tableaux d'utilisation }
For i:=0 to CODE_LENGTH-1 do Begin
secretUsed[i]:=False;
attemptUsed[i]:=False;
End;
{ V‚rifier les bonnes positions }
For i := 0 to CODE_LENGTH-1 do Begin
If Game.attempts[attempt][i]=Game.secretCode[i]Then Begin
Inc(correctPosition);
secretUsed[i]:=True;
attemptUsed[i]:=True;
End;
End;
{ V‚rifier les bonnes couleurs mal plac‚es }
For i := 0 to CODE_LENGTH-1 do Begin
If Not attemptUsed[i]Then Begin
For j:=0 to CODE_LENGTH-1 do Begin
If(Not secretUsed[j]) and(Game.attempts[attempt][i] = Game.secretCode[j])Then Begin
Inc(correctColor);
secretUsed[j]:=True;
Break;
End;
End;
End;
End;
{ Entreposer le feedback }
For i:=0 to correctPosition-1 do Game.feedback[attempt][i]:=2; { Noir - bonne position }
For i:=correctPosition to correctPosition+correctColor-1 do Game.feedback[attempt][i]:=1; { Blanc - bonne couleur }
End;
Function IsCodeComplete:Boolean;
Var
i:Integer;
Begin
IsCodeComplete:=True;
For i:=0 to CODE_LENGTH-1 do Begin
If Game.attempts[Game.currentAttempt][i]=-1 Then Begin
IsCodeComplete:=False;
Exit;
End;
End;
End;
Function IsWinningCode:Boolean;
Var
i:Integer;
Begin
IsWinningCode:=True;
For i:=0 to CODE_LENGTH-1 do Begin
If Game.attempts[Game.currentAttempt][i]<>Game.secretCode[i]Then Begin
IsWinningCode:=False;
Exit;
End;
End;
End;
Procedure HandleInput;
Var
key:Char;
Begin
If KeyPressed Then Begin
key:=ReadKey;
Case key of
'1'..'6':Begin
If Game.gameState=gsPlaying Then Begin
Game.attempts[Game.currentAttempt][Game.currentPosition]:=Ord(key)-Ord('1');
Inc(Game.currentPosition);
If Game.currentPosition>=CODE_LENGTH Then Game.currentPosition:=CODE_LENGTH-1;
Game.needRedraw:=True;
End;
End;
#8:Begin { Backspace }
If (Game.gameState=gsPlaying)and(Game.currentPosition>0)Then Begin
Dec(Game.currentPosition);
Game.attempts[Game.currentAttempt][Game.currentPosition]:=-1;
Game.needRedraw:=True;
End;
End;
#13:Begin { Entr‚e }
If (Game.gameState = gsPlaying)and IsCodeComplete Then Begin
CalculateFeedback(Game.currentAttempt);
If IsWinningCode Then Game.gameState:=gsWon
Else
Begin
Inc(Game.currentAttempt);
Game.currentPosition:=0;
If Game.currentAttempt>=MAX_ATTEMPTS Then Game.gameState:=gsLost;
End;
Game.needRedraw:=True;
End;
End;
'n', 'N':Begin
If Game.gameState in[gsWon, gsLost]Then Begin
InitializeGame;
Game.needRedraw:=True;
End;
End;
#27:Game.gameState:=gsQuit; { ESC }
End;
End;
End;
Procedure DrawPeg(x,y,colorIndex:Integer);Begin
If colorIndex>=0 Then Begin
SetColor(Colors[colorIndex]);
SetFillStyle(1,Colors[colorIndex]);
FillEllipse(x,y,PEG_SIZE div 2,PEG_SIZE div 2);
SetColor(0);
Circle(x,y,PEG_SIZE div 2);
End
Else
Begin
SetColor(8);
SetFillStyle(1, 8);
FillEllipse(x, y, PEG_SIZE div 2, PEG_SIZE div 2);
SetColor(15);
Circle(x, y, PEG_SIZE div 2);
End;
End;
Procedure DrawFeedbackPeg(x,y,feedbackType:Integer);Begin
Case feedbackType of
2: Begin { Noir - bonne position }
SetColor(0);
SetFillStyle(1, 0);
End;
1:Begin { Blanc - bonne couleur }
SetColor(15);
SetFillStyle(1, 15);
End;
Else Begin { Vide }
SetColor(8);
SetFillStyle(1, 8);
End;
End;
FillEllipse(x, y, FEEDBACK_SIZE div 2, FEEDBACK_SIZE div 2);
SetColor(7);
Circle(x,y,FEEDBACK_SIZE div 2);
End;
Procedure DrawAttempt(attemptNum:Integer);
Var
i: Integer;
x, y: Integer;
Begin
y := 60 + attemptNum * 40;
{ Dessiner le num‚ro de tentative }
SetColor(15);
SetTextStyle(0, 0, 1);
OutTextXY(10, y-5, Chr(Ord('1')+attemptNum));
{ Dessiner les pegs de la tentative }
For i:=0 to CODE_LENGTH-1 do Begin
x := 50 + i * 40;
DrawPeg(x, y, Game.attempts[attemptNum][i]);
End;
{ Dessiner le feedback }
For i := 0 to CODE_LENGTH-1 do Begin
x := 250 + (i mod 2) * 20;
y := 55 + attemptNum * 40 + (i div 2) * 20;
DrawFeedbackPeg(x, y, Game.feedback[attemptNum][i]);
End;
End;
Procedure DrawCurrentAttempt;
Var
i: Integer;
x, y: Integer;
Begin
If Game.gameState = gsPlaying Then Begin
y := 60 + Game.currentAttempt * 40;
{ Dessiner le num‚ro de tentative }
SetColor(14);
SetTextStyle(0, 0, 1);
OutTextXY(10, y - 5, Chr(Ord('1') + Game.currentAttempt));
{ Dessiner les pegs de la tentative courante }
For i := 0 to CODE_LENGTH-1 do
Begin
x := 50 + i * 40;
DrawPeg(x, y, Game.attempts[Game.currentAttempt][i]);
{ Surbrillance pour la position courante }
If i = Game.currentPosition Then
Begin
SetColor(14);
Circle(x, y, PEG_SIZE div 2 + 2);
End;
End;
End;
End;
Procedure DrawColorPalette;
Var
i: Integer;
x, y: Integer;
Begin
SetColor(15);
SetTextStyle(0, 0, 1);
OutTextXY(350, 50, 'Couleurs disponibles:');
For i := 0 to NUM_COLORS-1 do
Begin
x := 350 + (i mod 3) * 50;
y := 80 + (i div 3) * 50;
DrawPeg(x, y, i);
{ Afficher le num‚ro }
SetColor(15);
OutTextXY(x - 5, y + 25, Chr(Ord('1') + i));
End;
End;
Procedure DrawSecretCode;
Var
i: Integer;
x: Integer;
Begin
SetColor(15);
SetTextStyle(0, 0, 1);
OutTextXY(50, 0, 'Code secret:');
For i := 0 to CODE_LENGTH-1 do
Begin
x := 50 + i * 40;
If Game.gameState in [gsWon, gsLost] Then
DrawPeg(x, 30, Game.secretCode[i])
Else
Begin
SetColor(0);
SetFillStyle(1, 0);
FillEllipse(x, 30, PEG_SIZE div 2, PEG_SIZE div 2);
SetColor(15);
Circle(x, 30, PEG_SIZE div 2);
OutTextXY(x - 5, 25, '?');
End;
End;
End;
Procedure DrawInstructions;
Begin
SetColor(15);
SetTextStyle(0, 0, 1);
OutTextXY(350, 200, 'Instructions:');
OutTextXY(350, 220, '1-6: Choisir couleur');
OutTextXY(350, 240, 'Retour: Effacer');
OutTextXY(350, 260, 'Entr‚e: Valider');
OutTextXY(350, 280, 'ESC: Quitter');
SetColor(12);
OutTextXY(350, 320, 'Feedback:');
DrawFeedbackPeg(350, 340, 2);
OutTextXY(370, 335, 'Bonne position');
DrawFeedbackPeg(350, 360, 1);
OutTextXY(370, 355, 'Bonne couleur');
End;
Procedure DrawGameStatus;
Begin
SetColor(15);
SetTextStyle(0, 0, 1);
Case Game.gameState of
gsPlaying: Begin
OutTextXY(350, 400, 'Tentative: ' + Chr(Ord('1') + Game.currentAttempt));
OutTextXY(350, 420, 'Position: ' + Chr(Ord('1') + Game.currentPosition));
End;
gsWon: Begin
SetColor(10);
SetTextStyle(0, 0, 2);
OutTextXY(350, 400, 'GAGNE!');
SetColor(15);
SetTextStyle(0, 0, 1);
OutTextXY(350, 430, 'N: Nouvelle partie');
End;
gsLost: Begin
SetColor(12);
SetTextStyle(0, 0, 2);
OutTextXY(350, 400, 'PERDU!');
SetColor(15);
SetTextStyle(0, 0, 1);
OutTextXY(350, 430, 'N: Nouvelle partie');
End;
End;
End;
Procedure Render;
Var
i:Integer;
Begin
If Game.needRedraw Then Begin
{ Fond noir }
SetColor(0);
SetFillStyle(1,0);
Bar(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
DrawSecretCode;
DrawColorPalette;
DrawInstructions;
DrawGameStatus;
{ Dessiner toutes les tentatives }
For i:=0 to Game.currentAttempt-1 do DrawAttempt(i);
{ Dessiner la tentative courante }
DrawCurrentAttempt;
Game.needRedraw := False;
End;
End;
Procedure ShowTitle;
Begin
SetColor(0);
SetFillStyle(1, 0);
Bar(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
SetColor(14);
SetTextStyle(0, 0, 3);
OutTextXY(SCREEN_WIDTH div 2 - 100, 100, 'MASTERMIND');
SetColor(15);
SetTextStyle(0, 0, 1);
OutTextXY(50, 200, 'Devinez la combinaison secrŠte de 4 couleurs.');
OutTextXY(50, 220, 'Vous avez 10 tentatives.');
OutTextXY(50, 240, 'Utilisez les chiffres 1-6 pour choisir les couleurs.');
OutTextXY(50, 260, 'Un peg noir indique une couleur … la bonne position.');
OutTextXY(50, 280, 'Un peg blanc indique une bonne couleur mal plac‚e.');
OutTextXY(50, 320, 'Appuyez sur ENTRE pour commencer...');
OutTextXY(50, 340, 'ESC pour quitter');
Repeat
If KeyPressed Then Begin
Case ReadKey of
#13: Exit;
#27: Halt;
End;
End;
Delay(50);
Until False;
End;
Procedure GameLoop;Begin
While Game.gameState<>gsQuit do Begin
HandleInput;
Render;
Delay(100);
End;
End;
Procedure InitializeGraphics;
Var
Driver,Mode:Integer;
ErrCode:Integer;
Begin
{$IFDEF FPC}
Driver:=VGA;
Mode:=VGAHi;
{$ELSE}
Driver:=Detect;
Mode:=VGAHi;
{$ENDIF}
InitGraph(Driver, Mode, '');
ErrCode:=GraphResult;
If ErrCode<>grOk Then Begin
WriteLn('Erreur graphique : ', GraphErrorMsg(ErrCode));
Halt;
End;
SetLineStyle(0, 0, 1);
End;
BEGIN
InitializeGraphics;
InitializeColors;
ShowTitle;
InitializeGame;
GameLoop;
CloseGraph;
END.