<!--

function print(s) {
//	this.writeln( s + '<br>');
	document.writeln( s);
	return this;
}
function println(s) {
//	this.writeln( s + '<br>');
	document.writeln( s + '<br>');
	return this;
}

function listForm() {
   for (i=0; i<document.score.length; i++) {
      println( document.score[i].value);
   }
}

function rndDe() {
   return Math.round( Math.random()*6 + 0.5);
//   return Math.round( Rnd()*6 + 0.5);
}

var k_col = 3
var k_ligne = 15

var Des;
var DesGardes;
var DesImg;
var KeepImg;
var EtatImg;
var NombreLance;
var Score;
var ScoreCours;
var Etat; // 0 clos, 1 Interdit, 2 Bon, 3 Mauvais
var IdxDescend;
var IdxMonte;
var NbTour;
var TotalScore;
var BestScore;

// tmp
var nbDes; // nombre de des d'un chiffre donne

function initAll() {
   // les valeurs
   Des = new Array(5)
   nbDes = new Array(6) // des a 6 faces
   // les images correspondant aux faces des dés
   DesImg = new Array(6)
   for (i=1; i<7; i++) {
      DesImg[i] = new Image(64,48)
      DesImg[i].src = "de" + eval(i) + ".gif"
   }
   // les des a garder
   DesGardes = new Array(5)
   // les images pour gardes les des
   KeepImg = new Array(2)
   for (i=0; i<2; i++) {
      KeepImg[i] = new Image(64,48)
      KeepImg[i].src = "keep" + eval(i) + ".gif"
   }
   // les images des etats
   EtatImg = new Array(4)
   for (i=0; i<4; i++) {
      EtatImg[i] = new Image(16,16)
      EtatImg[i].src = "etat" + eval(i) + ".gif"
   }
   // les Scores
   Score = new Array(k_col)
   Etat = new Array(k_col)
   ScoreCours = new Array(k_ligne)
   for (i=0; i<Score.length; i++) {
      Score[i] = new Array(k_ligne)
      Etat[i] = new Array(k_ligne)
      for (j=0; j<Score[i].length; j++) {
         Score[i][j] = 0 //i*k_ligne+j //!!!
         Etat[i][j] = 1 // interdit
         ScoreCours[i] = 0
      }
   }
}

function genereToutDes() {
   for (i=0; i<5; i++) {
      Des[i] = rndDe();
      DesGardes[i] = 0;
   }
}

function afficheDes() {
   document.DE0.src = DesImg[Des[0]].src
   document.DE1.src = DesImg[Des[1]].src
   document.DE2.src = DesImg[Des[2]].src
   document.DE3.src = DesImg[Des[3]].src
   document.DE4.src = DesImg[Des[4]].src
}
// attention juste efface
function afficheKeep() {
   document.KEEP0.src = KeepImg[0].src
   document.KEEP1.src = KeepImg[0].src
   document.KEEP2.src = KeepImg[0].src
   document.KEEP3.src = KeepImg[0].src
   document.KEEP4.src = KeepImg[0].src
}

function gardeDeN( deN) {
    if ((NbTour != (k_ligne-1)*k_col+1) && (NombreLance<3)) {
        DesGardes[deN] = ! DesGardes[deN]
        if (deN == 0) document.KEEP0.src = KeepImg[(DesGardes[deN])?1:0].src;
        if (deN == 1) document.KEEP1.src = KeepImg[(DesGardes[deN])?1:0].src;
        if (deN == 2) document.KEEP2.src = KeepImg[(DesGardes[deN])?1:0].src;
        if (deN == 3) document.KEEP3.src = KeepImg[(DesGardes[deN])?1:0].src;
        if (deN == 4) document.KEEP4.src = KeepImg[(DesGardes[deN])?1:0].src;
    }
}


