2015-11-06 34 views
0

我是一個新手程序員,我一直在使用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

謝謝你的擡頭。 –

回答

1

在一個字符串的第一個字符是的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

0

我知道這是不是真的對這個話題,但我不得不這樣做同樣的任務,發現代碼中的真正有用的例子。我有點修改它,建議你的東西,那就是在我看來,(這是沒有必要的是更好的選擇,只是我的觀點)

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; 
    } 

我希望不是,我幫助。

相關問題