-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSODOKU.PAS
173 lines (162 loc) · 3.69 KB
/
SSODOKU.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
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
{ @author: Sylvain Maltais (support@gladir.com)
@created: 2024
@website(https://www.gladir.com/sodoku-0)
@abstract(Target: Turbo Pascal 7, Free Pascal 3.2)
}
Program SSODOKU;
Var
SodokuFile:Text;
ShowTry:Boolean;
LoadedGrid,SolutionGrid:Array[1..9,1..9] of Integer;
FileName,CurrLine:String;
Function StrToUpper(S:String):String;
Var
I:Byte;
Begin
For I:=1 to Length(S)do Begin
If S[I] in['a'..'z']Then S[I]:=Chr(Ord(S[I])-32);
End;
StrToUpper:=S;
End;
Function Left(Const Str:String;Num:Byte):String;Begin
Left:=Copy(Str,1,Num);
End;
Procedure DelChrAt(Var S:String;P:Byte);Begin
If P=1Then S:=Copy(S,2,255)
Else S:=Left(S,P-1)+Copy(S,P+1,255)
End;
Function VerticalFound(X,YMax,Number:Integer):Boolean;
Var
J:Integer;
Begin
VerticalFound:=False;
For J:=1 to YMax do Begin
If SolutionGrid[J,X]<>0 Then Begin
If(SolutionGrid[J,X]=Number)Then Begin
VerticalFound:=True;
Exit;
End;
End;
End;
End;
Function FindZero:Boolean;
Var
I,J:Integer;
Begin
FindZero:=False;
For J:=1 to 9 do For I:=1 to 9 do Begin
If SolutionGrid[J,I]=0 Then Begin
FindZero:=True;
End;
End;
End;
Procedure FindSolution;
Var
Base:String;
K:LongInt;
I,J,L:Integer;
R,P:Integer;
Skip:Boolean;
Begin
Randomize;
For K:=1 to 2000000 do Begin
FillChar(SolutionGrid,SizeOf(SolutionGrid),0);
For J:=1 to 9 do Begin
For I:=1 to 9 do SolutionGrid[J,I]:=LoadedGrid[J,I];
End;
For J:=1 to 9 do Begin
Base[0]:=#0;
For I:=1 to 9 do Begin
Skip:=False;
For L:=1 to 9 do If LoadedGrid[J,L]=I Then Begin
Skip:=True;
Break;
End;
If Not(Skip)Then Base:=Base+Chr(I);
End;
For I:=1 to 9 do Begin
If SolutionGrid[J,I]=0 Then Begin
R:=Random(Length(Base))+1;
If VerticalFound(I,9,Byte(Base[R]))Then Begin
For L:=1 to Length(Base)do Begin
If Not VerticalFound(I,9,Byte(Base[L]))Then Begin
SolutionGrid[J,I]:=Byte(Base[L]);
DelChrAt(Base,L);
Break;
End;
End;
End
Else
Begin
SolutionGrid[J,I]:=Byte(Base[R]);
DelChrAt(Base,R);
End;
End;
End;
End;
If Not FindZero Then Begin
If(ShowTry)Then Begin
WriteLn('Nombre d''essaie : ',K);
WriteLn;
End;
Break;
End;
End;
If(FindZero)Then Begin
If(ShowTry)Then Begin
WriteLn('Nombre d''essaie : ',K);
WriteLn;
End;
End;
End;
Var
I,J:Byte;
BEGIN
If(ParamStr(1)='/?')or(ParamStr(1)='--help')or(ParamStr(1)='-h')or
(ParamStr(1)='/h')or(ParamStr(1)='/H')Then Begin
WriteLn('SSODOKU : Cette commande permet de lancer le Solveur Sodoku.');
WriteLn;
WriteLn('Syntaxe : SSODOKU fichier.SDK [/SHOWTRY]');
WriteLn;
WriteLn(' fichier.SDK Ce paramŠtre permet d''indiquer le fichier … r‚soudre');
WriteLn(' /SHOWTRY Ce paramŠtre permet d''afficher le nombre d''essaie.');
End
Else
Begin
ShowTry:=False;
FileName:='';
For I:=1 to ParamCount do Begin
If StrToUpper(ParamStr(I))='/SHOWTRY'Then ShowTry:=True
Else FileName:=ParamStr(I);
End;
If FileName=''Then Begin
WriteLn('Fichier requis !');
Halt(1);
End;
{$I-}Assign(SodokuFile,FileName);
Reset(SodokuFile);{$I+}
If IOResult<>0 Then Begin
WriteLn('Erreur de lecture du fichier ',FileName,'!');
Halt(2);
End;
FillChar(LoadedGrid,SizeOf(LoadedGrid),0);
J:=1;
While Not EOF(SodokuFile)do Begin
ReadLn(SodokuFile,CurrLine);
For I:=1 to 9 do Begin
If CurrLine[I]in['1'..'9']Then LoadedGrid[J,I]:=Byte(CurrLine[I])-Ord('0');
End;
Inc(J);
End;
Close(SodokuFile);
FindSolution;
If(FindZero)Then Begin
WriteLn('Nombre d''essaie insuffisant !');
Halt(3);
End;
For J:=1 to 9 do Begin
For I:=1 to 9 do Write(SolutionGrid[J,I],' ');
WriteLn;
End;
End;
END.