-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathREVERSI.PAS
More file actions
697 lines (615 loc) · 14.9 KB
/
REVERSI.PAS
File metadata and controls
697 lines (615 loc) · 14.9 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
{ @author: Sylvain Maltais ([email protected])
@created: 2025
@website(https://www.gladir.com/7iles)
@abstract(Target: Turbo Pascal 7, Free Pascal 3.2)
@description: Jeu de Reversi (Othello) - Retournez les pions adverses
}
Program Reversi;
Uses
{$IFDEF FPC}
PtcCrt, PtcGraph, PtcMouse
{$ELSE}
DOS, Crt, Graph
{$ENDIF};
Const
SCREEN_WIDTH = 640;
SCREEN_HEIGHT = 480;
BOARD_SIZE = 8;
CELL_SIZE = 40;
BOARD_X = 50;
BOARD_Y = 50;
DISC_RADIUS = 15;
Type
TPlayer = (pEmpty, pBlack, pWhite);
TGameState = (gsPlaying, gsBlackWins, gsWhiteWins, gsDraw, gsQuit);
TGameMode = (gmHumanVsHuman, gmHumanVsComputer);
TBoard = Array[0..BOARD_SIZE-1, 0..BOARD_SIZE-1] of TPlayer;
TGame = Record
board: TBoard;
currentPlayer: TPlayer;
gameState: TGameState;
gameMode: TGameMode;
selectedRow, selectedCol: Integer;
needRedraw: Boolean;
blackScore, whiteScore: Integer;
canMove: Boolean;
passCount: Integer;
End;
Var
Game: TGame;
{ Déclarations forward }
Procedure InitializeGame; Forward;
Function GetPlayerColor(player: TPlayer): Integer;
Begin
Case player of
pBlack: GetPlayerColor := 0;
pWhite: GetPlayerColor := 15;
else GetPlayerColor := 8;
End;
End;
Function GetPlayerName(player: TPlayer): String;
Begin
Case player of
pBlack: GetPlayerName := 'Noir';
pWhite: GetPlayerName := 'Blanc';
else GetPlayerName := 'Vide';
End;
End;
Function OpponentPlayer(player: TPlayer): TPlayer;
Begin
If player = pBlack Then
OpponentPlayer := pWhite
Else
OpponentPlayer := pBlack;
End;
Procedure ClearBoard;
Var
row, col: Integer;
Begin
For row := 0 to BOARD_SIZE-1 do
For col := 0 to BOARD_SIZE-1 do
Game.board[row, col] := pEmpty;
End;
Procedure SetupInitialPosition;
Begin
ClearBoard;
{ Position initiale du Reversi }
Game.board[3, 3] := pWhite;
Game.board[3, 4] := pBlack;
Game.board[4, 3] := pBlack;
Game.board[4, 4] := pWhite;
End;
Function IsValidPosition(row, col: Integer): Boolean;
Begin
IsValidPosition := (row >= 0) and (row < BOARD_SIZE) and
(col >= 0) and (col < BOARD_SIZE);
End;
Function CheckDirection(row, col, deltaRow, deltaCol: Integer; player: TPlayer): Integer;
Var
r, c, count: Integer;
opponent: TPlayer;
Begin
CheckDirection := 0;
opponent := OpponentPlayer(player);
r := row + deltaRow;
c := col + deltaCol;
count := 0;
{ Compter les pions adverses dans cette direction }
While IsValidPosition(r, c) and (Game.board[r, c] = opponent) do
Begin
Inc(count);
r := r + deltaRow;
c := c + deltaCol;
End;
{ Vérifier si on trouve un pion de notre couleur à la fin }
If IsValidPosition(r, c) and (Game.board[r, c] = player) and (count > 0) Then
CheckDirection := count;
End;
Function IsValidMove(row, col: Integer; player: TPlayer): Boolean;
Var
deltaRow, deltaCol: Integer;
Begin
IsValidMove := False;
{ La case doit être vide }
If Game.board[row, col] <> pEmpty Then Exit;
{ Vérifier toutes les directions }
For deltaRow := -1 to 1 do
For deltaCol := -1 to 1 do
If (deltaRow <> 0) or (deltaCol <> 0) Then
If CheckDirection(row, col, deltaRow, deltaCol, player) > 0 Then
Begin
IsValidMove := True;
Exit;
End;
End;
Procedure FlipDiscs(row, col: Integer; player: TPlayer);
Var
deltaRow, deltaCol, flipCount, i: Integer;
r, c: Integer;
Begin
{ Vérifier toutes les directions }
For deltaRow := -1 to 1 do
For deltaCol := -1 to 1 do
If (deltaRow <> 0) or (deltaCol <> 0) Then
Begin
flipCount := CheckDirection(row, col, deltaRow, deltaCol, player);
If flipCount > 0 Then
Begin
{ Retourner les pions dans cette direction }
r := row + deltaRow;
c := col + deltaCol;
For i := 1 to flipCount do
Begin
Game.board[r, c] := player;
r := r + deltaRow;
c := c + deltaCol;
End;
End;
End;
End;
Function MakeMove(row, col: Integer; player: TPlayer): Boolean;
Begin
MakeMove := False;
If IsValidMove(row, col, player) Then
Begin
Game.board[row, col] := player;
FlipDiscs(row, col, player);
MakeMove := True;
End;
End;
Function HasValidMoves(player: TPlayer): Boolean;
Var
row, col: Integer;
Begin
HasValidMoves := False;
For row := 0 to BOARD_SIZE-1 do
For col := 0 to BOARD_SIZE-1 do
If IsValidMove(row, col, player) Then
Begin
HasValidMoves := True;
Exit;
End;
End;
Procedure CountScore;
Var
row, col: Integer;
Begin
Game.blackScore := 0;
Game.whiteScore := 0;
For row := 0 to BOARD_SIZE-1 do
For col := 0 to BOARD_SIZE-1 do
Case Game.board[row, col] of
pBlack: Inc(Game.blackScore);
pWhite: Inc(Game.whiteScore);
End;
End;
Procedure CheckGameEnd;
Begin
CountScore;
If not HasValidMoves(Game.currentPlayer) Then
Begin
Game.currentPlayer := OpponentPlayer(Game.currentPlayer);
Inc(Game.passCount);
If not HasValidMoves(Game.currentPlayer) Then
Begin
{ Aucun joueur ne peut jouer - fin de partie }
If Game.blackScore > Game.whiteScore Then
Game.gameState := gsBlackWins
Else If Game.whiteScore > Game.blackScore Then
Game.gameState := gsWhiteWins
Else
Game.gameState := gsDraw;
End
Else
Game.passCount := 0;
End
Else
Game.passCount := 0;
End;
Function EvaluatePosition(player: TPlayer): Integer;
Var
row, col, score, corners, edges: Integer;
Begin
score := 0;
corners := 0;
edges := 0;
For row := 0 to BOARD_SIZE-1 do
For col := 0 to BOARD_SIZE-1 do
If Game.board[row, col] = player Then
Begin
Inc(score);
{ Bonus pour les coins }
If ((row = 0) or (row = BOARD_SIZE-1)) and
((col = 0) or (col = BOARD_SIZE-1)) Then
Inc(corners, 25);
{ Bonus pour les bords }
If (row = 0) or (row = BOARD_SIZE-1) or
(col = 0) or (col = BOARD_SIZE-1) Then
Inc(edges, 5);
End
Else If Game.board[row, col] = OpponentPlayer(player) Then
Begin
Dec(score);
{ Malus pour les coins adverses }
If ((row = 0) or (row = BOARD_SIZE-1)) and
((col = 0) or (col = BOARD_SIZE-1)) Then
Dec(corners, 25);
End;
EvaluatePosition := score + corners + edges;
End;
Function GetBestMove(player: TPlayer; Var bestRow, bestCol: Integer): Boolean;
Var
row, col, score, bestScore: Integer;
tempBoard: TBoard;
i, j: Integer;
Begin
GetBestMove := False;
bestScore := -1000;
bestRow := -1;
bestCol := -1;
{ Sauvegarder le plateau }
For i := 0 to BOARD_SIZE-1 do
For j := 0 to BOARD_SIZE-1 do
tempBoard[i, j] := Game.board[i, j];
For row := 0 to BOARD_SIZE-1 do
For col := 0 to BOARD_SIZE-1 do
If IsValidMove(row, col, player) Then
Begin
{ Simuler le coup }
Game.board[row, col] := player;
FlipDiscs(row, col, player);
score := EvaluatePosition(player);
If score > bestScore Then
Begin
bestScore := score;
bestRow := row;
bestCol := col;
GetBestMove := True;
End;
{ Restaurer le plateau }
For i := 0 to BOARD_SIZE-1 do
For j := 0 to BOARD_SIZE-1 do
Game.board[i, j] := tempBoard[i, j];
End;
End;
Procedure ComputerMove;
Var
row, col: Integer;
Begin
If (Game.gameMode = gmHumanVsComputer) and (Game.currentPlayer = pWhite) and
(Game.gameState = gsPlaying) Then
Begin
Delay(500);
If GetBestMove(pWhite, row, col) Then
Begin
MakeMove(row, col, pWhite);
Game.currentPlayer := pBlack;
Game.needRedraw := True;
CheckGameEnd;
End;
End;
End;
Procedure HandleInput;
Var
key: Char;
Begin
If KeyPressed Then
Begin
key := ReadKey;
Case key of
#0: Begin
key := ReadKey;
Case key of
#72: Begin { Flèche haut }
If Game.selectedRow > 0 Then
Begin
Dec(Game.selectedRow);
Game.needRedraw := True;
End;
End;
#80: Begin { Flèche bas }
If Game.selectedRow < BOARD_SIZE-1 Then
Begin
Inc(Game.selectedRow);
Game.needRedraw := True;
End;
End;
#75: Begin { Flèche gauche }
If Game.selectedCol > 0 Then
Begin
Dec(Game.selectedCol);
Game.needRedraw := True;
End;
End;
#77: Begin { Flèche droite }
If Game.selectedCol < BOARD_SIZE-1 Then
Begin
Inc(Game.selectedCol);
Game.needRedraw := True;
End;
End;
End;
End;
#13, ' ': Begin { Entrée ou Espace }
If (Game.gameState = gsPlaying) and
((Game.gameMode = gmHumanVsHuman) or (Game.currentPlayer = pBlack)) Then
Begin
If MakeMove(Game.selectedRow, Game.selectedCol, Game.currentPlayer) Then
Begin
Game.currentPlayer := OpponentPlayer(Game.currentPlayer);
Game.needRedraw := True;
CheckGameEnd;
End;
End;
End;
'p', 'P': Begin { Passer le tour }
If Game.gameState = gsPlaying Then
Begin
Game.currentPlayer := OpponentPlayer(Game.currentPlayer);
Game.needRedraw := True;
CheckGameEnd;
End;
End;
'n', 'N': Begin { Nouvelle partie }
InitializeGame;
End;
'm', 'M': Begin { Changer mode }
If Game.gameMode = gmHumanVsHuman Then
Game.gameMode := gmHumanVsComputer
Else
Game.gameMode := gmHumanVsHuman;
InitializeGame;
End;
#27: Game.gameState := gsQuit; { ESC }
End;
End;
End;
Procedure DrawDisc(x, y: Integer; player: TPlayer; selected: Boolean);
Begin
If selected Then
Begin
SetColor(14);
Circle(x, y, DISC_RADIUS + 2);
End;
Case player of
pBlack: Begin
SetColor(0);
SetFillStyle(1, 0);
FillEllipse(x, y, DISC_RADIUS, DISC_RADIUS);
SetColor(15);
Circle(x, y, DISC_RADIUS);
End;
pWhite: Begin
SetColor(15);
SetFillStyle(1, 15);
FillEllipse(x, y, DISC_RADIUS, DISC_RADIUS);
SetColor(0);
Circle(x, y, DISC_RADIUS);
End;
End;
End;
Procedure DrawBoard;
Var
row, col, x, y: Integer;
validMove: Boolean;
Begin
{ Dessiner le plateau }
SetColor(2);
SetFillStyle(1, 2);
Bar(BOARD_X - 5, BOARD_Y - 5,
BOARD_X + BOARD_SIZE * CELL_SIZE + 5,
BOARD_Y + BOARD_SIZE * CELL_SIZE + 5);
For row := 0 to BOARD_SIZE-1 do
For col := 0 to BOARD_SIZE-1 do
Begin
x := BOARD_X + col * CELL_SIZE;
y := BOARD_Y + row * CELL_SIZE;
{ Dessiner la case }
SetColor(0);
Rectangle(x, y, x + CELL_SIZE, y + CELL_SIZE);
{ Marquer les coups valides }
validMove := IsValidMove(row, col, Game.currentPlayer);
If validMove and (Game.gameState = gsPlaying) Then
Begin
SetColor(14);
SetFillStyle(1, 14);
Bar(x + 2, y + 2, x + CELL_SIZE - 2, y + CELL_SIZE - 2);
End;
{ Dessiner le pion }
If Game.board[row, col] <> pEmpty Then
Begin
DrawDisc(x + CELL_SIZE div 2, y + CELL_SIZE div 2,
Game.board[row, col],
(row = Game.selectedRow) and (col = Game.selectedCol));
End
Else If (row = Game.selectedRow) and (col = Game.selectedCol) Then
Begin
SetColor(15);
Rectangle(x + 5, y + 5, x + CELL_SIZE - 5, y + CELL_SIZE - 5);
End;
End;
End;
Procedure DrawGameInfo;
Var
modeStr, scoreStr: String;
Begin
SetColor(15);
SetTextStyle(0, 0, 1);
If Game.gameMode = gmHumanVsHuman Then
modeStr := '2 Joueurs'
Else
modeStr := 'vs Ordinateur';
OutTextXY(400, 50, 'Mode: ' + modeStr);
Str(Game.blackScore, scoreStr);
OutTextXY(400, 80, 'Noir: ' + scoreStr);
Str(Game.whiteScore, scoreStr);
OutTextXY(400, 100, 'Blanc: ' + scoreStr);
If Game.gameState = gsPlaying Then
OutTextXY(400, 130, 'Tour: ' + GetPlayerName(Game.currentPlayer))
Else
OutTextXY(400, 130, 'Partie terminee');
If Game.passCount > 0 Then
OutTextXY(400, 150, 'Joueur passe...');
End;
Procedure DrawInstructions;
Begin
SetColor(15);
SetTextStyle(0, 0, 1);
OutTextXY(400, 200, 'REVERSI (OTHELLO)');
OutTextXY(400, 230, 'Regles:');
OutTextXY(400, 250, '- Encadrez les pions');
OutTextXY(400, 270, ' adverses pour les');
OutTextXY(400, 290, ' retourner');
OutTextXY(400, 310, '- Plus de pions = victoire');
OutTextXY(400, 350, 'Commandes:');
OutTextXY(400, 370, 'Fleches: Deplacer curseur');
OutTextXY(400, 390, 'ENTREE: Jouer');
OutTextXY(400, 410, 'P: Passer tour');
OutTextXY(400, 430, 'N: Nouvelle partie');
OutTextXY(400, 450, 'M: Changer mode');
End;
Procedure DrawGameStatus;
Begin
SetColor(14);
SetTextStyle(0, 0, 2);
Case Game.gameState of
gsPlaying: Begin
If not HasValidMoves(Game.currentPlayer) Then
Begin
SetColor(12);
OutTextXY(50, 400, 'Aucun coup possible!');
SetColor(15);
SetTextStyle(0, 0, 1);
OutTextXY(50, 420, 'Appuyez sur P pour passer');
End;
End;
gsBlackWins: Begin
OutTextXY(50, 400, 'NOIR GAGNE!');
SetColor(15);
SetTextStyle(0, 0, 1);
OutTextXY(50, 430, 'N: Nouvelle partie');
End;
gsWhiteWins: Begin
OutTextXY(50, 400, 'BLANC GAGNE!');
SetColor(15);
SetTextStyle(0, 0, 1);
OutTextXY(50, 430, 'N: Nouvelle partie');
End;
gsDraw: Begin
OutTextXY(50, 400, 'MATCH NUL!');
SetColor(15);
SetTextStyle(0, 0, 1);
OutTextXY(50, 430, 'N: Nouvelle partie');
End;
End;
End;
Procedure Render;
Begin
If Game.needRedraw Then
Begin
{ Fond }
SetColor(8);
SetFillStyle(1, 8);
Bar(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
{ Titre }
SetColor(15);
SetTextStyle(0, 0, 2);
OutTextXY(50, 20, 'REVERSI');
DrawBoard;
DrawGameInfo;
DrawInstructions;
DrawGameStatus;
Game.needRedraw := False;
End;
End;
Procedure ShowTitle;
Begin
SetColor(8);
SetFillStyle(1, 8);
Bar(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
SetColor(15);
SetTextStyle(0, 0, 3);
OutTextXY(SCREEN_WIDTH div 2 - 100, 80, 'REVERSI');
OutTextXY(SCREEN_WIDTH div 2 - 120, 120, '(OTHELLO)');
SetColor(15);
SetTextStyle(0, 0, 1);
OutTextXY(50, 180, 'Le but est d''avoir le plus de pions de votre couleur');
OutTextXY(50, 200, 'a la fin de la partie.');
OutTextXY(50, 230, 'Regles:');
OutTextXY(50, 250, '- Placez un pion pour encadrer des pions adverses');
OutTextXY(50, 270, '- Tous les pions encadres sont retournes');
OutTextXY(50, 290, '- Vous devez encadrer au moins un pion');
OutTextXY(50, 310, '- Si aucun coup possible, vous passez votre tour');
OutTextXY(50, 350, 'Commandes:');
OutTextXY(50, 370, 'Fleches: Deplacer le curseur');
OutTextXY(50, 390, 'ENTREE/ESPACE: Placer un pion');
OutTextXY(50, 410, 'P: Passer votre tour');
OutTextXY(50, 450, 'Choisissez le mode: 1=vs IA, 2=2 joueurs, ESC=Quitter');
Repeat
If KeyPressed Then
Begin
Case ReadKey of
'1': Begin
Game.gameMode := gmHumanVsComputer;
Exit;
End;
'2': Begin
Game.gameMode := gmHumanVsHuman;
Exit;
End;
#27: Halt;
End;
End;
Delay(50);
Until False;
End;
Procedure InitializeGame;
Begin
SetupInitialPosition;
Game.currentPlayer := pBlack;
Game.gameState := gsPlaying;
Game.selectedRow := 3;
Game.selectedCol := 3;
Game.needRedraw := True;
Game.passCount := 0;
CountScore;
End;
Procedure GameLoop;
Begin
While Game.gameState <> gsQuit do
Begin
HandleInput;
ComputerMove;
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);
Randomize;
End;
BEGIN
InitializeGraphics;
ShowTitle;
InitializeGame;
GameLoop;
CloseGraph;
END.