/* PING
   ping.js JavaScript
   ping.html
   Jean-Paul Davalan © 2002-2005 jpdavalan@wanadoo.fr

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

   ---------------------------------------
   Au départ un article de Jean-Paul Delahaye dans la revue
   Pour la Science de juillet 2002 (Ping et Pong)

   ---------------------------------------

   Trouvé sur le net sous le nom de Flippo à
   http://www.archimedes-lab.org/game_flippo/flippo.html
   mais attention, il existe d'autres types de 'flippo' :
   http://www.coster.demon.nl/e_flippo.htm
   http://home.wanadoo.nl/wvdput/spel/flippo/flippo.htm
   http://www.geocities.com/Area51/3450/maze/flippo.html
   ---------------------------------------
   ---------------------------------------
   ---------------------------------------
   Quelques solutions

   Neutre 3x3
   101
   000
   101
   Neutre 4x4
   1001
   1111
   1111
   1001
   Neutre 5x5
   10101
   00000
   10101
   00000
   10101
   Neutre 8x6
   11100111
   01111110
   10111101
   10111101
   01111110
   11100111
   
 */
var Nx=6, Ny=8, Nmax=15, tb=new Array(), tc=new Array();
var Im=new Array(), Imsrc=new Array("cb.png","cn.png","c_.png", "fermeup.png","fermedown.png", "cbo.png", "cno.png","jeuxup.png","jeuxdown.png");

for(var i=0;i<=8;i++){Im[i]=new Image; Im[i].src=Imsrc[i];}

function initgame() {
  var i, j;
  for(i=Nmax;i>0;i--)
  for(j=1;j<=Nmax;j++)
    document.images['c'+i+'_'+j].src=Im[2].src;
  for(i=0; i<=Nmax+1; i++) {
    for(j=0; j<=Nmax+1; j++) {
      tb[i*(Nmax+2)+j]=0;
      tc[i*(Nmax+2)+j]=0;
    }
  }
  maj();
}

function initvalues() {
  Nx = Math.floor(document.frm.xinput.value);
  Ny = Math.floor(document.frm.yinput.value);
  if(Nx <= 0) Nx = 5;  
  if(Ny <= 0) Ny = 5;
  if(Nx > Nmax) Nx = Nmax;  
  if(Ny > Nmax) Ny = Nmax;
  document.frm.xinput.value=Nx;
  document.frm.yinput.value=Ny;
  initgame();
}

function efface() {
  initgame()
}

function maj() {
  var i, j;
  for(i=1; i <= Nmax; i++) {
    for(j=1; j <= Nmax; j++) {
       if(i>Nx || j> Ny) 
         document.images["c"+i+"_"+j].src=Im[2].src;
       else if(tb[i*(Nmax+2)+j]==0) {
         if(tc[i*(Nmax+2)+j]==0) document.images["c"+i+"_"+j].src=Im[0].src;
         else document.images["c"+i+"_"+j].src=Im[5].src;
       } else {
         if(tc[i*(Nmax+2)+j]==0) document.images["c"+i+"_"+j].src=Im[1].src;
         else document.images["c"+i+"_"+j].src=Im[6].src;
       }
    }
  }
}

function maj2() {
  var i, j;
  for(i=1;i<=Nx;i++) {
    for(j=1;j<=Ny;j++) {
       if(tb[i*(Nmax+2)+j]==0) {
//         document.images["c"+i+"_"+j].src=Im[0].src;
         if(tc[i*(Nmax+2)+j]==0) document.images["c"+i+"_"+j].src=Im[0].src;
         else document.images["c"+i+"_"+j].src=Im[5].src;
       } else {
//           document.images["c"+i+"_"+j].src=Im[1].src;
         if(tc[i*(Nmax+2)+j]==0) document.images["c"+i+"_"+j].src=Im[1].src;
         else document.images["c"+i+"_"+j].src=Im[6].src;
       }
    }
  }
}

function effectue(x,y) {
  var i, j;
  tc[x*(Nmax+2)+y] = 1 - tc[x*(Nmax+2)+y];
  for(i = x-1; i <= x+1; i++)
    for(j = y-1; j <= y+1; j++)
      if(i != x || j != y) tb[i*(Nmax+2)+j]=1-tb[i*(Nmax+2)+j];
  maj2();
}

function ectable() {
  var i, j;
  document.write('<center>\n');
  document.write('<table border=0 cellpadding=0 cellspacing=0>\n');
  for(i=Nmax;i>0;i--) {
    document.write('<tr>\n');
    for(j=1;j<=Nmax;j++) {
      document.write('<td>');
      document.write('<a href="javascript:effectue('+i+','+j+')">');
      document.write('<img name="c'+i+'_'+j+'"  border=0 width="19" height="19" alt="">');
      document.write('</a>');
      document.write('</td>\n');
    }
    document.write('</tr>\n');
  }
  document.write('</table>\n');
  document.write('</center>\n');
}

