JavaScript bug,战舰游戏

时间:2014-12-28 16:30:20

标签: javascript html

我最近开始学习JS,我做了一个非常简单的游戏。这是一款简单的战舰游戏,没有动画。这是代码:

<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="CSS/Game.css" type="text/css" charset="utf-8">

</head>

<body>

    <marquee id="output"></marquee>

    <script>
        var loc1,loc2,loc3;
        var guess;
        var hits = 0;
        var guesses = 0;
        var isSunk = false;
        var locRandom = Math.floor(Math.random() * 9);

        loc1 = locRandom;
        loc2 = loc1 + 1;
        loc3 = loc2 + 1;



    while(isSunk == false){

            guess = prompt("Enter a number from 1-10");

        if(guess == null){
                alert("Bye");
            promt.close;
        }

       else if(guess < 0 || guess > 10 || guess == "" || guess == " "){


                alert("Write a valid number");
            }
            else
            {
            guesses = guesses + 1;  

            if(guess == loc1 || guess == loc2 || guess == loc3) {
                alert("HIT!");    
                hits = hits + 1;

                    if(hits == 3){
                        isSunk = true;
                        alert("You sank my battleship!");
                    }
                } else {
                    alert("MISS");
                }
            }

        }
    var stats = "You took " + guesses  + " guesses to sink the battleship, " + "which means that  your accuracy is " + (3/guesses) + " hit(s) per guess";
        alert(stats);
        alert("Thanks for playing!");
        document.getElementById("output").innerHTML = "Refresh to play again!";
        document.write("<br><br>This game was created by Swegre Designs");
    </script>





</body>

但问题是,当我在数字1上获得“HIT”时,我可以在预告中输入3次并且战舰沉没,但这不是应该发生的事情,我希望你理解这个虫子,有人能帮助我吗?

谢谢!

4 个答案:

答案 0 :(得分:2)

您没有跟踪用户之前的任何猜测。

您可以使用数组跟踪猜测的位置。例如:

var guessedPositions = [];

然后,当您收到用户的输入时,将他们猜测的位置推送到数组中:

guessedPositions.push(guess);

然后,您必须将guessedPositions与用户guess进行比较,您可以为此创建一个函数:

function alreadyGuessed(userGuess) {
   for(var i=0; i<guessedPositions.length;i++) {
      if (guessedPositions[i] == userGuess) {
          return true;
      }
   }
   return false;
}

您可以通过以下方式调用此方法:alreadyGuessed(guess);

如果该条目在数组中,则返回true或false。

编辑 - 来自您的评论

while(isSunk == false){

 guess = prompt("Enter a number from 1-10");

  if (guess == null){
     alert("Bye");
     promt.close;
  }

  else if(guess < 0 || guess > 10 || guess == "" || guess == " "){
      alert("Write a valid number");
  }
  else if (alreadyGuessed(guess)) { // This is the line you are looking for.
     alert("You have already guessed " + guess + ", please enter a different location");
  }
  else
  {

答案 1 :(得分:1)

不是真正的错误 - 这就是正在发生的事情

 if(guess == loc1 || guess == loc2 || guess == loc3)

没有考虑他们是否已经猜到了这个位置。我会跟踪他们已经在阵列中猜到的位置,然后使用它来检查它是否是一个新的猜测。或者,您可以将所有“可命中”位置放在一个数组中,然后当他们猜出一个正确的位置时,将其从数组中删除。

答案 2 :(得分:0)

只要记住他们击中的三个位置中的哪一个,并且不要让他们随后尝试击中该位置。

答案 3 :(得分:0)

我猜你不能点击位置0?如果是这样,只要命中它就可以将所需的loc var设置为0.