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
| type
tcoup = record
ligne:byte;
colonne:byte;
end;
tcoups = record
coups :array [0..64] of tcoup;
nb_coups:integer;
end;
const
couleur_cpu=1;
couleur_humain=2;
uses math;
function DecisionAlphaBeta (tableau:ttab)
// Décide du meilleur coup à jouer par le joueur J dans la situation e
var val:real;
cp:tcoups;
x:integer;
begin
cp:=coupsPossibles(tableau,couleur_cpu);
for x := 0 to cp.nb_coups-1 do
val = ValeurAlphaBeta(jouer(tableau, couleur_cpu, a_joue,cp.coups[x]),couleur_cpu,-infinity,+infinity,false,4);//pmax=4
if val>alpha then
begin
result= coup;
alpha = val;
end;
end;
end;
function ValeurAlphaBeta (tableau:ttab;joueur:integer;alpha:real;beta:real;EstUnEtatMax:boolean;pmax:integer):real ;
{ Calcule la valeur de e pour le joueur J selon que e EstUnEtatMax ou pas
et pmax la profondeur maximale }
var
x:integer;
cp:tcoups;
begin
if gagne(tableau) =joueur then result:=+infinity
else if gagne(tableau)=3-joueur then result:=-infinity
else if PartieNulle(tableau) then result:=0
else
if pmax=0 then result:=evalue(tableau,joueur)
else
if EstUnEtatMax then // noeud MAX
begin
cp:=coupsPossibles(tableau,joueur);
for x := 0 to cp.nb_coups-1 do
begin
alpha := max(alpha,ValeurAlphaBeta(jouer(tableau, couleur_cpu, a_joue,cp.coups[x]),couleur_cpu,alpha,beta,not(EstUnEtatMax),pmax-1));
if alpha >= beta then
begin
result:=alpha; // coupe beta
exit;
end;
end;
result:=alpha;
end
else
begin //noeud MIN
cp:=coupsPossibles(tableau,joueur);
for x := 0 to cp.nb_coups-1 do
begin
beta := min(beta,ValeurAlphaBeta(jouer(tableau, couleur_humain, a_joue,cp.coups[x]),couleur_humain,alpha,beta,not(EstUnEtatMax),pmax-1));
if beta <= alpha then
begin
result:=beta; // coupe alpha
exit;
end;
end;
result:=beta;
end;
end; |
Partager