[英]Am I understanding For Loops correctly? What am I missing? - JavaScript
這是我嘗試嘗試使用JavaScript的一款小游戲,基本上,玩家選擇了四個顏色代碼,並且計算機有十個回合來猜測它。 我設置了兩個for循環,一個循環播放播放器的四個顏色代碼,另一個循環播放通過計算機的代碼(無論它產生什么猜測)。 在循環中進行檢查,如果計算機的猜測值與播放器放置的內容相匹配,則我希望保存該值,然后計算機隨機選擇其他顏色,我們以這種方式繼續進行10轉,直到計算機播放器猜測整個顏色碼。 我遇到的問題是,如果計算機的猜測與玩家的猜測相符,則該信息不會在下一回合繼續進行。 因此,如果計算機猜測第一種顏色為紫色,而第一種顏色為紫色,則下一輪的第一個猜測應以紫色開始。 希望這是有道理的。 我懷疑這樣做有更好的方法,但我是新手。 這是我正在做的事情的鏈接。
http://codepen.io/terratunaz/pen/bNBoVY
var colorSelection; //the current selected color
var clickCount = 0;
var playerCode = []; //holds player's code
var computerGuess = [];
var numColorValue = 0;
var computerSaveGuess = [];
//Fancy yet simple code to make the selectable colors respond to the user's actions
$(document).ready(function() {
$("div.codeOption").mouseenter(function() {
$(this).animate({
opacity: "0.1"
}, "slow");
});
$("div.codeOption").mouseleave(function() {
$(this).animate({
opacity: "1"
}, "fast");
});
//Have the player choose a 4 color code amongst the 6 possible choices on screen and store that information
$("div.codeOption").click(function() {
if (clickCount < 4) {
colorSelection = $(this).attr("id");
playerCode[clickCount] = colorSelection;
clickCount++
}
if (playerCode.length === 4) {
$("div.codeOption, #inGameInstructions").css("display", "none");
$("#hackCode").css("display", "block");
$("#playersFinalCode").append("<p id='furtherInstructions'>This is the code you have selected. Press the [HackCode] button to continue, or use the button at the top right corner to go back to the main menu to start over from the beginning.</p>");
for (i = 0; i < playerCode.length; i++) {
if (playerCode[i] === "red") {
$("#playersFinalCode").append("<div class= 'playerCodeChoice'><div class='" + playerCode[i] + "'></div></div");
} else if (playerCode[i] === "green") {
$("#playersFinalCode").append("<div class= 'playerCodeChoice'><div class='" + playerCode[i] + "'></div></div");
} else if (playerCode[i] === "orange") {
$("#playersFinalCode").append("<div class= 'playerCodeChoice'><div class='" + playerCode[i] + "'></div></div");
} else if (playerCode[i] === "yellow") {
$("#playersFinalCode").append("<div class= 'playerCodeChoice'><div class='" + playerCode[i] + "'></div></div");
} else if (playerCode[i] === "blue") {
$("#playersFinalCode").append("<div class= 'playerCodeChoice'><div class='" + playerCode[i] + "'></div></div>");
} else {
$("#playersFinalCode").append("<div class= 'playerCodeChoice'><div class='" + playerCode[i] + "'></div></div>");
}
}
}
//
//
});
function HackCode() {
$("#hackCode").click(function() {
$("#hackCode").css("display", "none");
$(".playerCodeChoice, #furtherInstructions").css("display", "none");
//Computer logic for guessing
var computerTurn = 10;
while (computerTurn > 0)
{
/*
for(i=0;i<playerCode.length; i++){
if(playerCode[i].length===5){
//red
}
else if(playerCode[i].length===5){
//green
}
else if(playerCode[i].length ===4){
//blue
}
else
{ if(playerCode[i].length ===6 && playerCode[i][0] === "o" ){
//orange
}
else if(playerCode[i].length ===6 && playerCode[i][0] === "p" ){
//purple
}
else{//yellow
}
}
computerGuess.push(playerCode[i]);
}*/
for (c = 0; c != 4; c++) {
for (i = 0; i != 4; i++) {
//if(computerGuess[c] === playerCode[i])
//
// numColorValue = Math.floor((Math.random() * 6) + 1);
if (computerGuess[c] === playerCode[i]) {
computerGuess[c] = playerCode[i];
} else {
numColorValue = Math.floor((Math.random() * 6) + 1);
if (numColorValue === 1) {
computerGuess[c] = ("red");
} else if (numColorValue === 2) {
computerGuess[c] = ("green");
} else if (numColorValue === 3) {
computerGuess[c] = ("orange");
} else if (numColorValue === 4) {
computerGuess[c] = ("yellow");
} else if (numColorValue === 5) {
computerGuess[c] = ("blue");
} else {
computerGuess[c] = ("purple");
}
}
}
}
$("#guessList").append("<li class='list'>" + computerGuess + "</li>");
computerTurn--;
}
});
}
HackCode();
//Clears whatever is on the screen and displays the main menu when the player clicks on the button to go there
function hideToMainMenu() {
$("#toMainMenu").click(function() {
$(".codeOption, #toMainMenu, #inGameInstructions,#helpMenuInstructions, #hackCode,.playerCodeChoice, #furtherInstructions, .list").css("display", "none");
$("#mainTitle, #playGame, #helpMenu").css("display", "block");
playerCode.length = 0;
computerGuess.length = 0;
clickCount = 0;
});
}
hideToMainMenu();
//Clears whatever is on the screen and displays the actual beginning of the game when the player clicks on the button to go there
function playGame() {
$("#playGame").click(function() {
$("#mainTitle, #playGame, #helpMenu, #helpMenuInstructions").css("display", "none");
$("#toMainMenu").css({
"margin-top": "-5px",
"float": "right"
});
$(".codeOption, #toMainMenu, #inGameInstructions").css("display", "block");
});
}
playGame();
//Clears whatever is on the screen and displays the main menu when the player clicks on the button to go there
function hideHelpMenu() {
$("#helpMenu").click(function() {
$("#mainTitle,#helpMenu, #inGameInstructions").css("display", "none");
$("#toMainMenu").css({
"margin-top": "5px",
"float": "none"
});
$("#helpMenuInstructions, #toMainMenu").css("display", "block");
});
}
hideHelpMenu();
});
/
我覺得您的邏輯需要改進。 該游戲僅需要一個for-loop,因為您只需要檢查第一個是否匹配第一,第二個是否匹配第二,等等。您當前的問題是它會檢查計算機的選擇是否與所有玩家的選擇相匹配,並且只會被保留如果與最后一個匹配。
您也可以將正確的猜測保存在另一個布爾數組中。 然后在for循環開始時檢查是否已經被猜到。
如果您確實希望計算機能夠猜測第一個=紅色,並將其與玩家的第二個=紅色匹配,則應該添加一個中斷; 循環中的語句停止它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.