简体   繁体   中英

Guess a number (rigged) - Javascript

everyone. I am making a little experiment with Javascript. I would like to make a guessing game, where the user guesses a number from 0-100 and the computer tells him/her "too low", "too high", or "correct." However, I want this to be rigged . That is, the computer should choose the longest path possible for the user to get the answer right. For example:

  1. I guess 10. Since there are approximately ninety numbers more than 10 but only 10 numbers less than 10, the computer would say "My number is more than that" (in order to prolong the game).
  2. I then guess 80. The computer should say "My number is less than that."
  3. So on, until there is only one possibility left, and the computer is forced to say I got the right answer.

Right now, I just randomly say "too low" or "too high" which is problematic. For example:

  1. I say 50. The computer says "My number is more than that"
  2. I say 25 (even though I know the number is more than 50). The computer says "My number is less than that" which implies that it is cheating.

How can I avoid this? The computer should prolong the game but not be illogical.

My ideas:

  • Create a minValue and maxValue variable which keep track of the current bounds of the guess. Unfortunately, I'm not sure how to implement this well.
  • Create an array with all the possible values that the computer could be thinking of. Every time the user guesses, remove the values that are no longer possible. Repeat until there is only one element left in the array.

The ideas are basically the same. However, I'm stuck on implementing them. Thank you in advance.

I would like some steps (or code) on how to implement these ideas in Javascript.

Jsfiddle (although it may not help much, there is little in it): http://jsfiddle.net/prankol57/f4JWw/

Yeah, just use two bounding variables.

maxNum= 100
minNum = 0

Example: user guesses 51
Result: computer says the number is less than 51, and sets maxNum = 50

if (guess > maxNum)
too high

else if (guess < minNum)
too low

else if (guess > ((maxNum+minNum)/2)) the computer would say the actual number is below the guess

else if (guess < ((maxNum+minNum)/2)) the computer would say the actual number is above the guess

//should only happen if the number is exactly the mean(maxNum,minNum)
else
i guess just default to the computer saying that the actual number is above the guess. it doesn't really matter either way

anyway, there's some pseudo-code that I think answers your question. In general though, I really recommend banging your head against the wall on problems like this. Understanding the logic behind an implementation is the only way to be good at what you do.

Created using @indiscrete pseudo code. :)

JSFiddle Example Here

var min = 0;
var max = 100;

$("#guessForm").submit(function (e) {
    var guess = parseInt($("#guess").val()) || 0;
    var hint = checkGuess(guess);
    $("<p>").addClass("guessRecord").html(hint).appendTo("#guessRecords");
    $("#guess").val("");
    e.preventDefault();
});

function checkGuess(guess) {
    var mid = max - (Math.abs(max - min) / 2);
    if (guess > mid) {
        if (guess <= max) max = guess;
        return "My number is less than " + guess + ".";
    } else if (guess < mid) {
        if (guess >= min) min = guess;
        return "My number is greater than " + guess + ".";
    } else {
        return "Correct";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM