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
|
//La fonction qui doit jouer le coup à faire
public void jeu_echec_IA(MaFenetre fenetre,int profondeur)
{
ArrayList<Coup> listeCoups = new ArrayList();
Coup coup=new Coup(0, 0);
Coup meilleurcoup=new Coup(0, 0);
for(int i=0 ; i<8;i++)
{
for(int j=0 ; j<8 ;j++)
{
if(champs[getChamps(i,j)] != CV)
{
listeCoups=coupPossiblesNoir(i, j);
for(int k=0 ; k<listeCoups.size();k++)
{
fenetre.faireCoup((Coup)listeCoups.get(k));
coup=minmax((Coup)listeCoups.get(k),profondeur,fenetre);
fenetre.annulerCoup();
if ((coup.getValeur() + listeCoups.get(k).getValeur()) >= meilleurcoup.getValeur()){
meilleurcoup=(Coup)listeCoups.get(k);
}
}
}
}
}
fenetre.faireCoup(meilleurcoup);
}
// les fonctions qui font le traitement de MinMax
public Coup minmax(Coup coup,int profondeur,MaFenetre fenetre)
{
return MaxMove(coup,profondeur,fenetre);
}
public Coup MaxMove(Coup coup,int profondeur,MaFenetre fenetre)
{
if(profondeur==0)
{
return evaluerPiece(coup);
}
else
{
ArrayList<Coup> listeCoups = new ArrayList();
Coup meilleurcoup= new Coup(0, 0);
Coup moncoup= new Coup(0, 0);
meilleurcoup.setValeur(0);
for(int i=0 ; i<8;i++)
{
for(int j=0 ; j<8 ;j++)
{
if(champs[getChamps(i,j)] != CV)
{
listeCoups=fenetre.getEchiquierCourant().coupPossiblesBlanc(i,j);
for(int k=0 ; k<listeCoups.size();k++)
{
fenetre.faireCoup((Coup)(Coup)listeCoups.get(k));
moncoup = MinMove((Coup)listeCoups.get(k),profondeur-1,fenetre);
fenetre.annulerCoup();
if(moncoup.getValeur() > meilleurcoup.getValeur())
{
meilleurcoup=moncoup;
}
}
}
}
}
return meilleurcoup;
}
}
public Coup MinMove(Coup coup , int profondeur,MaFenetre fenetre)
{
if(profondeur==0)
{
return evaluerPiece(coup);
}
else
{
ArrayList<Coup> listeCoups = new ArrayList();
Coup meilleurcoup= new Coup(0, 0);
Coup moncoup= new Coup(0, 0);
meilleurcoup.setValeur(0);
for(int i=0 ; i<8;i++)
{
for(int j=0 ; j<8 ;j++)
{
if(champs[getChamps(i,j)] != CV)
{
listeCoups=fenetre.getEchiquierCourant().coupPossiblesNoir(coup.getSourceLigne(), coup.getSourceColonne());
for(int k=0 ; k<listeCoups.size();k++)
{
fenetre.faireCoup((Coup)(Coup)listeCoups.get(k));
moncoup = minmax((Coup)listeCoups.get(k),profondeur - 1,fenetre);
fenetre.annulerCoup();
if(moncoup.getValeur() < meilleurcoup.getValeur())
{
meilleurcoup=moncoup;
}
}
}
}
}
return meilleurcoup;
}
} |
Partager