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
| <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
<title>Morbac</title>
<style type="text/css">
#morbac {
table-layout: fixed;
border-spacing: 1px;
empty-cells: show;
border: 3px outset #000;
font-weight: bold;
}
#morbac td {
width: 30px;
height: 30px;
border: 2px inset #000;
text-align: center;
}
</style>
<script type="text/javascript">
function melange () {
this.sort(function () { return Math.random()*3-1 });
}
Array.prototype.shuffle = melange;
function mor_joue (place) {
if (this.lock) {
alert("Impossible de jouer\n plus aucune partie n'est en cours");
return false;
}
if (place < 0 || place > this.cases.length || (this.grille[0] | this.grille[1]) & (1 << place)) {
alert("Cette case est déjà prise");
return false;
}
this.grille[this.qui] |= 1 << place;
this.cases[place].data = (this.qui == 1) ? 'X' : 'O';
if (this.testFin()) {
this.newPartie();
return false;
}
if (this.qui == 1 && this.onplay1) {
if (typeof this.onplay1 == 'function') this.onplay1(place);
else if (typeof this.onplay1[0][this.onplay1[1]] == 'function') this.onplay1[0][this.onplay1[1]](place);
} else if (this.qui == 0 && this.onplay2) {
if (typeof this.onplay2 == 'function') this.onplay2(place);
else if (typeof this.onplay2[0][this.onplay2[1]] == 'function') this.onplay2[0][this.onplay2[1]](place);
}
this.qui = 1 - this.qui; //comme ça on alterme entre 0 et 1
if (this.qui == 1 && this.joueur1) {
if (typeof this.joueur1 == 'function') this.joueur1();
else if (typeof this.joueur1[0][this.joueur1[1]] == 'function') this.joueur1[0][this.joueur1[1]]();
} else if (this.qui == 0 && this.joueur2) {
if (typeof this.joueur2 == 'function') this.joueur2();
else if (typeof this.joueur2[0][this.joueur2[1]] == 'function') this.joueur2[0][this.joueur2[1]]();
}
return true;
}
function mor_testGagne () {
//motifs d'alignement horizontaux et verticaux
var hzalign = (1 << this.taille) - 1; // 111b
var vzalign = 0;
for (var i=0; i<this.taille; i++) vzalign |= (1 << i*this.taille); // 001'001'001b
//parcours du tableau horizontalement et verticalement
for (var i=0; i<this.taille; i++) {
var horiz = hzalign << (i*this.taille);
var vertz = vzalign << i;
if (((this.grille[0] & horiz) == horiz) || ((this.grille[0] & vertz) == vertz)) {
return -1;
} else if (((this.grille[1] & horiz) == horiz) || ((this.grille[1] & vertz) == vertz)) {
return 1;
}
}
var diag1 = 0, diag2 = 0;
//en diagonal
for (var i=0; i<this.taille; i++) {
diag1 |= 1 << (i*this.taille + i);
diag2 |= 1 << ((i+1)*this.taille - (i+1));
}
if (((this.grille[0] & diag1) == diag1) || ((this.grille[0] & diag2) == diag2)) {
return -1;
} else if (((this.grille[1] & diag1) == diag1) || ((this.grille[1] & diag2) == diag2)) {
return 1;
}
return 0;
}
function mor_testFin() {
var win = this.testGagne();
if (win) {
this.total.data++;
this.lock = true;
}
if (win == 1) {
this.gagne1.data++;
alert("Le joueur 1 à gagné");
} else if (win == -1) {
this.gagne2.data++;
alert("Le joueur 2 a gagné");
} else {
//si personne n'a gagné c'est un match null ou un match pas terminé
//si il reste une case vide c'est pas fini
if ((this.grille[0] | this.grille[1]) < 511) return false;
//si la boucle se fini il n'y a plus de cases libres
this.nuls.data++;
this.total.data++;
this.lock = true;
alert("Match nul");
}
return true;
}
function mor_newPartie () {
this.grille = new Array(0, 0); //on ne fait que des grilles carrés
for (var i=0; i<this.cases.length; i++) {
this.cases[i].data = ' ';
}
this.qui = (~this.total.data) & 1;
this.lock = false;
if (this.onnew) {
if (typeof this.onnew == 'function') this.onnew();
else if (typeof this.onnew[0][this.onnew[1]] == 'function') this.onnew[0][this.onnew[1]]();
}
if (this.qui == 1 && this.joueur1) {
if (typeof this.joueur1 == 'function') this.joueur1();
else if (typeof this.joueur1[0][this.joueur1[1]] == 'function') this.joueur1[0][this.joueur1[1]]();
} else if (this.qui == 0 && this.joueur2) {
if (typeof this.joueur2 == 'function') this.joueur2();
else if (typeof this.joueur2[0][this.joueur2[1]] == 'function') this.joueur2[0][this.joueur2[1]]();
}
}
function mor_reset() {
this.total.data = 0;
this.gagne1.data = 0;
this.gagne2.data = 0;
this.nuls.data = 0;
this.grille = new Array(0, 0);
this.cases = new Array();
for (var i=0; i<this.cases.length; i++) {
this.cases[i].data = ' ';
}
this.lock = false;
}
function morpion_grille(taille, tablo, total, gagne1, gagne2, nuls) {
this.lock = false;
this.taille = taille;
this.total = total.firstChild;
this.gagne1 = gagne1.firstChild;
this.gagne2 = gagne2.firstChild;
this.nuls = nuls.firstChild;
this.cases = new Array();
var elts = tablo.getElementsByTagName('td');
for (var i=0; i<elts.length; i++) {
this.cases[i] = elts[i].firstChild;
}
this.joue = mor_joue;
this.testGagne = mor_testGagne;
this.testFin = mor_testFin;
this.newPartie = mor_newPartie;
this.reset = mor_reset;
}
/***************/
/*Début de l'AI*/
/***************/
function AI_played(place) {
this.grille[0] |= 1 << place;
}
function AI_joue() {
var scoreCases = new Array();
for (var i=0; i<9; i++) {
if ((this.grille[0] | this.grille[1]) & (1 << i)) continue;
//elle est bien cette case ?
scoreCases.push([i, this.bienLaCase(i, 1)]);
}
scoreCases.sort(function (a, b) {return b[1]-a[1];});
while (scoreCases.length > 1) {
if (scoreCases[0][1] > scoreCases[scoreCases.length-1][1]) scoreCases.pop();
else break;
}
scoreCases.shuffle();
var prochain = scoreCases[0][0];
this.grille[1] |= 1 << prochain;
if (typeof this.doPlay == 'function') this.doPlay(prochain);
else if (typeof this.doPlay[0][this.doPlay[1]] == 'function') this.doPlay[0][this.doPlay[1]](prochain);
return prochain;
}
function AI_bien (place, qui) {
//juste un accélérateur, l'algo donnerait le même résultat
//toutes les cases donne un score de 0 si on joue le premier
if ((this.grille[0] | this.grille[1]) == 0) return 0;
this.grille[qui] |= 1 << place;
//win the yes, need the know to win... against the no !
for (var i=0; i<this.winCombi.length; i++) {
if ((this.grille[qui] & this.winCombi[i]) == this.winCombi[i]) {
//si je peux gagner il FAUT jouer là
//si ça lui permet de gagner il NE FAUT PAS jouer là
this.grille[qui] &= ~(1 << place);
return (qui == 1) ? 1 : -1;
}
}
//si je ne peux ni gagner ni empêcher de gagner on regarde le coup suivant
var scoreNext = new Array();
for (var i=0; i<9; i++) {
if ((this.grille[0] | this.grille[1]) & (1 << i)) continue;
scoreNext.push([i, this.bienLaCase(i, 1-qui)]);
}
this.grille[qui] &= ~(1 << place);
if (scoreNext.length == 0) return 0;
scoreNext.sort(function (a, b) {return b[1]-a[1];});
if (qui == 1) scoreNext.reverse();
return scoreNext[0][1];
}
function AI_restart() {
this.grille = new Array(0, 0);
}
function AI_new(callPlay) {
this.winCombi = new Array();
this.doPlay = callPlay;
this.played = AI_played;
this.joue = AI_joue;
this.bienLaCase = AI_bien;
this.restart = AI_restart;
this.winCombi.push(7, 7 << 3, 7 << 6, 73, 73 << 1, 73 << 2, 273, 84);
}
function init() {
morpion = new morpion_grille(3, nodeGratte = document.getElementById('morbac'),
document.getElementById('nbParties'),
document.getElementById('nbGagne1'),
document.getElementById('nbGagne2'),
document.getElementById('nbNuls'));
var cases = nodeGratte.getElementsByTagName('td');
for (var i=0; i<cases.length; i++) {
cases[i].onclick = new Function("morpion.joue("+i+")");
}
AI = new AI_new([morpion, 'joue']);
//AI2 = new AI_new([morpion, 'joue']);
morpion.onplay1 = [AI, 'played'];
//morpion.onplay2 = [AI2, 'played'];
morpion.onnew = [AI, 'restart'];
//morpion.onnew = function () { AI.restart(); AI2.restart();};
morpion.joueur2 = [AI, 'joue'];
//morpion.joueur1 = [AI2, 'joue'];
morpion.newPartie();
}
</script>
</head>
<body onload="init()">
<div>Nombre de parties joués : <var id="nbParties">0</var></div>
<div>Nombre de parties gagnés par <em>vous</em> : <var id="nbGagne1">0</var></div>
<div>Nombre de parties gagnés par <em>l'autre</em> : <var id="nbGagne2">0</var></div>
<div>Nombre de matchs nuls : <var id="nbNuls">0</var></div>
<table id="morbac">
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html> |