2017-04-01 57 views
0

我是一个新手JS,我正在做一个游戏,用户必须猜测随机生成的颜色。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

我们不能调试整个程序为您服务。必须选择一个问题并编辑您的问题或重新发布只针对该问题每个stackoverflow规则。如果你不解决,他们可能会结束这个问题。 –

+0

@AuroraRuggieri您需要为我们提供更多数据,比如'colors_message','guesses','target','finished'等等。 –

+0

@AuroraRuggieri我收到了我向你询问的数据,并在下面的Q中得到了答案。接受它,如果你发现它是正确的,并关闭此Q –

回答

0

参考你的代码修改,我得到了所需的数据。有发现这样的一些小问题:

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

下面是工作代码:

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() { 
 
\t var target_index = Math.random() * colors.length; 
 
\t var target_index_integer = Math.floor(target_index); 
 
\t target = target_index_integer; 
 

 
\t var answer = String(colors[target]).toLowerCase(); 
 
\t //Alert correct answer for testing purposes 
 
\t 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){ 
 
      \t guess_input_text = prompt("What is the color I was thinking of?"); 
 
      \t } 
 
      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 { 
 
      \t 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; 
 
     } 
 
    }
<body onload="do_game()"></body>

+1

我感谢你的澄清,这有助于了解我出了什么问题,并成长,再次感谢。 –

+0

@AuroraRuggieri如果你的问题解决了,你现在可以关闭这个问题 –