function calculDouble() {
    for (i=0; i<6; i++) {
        nbDes[i] = 0
    }
    for (i=0; i<5; i++) {
        nbDes[Des[i]-1]++
    }
}
// total des des
function totalDES() {
    tot = 0
    for (i=0; i<5; i++) {
        tot += Des[i];
    }
    return tot;
}
// total des des pout "les X"
function totalLESX( x) {
    tot = 0
    for (i=0; i<5; i++) {
        if (Des[i]==x) tot += x;
    }
    return tot;
}
// total petit suite
function totalPETITESUITE() {
    calculDouble()
    tot = 0
    if (
       // petite
       (nbDes[0]>=1 && nbDes[1]>=1 && nbDes[2]>=1 && nbDes[3]>=1) ||
       (nbDes[1]>=1 && nbDes[2]>=1 && nbDes[3]>=1 && nbDes[4]>=1) ||
       (nbDes[2]>=1 && nbDes[3]>=1 && nbDes[4]>=1 && nbDes[5]>=1) ||
       // ou grande
       (nbDes[0]==1 && nbDes[1]==1 && nbDes[2]==1 && nbDes[3]==1 && nbDes[4]==1 && nbDes[5]==0) ||
       (nbDes[0]==0 && nbDes[1]==1 && nbDes[2]==1 && nbDes[3]==1 && nbDes[4]==1 && nbDes[5]==1) 
       ) {
        tot = 25
    }
    return tot;
}
// total petit suite
function totalGRANDESUITE() {
    calculDouble()
    tot = 0
    if (
       (nbDes[0]==1 && nbDes[1]==1 && nbDes[2]==1 && nbDes[3]==1 && nbDes[4]==1 && nbDes[5]==0) ||
       (nbDes[0]==0 && nbDes[1]==1 && nbDes[2]==1 && nbDes[3]==1 && nbDes[4]==1 && nbDes[5]==1) 
       ) {
        tot = 35
    }
   return tot;
}
// total paire
function totalPAIRE() {
    tot = 0
    paire = false
    for (i=0; i<5; i++) {
        for (j=i+1; j<5; j++) {
            paire = paire || (Des[i]==Des[j]);
        }
    }
    if (paire) { tot = totalDES(); }
    return tot;
}
// total double paire
function totalDOUBLEPAIRE() {
    tot = 0
    calculDouble()
    paire1 = false
    paire2 = false
    for (i=0; i<6; i++) {
        paire1 = (nbDes[i]==5) || (nbDes[i]==4) || paire1
        paire2 = (nbDes[i]==5) || (nbDes[i]==4) || (paire1 && (nbDes[i]==3 || nbDes[i]==2)) || paire2
        paire1 = (nbDes[i]==3) || (nbDes[i]==2) || paire1
    }
    if (paire1 && paire2) { tot = 10+ totalDES(); }
    return tot;
}
// total combinaison simple
function totalCombi( comb) {
    calculDouble()
    tot = 0
    combi = false
    for (i=0; i<6; i++) {
        combi = combi || (nbDes[i]==comb)
    }
    if (combi) { tot = totalDES(); }
    return tot
}
// total BRELAN
function totalBRELAN() {
    tot = totalCombi(3) + totalCombi(4) + totalCombi(5);
    if (tot>0) tot += 20;
    return tot
}
// total CARRE
function totalCARRE() {
    tot = totalCombi(4) + totalCombi(5);
    if (tot>0) tot += 40;
    return tot
}
// total YAMS
function totalYAMS() {
    tot = totalCombi(5);
    if (tot>0) tot += 50;
    return tot
}
// total FULL
function totalFULL() {
    if ((totalCombi(2)!=0 && totalCombi(3)!=0) || 
        (totalCombi(5)!=0)) {
        return 30 + totalDES();
    } else return 0;
}


function calculScoreCours() {
    ScoreCours[0] = totalLESX(1)
    ScoreCours[1] = totalLESX(2)
    ScoreCours[2] = totalLESX(3)
    ScoreCours[3] = totalLESX(4)
    ScoreCours[4] = totalLESX(5)
    ScoreCours[5] = totalLESX(6)

    ScoreCours[6] = totalPETITESUITE()
    ScoreCours[7] = totalGRANDESUITE()

    ScoreCours[8] = totalPAIRE()
    ScoreCours[9] = totalDOUBLEPAIRE()
    ScoreCours[10] = totalBRELAN()
    ScoreCours[11] = totalFULL()
    ScoreCours[12] = totalCARRE()
    ScoreCours[13] = totalYAMS()
}
function vuEtatDBG() {
    calculScoreCours()
    for (i=0; i<k_ligne-1; i++) {
        Score[0][i] = ScoreCours[i];
    }
}

