JS代码猜测游戏颜色不工作

时间:2017-04-01 15:34:03

标签: javascript html5 function

我是一个新手JS,我正在制作一个游戏,用户必须猜测随机生成的颜色。

我注意到我尝试编写的代码会导致错误,也就是说,当我在提示符中输入颜色时,即使它出错也总是说正确的事情。我在check_guess函数中遇到了错误,我写了条件。

我在游戏结束时发现的另一个错误应该是在背景中出现猜测它的颜色,也不应该。

你能帮我弄清楚我错在哪里吗?

while (!finished) {
            guess_input_text = prompt("I am thinking of one of these colors:\n\n" +
              colors_message + "\n\n What is the color am I thinking of?");
            guess_input = guess_input_text.toLowerCase();
            guesses += 1;
            finished = check_guess();
          }
        }

        function check_guess() {
          if (guess_input == -1) {
            alert("Sorry, I don't recognize your color. \n\n Please try again.");
            return false;
          } else if (guess_input > target) {
            alert("Sorry, your guess is not correct!\n\nHint: Your color is alphabetically higher than mine.\n\nPlease try again.");
            return false;
          } else if (guess_input < target) {
            alert("Sorry, your guess is not correct!\n\nHint: Your color is alphabetically lower than mine.\n\nPlease try again.");
            return false;
          } else {
            alert("Congratulations! You have guessed the color!\n\nIt took you " + guesses + " guesses to finish the game!\n\nYou can see the colour in the background.");
            var myBody = document.getElementsByTagName("body")[0];
            myBody.style.background = target;

            return true;
          }
        }
      </script>

1 个答案:

答案 0 :(得分:0)

参考您的代码修订版,我获得了所需的数据。发现的故障几乎没有:

  1. 您正在使用guess_input检查target,其中guess_input包含用户输入的字符串值,target包含正确答案的整数值。在这种情况下,不会满足任何条件,因此最后else部分将始终执行。也就是alert("Congratulations! You ha..);
  2. myBody.style.background = target;将设置background 属性为整数(colors数组中的答案索引)。你需要 将其设置为myBody.style.background = colors[target];
  3. 以下是工作代码:

    &#13;
    &#13;
    var colors = ["antiquewhite", "blueviolet", "crimson", "deepskyblue", "forestgreen", "gold", "lawngreen", "magenta", "palegreen", "skyblue"];
    var colors_message = colors.join(", ");
    var target;
    var guess_input_text;
    var guess_input;
    var finished = false;
    var guesses = 0;
    var ask_again = false;
    
    
    function do_game() {
    	var target_index = Math.random() * colors.length;
    	var target_index_integer = Math.floor(target_index);
    	target = target_index_integer;
    
    	var answer = String(colors[target]).toLowerCase();
    	//Alert correct answer for testing purposes
    	alert("The correct answer is: " + answer);
    
    
        guess_input_text = prompt("I am thinking of one of these colors:\n\n" + colors_message + "\n\n What is the color am I thinking of?");
        while (!finished) {
            if(ask_again){
              	guess_input_text = prompt("What is the color I was thinking of?");
              	}
                guess_input = guess_input_text.toLowerCase();
                guesses += 1;
                finished = check_guess(colors.indexOf(guess_input));
            }
        }
    
        function check_guess(guess_index) {
            ask_again = true;
            if (guess_index == -1) {
                alert("Sorry, I don't recognize your color. \n\n Please try again.");
                return false;
            } else if (guess_index > target) {
                alert("Sorry, your guess is not correct!\n\nHint: Your color is alphabetically higher than mine.\n\nPlease try again.");
                return false;
            } else if (guess_index < target) {
                alert("Sorry, your guess is not correct!\n\nHint: Your color is alphabetically lower than mine.\n\nPlease try again.");
                return false;
            } else {
              	ask_again = true;
                alert("Congratulations! You have guessed the color!\n\nIt took you " + guesses + " guesses to finish the game!\n\nYou can see the colour in the background.");
                var myBody = document.getElementsByTagName("body")[0];
                myBody.style.background = colors[target];
                return true;
            }
         }
    &#13;
    <body onload="do_game()"></body>
    &#13;
    &#13;
    &#13;