<!-- 
//----------------------------------------------------------------------
// Auteur : Frédéric MAIRE (c) 1997 All right reserved
// Dec-1997 : 
//   Ajout d'un bouton nouveau pour deuxième configuration initiale
//   Gestion des boutons pour le retour en arrière
// Nov-1997 : Version d'origine
//   Alignement des pierres calculer pour Netscape 4
//----------------------------------------------------------------------

var k_img = 5
var Img;
var plateau;

var actCoup;  // le coup actuellement affiché
var maxCoup;  // le coup maximum joué en mémoire

var premier;
var choisi;
var choixSrcX, choixSrcY; 
var choixDestX, choixSrcY;
var choixRetX, choixRetY;

function Init() {
  plateau = new Array(50) // nombre de tour
  for (i=0; i<plateau.length; i++) {
    plateau[i] = new Array(7) // ligne
    for (j=0; j<7; j++) {
      plateau[i][j] = new Array(7)
      for (k=0; k<7; k++) plateau[i][j][k] = 1
    }
  }

  plateau[0][0][0] = -1
  plateau[0][0][1] = -1
  plateau[0][0][5] = -1
  plateau[0][0][6] = -1
  plateau[0][1][0] = -1
  plateau[0][1][6] = -1

  plateau[0][6][0] = -1
  plateau[0][6][1] = -1
  plateau[0][6][5] = -1
  plateau[0][6][6] = -1
  plateau[0][5][0] = -1
  plateau[0][5][6] = -1

  premier = true
  actCoup = 0
  maxCoup = 0
  choisi = false

//start !!!!
//  plateau[0][3][3] = 0
//  document.images[k_img+3*7+3].src = Img[0].src
}

function chargeImg() {
  Img = new Array(3)
  Img[0] = new Image(44,44)
  Img[0].src = "solit_pion_vide.gif"
  Img[1] = new Image(44,44)
  Img[1].src = "solit_pion.gif"
  Img[2] = new Image(44,44)
  Img[2].src = "solit_pion_sel.gif"
}

function afficheCoup( n) {
  for (i=0; i<7; i++) {
    for (j=0; j<7; j++) {
      document.images[k_img+i*7+j].src = Img[ plateau[n][i][j]>0 ? plateau[n][i][j]:0 ].src
    }
  }
}

function nouveau( typ) {
  Init()
  if (typ==2) {
    plateau[0][1][1] = -1
    plateau[0][1][5] = -1
    plateau[0][5][1] = -1
    plateau[0][5][5] = -1
  }
  afficheCoup(0)
}

function choix( x,y) {
    if (premier) {
        premier = false
        plateau[0][x][y] = 0
        document.images[k_img+x*7+y].src = Img[0].src
        return
    }

    // choix d'un nouveau pion de depart
    if (plateau[actCoup][x][y] == 1) {
        // est-t-on en train de rechoisir un nouveau pion
        if (choisi) {
            document.images[k_img+choixSrcX*7+choixSrcY].src = Img[1].src
        }
        choisi = true
        choixSrcX = x
        choixSrcY = y
        document.images[k_img+choixSrcX*7+choixSrcY].src = Img[2].src
    } 
    // choix d'une case de destination, a condition d'avoir choisi le pion de depart
    if ((plateau[actCoup][x][y] == 0) && choisi) {
        choixDestX = x
        choixDestY = y

        // trouve le pion que l'on cherche a sauter
        choixRetX = choixSrcX + (( choixDestX - choixSrcX )>>1)
        choixRetY = choixSrcY + (( choixDestY - choixSrcY )>>1)

        // verification d'un saut valide
        if (
            (plateau[actCoup][choixRetX][choixRetY] == 1) && (
            (choixSrcX==choixRetX+1 && choixSrcX==choixDestX+2 && choixSrcY==choixRetY && choixSrcY==choixDestY) ||
            (choixSrcX==choixRetX-1 && choixSrcX==choixDestX-2 && choixSrcY==choixRetY && choixSrcY==choixDestY) ||
            (choixSrcX==choixRetX && choixSrcX==choixDestX && choixSrcY==choixRetY+1 && choixSrcY==choixDestY+2) ||
            (choixSrcX==choixRetX && choixSrcX==choixDestX && choixSrcY==choixRetY-1 && choixSrcY==choixDestY-2) 
           )) 
        {
            for (i=0; i<7; i++) for (j=0; j<7; j++) plateau[actCoup+1][i][j] = plateau[actCoup][i][j];
            actCoup++
            maxCoup++
            plateau[actCoup][choixSrcX][choixSrcY] = 0
            plateau[actCoup][choixRetX][choixRetY] = 0
            plateau[actCoup][choixDestX][choixDestY] = 1
            document.images[k_img+choixSrcX*7+choixSrcY].src = Img[0].src
            document.images[k_img+choixRetX*7+choixRetY].src = Img[0].src
            document.images[k_img+choixDestX*7+choixDestY].src = Img[1].src
            choisi = false
        } //else { alert("non valide : " +choixSrcX+choixSrcY+":" +choixRetX+choixRetY+":" +choixDestX+choixDestY+":") }
    }
}

// retour en arriere ou avance
function retour( av) {
  if (av>0) {
    if (actCoup+av<=maxCoup) {
      actCoup += av
    }
  } else {
    if (actCoup>0) {
      actCoup += av
    }
  }
  afficheCoup( actCoup)
}

function randomChange() {
//  for (i=0; i<30; i++) {
//    plateau[0][ Math.round( Math.random()*6 + 0.5)][ Math.round( Math.random()*6 + 0.5)] = Math.round( Math.random()*2 + 0.5) -1
//  }
  for (i=0; i<7; i++) for (j=0; j<7; j++) plateau[0][i][j] = Math.round( Math.random()*2 + 0.5) -1

/*
  for (i=0; i<30; i++) {
    document.images[k_img+ Math.round( Math.random()*48 + 0.5)].src = Img[0].src
  }
  for (i=0; i<30; i++) {
    document.images[k_img+ Math.round( Math.random()*48 + 0.5)].src = Img[1].src
  }
*/
  document.images[k_img+3*7+3].src = Img[2].src
}

//-->
