-
Notifications
You must be signed in to change notification settings - Fork 4
/
HANOIS.PAS
126 lines (119 loc) · 2.34 KB
/
HANOIS.PAS
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
{ @author: Sylvain Maltais (support@gladir.com)
@created: 2011
@website(https://www.gladir.com/7iles)
@abstract(Target: Turbo Pascal, Free Pascal)
}
Program Hanois;
Uses Crt;
Procedure Main;
Const
C:Array[1..7]of Char=
{$IFDEF FPC}
'======='
{$ELSE}
Char(177)+Char(177)+Char(177)+Char(177)+Char(177)+Char(177)+Char(177)
{$ENDIF};
C2:Array[1..7]of Char=
{$IFDEF FPC}
'======='
{$ELSE}
Char(178)+Char(178)+Char(178)+Char(178)+Char(178)+Char(178)+Char(178)
{$ENDIF};
XD:Array[1..3]of Byte=(9,25,41);
Var
A:Array[1..3,0..8]of Byte;
I:Byte;
T,F,N:Integer;
K:Word;
Procedure Update;
Var
J,X,Y,Z:Byte;
Begin
I:=0;
For Y:=15downto 8do Begin
Inc(I);
For X:=1to 3do Begin
Z:=A[X,I];
If Z=0Then Begin
GotoXY(XD[X]-7,Y); Write(' ':7,{$IFDEF FPC}'I'{$ELSE}Chr(219){$ENDIF},' ':7);
End
Else
For J:=XD[X]-Z to XD[X]+Z do Begin
GotoXY(J,Y);
If Odd(Z)Then Write(C[Z])
Else Write(C2[Z]);
End;
End;
End;
End;
Function ChkOk(R:Byte):Boolean;Begin
ChkOk:=True; I:=Byte(K)-Byte('0');
If I in [1..3]Then Begin
If R=1Then F:=I Else T:=I;
Write(Char(K));
GotoXY(10,20);
Write(' ':30);
End
Else
Begin
GotoXY(10,20);
Write('R‚pondre 1, 2 ou 3 S.V.P.');
ChkOk:=False;
End;
End;
Begin
FillChar(A,SizeOf(A),0);
N:=1; A[2,0]:=7; For I:=1to 7do A[2,I]:=8-I;
TextMode(CO80);
TextColor(7);
TextBackground(0);
ClrScr;
GotoXY(13,1);
Write('Tours d''Hanois');
TextColor(0);
TextBackground(2);
GotoXY(1,16);
WriteLn(' ':8,'1',' ':15,'2',' ':15,'3',' ':8);
TextBackground(0);
TextColor(7);
Repeat
Update;
GotoXY(1,18);
Write('Coup:',N);
ClrEol;
GotoXY(12,18);
Write('Votre Jeu - De:');
Repeat
K:=Byte(ReadKey);
If K=0Then K:=(K shl 8) or Byte(ReadKey);
If K=27Then Exit;
Until ChkOk(1);
GotoXY(30,18);
Write(' .:');
Repeat
K:=Byte(ReadKey);
If K=0Then K:=(K shl 8) or Byte(ReadKey);
If K=27Then Exit;
Until ChkOk(2);
GotoXY(10,20);
If A[T,0]<>0Then Begin
If Not((A[F,0]>0)and(A[F,A[F,0]]<A[T,A[T,0]]))Then Begin
Write('Coup ill‚gal! Recommencez');
Continue;
End;
End
Else
Write(' ':30);
Inc(A[T,0]); A[T,A[T,0]]:=A[F,A[F,0]];
A[F,A[F,0]]:=0; Dec(A[F,0]); Inc(N);
If(A[1,0]=7)or(A[3,0]=7)Then Begin
Update;
GotoXY(1,19);
Write('F‚licitations - Il t''a fallu ',N-1,' coups');
Exit;
End;
Until False;
End;
BEGIN
Main;
END.