Bonjour tout le monde,

Dans un objectif de toucher un peu à l'IA et surtout débutant dans ce genre d'algo, je me suis dit "tiens puisque j'ai un petit morpion là ce serait plus sympas si je pouvais jouer conte l'ordi" ... Je me suis alors lancé dans la conception de cet IA selon l'algo MINMAX.

Seulement voila, au final le PC il est pas très fut fut ... enfin si mais il est pas logique !!! Si je déclare le niveau de profondeur de mon algo entre 1,2,5,6 il est relativement malin pour à chaque situation où j'ai deux croix alignés pour qu'il m'empêche de gagner.
Par contre si le niveau de profondeur est 4 ou 5 => et bien il y a une facon de jouer qui fait que il me laisse gagner après avoir MOI posé seulement deux croix (soit dit en passant : c'est la sele case qu'il devrait jouer sinon je gagne à coup sûr mais il ne le fait pas ...)... Bon OK comme ça c'est un peu difficile à cerner ce que je dis ... Bref voila un bout de code représentant l'IA, si quelqu'un à le courage de m'aider je lui en serais extrêmement reconnaissant ...

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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.
Par avance Merci