Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var trattini,word,hidden;
  2. var tot = 0;
  3. var errori = 0;
  4. var montepremi = document.getElementById("montepremi");
  5. var lettere = 0;
  6.  
  7. function setWord(){
  8.   word = document.getElementById("word").value;
  9.   hidden = document.getElementById("hidden");
  10.   trattini = word.replace(/./g, "-");
  11.   hidden.innerHTML = trattini;
  12. }
  13.  
  14. function checkWord(){
  15.   var wordInput = document.getElementById("guess").value;
  16.   var ciclo = 0;
  17.   var indovinato = false;
  18.   if(errori <= 10)
  19.   {
  20.     for(var i=0;i<word.length;i++)
  21.     {
  22.       if(trattini.indexOf("-") < 0)
  23.       {
  24.         while(!confirm("Hai vinto! Premi OK per una nuova partita"));
  25.         location.reload();
  26.         break;
  27.       }
  28.       else if(word.indexOf(wordInput, i) > -1)
  29.       {
  30.         trattini = replaceCharAt(trattini,wordInput,word.indexOf(wordInput, i));
  31.         if(ciclo === 0)
  32.         {
  33.           premio();
  34.           lettere++;
  35.         }
  36.         indovinato = true;
  37.         ciclo++;
  38.       }
  39.       else{
  40.         if(indovinato === false)
  41.         {
  42.           errori++;
  43.           sbagliato(errori);
  44.         }
  45.         ciclo++;
  46.         return;
  47.       }
  48.       hidden.innerHTML = trattini;
  49.     }
  50.   }  
  51. }
  52.  
  53. function premio(){
  54.     var random = Math.floor((Math.random() * 100) + 1);
  55.    
  56.     alert("Hai indovinato! Guadagni "+random+" Euro!");
  57.     tot += random;
  58.     montepremi.innerHTML = "Il tuo montepremi ammonta a "+tot+" Euro!";
  59. }
  60.  
  61. function sbagliato(errori){
  62.     var random = Math.floor((Math.random() * 100) + 1);
  63.     var immagine = document.getElementById("impiccato");
  64.    
  65.     tot -= random;
  66.     var impiccato = ["./img/0.bmp","./img/1.bmp","./img/2.bmp","./img/3.bmp","./img/4.bmp","./img/5.bmp","./img/6.bmp","./img/7.bmp","./img/8.bmp","./img/9.bmp","./img/10.bmp"];
  67.     alert("Mi dispiace, hai sbagliato. Perdi "+random+" Euro");
  68.     if(errori <= 10)
  69.     {
  70.         immagine.src = impiccato[errori];
  71.     }
  72.     else
  73.     {
  74.         while(!confirm("Hai perso! Premi OK per una nuova partita"));
  75.         location.reload();
  76.     }
  77.     montepremi.innerHTML = "Il tuo montepremi ammonta a "+tot+" Euro";
  78. }
  79.  
  80. function replaceCharAt(string,letter,index){
  81.   return string.substring(0, index) + letter + string.substring(index + 1);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement