色を推測するためのコードを書きました。しかし問題は、このコードを実行するたびに、ランダムな色を選択するのではなく、同じ色を選択することです。誰かが私を助けてくれるのだろうかと思っています。私はとても感謝しています!色を推測するゲームの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>
私は助けてください:) – Malka
それはうまくいきます.... – Rayon
盗作は自由ですが、少なくとも著者を変更してください:) ' ' – Wajeb