我是一個新手程序員,我一直在使用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-->
歡迎來到SO。請通過[JSFiddle](http://jsfiddle.net)分享您的代碼。分享,編輯和討論變得很容易。 – alix
謝謝你的擡頭。 –