2016-08-06 5 views
0

色を推測するためのコードを書きました。しかし問題は、このコードを実行するたびに、ランダムな色を選択するのではなく、同じ色を選択することです。誰かが私を助けてくれるのだろうかと思っています。私はとても感謝しています!色を推測するゲームのjavascriptでrandomの代わりに同じ色を選択するたびに

<head> 
    <title>Color Guessing Game</title> 
    <meta name="author" content="Wajeb Saab"> 
</head> 

<body onload="do_game()"> 

    <script> 
     var target_color; 
     var target_index; 
     var guessed_color; 
     var guesses = 0; 
     var color = ["red", "blue", "yellow", "crimson", "darkorange", "olive", "silver", "steelblue", "tomato", "green"]; 
     var finished = false; 

     function do_game() 
     { 
      color.sort(); 
      var random_number = Math.random() * (color.length-1); 
      target_index = Math.floor(random_number); 
      target_color = color[target_index]; 
      alert("Hint: The target color is " + target_color); 

      var text = "I am thinking of one of these colors:\n\n" + color.join(", ") 
         + "\n\n What color am I thinking of?"; 

      while(!finished) 
      { 
       guessed_color = prompt(text); 
       guesses++; 
       finished = check_guess(); 
      } 

      myBody=document.getElementsByTagName("body")[0]; 
       myBody.style.background = target_color; 
     } 

     function check_guess() 
     { 
      if(color.indexOf(guessed_color) < 0) // check if guessed color is not included in array 
      { 
       alert("Sorry, I don't recognize your color.\n\n" + "Please try again."); 
       return false; 
      } 
      else if(guessed_color > target_color) 
      { 
       alert("Sorry, your guess is not correct!\n\n" + "Hint: your color is alphabetically higher than mine.\n\n" 
         + "Please try again."); 
       return false; 
      } 
      else if(guessed_color < target_color) 
      { 
       alert("Sorry, your guess is not correct!\n\n" + "Hint: your color is alphabetically lower than mine.\n\n" 
         + "Please try again."); 
       return false; 
      } 
      else 
      { 

       alert("Congratulations! You have guessed the color!\n\n" + "It took you " + guesses + " guesses to finish the game.\n\n" 
         + "You can see the color in the background."); 
       return true; 
      } 
     } 

    </script> 

</body> 

+0

私は助けてください:) – Malka

+0

それはうまくいきます.... – Rayon

+0

盗作は自由ですが、少なくとも著者を変更してください:) ' ' – Wajeb

答えて

0

問題は、あなたがcheck_guesses()関数にtrueを返すときにguessed_colorとtarget_colorをリセットする必要がある、あなたは最初の行として偽=完成を設定する必要がありますあなたのdo_game()関数。そう、あなたがそれを実行次回の今のように...

function do_game() { 
    finished = false; 
    color.sort(); 
    var random_number = Math.random() * (color.length - 1); 
    target_index = Math.floor(random_number); 
    console.log(target_index); 
    target_color = color[target_index]; 
    var text = "I am thinking of one of these colors:\n\n" + color.join(", ") + "\n\n What color an I thinking of?"; 
    while(!finished) { 
     guessed_color = prompt(text); 
     guesses++; 
     finished = check_guesses(); 
    } 
} 

function check_guesses() { 
    if(color.indexOf(guessed_color) < 0) { 
     console.log("Sorry I don't know what color that is"); 
     return false; 
    } else if(guessed_color > target_color) { 
     console.log("Incorrect!\n\n Your color is Alphabetically higher than mine"); 
     return false; 
    } else if(guessed_color < target_color) { 
     console.log("Incorrect\n\n Your color is lower than mine"); 
     return false; 
    } else { 
     console.log("You WIN!"); 
     guessed_color = null; 
     target_color = null; 
     return true; 
    } 
} 

と同じように、あなたはそれをチェックし、色を設定していますが、trueを返すときにそれをリセットせず、ゲームが終了し、色が設定されていますそれは同じ色になります。

0

ありがとうございますが、私の問題はこれを適用して解決しました。

color = color.sort();

関連する問題