pinochio Posted October 14, 2009 Share Posted October 14, 2009 Hi guys, I need to add a "colder/hotter" ( children game when you get closer = it is hotter and etc) condition into a guess number game. Any ideas how can I add this condition? Thanks a lot ! <HTML> <HEAD> <TITLE>The number guessing game</TITLE> </HEAD> <BODY> <SCRIPT language = "javascript" type = "text/javascript"> //Define script variables var randomNo = 0; var gameOver = false; var keepPlaying = false; var guess = ""; var msg = "I am thinking of a number from 1 - 100. Try to guess it:" var reply = ""; //This loop allows the player to play as many games as desired while (gameOver == false) { randomNo = 1 + Math.random() * 99; //Generate a random number //from 1 - 100 randomNo = Math.round(randomNo); //Turn the number into a integer keepPlaying = false; //Set value to indicate that the current //round of play should continue //Loop until the player guess the secret number while (keepPlaying == false) { //Prompt the player to guess the secret number guess = window.prompt(msg); //Analyze the players guess if (guess == randomNo) { //See if the player guessed the number window.alert("Correct! You guessed the number."); reply = window.prompt("Would you like to play again? (y/n)"); if (reply == "y") { //See if the player wants to play again keepPlaying = true; gameOver = false; } else { //Let the current round of play continue keepPlaying = true; gameOver = true; } } else { if (guess > randomNo) { //See if the player's guess is too high window.alert("Incorrect. You guess was too high."); } else { //See if the player's guess is too low window.alert("Incorrect. Your guess was too low."); } } } } window.alert("Game over. Thanks for playing."); //Thank the player </SCRIPT> </BODY> </HTML> Quote Link to comment Share on other sites More sharing options...
tharford Posted October 15, 2009 Share Posted October 15, 2009 As well as the variable guess, create another variable previousGuess. Just before you ask the user for their next guess assign guess to previousGuess, then when they have made the new guess compare the difference between their new guess and the target number with the difference between their previous guess and the target number. Something like: previousGuess=guess; guess = window.prompt(msg); var newDiff = randomNo-guess; var previousDiff=randomNo-previousGuess; if(newDiff < previousDiff){ alert("warmer"); } else{ alert("getting cooler"); } But then you need to put in another check to see if the guesses are higher or lower than the target Quote Link to comment Share on other sites More sharing options...
pinochio Posted October 15, 2009 Author Share Posted October 15, 2009 I added everything but it is still does not work.... <BODY> <SCRIPT language = "javascript" type = "text/javascript"> //Define script variables var randomNo = 0; var gameOver = false; var keepPlaying = false; var guess = ""; var msg = "I am thinking of a number from 1 - 100. Try to guess it:" var reply = ""; var previousGuess=guess; var newDiff = randomNo-guess; var previousDiff=randomNo-previousGuess; //This loop allows the player to play as many games as desired while (gameOver == false) { randomNo = 1 + Math.random() * 99; //Generate a random number //from 1 - 100 randomNo = Math.round(randomNo); //Turn the number into a integer keepPlaying = false; //Set value to indicate that the current //round of play should continue //Loop until the player guess the secret number while (keepPlaying == false) { //Prompt the player to guess the secret number guess = window.prompt(msg); //Analyze the players guess if (guess == randomNo) { //See if the player guessed the number window.alert("Correct! You guessed the number."); reply = window.prompt("Would you like to play again? (y/n)"); if (reply == "y") { //See if the player wants to play again keepPlaying = true; gameOver = false; } else { //Let the current round of play continue keepPlaying = true; gameOver = true; } } else { if (guess > randomNo) { //See if the player's guess is too high window.alert("Incorrect. You guess was too high."); } else { //See if the player's guess is too low window.alert("Incorrect. Your guess was too low."); } else { if( newDiff < previousDiff){ alert("warmer"); } else { alert("cooler"); } } } } window.alert("Game over. Thanks for playing."); //Thank the player </SCRIPT> </BODY> </HTML> Quote Link to comment Share on other sites More sharing options...
haku Posted October 16, 2009 Share Posted October 16, 2009 Because you have this at the top of your code. var guess = ""; var previousGuess=guess; Previous guess will always be equal to a blank value. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.