2015-01-04 54 views
0

這是一個我正在嘗試練習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(); 


}); 

/
+0

您不想在循環構造函數中使用'c!= 4',而想使用關係運算符:=,<, >,<=, and > =,例如:'c <4'。這樣,您將能夠包含0 - > 3(0,1,2,3)但不是4的所有實例。 – wkcamp

+0

您在哪裏保存計算機的猜測?你的computerGuess數組在哪裏聲明?一些代碼似乎缺失。 – wmock

+0

我的數組包含在我的JS文件的頂部。至於保存計算機的猜測,我已經創建了另一個數組,這個數組也是在頂部聲明的,但還沒有使用它。我將編輯我正在使用的整個JS的原始帖子。 – terratunaz

回答

1

我覺得你的邏輯需要一些改進。這個遊戲應該只需要一個for-loop,因爲你只需要檢查第一個匹配第一個,第二個匹配第二個等。你現在的問題是它會去檢查計算機的選擇是否匹配所有玩家的選擇,並且只會被結轉如果它匹配最後一個。

你也可以將正確的猜測保存在另一個布爾數組中。然後在for循環檢查開始時檢查它是否已被猜測。如果你確實希望計算機能夠猜出第一個=紅色,並且將其與玩家的第二個=紅色相匹配,那麼你應該添加一箇中斷;語句在循環內停止它。

+0

原諒我,但你究竟是什麼意思的布爾數組。一個真/假的數組裏面?我將嘗試使用開關/外殼/中斷。謝謝 – terratunaz

+0

是的。那麼你可以有這樣的東西(僞代碼): – juunas

+0

對不起,意外進入按下那裏。這裏是代碼 'guesses = [紅色,紅色,紅色,紅色]; player = [green,yellow,red,blue]; 左轉{ rightGuesses = 0; for i = 0 to 3 { if guess [i]!= player [i] { guesses [i] = new random color; } else { rightGuesses ++; } if rightGuesses == 4 { computer has won; } }' – juunas