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
|
unit Moteur;
{
Moteur pour un jeu d'échecs.
Adaptation d'un programme de Jürgen Schlottke.
https://sites.google.com/site/maciejszmit/my-projects/computer-chess
http://maciej.szmit.info/resources/JSSchach.zip
}
{$IFDEF VPASCAL}{&USE32+}{$ELSE}{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}{$ENDIF}
interface
type
tDamier = array[-10..109] of integer; // 11 - 21 .. 88 + 21
tRoque = (e1g1, e1c1, e8g8, e8c8);
tCoup = record
a, b, v: integer; // départ, arrivée, valeur
end;
tListe = array[1..100] of tCoup;
tPosition = record
damier: tDamier;
couleur: integer; // couleur active
roque: array[tRoque] of boolean;
enPassant: array[-1..1] of integer; // possibilité de prise en passant (case d'arrivée ou 0) en fonction de la couleur du pion à prendre
coups: tListe;
nombre: integer; // nombre de coups
balance: integer;
end;
procedure Installe;
function ChainePositionCourante: string;
function CoupUtilisateur(s: string): boolean; // vérifie la légalité du coup et le joue
procedure ChangeProfondeur(const aProfondeur, aProfondeurMaximale: integer);
function CoupMoteur(var drapeau: integer): string; // renvoie le coup et le joue
implementation
const
cNeant = 000;
cPion = 002;
cFou = 006;
cCavalier = 007;
cTour = 010;
cDame = 019;
cRoi = 126;
cDehors = 127;
mPion : array[1..4] of integer = (+00 +01, +00 +02, -10 +01, +10 +01);
mCavalier: array[1..8] of integer = (+10 +02, +20 +01, +20 -01, +10 -02,
-10 -02, -20 -01, -20 +01, -10 +02);
mFou : array[1..4] of integer = (+10 +01, -10 -01, +10 -01, -10 +01);
mTour : array[1..4] of integer = (+00 -01, +00 +01, -10 +00, +10 +00);
mRoi : array[1..8] of integer = (+00 -01, +00 +01, -10 +00, +10 +00,
+10 +01, -10 -01, +10 -01, -10 +01);
var
positionCourante, positionInitiale: tPosition;
demiCoups, profondeur, profondeurMaximale, seuil: integer;
meilleurs: tListe;
function EvaluationMaterielle(const aPosition: tPosition; const aCouleur: integer): integer;
begin
result := aPosition.balance * aCouleur;
end;
procedure Installe;
const
cDamier: tDamier = (
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 010, 002, 000, 000, 000, 000,-002,-010, 127,
127, 007, 002, 000, 000, 000, 000,-002,-007, 127,
127, 006, 002, 000, 000, 000, 000,-002,-006, 127,
127, 019, 002, 000, 000, 000, 000,-002,-019, 127,
127, 126, 002, 000, 000, 000, 000,-002,-126, 127,
127, 006, 002, 000, 000, 000, 000,-002,-006, 127,
127, 007, 002, 000, 000, 000, 000,-002,-007, 127,
127, 010, 002, 000, 000, 000, 000,-002,-010, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127
);
var
r: tRoque;
begin
with positionInitiale do
begin
damier := cDamier;
couleur := +1;
for r := e1g1 to e8c8 do
roque[r] := true;
enPassant[-1] := 0;
enPassant[+1] := 0;
balance := 0;
end;
positionCourante := positionInitiale;
demiCoups := 0;
profondeur := 3;
profondeurMaximale := 5;
end;
function ChainePositionCourante: string;
var
x, y: integer;
c: char;
begin
result :=
' 1 2 3 4 5 6 7 8'#13#10 +
'8 '#13#10 +
'7 '#13#10 +
'6 '#13#10 +
'5 '#13#10 +
'4 '#13#10 +
'3 '#13#10 +
'2 '#13#10 +
'1 '#13#10 +
' 1 2 3 4 5 6 7 8';
for x := 1 to 8 do
for y := 1 to 8 do
begin
case Abs(positionCourante.damier[10 * x + y]) of
cNeant : c := '.';
cPion : c := 'P';
cCavalier: c := 'C';
cFou : c := 'F';
cTour : c := 'T';
cRoi : c := 'R';
cDame : c := 'D';
end;
if positionCourante.damier[10 * x + y] < cNeant then
Inc(c, Ord('a') - Ord('A'));
result[2 * x + 19 * (9 - y) + 1] := c;
end;
end;
procedure Deplace(var aPosition: tPosition; aDepart, aArrivee: integer; const aPromotion: integer);
begin
with aPosition do
begin
if damier[aDepart] * couleur = cRoi then
case aDepart of
51: begin
roque[e1g1] := false;
roque[e1c1] := false;
end;
58: begin
roque[e8g8] := false;
roque[e8c8] := false;
end;
end;
if damier[aDepart] * couleur = cTour then
case aDepart of
81: roque[e1g1] := false;
11: roque[e1c1] := false;
88: roque[e8g8] := false;
18: roque[e8c8] := false;
end;
if ((aArrivee - aDepart) * (aArrivee - aDepart) = 4) and (damier[aDepart] * couleur = cPion) then
enPassant[couleur] := aDepart + mPion[1] * couleur
else
enPassant[couleur] := 0;
if (aArrivee = enPassant[-1 * couleur]) and (damier[aDepart] * couleur = cPion) then
begin
enPassant[couleur] := 0;
Deplace(aPosition, aDepart, (aArrivee div 10) * 10 + aDepart mod 10, cDame);
couleur := -1 * couleur;
aDepart := (aArrivee div 10) * 10 + aDepart mod 10;
end;
if (aDepart in [51, 58]) and (aArrivee in [71, 31, 78, 38]) and (damier[aDepart] * couleur = cRoi) then
begin
damier[aArrivee] := damier[aDepart];
damier[aDepart] := cNeant;
if aArrivee div 10 = 7 then
begin
aDepart := (aDepart mod 10) + 80;
aArrivee := aDepart - 20;
end else
begin
aDepart := (aDepart mod 10) + 10;
aArrivee := aDepart + 30;
end;
damier[aArrivee] := damier[aDepart];
damier[aDepart] := cNeant;
end else
begin
balance := balance - damier[aArrivee];
damier[aArrivee] := damier[aDepart];
damier[aDepart] := cNeant;
if ((aArrivee mod 10 = 1) or (aArrivee mod 10 = 8)) and (damier[aArrivee] * couleur = cPion) then
begin
damier[aArrivee] := aPromotion * couleur;
balance := balance + (aPromotion - cPion) * couleur;
end;
end;
couleur := -1 * couleur;
end;
end;
procedure Ajouter(var aPosition: tPosition; const aDepart, aArrivee: integer);
begin
with aPosition do
begin
Inc(nombre);
coups[nombre].a := aDepart;
coups[nombre].b := aArrivee;
end;
end;
procedure GenereCoupsOrdinaires(var aPosition: tPosition);
var
depart, arrivee: integer;
x, y: integer;
i, j: integer;
begin
with aPosition do
begin
nombre := 0;
for x := 1 to 8 do
for y := 1 to 8 do
begin
depart := 10 * x + y;
if damier[depart] <> cNeant then
begin
case damier[depart] * couleur of
cPion:
begin
arrivee := depart + mPion[1] * couleur;
if damier[arrivee] = cNeant then
begin
Ajouter(aPosition, depart, arrivee);
if (couleur = +1) and (y = 2) or (couleur = -1) and (y = 7) then
begin
arrivee := depart + mPion[2] * couleur;
if (damier[arrivee] = cNeant) then
Ajouter(aPosition, depart, arrivee);
end;
end;
for i := 3 to 4 do
begin
arrivee := depart + mPion[i] * couleur;
if (-1 * damier[arrivee] * couleur in [cPion..cRoi]) or (arrivee = enPassant[-1 * couleur]) then
Ajouter(aPosition, depart, arrivee);
end;
end;
cCavalier:
begin
for i := 1 to 8 do
begin
arrivee := depart + mCavalier[i];
if -1 * damier[arrivee] * couleur in [cNeant..cRoi] then
Ajouter(aPosition, depart, arrivee);
end;
end;
cFou:
begin
for i := 1 to 4 do
begin
arrivee := depart;
repeat
Inc(arrivee, mFou[i]);
if -1 * damier[arrivee] * couleur in [cNeant..cRoi] then
Ajouter(aPosition, depart, arrivee);
until damier[arrivee] <> cNeant;
end;
end;
cTour:
begin
for i := 1 to 4 do
begin
arrivee := depart;
repeat
Inc(arrivee, mTour[i]);
if -1 * damier[arrivee] * couleur in [cNeant..cRoi] then
Ajouter(aPosition, depart, arrivee);
until damier[arrivee] <> 0;
end;
end;
cRoi:
begin
for i := 1 to 8 do
begin
arrivee := depart + mRoi[i];
if -1 * damier[arrivee] * couleur in [cNeant..cRoi] then
begin
j := 0;
repeat
Inc(j);
until (-1 * damier[arrivee + mRoi[j]] * couleur = cRoi) or (j = 8);
if j = 8 then
Ajouter(aPosition, depart, arrivee);
end;
end;
end;
cDame:
begin
for i := 1 to 8 do
begin
arrivee := depart;
repeat
Inc(arrivee, mRoi[i]);
if -1 * damier[arrivee] * couleur in [cNeant..cRoi] then
Ajouter(aPosition, depart, arrivee);
until damier[arrivee] <> cNeant;
end;
end;
end;
end;
end;
end;
end;
function Echec(var aPosition: tPosition): boolean;
var
i: integer;
begin
result := false;
with aPosition do
begin
couleur := -1 * couleur;
GenereCoupsOrdinaires(aPosition);
couleur := -1 * couleur;
result := false;
for i := 1 to nombre do
if damier[coups[i].b] * couleur = cRoi then
result := true;
end;
end;
function EchecRoque(var aPosition: tPosition; const departRoi, pasRoi: integer): boolean;
var
i: integer;
begin
result := false;
with aPosition do
begin
couleur := -1 * couleur;
GenereCoupsOrdinaires(aPosition);
couleur := -1 * couleur;
result := false;
for i := 1 to nombre do
if (coups[i].b mod 10 = departRoi mod 10) and ((coups[i].b - departRoi) div pasRoi >= 0) then
result := true;
end;
end;
procedure GenereRoque(var aPosition: tPosition; const departRoi, pasRoi, departTour: integer);
var
copiePosition: tPosition;
i: integer;
begin
with aPosition do
begin
if damier[departTour] * couleur <> cTour then
exit;
i := departRoi + pasRoi;
repeat
if damier[i] <> cNeant then
exit;
Inc(i, pasRoi);
until i = departTour;
copiePosition := aPosition;
if not EchecRoque(copiePosition, departRoi, pasRoi) then
Ajouter(aPosition, departRoi, departRoi + 2 * pasRoi);
end;
end;
procedure GenereCoups(var aPosition: tPosition);
begin
GenereCoupsOrdinaires(aPosition);
with aPosition do
begin
if couleur = +1 then
begin
if roque[e1g1] then
GenereRoque(aPosition, 51, +10, 81);
if roque[e1c1] then
GenereRoque(aPosition, 51, -10, 11);
end else
begin
if roque[e8g8] then
GenereRoque(aPosition, 58, +10, 88);
if roque[e8c8] then
GenereRoque(aPosition, 58, -10, 18);
end;
end;
end;
function EvaluationRecursive(var aPosition: tPosition; aCouleur, aProfondeur, alpha: integer): integer;
var
v, index: integer;
copiePosition: tPosition;
beta: integer;
arret: boolean;
begin
GenereCoups(aPosition);
index := 0;
beta := -32000 * aPosition.couleur * aCouleur;
arret := false;
while (index < aPosition.nombre) and not arret do
begin
Inc(index);
copiePosition := aPosition;
with aPosition do Deplace(copiePosition, coups[index].a, coups[index].b, cDame);
if ((aProfondeur >= profondeur) and (aPosition.damier[aPosition.coups[index].b] = 0)) or (aProfondeur = profondeurMaximale) then
v := EvaluationMaterielle(copiePosition, aCouleur)
else
v := EvaluationRecursive(copiePosition, aCouleur, aProfondeur + 1, beta);
if aPosition.couleur = aCouleur then
begin
if v > beta then
beta := v;
if beta > alpha then
arret := true;
end else
begin
if v < beta then
beta := v;
if beta < alpha then
arret := true;
end;
aPosition.coups[index].v := v;
end;
result := beta;
end;
function CoupUtilisateur(s: string): boolean;
var
i: integer;
copiePosition: tPosition;
coup, erreur: integer;
depart, arrivee: integer;
existe: boolean;
begin
result := true;
s[1] := Chr(Ord(s[1]) - 48);
s[3] := Chr(Ord(s[3]) - 48);
Val(s, coup, erreur);
if erreur = 0 then
begin
depart := coup div 100;
arrivee := coup mod 100;
GenereCoups(positionCourante);
existe := false;
with positionCourante do
for i := 1 to nombre do
if coup = 100 * coups[i].a + coups[i].b then
existe := true;
if existe then
begin
copiePosition := positionCourante;
Deplace(copiePosition, depart, arrivee, cDame);
copiePosition.couleur := -1 * copiePosition.couleur;
if Echec(copiePosition) then
result := false
else
begin
Deplace(positionCourante, depart, arrivee, cDame);
Inc(demiCoups);
end;
end
else
result := false;
end
else
result := false;
end;
function ChaineCoup(const aDepart, aArrivee: integer): string;
begin
Str(100 * aDepart + aArrivee, result);
result[1] := Chr(Ord(result[1]) + Ord('0'));
result[3] := Chr(Ord(result[3]) + ord('0'));
end;
function IdentiquePositionInitiale(const aPosition: tPosition; const i: integer): boolean;
begin
result := aPosition.damier[i] = positionInitiale.damier[i];
end;
function MeilleurCoup: integer;
var
copiePosition: tPosition;
i, j: integer;
maximum: integer;
n: integer; // nombre de "meilleurs", c'est-à-dire de coups admis dans la deuxième liste
begin
with positionCourante do
begin
n := 0;
for i := 1 to nombre do
begin
if coups[i].v = seuil then
begin
Inc(n);
meilleurs[n].a := coups[i].a;
meilleurs[n].b := coups[i].b;
meilleurs[n].v := 0;
end;
end;
end;
maximum := 0;
result := 1;
for i := 1 to n do
begin
copiePosition := positionCourante;
meilleurs[i].v := 0;
with copiePosition do
begin
if IdentiquePositionInitiale(copiePosition, meilleurs[i].a) then
begin
Inc(meilleurs[i].v, 5);
if damier[meilleurs[i].a] * couleur = cPion then
Inc(meilleurs[i].v, 2);
end;
if damier[meilleurs[i].a] * couleur = cRoi then
Dec(meilleurs[i].v, 10);
if (demiCoups < 32) and (damier[meilleurs[i].a] * couleur in [cPion, cFou, cCavalier]) then
Inc(meilleurs[i].v, 20);
if (meilleurs[i].a div 10 = 1)
or (meilleurs[i].a div 10 = 8)
or (meilleurs[i].a mod 10 = 1)
or (meilleurs[i].a mod 10 = 8) then
Inc(meilleurs[i].v, 2);
if (meilleurs[i].b div 10 = 1)
or (meilleurs[i].b div 10 = 8)
or (meilleurs[i].b mod 10 = 1)
or (meilleurs[i].b mod 10 = 8) then
Dec(meilleurs[i].v, 2);
end;
copiePosition := positionCourante;
Deplace(copiePosition, meilleurs[i].a, meilleurs[i].b, cDame);
if IdentiquePositionInitiale(copiePosition, meilleurs[i].b) then
Dec(meilleurs[i].v, 10);
copiePosition.couleur := -1 * copiePosition.couleur;
GenereCoupsOrdinaires(copiePosition);
with copiePosition do
begin
for j := 1 to nombre do
begin
Inc(meilleurs[i].v);
if damier[coups[j].b] <> cNeant then
Inc(meilleurs[i].v);
end;
end;
copiePosition.couleur := -1 * copiePosition.couleur;
GenereCoupsOrdinaires(copiePosition);
with copiePosition do
begin
for j := 1 to nombre do
begin
Dec(meilleurs[i].v);
if damier[coups[j].b] <> cNeant then
Dec(meilleurs[i].v);
end;
end;
if meilleurs[i].v >= maximum then
begin
maximum := meilleurs[i].v;
result := i;
end;
end;
end;
procedure ChangeProfondeur(const aProfondeur, aProfondeurMaximale: integer);
begin
profondeur := aProfondeur;
profondeurMaximale := aProfondeurMaximale;
end;
function CoupMoteur(var drapeau: integer): string;
var
i: integer;
echecAnterieur: boolean;
begin
seuil := EvaluationRecursive(positionCourante, positionCourante.couleur, 1, 32000);
i := MeilleurCoup;
Inc(demiCoups);
echecAnterieur := Echec(positionCourante);
Deplace(positionCourante, meilleurs[i].a, meilleurs[i].b, cDame);
result := ChaineCoup(meilleurs[i].a, meilleurs[i].b);
positionCourante.couleur := -1 * positionCourante.couleur;
if Echec(positionCourante) then
begin
if echecAnterieur then
drapeau := 3
else
drapeau := 2;
end else
begin
positionCourante.couleur := -1 * positionCourante.couleur;
if Echec(positionCourante) then
drapeau := 1
else
drapeau := 0;
end;
end;
end. |
Partager