這是一個我正在嘗試練習JavaScript的小遊戲,基本上玩家會選擇一個四色代碼,並且計算機有十轉來猜測它。我設置了兩個循環,一個貫穿玩家的四個顏色代碼,另一個運行通過計算機的代碼(無論猜測如何)。在循環內部是檢查,如果計算機的猜測與玩家放置的東西相匹配,我希望該值可以保存,否則計算機隨機選擇不同的顏色,我們繼續以這種方式進行10回合或直到電腦玩家猜測整個碼。我遇到的問題是,如果計算機的猜測與玩家的信息不匹配進入下一回合。所以如果計算機猜出第一個顏色是紫色的,第一個顏色是紫色的,那麼接下來的第一個猜測應該以紫色開始。希望這是有道理的。我懷疑這樣做有更好的方法,但我是一個新手。這是我正在做的事情的一個鏈接。我理解對於循環正確嗎?我錯過了什麼? - JavaScript
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();
});
/
您不想在循環構造函數中使用'c!= 4',而想使用關係運算符:=,<, >,<=, and > =,例如:'c <4'。這樣,您將能夠包含0 - > 3(0,1,2,3)但不是4的所有實例。 – wkcamp
您在哪裏保存計算機的猜測?你的computerGuess數組在哪裏聲明?一些代碼似乎缺失。 – wmock
我的數組包含在我的JS文件的頂部。至於保存計算機的猜測,我已經創建了另一個數組,這個數組也是在頂部聲明的,但還沒有使用它。我將編輯我正在使用的整個JS的原始帖子。 – terratunaz