function vuPossible() {
    calculScoreCours()
    for (i=0; i<k_col; i++) {
        for (j=0; j<k_ligne-1; j++) {
            if (Etat[i][j]>1) {
                if (ScoreCours[j]>0) {
                    Etat[i][j] = 2
                } else {
                    Etat[i][j] = 3
                }
            }
            document.images[i+j*k_col + 0].src = EtatImg[Etat[i][j]].src
        }
    }
}

function mettreScoreAJour() {
    for (i=0; i<k_col; i++) {
        Score[i][k_ligne-1] = 0
        for (j=0; j<k_ligne-1; j++) {
            Score[i][k_ligne-1] += Score[i][j]
        }
    } 
    for (i=0; i<document.score.length; i++) {
        idx = (i%k_col)*k_ligne + Math.round(i/k_col-0.5)
        document.score[i].value = Score[Math.round(idx/k_ligne-0.5)][idx%k_ligne]
    }

	TotalScore = Score[0][k_ligne-1] + Score[1][k_ligne-1] + Score[2][k_ligne-1];
    document.Total[0].value = TotalScore;

}

function relance() {
    if ((NbTour != (k_ligne-1)*k_col+1) && (NombreLance<3)) {
        for (i=0; i<5; i++) {
           if (DesGardes[i]) Des[i] = rndDe();
           DesGardes[i] = 0;
        }
        afficheDes();
        afficheKeep();

        NombreLance += 1
        document.Lance[0].value = NombreLance;
     
        calculScoreCours()
        vuPossible()
        //mettreScoreAJour()
    }
}

function affectScore( col, lig) {
    if (Etat[col][lig] > 1) {
        Score[col][lig] = ScoreCours[lig] 
        Etat[col][lig] = 0 // clos
        document.images[col+lig*k_col + 0].src = EtatImg[0].src
        mettreScoreAJour()

        if (col == 0) { IdxDescend++; Etat[0][IdxDescend] = 2; }
        if (col == 1) { IdxMonte--; Etat[1][IdxMonte] = 2; }

        NombreLance = 0
        document.Lance[0].value = NombreLance;
      
        NbTour += 1
        if (NbTour != (k_ligne-1)*k_col+1) {
            for (i=0; i<5; i++) { DesGardes[i] = true; }
            relance()
        }
        if (Score[0][k_ligne-1] + Score[1][k_ligne-1] + Score[2][k_ligne-1] > BestScore) {
            BestScore = Score[0][k_ligne-1] + Score[1][k_ligne-1] + Score[2][k_ligne-1];
            document.Total[1].value = BestScore;
        }
        // si fin
        if (NbTour == (k_ligne-1)*k_col+1) {
          //setCookie( "interJEUX_Yathzee", eval(BestScore)); //, new Date("1/1/2030") );
          setCookie( "interJEUX_Yathzee", eval(BestScore), new Date("1/1/2030") );
        }
    }
}


function nouveauJeu() {

   genereToutDes();
   afficheDes();
   afficheKeep();

   NbTour = 1;
   NombreLance = 1;
   document.Lance[0].value = NombreLance;

   IdxDescend = 0;
   IdxMonte = k_ligne-2;

   for (i=0; i<k_ligne; i++) {
      Etat[0][i] = 1 // interdit
      Etat[1][i] = 1
      Etat[2][i] = 2
      for (j=0; j<k_col; j++) Score[j][i] = 0;
   }
   Etat[0][IdxDescend] = 2
   Etat[1][IdxMonte] = 2

   calculScoreCours()
   vuPossible()
   mettreScoreAJour();
   document.Total[1].value = BestScore;
}

//-->
