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
| unit UAI;
interface
uses UJeu;
const MAX_VAL : integer = 100;
NUL_VAL : integer = 0;
PROF_MAX = 3;
function DecisionMinMax(P : TPlateau; Profondeur : integer):TCoo;
implementation
Function JouerUneCase(p:TPlateau;Joueur : Char;ACol, ARow : integer):TPlateau;
begin
P[ACol,ARow]:=Joueur;
Result:=P;
end;
Function CaseVide(P : TPlateau;ACol,ARow : integer):Boolean;
Begin
Result:=P[ACol,ARow]=VIDE;
end;
Function CasesPleines(P : TPlateau):Boolean;
var
ACol: Integer;
ARow: Integer;
begin
Result:=True;
for ACol := 1 to MAX_COL do
for ARow := 1 to MAX_ROW do
Result:=Result AND (P[ACol,ARow]<>VIDE);
end;
Function Victoire(P:TPlateau; J : Char):Boolean;
//***********************************************************************//
//************SOUS FONCTION DE TEST**************************************//
//***********************************************************************//
function TestAlignement(dx,dy : integer):boolean;
var nb,X,Y,X1,Y1 : integer;
begin
X:=1;
Result:=False;
if dy=-1 then Y:=MAX_COL else Y:=1;
While ((X<=MAX_COL) AND (Y<=MAX_ROW) AND Not(Result)) do
begin
X1:=X;
Y1:=Y;
nb:=0;
While ((X1<=MAX_COL) AND (Y1<=MAX_ROW)) do
begin
if P[X1,Y1]=J then Inc(nb) else nb:=0;
X1:=X1+dx;
Y1:=Y1+dy;
end;
Result:=nb=3;
if dy <> -1 then
Begin
X:=X+dy;
Y:=Y+dx;
end
else exit;
end;
end;
//************************************************************************//
//************************************************************************//
begin
Result:=TestAlignement(1,0) or // horizontale -
TestAlignement(0,1) or // verticale |
TestAlignement(1,1) or // diagonale \
TestAlignement(1,-1); // diagonale /
end;
Function ValeurMinMax(P : TPlateau;Profondeur : Integer;EtatMax : boolean):Integer;
var
ACol,
ARow,
Valeur : Integer;
begin
if Victoire(P,RONDS) then
Result:=MAX_VAL
else if Victoire(P,CROIX) then
Result:=-MAX_VAL
else if ((Profondeur=0) OR CasesPleines(P)) then
Result:=NUL_VAL
else begin
if EtatMax then
Begin
Result:=-MAX_VAL;
for ACol := 1 to MAX_COL do
for ARow := 1 to MAX_ROW do
if (CaseVide(P,ACol,ARow)) then
begin
Valeur:=ValeurMinMax(JouerUneCase(P,RONDS,ACol,ARow),Profondeur-1,False);
if (Valeur>Result) then
Result:=Valeur;
end;
end
else begin
Result:=MAX_VAL;
for ACol := 1 to MAX_COL do
for ARow := 1 to MAX_ROW do
if (CaseVide(P,ACol,ARow)) then
begin
Valeur:=ValeurMinMax(JouerUneCase(P,CROIX,ACol,ARow),Profondeur-1,True);
if (Valeur<Result) then
Result:=Valeur;
end;
end;
end;
end;
Function DecisionMinMax(P:TPlateau; Profondeur : Integer):TCoo;
var ValeurMax,
Valeur : integer;
ACol: Integer;
ARow: Integer;
Begin
Result[1]:=-1;
Result[2]:=-1;
ValeurMax:=-MAX_VAL;
for ACol := 1 to MAX_COL do
for ARow := 1 to MAX_ROW do
if (CaseVide(P,ACol,ARow)) then
begin
Valeur:=ValeurMinMax(JouerUnecase(P,RONDS,ACol,ARow),Profondeur-1,False);
if (Valeur>ValeurMax) then
begin
ValeurMax:=Valeur;
Result[1]:=ACol;
Result[2]:=ARow;
end;
end;
if Result[1]=-1 then
Begin
result[1]:=Random(MAX_COL)+1;
result[2]:=Random(MAX_ROW)+1;
while Not(CaseVide(P,Result[1],Result[2])) do
begin
result[1]:=Random(MAX_COL)+1;
result[2]:=Random(MAX_ROW)+1;
end;
end;
end;
initialization
Randomize;
end. |
Partager