Jump to content

adding conditions


pinochio

Recommended Posts

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.