/* sol.js

   jeu du solitaire

   Jean-Paul Davalan © 2003
   jpdvl@wanadoo.fr

   23 Juillet 2003
   -----------------------------------------------------
                               j=0  1  2  3  4  5  6
           1  2  3        i=0         02 03 04
	4  5  6  7  8       1      11 12 13 14 15
     9 10 11 12 13 14 15    2   20 21 22 23 24 25 26
    16 17 18 19 20 21 22    3   30 31 32 33 34 35 36
    23 24 25 26 27 28 29    4   40 41 42 43 44 45 46
       30 31 32 33 34       5      51 52 53 54 55
          35 36 37          6         62 63 64
	  
                               0 <= x <= 6
			       0 <= y <= 6
	                       2 <= x+y <= 10
			      -4 <= x-y <=  4

   -----------------------------------------------------
*/

var sel, xpos, ypos,total;
var lettre = new Array("a","b","c","d");
var cols = new Array("#999999","#ffffff","#ffffff","#7799bb");
var color=Math.floor(4*Math.random());
var pre = lettre[color];
var placement=false;
var totalsv;

var Im = new Array();
Im[0] = new Image(); Im[0].src=pre+"0.png";
Im[1] = new Image(); Im[1].src=pre+"1.png";
Im[2] = new Image(); Im[2].src=pre+"2.png";
Im[3] = new Image(); Im[3].src=pre+"1b.png";

var tab = new Array();
var svtab = new Array();
for(var i=0;i<7;i++) {
  tab[i] = new Array();
  svtab[i] = new Array();
  for(var j=0;j<7;j++) {
    svtab[i][j] = 0;
    tab[i][j] = 0;
  }
}

function sve() {
//  alert("sve");
  for(var i=0;i<7;i++)
    for(var j=0;j<7;j++)
       svtab[i][j] = tab[i][j];
  totalsv = total;
}

function retrv() {
//  alert("retrv");
  efface();
  for(var i=0;i<7;i++)
    for(var j=0;j<7;j++) {
       tab[i][j] = svtab[i][j];
       document.images["im"+i+"_"+j].src = Im[tab[i][j]].src;
    }
  total = totalsv;
  document.frm.comm.value="Partie en cours";
  placement=false;
  document.frm.pl.value="JEU / billes";
}

function dw(a) {
document.write(a);
}

function mksol() {
  dw('<center><table border=0 cellspacing=0 cellpadding=0 bgcolor="'+cols[color]+'">');
  for(var i=0;i<7;i++) {
    dw('<tr>\n');
    for(var j=0;j<7;j++) {
      dw('<td nowrap><a href="javascript:eff('+i+','+j+')"><img border=0 name="im'+i+'_'+j+'" width=31 height=31 alt=""></a></td>');
    }
    dw('</tr>\n');
  }
  dw('</table></center>\n');
  init();
}

function init0() {
  var i, j;
  for(i=0;i<7;i++)
    for(j=0;j<7;j++)
      tab[i][j] = 1;
      
  tab[0][0]=2;tab[0][1]=2;tab[0][5]=2;
  tab[0][6]=2;tab[1][0]=2;tab[1][6]=2;
  tab[5][0]=2;tab[5][6]=2;tab[6][0]=2;
  tab[6][1]=2;tab[6][5]=2;tab[6][6]=2;
//  tab[3][3]=0;
  
  for(i=0;i<7;i++)
    for(j=0;j<7;j++) {
       document.images["im"+i+"_"+j].src = Im[tab[i][j]].src
    }
  sel = false;
  placement=false;
  total=36;
}

function init() {
  init0();
  tab[3][3]=0;
  document.images["im3_3"].src = Im[0].src;
}

function init2() {
  init();
  sel = false;
  placement=false;
  document.frm.pl.value="JEU / billes";//"joue / PLACE";
  document.frm.comm.value="Partie en cours";
}

function corsaire() {
  init0();
  tab[0][2]=0;
  document.images["im0_2"].src = Im[0].src;
}

function efface() {
//  alert("efface");
  var i, j;
  for(i=0;i<7;i++) {
    for(j=0;j<7;j++) {
      tab[i][j] = 0;
    }
  }
  tab[0][0]=2;tab[0][1]=2;tab[0][5]=2;
  tab[0][6]=2;tab[1][0]=2;tab[1][6]=2;
  tab[5][0]=2;tab[5][6]=2;tab[6][0]=2;
  tab[6][1]=2;tab[6][5]=2;tab[6][6]=2;
  tab[3][3]=0;

  for(i=0;i<7;i++) {
    for(j=0;j<7;j++) {
       document.images["im"+i+"_"+j].src = Im[tab[i][j]].src
     //document.Images["im"+i+"_"+j].src = "a"+tab[i][j]+".png";
    }
  }
  sel = false;
  placement=true;
  document.frm.comm.value="Placement des billes";
  document.frm.pl.value="jeu / BILLES";//"joue / PLACE";
  total=0;
}

