/* grenouilles.js
   jeu des grenouilles

   (C) 2002-2005 Jean-Paul Davalan <jpdvl@wanadoo.fr>

   page
   http://perso.wanadoo.fr/jean-paul.davalan/jeux/solitaires/grenouilles/index.html

   site
   http://perso.wanadoo.fr/jean-paul.davalan/index.html


 */


function alerte(st) {
  document.getElementById('commt').innerHTML=st;
}
var lg="fr"
var m=3;
var n=2*m+1;
var pion = new Array();
var reussi=0;
var bloque=0;
var Im=new Array();
Im[0]=new Image; Im[0].src="gb.png";
Im[1]=new Image; Im[1].src="gn.png";
Im[2]=new Image; Im[2].src="gv.png";

Im[3]=new Image; Im[3].src="fermeup.png";
Im[4]=new Image; Im[4].src="fermedown.png";
Im[5]=new Image; Im[5].src="nouvelleup.png";
Im[6]=new Image; Im[6].src="nouvelledown.png";

Im[7]=new Image; Im[7].src="jeuxup.png";
Im[8]=new Image; Im[8].src="jeuxdown.png";

function myrand(n) {
  return Math.floor(n*Math.random());
}

function init() {
  for(var i=0; i<n; i++) {
    if(i==m) pion[i] = 0
    else if(i<m) pion[i] = -1;
    else pion[i] = 1;
  }

  for(i=0;i<n;i++) {
    if(pion[i] == 0) document.images['gre_'+i].src=Im[0].src;
    else if(pion[i] == -1) document.images['gre_'+i].src=Im[2].src;
    else document.images['gre_'+i].src=Im[1].src;
  }
  reussi=0;
  bloque = 0;
}


function testfin() {
  for(var i=0; i<n; i++) {
    if(i<m && pion[i] != 1) return 0;
    else if(i>m && pion[i] != -1) return 0;
    else if(pion[m] != 0)  return 0;
  }
  return 1;
}


function fbloque() {
  for(var i=0;i<n;i++) {
    if(i<n-1 && pion[i]==-1 && pion[i+1]==0) return 0;
    if(i<n-2 && pion[i]==-1 && pion[i+2]==0 && pion[i+1]==1) return 0;
    if(i>0 && pion[i]==1 && pion[i-1]==0) return 0;
    if(i>1 && pion[i]==1  && pion[i-1]==-1 && pion[i-2]==0) return 0;
  }
  return 1;
}

function ef(x){
  if(pion[x]==-1) {   // noir
    if(x<n-1 && pion[x+1]==0) {  // vide à droite
      pion[x]=0;
      pion[x+1] = -1;
      document.images['gre_'+x].src=Im[0].src;
      document.images['gre_'+(x+1)].src=Im[2].src;
    } else if ( x < n-2 && pion[x+1] == 1 && pion[x+2] == 0) {
      pion[x]=0;
      pion[x+2] = -1;
      document.images['gre_'+x].src=Im[0].src;
      document.images['gre_'+(x+2)].src=Im[2].src;
    }
  } else if (pion[x]==1) {   // blanc
    if(x>0 && pion[x-1]==0) {  // vide à gauche
      pion[x]=0;
      pion[x-1] = 1;
      document.images['gre_'+x].src=Im[0].src;
      document.images['gre_'+(x-1)].src=Im[1].src;
    } else if (x>=2 && pion[x-1]==-1 && pion[x-2]== 0) {
      pion[x]=0;
      pion[x-2] = 1;
      document.images['gre_'+(x)].src=Im[0].src;
      document.images['gre_'+(x-2)].src=Im[1].src;
    }
  }
  alerte("");
  if(reussi==1) {
    if(lg=="fr")alerte("Encore bravo, j'ai réinitialisé pour que vous puissiez rejouer.");
    else alerte("New Game");
    init();
  } else if(reussi==0 && testfin() == 1) {
    
    if(lg=="fr") alerte("Félicitations, vous avez réussi.");
    else alerte("Congratulations");
    reussi=1
  } else if(reussi==0 && fbloque()==1) { 
    if(bloque == 0) {
      if(lg=="fr") alerte("Le jeu est bloqué.");
      else alerte("Lost! (Game over)");
      bloque=1;
    } else {
      if(lg=="fr") alerte("Le jeu était bloqué, j'ai réinitialisé pour que vous puissiez rejouer.");
      else alerte("New Game");
      init();
    } 
  }
}

var MaxGrenouilles=15;

function ecrtab() {
  var i;
  var largeur=50;
  m=parseInt(document.frm.nbv.value);
  if(m<1) m=1;
  else if(m>MaxGrenouilles) m=MaxGrenouilles;
  if(m>7) largeur=40;
  if(m>9) largeur=30;
  if(m>11) largeur=20;
  document.frm.nbv.value=m;
  n = 2*m+1;
  var s = '<center>' +
    '<table border="0" cellspacing="0" cellpadding="0">' +
    '<tr>';
  for(i=0;i<n;i++) {
    s += '<td><a href="javascript:ef('+i+')">' +
         '<img name="gre_'+i+'" border=0 width='+largeur+' height='+largeur+' alt=""></a></td>';
  }
  s += '</tr>\n</table>';
if(lg=="fr") {
  s +='<br><a href="javascript:init()" onmouseover="document.images[\'nouvelle\'].src=Im[6].src"' +
  ' onmouseout="document.images[\'nouvelle\'].src=Im[5].src"><img ' +
  ' name="nouvelle" border=0 src="nouvelleup.png" width="140" height="24" alt=""></a>';
} else {
  s += '<br><a href="javascript:init()" onmouseover="document.images[\'nouvelle\'].src=Im[6].src"'+
  ' onmouseout="document.images[\'nouvelle\'].src=Im[5].src"><img ' +
  ' name="nouvelle" border=0 src="newplayup.png" width="120" height="28" alt=""></a> ';
}
  s +='</center>';
  document.getElementById("JEU").innerHTML=s;
  init();
}

function augm(p){
  m += p;
  if(m<1) m=1;
  else if(m>MaxGrenouilles) m=MaxGrenouilles;
  document.frm.nbv.value=m;
  n = 2*m+1;
  ecrtab()
}

