我在使用JavaScript编程我的公牛和牛游戏时遇到了麻烦

问题描述:

我是一个新手程序员,我一直在使用JavaScript来制作一个公牛和奶牛的简单游戏,用公牛和牛的数量进行更新每个猜测都有一段。我一直使用“'1234'”代替“arr.join(”“)”来测试它,它几乎在那里,但它默认添加一个公牛,而不考虑数字的第一个数字正在提交。当我提交正确的号码时,一切都很顺利。我只是想知道为什么我似乎没有得到适量的公牛和牛。我在使用JavaScript编程我的公牛和牛游戏时遇到了麻烦

// This is our click counter that tells you how many times you've guessed in the game. 
 

 
var clicks = 0; 
 
$("#submit").click(function() { 
 
    clicks++; 
 
    $("#guesses").html(clicks); 
 
}); 
 

 
// Generates a non-repeating, random number and stores it in a global variable known as "secret". 
 

 
$("#start").click(function() { 
 
    var arr = []; 
 
    while (arr.length < 4) { 
 
    var randomnumber = Math.ceil(Math.random() * 9); 
 
    var found = false; 
 
    for (var i = 0; i < arr.length; i++) { 
 
     if (arr[i] === randomnumber) { 
 
     found = true; 
 
     break; 
 
     } 
 
    } 
 
    if (!found) arr[arr.length] = randomnumber; 
 
    } 
 
    secret = "1234"; 
 
}); 
 

 
// When you click the "Hide Rules" button, it hides the Rules form and shows the "Show Rules" button. 
 

 
$("#hide").click(function() { 
 
    $("#rulesform").hide(); 
 
    $("#show").show(); 
 
    $("#hide").hide(); 
 
}); 
 

 
// When you click the "Show Rules" button, it shows the rules form and the "Hide Rules" button and hides the "Show Rules" button. 
 

 
$("#show").click(function() { 
 
    $("#rulesform").show(); 
 
    $("#show").hide(); 
 
    $("#hide").show(); 
 
}); 
 

 
// When you click "Start playing!", it hides the button, shows the game, sets guesses equal to 0, and generates the number for the game. 
 

 
$("#start").click(function() { 
 
    $("#game").show(); 
 
    $("#start").hide(); 
 
    $("#submit").show(); 
 
    $("#guesses").html("0"); 
 
}); 
 

 
// When you hit the "Stop playing." button, it clears the results of the game and guess input box, in addition to hiding the game functions and showing the "Start playing!" button. 
 

 
$("#stop").click(function() { 
 
    $("#game").hide(); 
 
    $("#start").show(); 
 
    $("#results").html(""); 
 
    $("#guess").val(""); 
 
    clicks = 0; 
 
}); 
 

 
function game() { 
 

 
    // Stores your guess in a variable 
 

 
    var guess = $("#guess").val(); 
 

 
    // Makes sure the number is 4 digits 
 

 
    if (guess.length != 4) { 
 
    alert("This number is too long or short to be valid."); 
 
    } 
 

 
    // Makes sure the numbers are non-repeating if they're 4 digits. 
 
    else if (guess.charAt(1) === guess.charAt(2) || guess.charAt(1) === guess.charAt(3) || guess.charAt(1) === guess.charAt(4) || guess.charAt(2) === guess.charAt(3) || guess.charAt(2) === guess.charAt(4) || guess.charAt(3) === guess.charAt(4)) { 
 
    alert("This game doesn't have any repeating digits."); 
 
    } 
 

 
    // This is the actual game. 
 
    else { 
 

 
    // These two variables will be updated with each guess the user inputs. 
 

 
    var bulls = 0; 
 
    var cows = 0; 
 

 
    // This is where JavaScript checks the bulls and cows and adds them up accordingly. 
 

 
    if (guess !== secret) { 
 
     if (guess.charAt(1) === secret.charAt(1)) { 
 
     bulls += 1; 
 
     } else if (guess.charAt(1) === secret.charAt(2) || guess.charAt(1) === secret.charAt(3) || guess.charAt(1) === secret.charAt(4)) { 
 
     cows += 1; 
 
     } 
 
     if (guess.charAt(2) === secret.charAt(2)) { 
 
     bulls += 1; 
 
     } else if (guess.charAt(2) === secret.charAt(1) || guess.charAt(2) === secret.charAt(3) || guess.charAt(2) === secret.charAt(4)) { 
 
     cows += 1; 
 
     } 
 
     if (guess.charAt(3) === secret.charAt(3)) { 
 
     bulls += 1; 
 
     } else if (guess.charAt(3) === secret.charAt(1) || guess.charAt(3) === secret.charAt(2) || guess.charAt(3) === secret.charAt(4)) { 
 
     cows += 1; 
 
     } 
 
     if (guess.charAt(4) === secret.charAt(4)) { 
 
     bulls += 1; 
 
     } else if (guess.charAt(4) === secret.charAt(1) || guess.charAt(4) === secret.charAt(2) || guess.charAt(4) === secret.charAt(3)) { 
 
     cows += 1; 
 
     } 
 
     $("#results").html("Bulls: " + bulls + " & Cows: " + cows + ""); 
 
    } else if (guess === secret) { 
 
     $("#results").html("<strong>Congrats, you won! <a href='images/cookie.gif'>Here's a picture</a> of a cookie.</strong>"); 
 
     $("#submit").hide(); 
 
    } 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="wrapper"> 
 
    <h1>Want to play a game?</h1> 
 
    <div id="rules"> 
 
    <form id="rulesform"> 
 
     <h2>Rules:</h2> 
 
     <p>The computer will generate a random 4-digit number without any of them repeating. You will do your best to guess the number in the input field provided. If a digit you entered is not used, you will not get any bulls or cows. If it's in the correct 
 
     spot, then you'll get 1 bull. If it's in the wrong spot, it will be a cow. The game ends when you guess the number. The amount of guesses it takes for you to figure it out will be provided.</p> 
 
    </form> 
 
    <input type="button" id="hide" value="Hide Rules"> 
 
    <input type="button" id="show" value="Show Rules"> 
 
    <br> 
 
    <input type="button" id="start" value="Start playing!"> 
 
    </div> 
 
    <!--closes rules--> 
 
    <div id="game"> 
 
    <input type="number" id="guess" placeholder="Your Guess"> 
 
    <input type="button" id="submit" value="Submit your guess." onclick="game()"> 
 
    <br> 
 
    <p id="guesstext">Guesses:</p> 
 
    <p id="guesses"></p> 
 
    <p id="results"></p> 
 
    <input type="button" id="stop" value="Stop playing."> 
 
    </div> 
 
    <!--closes game--> 
 
</div> 
 
<!--closes wrapper-->

+0

欢迎来到SO。请通过[JSFiddle](http://jsfiddle.net)分享您的代码。分享,编辑和讨论变得很容易。 – alix

+0

谢谢你的抬头。 –

在一个字符串的第一个字符是的charAt(0)在该字符串,无法的charAt(1)。减少你所有的charAt引用,看看是否有效。

下面是它应该是什么样子:

// This is our click counter that tells you how many times you've guessed in the game. 

var clicks = 0; 
$("#submit").click(function() { 
    clicks++; 
    $("#guesses").html(clicks); 
}); 

// Generates a non-repeating, random number and stores it in a global variable known as "secret". 

$("#start").click(function() { 
    var arr = []; 
    while (arr.length < 4) { 
    var randomnumber = Math.ceil(Math.random() * 9); 
    var found = false; 
    for (var i = 0; i < arr.length; i++) { 
     if (arr[i] === randomnumber) { 
     found = true; 
     break; 
     } 
    } 
    if (!found) arr[arr.length] = randomnumber; 
    } 
    window.secret = "1234"; 
}); 

// When you click the "Hide Rules" button, it hides the Rules form and shows the "Show Rules" button. 

$("#hide").click(function() { 
    $("#rulesform").hide(); 
    $("#show").show(); 
    $("#hide").hide(); 
}); 

// When you click the "Show Rules" button, it shows the rules form and the "Hide Rules" button and hides the "Show Rules" button. 

$("#show").click(function() { 
    $("#rulesform").show(); 
    $("#show").hide(); 
    $("#hide").show(); 
}); 

// When you click "Start playing!", it hides the button, shows the game, sets guesses equal to 0, and generates the number for the game. 

$("#start").click(function() { 
    $("#game").show(); 
    $("#start").hide(); 
    $("#submit").show(); 
    $("#guesses").html("0"); 
}); 

// When you hit the "Stop playing." button, it clears the results of the game and guess input box, in addition to hiding the game functions and showing the "Start playing!" button. 

$("#stop").click(function() { 
    $("#game").hide(); 
    $("#start").show(); 
    $("#results").html(""); 
    $("#guess").val(""); 
    clicks = 0; 
}); 

window.game = function() { 

    // Stores your guess in a variable 

    var guess = $("#guess").val(); 

    // Makes sure the number is 4 digits 

    if (guess.length != 4) { 
    alert("This number is too long or short to be valid."); 
    } 

    // Makes sure the numbers are non-repeating if they're 4 digits. 
    else if (guess.charAt(0) === guess.charAt(1) || guess.charAt(0) === guess.charAt(2) || guess.charAt(0) === guess.charAt(3) || guess.charAt(1) === guess.charAt(2) || guess.charAt(1) === guess.charAt(3) || guess.charAt(2) === guess.charAt(3)) { 
    alert("This game doesn't have any repeating digits."); 
    } 

    // This is the actual game. 
    else { 

    // These two variables will be updated with each guess the user inputs. 

    var bulls = 0; 
    var cows = 0; 

    // This is where JavaScript checks the bulls and cows and adds them up accordingly. 

    if (guess !== secret) { 
     if (guess.charAt(0) === secret.charAt(0)) { 
     bulls += 1; 
     } else if (guess.charAt(0) === secret.charAt(1) || guess.charAt(0) === secret.charAt(2) || guess.charAt(0) === secret.charAt(3)) { 
     cows += 1; 
     } 
     if (guess.charAt(1) === secret.charAt(1)) { 
     bulls += 1; 
     } else if (guess.charAt(1) === secret.charAt(0) || guess.charAt(1) === secret.charAt(2) || guess.charAt(1) === secret.charAt(3)) { 
     cows += 1; 
     } 
     if (guess.charAt(2) === secret.charAt(2)) { 
     bulls += 1; 
     } else if (guess.charAt(2) === secret.charAt(0) || guess.charAt(2) === secret.charAt(1) || guess.charAt(2) === secret.charAt(3)) { 
     cows += 1; 
     } 
     if (guess.charAt(3) === secret.charAt(3)) { 
     bulls += 1; 
     } else if (guess.charAt(3) === secret.charAt(0) || guess.charAt(3) === secret.charAt(1) || guess.charAt(3) === secret.charAt(2)) { 
     cows += 1; 
     } 
     $("#results").html("Bulls: " + bulls + " & Cows: " + cows + ""); 
    } else if (guess === secret) { 
     $("#results").html("<strong>Congrats, you won! <a href='images/cookie.gif'>Here's a picture</a> of a cookie.</strong>"); 
     $("#submit").hide(); 
    } 
    } 
} 
+0

这是我的问题。谢谢。 –

+0

很高兴帮助!如果这个或任何答案已解决您的问题,请考虑通过点击复选标记来接受它。这向更广泛的社区表明,您已经找到了解决方案,并为答复者和您自己提供了一些声誉。但是,没有义务这样做。快乐的编码! – TAGraves

我知道这是不是真的对这个话题,但我不得不这样做同样的任务,发现代码中的真正有用的例子。我有点修改它,建议你的东西,那就是在我看来,(这是没有必要的是更好的选择,只是我的观点)

for (var i = 0; i < length; i++) { 
    if(String(guess).charAt(i) == String(number).charAt(i)){ 
     bulls++; 
    }else{ 
     for (var j = 0; j < length; j++) { 
      if ((String(guess).charAt(i) == String(number).charAt(j)) && (i !== j)) { 
       cows++; 
      }; 
     }; 
    } 
}; 

哪里的猜测是你的猜测和数量是更好的选择生成的数字。 上面的代码可能会被放

 if (guess.charAt(0) === secret.charAt(0)) { 
    bulls += 1; 
    } else if (guess.charAt(0) === secret.charAt(1) || guess.charAt(0) === secret.charAt(2) || guess.charAt(0) === secret.charAt(3)) { 
    cows += 1; 
    } 
    if (guess.charAt(1) === secret.charAt(1)) { 
    bulls += 1; 
    } else if (guess.charAt(1) === secret.charAt(0) || guess.charAt(1) === secret.charAt(2) || guess.charAt(1) === secret.charAt(3)) { 
    cows += 1; 
    } 
    if (guess.charAt(2) === secret.charAt(2)) { 
    bulls += 1; 
    } else if (guess.charAt(2) === secret.charAt(0) || guess.charAt(2) === secret.charAt(1) || guess.charAt(2) === secret.charAt(3)) { 
    cows += 1; 
    } 
    if (guess.charAt(3) === secret.charAt(3)) { 
    bulls += 1; 
    } else if (guess.charAt(3) === secret.charAt(0) || guess.charAt(3) === secret.charAt(1) || guess.charAt(3) === secret.charAt(2)) { 
    cows += 1; 
    } 

我希望不是,我帮助。