function croix() {
  efface();
  for(var i=1;i<6;i++) {
    tab[i][3]=1; tab[3][i]=1;
    document.images["im"+i+"_3"].src = Im[1].src
    document.images["im3_"+i].src = Im[1].src
  }
  total=9;
}

function pipe() {
  efface();
  for(var i=2;i<5;i++) {
    tab[i][3]=1; tab[i][4]=1;
    document.images["im"+i+"_3"].src = Im[1].src;
    document.images["im"+i+"_4"].src = Im[1].src;
  }
  tab[4][2]=1;
  document.images["im4_2"].src = Im[1].src;
  total=7;
}

function U() {
  efface();
  for(var i=2;i<5;i++) {
    for(var j=2;j<5;j++) {
      tab[i][j]=1;
      document.images["im"+i+"_"+j].src = Im[1].src;
    }
  }
  tab[2][3]=0;
  document.images["im2_3"].src = Im[0].src;
  total=8;
}
function square() {
  efface();
  for(var i=2;i<5;i++) {
    for(var j=2;j<5;j++) {
      tab[i][j]=1;
      document.images["im"+i+"_"+j].src = Im[1].src;
    }
  }
  tab[4][2]=0;
  document.images["im4_2"].src = Im[0].src;
  total=8;
}
function pl(x,y, a) {
  tab[x][y]=a; document.images["im"+x+"_"+y].src = Im[a].src;
}
function plus_() {
  efface();
  pl(2,3,1);pl(3,2,1);pl(3,3,1);pl(3,4,1);pl(4,2,1);
  pl(4,3,1);pl(5,2,1);
  total=7;
}
function f5() {
  efface();
  pl(3,4,1);pl(4,4,1);pl(3,3,1);pl(5,3,1);pl(2,2,1);pl(3,2,1);
  total=6;
}
function possible(x,y) {
 if(tab[x][y] != 1) return false;
 if(x>=2 && tab[x-1][y]==1&&tab[x-2][y]==0) return true;
 if(y>=2 && tab[x][y-1]==1&&tab[x][y-2]==0) return true;
 if(x<=4 && tab[x+1][y]==1&&tab[x+2][y]==0) return true;
 if(y<=4 && tab[x][y+1]==1&&tab[x][y+2]==0) return true;
 return false;
}

function jouable(x,y) {
  var u, v;
  if(x<0 || x>6 || y<0 || y>6 || tab[x][y] !=0 ) return false
  if(x==xpos+2 && y==ypos) {
     u=xpos+1; v= y;
  } else if(x==xpos-2 && y==ypos) {
     u= xpos-1;v= y;
  } else if(x==xpos && y==ypos+2) {
    u= x; v = ypos+1;
  } else if(x==xpos && y==ypos-2) {
    u=x; v=ypos-1;
  } else {
    return false;
  }
  if(tab[u][v]!=1) return false;
  tab[xpos][ypos]=0; tab[u][v]=0;
  tab[x][y]=1;
  document.images["im"+xpos+"_"+ypos].src = Im[0].src;
  document.images["im"+u+"_"+v].src = Im[0].src;
  document.images["im"+x+"_"+y].src = Im[1].src;
  total = total-1;
  document.frm.comm.value=total+" billes";
  sel=false;
  return true;
}

function eff(x, y) {
  if(placement==false) {
  var a = possible(x,y);;
  if(sel==false) {
    if(a==true) {
      xpos = x; ypos=y;
      sel = true;
    } else {
      sel = false;
    }

  } else {
    a=jouable(x,y);
    sel = false;
    a = possible(x,y);
    if(a==true) {
      sel = true;
      xpos=x; ypos=y;
    }
  }
  } else {
    if(tab[x][y]==1) {
      tab[x][y]=0;
      document.images["im"+x+"_"+y].src = Im[0].src;
      total=total-1;
    } else if(tab[x][y]==0) {
      tab[x][y]=1;
      total=total+1;
      document.images["im"+x+"_"+y].src = Im[1].src;
    }
  }
}

function place() {
  if(placement==true) {
    placement=false;
    document.frm.pl.value="JEU / billes";//"joue / PLACE";
    document.frm.comm.value="Partie en cours";
  } else if(placement==false) {
    //clear();
    total=0;
    placement=true;
    document.frm.pl.value="jeu / BILLES";//"JOUE / place";
    document.frm.comm.value="Placement des billes";
  }
}

