<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
      "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="content-type"
  content="application/xhtml+xml; charset=UTF-8" />
  <title>Test Javascript : A little game</title>
  <meta name="generator" content="Amaya, see http://www.w3.org/Amaya/" />
  <script type="text/javascript">
    <![CDATA[
      mots_mysteres = Array("amaya","bateau","cactus","dauphin","etoile","fromage","grenouille","helicoptere","icone","joueur","koala","lapin","moutarde","nuage","organisme","panorama","quatrieme","resistance","saturne","tournevis","universel","virage","wagon","xylophone","zebre");

    function play()
      {
      do
        {
        mot = mots_mysteres[Math.floor(Math.random()*(mots_mysteres.length-1))];
        trouve = Array();
        for(i = 0; i < mot.length ; i++)trouve[i] = false;

        gagne = false;
        tentatives = 9;

        while(!gagne && tentatives > 0)
          {
          texte = "";
          for(i = 0 ; i < mot.length ; i++)texte+=(trouve[i] ? mot.charAt(i) : "*");
          alert("remaining attempts : "+tentatives+"\n\nword to find : "+texte);
          proposition = prompt("Enter a letter or a word :","");
          proposition = proposition.toLowerCase();

          if(proposition.length == 1)
            {/* propositon d'une lettre */
            for(i = 0, bien = false, j = 0 ; i < mot.length ; i++)
              {
              if(mot.charAt(i) == proposition)
                {
                bien = true;
                trouve[i] = true;
                
                }
              if(trouve[i])j++;
              }
            if(bien)
              {
              alert("Good letter !");
              if(j == mot.length)gagne = true;
              }
            else {alert("Bad letter !"); tentatives--;}
            }
          else
            {/* proposition directe du mot */
            if(proposition == mot)gagne = true; else tentatives = 0;
            }
          }

        if(gagne)alert("You win !");
        else alert("You lose !");

        alert("The word was : "+mot);
        }
      while(confirm("Do you want to play again?"))
      }
      ]]>
  </script>
</head>

<body>
<h1>Test Javascript : A game</h1>
<ul>
  <li>You have to guess a french word.</li>
  <li>You can propose at most 9 letters.</li>
  <li>You can also try to directly give a name.</li>
  <li>To start the game, use the command <code>play()</code></li>
</ul>
</body>
</html>
