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
|
{
========================================
* Programme : .............. ESCHECS *
* Compilation : ... Turbo Pascal 7.0 *
* ................Virtual Pascal 2.1 *
* Description : ....... Jeu d'checs *
* Auteur : ......... Roland Chastain *
* Adresse : .......... Bamako (MALI) *
* Version : .............. Brouillon *
* Dernire modification : 20/02/2012 *
========================================
}
PROGRAM ESCHECS;
USES Crt;
CONST NomDossier:String='c:\Atelier\PAS\TEMP\';
(* N.B. Le chemin ci-dessus est
remplacer. Il *)
(* doit conduire
un dossier dj
existant qui *)
(* contiendra les fichiers temporaires utiliss *)
(* par le programme. *)
TYPE
{::::::::::::::::::::::::::::::::::::::::::::::::::::}
TCoord12 = -1..10 ;
TCoord8 = 1.. 8 ;
TPiece = -6.. 7 ;
TCouleur = -1.. 1 ;
TNomCaze = String[2] ;
TCoup = String[4] ;
TTableau12x12 = Array [TCoord12,TCoord12] of TPiece ;
TTableau8x8 = Array [TCoord8,TCoord8] of TPiece ;
TTableau4B = Array [0..3] of Boolean ;
TCaracteres = Array [TPiece] of Char ;
TListe = File of TCoup ;
{::::::::::::::::::::::::::::::::::::::::::::::::::::}
VAR
{:::::::::::::::::::::::::::}
Occupant12 : TTableau12x12 ;
Occupant8 : TTableau8x8 ;
Trait : Boolean ;
Condition1 : TTableau4B ;
CazePassant : TNomCaze ;
Echec : Boolean ;
Condition2 : TTableau4B ;
nbDemiCoups : Integer ;
nbCoups : Integer ;
Ordre : String ;
{:::::::::::::::::::::::::::}
CONST
Initiale:TTableau12x12=(
( 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 ),
( 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 ),
( 7 , 7 , 4 , 1 , 0 , 0 , 0 , 0 ,-1 ,-4 , 7 , 7 ),
( 7 , 7 , 2 , 1 , 0 , 0 , 0 , 0 ,-1 ,-2 , 7 , 7 ),
( 7 , 7 , 3 , 1 , 0 , 0 , 0 , 0 ,-1 ,-3 , 7 , 7 ),
( 7 , 7 , 5 , 1 , 0 , 0 , 0 , 0 ,-1 ,-5 , 7 , 7 ),
( 7 , 7 , 6 , 1 , 0 , 0 , 0 , 0 ,-1 ,-6 , 7 , 7 ),
( 7 , 7 , 3 , 1 , 0 , 0 , 0 , 0 ,-1 ,-3 , 7 , 7 ),
( 7 , 7 , 2 , 1 , 0 , 0 , 0 , 0 ,-1 ,-2 , 7 , 7 ),
( 7 , 7 , 4 , 1 , 0 , 0 , 0 , 0 ,-1 ,-4 , 7 , 7 ),
( 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 ),
( 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 ));
Caracteres: TCaracteres=(
'k','q','r','b','n','p',chr(176),
'P','N','B','R','Q','K',chr(177));
CaracterFR: TCaracteres=(
'r','d','t','f','c','p',chr(176),
'P','C','F','T','D','R',chr(177));
FUNCTION NomCaze(x,y:TCoord8):TNomCaze;
begin NomCaze:=chr(x+96)+chr(y+48);
end;
FUNCTION PositionFEN(
oc : TTableau8x8 ;
tr : Boolean ;
a1 : TTableau4B ;
pa : TNomCaze ;
c1 : Integer ;
c2 : Integer )
: STRING;
begin
PositionFEN:='...';
end; { positionFEN }
PROCEDURE Initialise;
var x,y:TCoord12;
begin
for x:=-1 to 10 do begin
for y:=-1 to 10 do begin
Occupant12[x,y]:=Initiale[x,y];
end;
end;
for x:=1 to 8 do begin
for y:=1 to 8 do begin
Occupant8[x,y]:=Initiale[x,y];
end;
end;
Trait:=False; { w }
Condition1[0]:=True; { q } { e8c8 }
Condition1[1]:=True; { k } { e8g8 }
Condition1[2]:=True; { Q } { e1c1 }
Condition1[3]:=True; { K } { e1g1 }
CazePassant:='-';
nbDemiCoups:=0;
nbCoups:=1;
end; { Initialise }
FUNCTION SGN(n:TPiece):TCouleur;
begin
case n of
-6..-1 :SGN:=-1 ;
0 :SGN:= 0 ;
1.. 7 :SGN:= 1 ;
end;
end; { SGN }
PROCEDURE AfficheCoordonnees(c:Byte);
const ABC:String=chr(254)+' A B C D E F G H '+chr(254);
var x,y:Byte;
begin
TextColor(c);
GotoXY(2,2);
Write(ABC);
for y:=8 downto 1 do
begin
GotoXY( 2,2*(9-y)+2); Write(y);
GotoXY(29,2*(9-y)+2); Write(y);
end;
GotoXY(2,20); Write(ABC);
TextColor(Black);
end; { AfficheCoordonnees }
PROCEDURE AfficheTablier(occup:TTableau8x8;c1,c2,c3:Byte);
{
c1 couleur pices blanches
c2 couleur pices noires
c3 couleur carreaux
}
var x,y:TCoord8;
begin
for y:=8 downto 1 do
begin
for x:=1 to 8 do
begin
case SGN(occup[x,y]) of
-1:TextColor(c2);
0:TextColor(c3);
1:TextColor(c1);
end;
GotoXY(3*x+2,2*(9-y)+2);
Write(CaracterFR[occup[x,y]]);
end;
end;
end; { AfficheTablier }
PROCEDURE CalculeECHEC(
occ: TTableau8x8;
trt: Boolean;
var chc: Boolean;
var cnd: TTableau4B;
NomFichier: String);
var
x1,y1,x2,y2: TCoord8;
dxSGN,dySGN: ShortInt;
dxABS,dyABS: Byte;
colorP1,colorP2: TCouleur;
cp: TCoup;
bon: Boolean;
lst: TListe;
procedure mvtPION;
begin
if SGN(dySGN)=colorP1 then
begin
if (colorP2=-1*colorP1) and (dxABS=1) and (dyABS=1) then bon:=True;
if (colorP2=0) and (dxABS=0) then
begin
if dyABS=1 then bon:=True;
if dyABS=2 then
begin
if (colorP1=-1) and (y1=7) and (occ[x1,y1-1]=0) then bon:=True;
if (colorP1=01) and (y1=2) and (occ[x1,y1+1]=0) then bon:=True;
end;
end;
end;
end; { mvtPION }
procedure mvtCAVALIER;
begin
if (colorP2=-1*colorP1) or (colorP2=0) then
begin
if dxABS*dxABS+dyABS*dyABS=5 then bon:=True;
end;
end; { mvtCAVALIER }
procedure mvtFOU;
var
ax,ay:TCouleur;
nx,ny:ShortInt;
obstat:Boolean;
begin
if (colorP2=-1*colorP1) or (colorP2=0) then
begin
if (dxABS>0) and (dxABS=dyABS) then
begin
ax:=SGN(dxSGN);
ay:=SGN(dySGN);
nx:=0;
ny:=0;
obstat:=False;
repeat
nx:=nx+ax;
ny:=ny+ay;
if nx=dxSGN then bon:=True;
if Abs(occ[x1+nx,y1+ny])>0 then obstat:=True;
until (bon=True) or (obstat=True);
end;
end;
end; { mvtFOU }
procedure mvtTOUR;
var
ax,ay:TCouleur;
nx,ny:ShortInt;
obstat:Boolean;
begin
if (colorP2=-1*colorP1) or (colorP2=0) then
begin
if (dxABS*dyABS=0) and (dxABS+dyABS>0) then
begin
ax:=SGN(dxSGN);
ay:=SGN(dySGN);
nx:=0;
ny:=0;
obstat:=False;
repeat
nx:=nx+ax;
ny:=ny+ay;
if (nx=dxSGN) and (ny=dySGN) then bon:=True;
if Abs(occ[x1+nx,y1+ny])>0 then obstat:=True;
until (bon=True) or (obstat=True);
end;
end;
end; { mvtTOUR }
procedure mvtDAME;
var
ax,ay:TCouleur;
nx,ny:ShortInt;
obstat:Boolean;
begin
if (colorP2=-1*colorP1) or (colorP2=0) then
begin
if (dxABS>0) and (dxABS=dyABS) then
begin
ax:=SGN(dxSGN);
ay:=SGN(dySGN);
nx:=0;
ny:=0;
obstat:=False;
repeat
nx:=nx+ax;
ny:=ny+ay;
if nx=dxSGN then bon:=True;
if Abs(occ[x1+nx,y1+ny])>0 then obstat:=True;
until (bon=True) or (obstat=True);
end;
if (dxABS*dyABS=0) and (dxABS+dyABS>0) then
begin
ax:=SGN(dxSGN);
ay:=SGN(dySGN);
nx:=0;
ny:=0;
obstat:=False;
repeat
nx:=nx+ax;
ny:=ny+ay;
if (nx=dxSGN) and (ny=dySGN) then bon:=True;
if Abs(occ[x1+nx,y1+ny])>0 then obstat:=True;
until (bon=True) or (obstat=True);
end;
end;
end; { mvtDAME }
procedure mvtROI;
begin
if (colorP2=-1*colorP1) or (colorP2=0) then
begin
if (dxABS<2) and (dyABS<2) then bon:=True;
end;
end; { mvtROI }
begin
Assign(lst,NomDossier+NomFichier);
Rewrite(lst);
for x1:=1 to 8 do
begin
for y1:=8 downto 1 do
begin
for x2:=1 to 8 do
begin
for y2:=8 downto 1 do
begin
dxSGN:=x2-x1; dxABS:=Abs(dxSGN);
dySGN:=y2-y1; dyABS:=Abs(dySGN);
colorP1:=SGN(occ[x1,y1]);
colorP2:=SGN(occ[x2,y2]);
bon:=False;
case Abs(occ[x1,y1]) of
1:mvtPION;
2:mvtCAVALIER;
3:mvtFOU;
4:mvtTOUR;
5:mvtDAME;
6:mvtROI;
end;
if bon=True then
begin
cp:=NomCaze(x1,y1)+NomCaze(x2,y2);
Write(lst,cp);
end;
end;
end;
end;
end;
Close(lst);
chc:=False;
cnd[0]:=True;
cnd[1]:=True;
cnd[2]:=True;
cnd[3]:=True;
Assign(lst,NomDossier+NomFichier);
Reset(lst);
while not Eof(lst) do
begin
Read(lst,cp);
x1:=Ord(cp[1])-96;
y1:=Ord(cp[2])-48;
x2:=Ord(cp[3])-96;
y2:=Ord(cp[4])-48;
colorP1:=SGN(occ[x1,y1]);
if (trt=False) and (colorP1=-1) then
begin
if occ[x2,y2]=6 then chc:=True;
if (y2=1) and (x2<6) then cnd[2]:=False;
if (y2=1) and (x2>5) then cnd[3]:=False;
end;
if (trt=True) and (colorP1=1) then
begin
if occ[x2,y2]=-6 then chc:=True;
if (y2=8) and (x2<6) then cnd[0]:=False;
if (y2=8) and (x2>5) then cnd[1]:=False;
end;
end;
Close(lst);
end; { CalculeECHEC }
PROCEDURE CoupsSpeciaux(
occ: TTableau8x8;
trt: Boolean;
cnd1: TTableau4B;
cnd2: TTableau4B;
pass: TNomCaze;
NomFichier: String);
var
x1,y1,x2,y2: TCoord8;
dxSGN,dySGN: ShortInt;
dxABS,dyABS: Byte;
colorP1,colorP2: TCouleur;
cp: TCoup;
bon: Boolean;
lst: TListe;
procedure mvtPION2;
begin
if (dySGN=colorP1) and (dxABS=1) then
begin
if NomCaze(x2,y2)=pass then bon:=True;
end;
end;
procedure mvtROI2;
begin
if (dyABS=0) and (dxABS=2) then
begin
if not trt then begin
if (occ[2,1]=0) and (occ[3,1]=0) and (occ[4,1]=0) then begin
if (cnd1[2] and cnd2[2]) then bon:=True; { Q }
end;
if (occ[6,1]=0) and (occ[7,1]=0) then begin
if (cnd1[3] and cnd2[3]) then bon:=True; { K }
end;
end;
if trt then begin
if (occ[2,8]=0) and (occ[3,8]=0) and (occ[4,8]=0) then begin
if (cnd1[0] and cnd2[0]) then bon:=True; { q }
end;
if (occ[6,8]=0) and (occ[7,8]=0) then begin
if (cnd1[1] and cnd2[1]) then bon:=True; { k }
end;
end;
end
end;
begin
Assign(lst,NomDossier+NomFichier);
Rewrite(lst);
for x1:=1 to 8 do
begin
for y1:=8 downto 1 do
begin
for x2:=1 to 8 do
begin
for y2:=8 downto 1 do
begin
dxSGN:=x2-x1; dxABS:=Abs(dxSGN);
dySGN:=y2-y1; dyABS:=Abs(dySGN);
colorP1:=SGN(occ[x1,y1]);
colorP2:=SGN(occ[x2,y2]);
bon:=False;
case Abs(occ[x1,y1]) of
1:mvtPION2;
6:mvtROI2;
end;
if bon=True then
begin
cp:=NomCaze(x1,y1)+NomCaze(x2,y2);
Write(lst,cp);
end;
end;
end;
end;
end;
Close(lst);
end;
PROCEDURE CoupsPermis(occ:TTableau8x8;trt:Boolean;nf1:String;nf2:String;nf3:String);
var
EchecA : Boolean ;
Cond2A : TTableau4B ;
Fictif : TTableau8x8 ;
x1,y1,x2,y2 : TCoord8 ;
cp : TCoup ;
lst,lstb : TListe ;
BCouleur : Boolean ;
procedure Tri;
begin
x1:=Ord(cp[1])-96;
y1:=Ord(cp[2])-48;
x2:=Ord(cp[3])-96;
y2:=Ord(cp[4])-48;
EchecA:=False;
Fictif:=occ;
Fictif[x2,y2]:=Fictif[x1,y1];
Fictif[x1,y1]:=0;
CalculeEchec(Fictif,trt,EchecA,Cond2A,'L');
BCouleur:=False;
case trt of
False:if SGN(occ[x1,y1])= 1 then BCouleur:=True;
True:if SGN(occ[x1,y1])=-1 then BCouleur:=True;
end;
end; { Tri }
begin
Assign(lstb,NomDossier+nf3); Rewrite(lstb);
Assign(lst,NomDossier+nf1); Reset(lst);
while not Eof(lst) do
begin
Read(lst,cp);
Tri;
if (not EchecA) and BCouleur then write(lstb,cp);
end;
Close(lst);
Assign(lst,NomDossier+nf2); Reset(lst);
while not Eof(lst) do
begin
Read(lst,cp);
Tri;
if (not EchecA) and BCouleur then write(lstb,cp);
end;
Close(lst);
Close(lstb);
end; { Coups permis }
PROCEDURE Informations;
var lst:TListe; AucunCoupPossible:Boolean; cp:TCoup;
begin
AucunCoupPossible:=False; cp:='';
Assign(lst,NomDossier+'LISTE3');
Reset(lst);
while not Eof(lst) do
begin
Read(lst,cp);
end;
Close(lst);
if cp='' then AucunCoupPossible:=True;
GotoXY(60,2); Write('Eschec au roi=',Echec); ClrEol;
GotoXY(60,4); Write('Eschec et mat=',Echec and AucunCoupPossible); ClrEol;
GotoXY(60,6); Write('Partie remise=',(not Echec) and AucunCoupPossible); ClrEol;
end;
PROCEDURE Commande(trt:Boolean;var cmnd:String);
const nomProg:String='ESCHECS';
var couleur:String;OK:Boolean;lst:TListe;cp:TCoup;valab:Boolean;
begin
OK:=False;
while OK=False do
begin
GotoXY(2,23);
Write(nomProg,'>');
if trt=False then couleur:='BLANC' else couleur:='NOIR';
Write(couleur,'>'); ClrEol; ReadLn(cmnd);
Assign(lst,NomDossier+'LISTE3'); Reset(lst); valab:=False;
while not Eof(lst) do
begin
Read(lst,cp);
if cmnd=cp then valab:=True;
end;
OK:=valab;
if cmnd='exit'then OK:=True;
end;
end;
PROCEDURE FichierTXT;
var
F:Text;
i:Byte;
lst:TListe;
cp:TCoup;
begin
Assign(F,NomDossier+'RESULTAT.txt');
Rewrite(F);
WriteLn(F,'FEN=',PositionFEN(Occupant8,Trait,Condition1,CazePassant,nbDemiCoups,nbCoups));
WriteLn(F,'Echec=',Echec);
for i:=0 to 3 do begin WriteLn(F,'C2[',i,']=',Condition2[i]); end;
WriteLn(F,'Possibles=');
Assign(lst,NomDossier+'LISTE3');
Reset(lst);
while not Eof(lst) do
begin
Read(lst,cp);
WriteLn(F,cp);
end;
Close(lst);
Close(F);
end; { FichierTXT }
PROCEDURE AffichePalette;
var i:Byte;
begin
for i:=15 downto 0 do
begin
GotoXY(3*i+2,25); TextColor(i); Write(chr(254));
end;
end;
PROCEDURE Mouvement(cp:TCoup);
var x1,y1,x2,y2,x,y:TCoord8;
piece:TPiece;
prise:TPiece;
dx,dy:ShortInt;
begin
x1:=Ord(cp[1])-96;
y1:=Ord(cp[2])-48;
x2:=Ord(cp[3])-96;
y2:=Ord(cp[4])-48;
piece:=Occupant8[x1,y1];
prise:=Occupant8[x2,y2];
dx:=Abs(x1-x2);
dy:=Abs(y1-y2);
if (Abs(piece)=1) and ( (y2=8) or (y2=1) ) then Occupant8[x1,y1]:=5*Occupant8[x1,y1] ;
{ promotion automatique du pion en dame }
Occupant8[x2,y2]:=Occupant8[x1,y1]; Occupant8[x1,y1]:=0;
if (Abs(piece)=1) and ( (dx=1) and (prise=0) ) then Occupant8[x2,y1]:=0; { prise du pion passant }
if (Abs(piece)=6) and (dx=2) then
begin
if NomCaze(x2,y2)='c8' then begin
Occupant8[1,8]:= 0;
Occupant8[4,8]:=-4;
end;
if NomCaze(x2,y2)='g8' then begin
Occupant8[8,8]:= 0;
Occupant8[6,8]:=-4;
end;
if NomCaze(x2,y2)='c1' then begin
Occupant8[1,1]:= 0;
Occupant8[4,1]:=+4;
end;
if NomCaze(x2,y2)='g1' then begin
Occupant8[8,1]:= 0;
Occupant8[6,1]:=+4;
end;
end;
for x:=1 to 8 do
begin
for y:=1 to 8 do
begin
Occupant12[x,y]:=Occupant8[x,y];
end;
end;
Trait:=not Trait;
if piece= -6 then
begin
Condition1[0]:=false;
Condition1[1]:=false;
end;
if piece= +6 then
begin
Condition1[2]:=false;
Condition1[3]:=false;
end;
if NomCaze(x1,y1)='a8' then Condition1[0]:=false;
if NomCaze(x1,y1)='h8' then Condition1[1]:=false;
if NomCaze(x1,y1)='a1' then Condition1[2]:=false;
if NomCaze(x1,y1)='h1' then Condition1[3]:=false;
if (Abs(piece)=1) and (dy=2) then CazePassant:=NomCaze(x2,y2-SGN(piece)) else CazePassant:='-';
if (Abs(piece)=1) or (prise<>0) then nbDemiCoups:=0 else Inc(nbDemiCoups);
if not Trait then Inc(nbCoups);
end; { Mouvement }
BEGIN
TextBackground(LightGray);ClrScr;
AfficheCoordonnees(4);
(*AffichePalette;*)
Initialise;
repeat
AfficheTablier(Occupant8,15,0,8);
TextColor(LightGray);
CalculeECHEC(Occupant8,Trait,Echec,Condition2,'LISTE1');
CoupsSpeciaux(Occupant8,Trait,Condition1,Condition2,CazePassant,'LISTE2');
CoupsPermis(Occupant8,Trait,'LISTE1','LISTE2','LISTE3');
TextColor(Black);
Informations;
FichierTXT;
Commande(Trait,Ordre);
if not (Ordre='exit') then Mouvement(Ordre);
until Ordre='exit';
END. |
Partager