<!--
// Auteur : Frédéric MAIRE (c) 1997 All right reserved

var premier
var qui
var k_img = 0;
var pla;
var ImgPion;
var dirx = new Array(-1,0,1, -1,1, -1,0,1);
var diry = new Array(1,1,1, 0,0, -1,-1,-1);
var heu;
var hval;

// global 
var retourneN

function Init() {
  // le plateau
  pla = new Array(8)
  heu = new Array(8)  
  hval = new Array(8)  
  for (i=0; i<8; i++) {
    pla[i] = new Array(8)
    heu[i] = new Array(8)
  }
  // les pions
  ImgPion = new Array(3)
  for (i=0; i<3; i++) {
    ImgPion[i] = new Image(64,64)
    ImgPion[i].src = "pion"+i+".gif"
  }

  // valeur case heuristique
  hval[0] = new Array(50,05,15,20,20,15,02,50)
  hval[1] = new Array(05,00,08,08,08,08,00,02)
  hval[2] = new Array(15,08,10,20,20,10,08,15)
  hval[3] = new Array(20,08,20,00,00,20,08,20)
  hval[4] = new Array(20,08,20,00,00,20,08,20)
  hval[5] = new Array(15,08,10,20,20,10,08,15)
  hval[6] = new Array(05,00,08,08,08,08,00,05)
  hval[7] = new Array(50,05,15,20,20,15,05,50)
}

function score( c) {
var s, i,j
  s = 0
  for (i=0; i<8; i++) for (j=0; j<8; j++) s+=(pla[i][j]==c)?1:0
  return s
}

function affscore() {
  document.score[0].value = score(1)
  document.score[1].value = score(2)
}

function place( x, y, c) {
  pla[x][y] = c
  document.images[k_img + x*8+y].src = ImgPion[c].src
}

function NouveauJeu( eff) {
  for (i=0; i<8; i++) {
    for (j=0; j<8; j++) {
      pla[i][j] = 0;
      if (eff) 
        document.images[k_img + i*8+j].src = ImgPion[0].src
    }
  }
  place(3,3,1)
  place(3,4,2)
  place(4,3,2)
  place(4,4,1)
  qui = 1
  affscore()
  premier = true
}

function chercheRetourne(x,y,c) {
var i
var cc
var trouve
var ex

  trouve = false
  cc = (c==1)?2:1;
  for (i=0; i<8; i++) {
    if (x+dirx[i]>=0 && x+dirx[i]<8 && y+diry[i]>=0 && y+diry[i]<8) {
      if (pla[x+dirx[i]][y+diry[i]] == cc) {
        // on est place a cote d'un pion adverse
        // recherche si dans la direction il y a un pivot
	ex = 2
        while (x+ex*dirx[i]>=0 && x+ex*dirx[i]<8 && y+ex*diry[i]>=0 && y+ex*diry[i]<8 && !trouve) {
          if (pla[x+ex*dirx[i]][y+ex*diry[i]] == c) trouve = true;
          ex++;
	}
      }
    }
    if (trouve) break; 
  }
  return trouve;
}

function retourne(x,y,c,doit) {
var i
var cc
var gtrouve, trouve, retourne, pasvide
var ex

  // calcul sur variable globale le nombre de pions retourne
  retourneN = 0

  gtrouve = false
  cc = (c==1)?2:1;
  for (i=0; i<8; i++) {
    if (x+dirx[i]>=0 && x+dirx[i]<8 && y+diry[i]>=0 && y+diry[i]<8) {
      if (pla[x+dirx[i]][y+diry[i]] == cc) {
        // on est place a cote d'un pion adverse
        // recherche si dans la direction il y a un pivot
	ex = 2
        retourne = false
        pasvide= true
        trouve = false
        while (x+ex*dirx[i]>=0 && x+ex*dirx[i]<8 && y+ex*diry[i]>=0 && y+ex*diry[i]<8 && !trouve && pasvide) {
          if (pla[x+ex*dirx[i]][y+ex*diry[i]] == c) { 
	    trouve = true;
            retourne = true;
          }
          if (pla[x+ex*dirx[i]][y+ex*diry[i]] == 0) { 
            pasvide = false
          }
          ex++;
	}
        gtrouve = gtrouve || trouve
        if (retourne) {
          for (ex-=2;ex>0;ex--) {
	      if (doit) place( x+ex*dirx[i], y+ex*diry[i], c)
            retourneN++
	    }
        }
      }
    }
  }
  return gtrouve;
}

function joueOrdi(c) {
var px,py,max
var passe
  passe = true
  max = 0
  for (i=0; i<8; i++) for (j=0; j<8; j++) heu[i][j] = 0;

  for (i=0; i<8; i++) {
    for (j=0; j<8; j++) {
      if ((pla[i][j]==0) && retourne(i,j,c,false)) {
        heu[i][j] += retourneN + hval[i][j] + Math.random();
      }
      if (heu[i][j] > max) {
        passe = false
        max = heu[i][j]
        px = i
        py = j
      }
    }
  }  
  if (!passe) {
    retourne(px,py,c,true)
    place(px,py,c)
  }
}

function joue( x,y)
{
  if ((pla[x][y]==0) && retourne(x,y,1,true)) {
    place(x,y,1)
    affscore()
  } else if (!premier) {
    joueOrdi(1)
    affscore()
  }
  premier = false
  joueOrdi(2)
  affscore()
  qui = (qui==1)?2:1
}

//-->
