简体   繁体   English

JavaScript中的Simon Game

[英]Simon Game in javascript

this is a javascript problem. 这是一个JavaScript问题。 So this is what I did. 这就是我所做的。 I created 2 arrays. 我创建了2个数组。 1 for the computer and one for the player. 1个用于计算机,一个用于播放器。 In the computerTurn function The computer creates a random number and places it into an array. 在computerTurn函数中计算机将创建一个随机数并将其放入数组中。 It will output the number from the array which will play a button. 它将从数组中输出一个将播放按钮的数字。 If the counter equals the length of the array then it will switch to the players turn. 如果计数器等于数组的长度,则它将切换到玩家回合。 In the playerTurn function the player has 5 seconds to follow the buttons that the computer chose. 在playerTurn功能中,播放器有5秒钟的时间可以跟随计算机选择的按钮。 The problem occurs after the second round when there are 2 random elements in the array. 当数组中有2个随机元素时,在第二轮之后会出现问题。 For some reason it doesn't clear my playerArray and just adds up numbers to it. 由于某种原因,它不会清除我的playerArray,而只是将数字加起来。 What am I doing wrong? 我究竟做错了什么?

The project can be found here: https://codepen.io/roger1891/full/vmYqwx/ 该项目可以在这里找到: https : //codepen.io/roger1891/full/vmYqwx/

I assume the problem lies here: 我认为问题出在这里:

var playerTurn = function() {
    if(currentPlayerTurn == "human" && onGoingGame == false) {
    playerArray = [];
    $(".my-btn").click(function(){
       var $input = $(this);
       var attrString = $input.attr("id");
       //only get number from id attribute
       var strNum = attrString.replace( /^\D+/g, '');
       //convert theNumber from string to number
       var theNum = parseInt(strNum);
       playerArray.push(theNum);
       console.log("this is the num the player picked " + theNum);
       console.log(playerArray);
    });  


    setTimeout(function () {
         var is_same = playerArray.length == sequenceArray.length && playerArray.every(function(element, index) {
          return element === sequenceArray[index]; 
        });
        is_same;
        console.log(is_same);
        if(is_same == true) {
          onGoingGame = true;
          currentPlayerTurn = "computer";
          computerTurn();

        }   

    }, 5000);

    }  
} 
    setTimeout(function () {
             var is_same = playerArray.length == sequenceArray.length && playerArray.every(function(element, index) {
              return element === sequenceArray[index]; 
            });
            is_same;
            console.log(is_same);
            if(is_same == true) {
              onGoingGame = true;
              currentPlayerTurn = "computer";
              computerTurn();

            }   

        }, 5000);

        }  
      } 

The main issue is that you should not define the click handler within a function that gets called several times ( playerTurn ), since that will accumulate the click handlers that will be executed at a single click. 主要问题在于,您不应在多次调用的函数( playerTurn )中定义单击处理程序,因为这会积累单击后执行的单击处理程序。 As a consequence there is a growing increase in the length of playerArray . 结果, playerArray的长度越来越多。 Instead define the click handler outside that function, and repeat the condition in that click handler like you have in playerTurn : 而是在该函数之外定义单击处理程序,并像在playerTurn中一样在该单击处理程序中重复条件:

$(".my-btn").click(function(){
    if(currentPlayerTurn != "human" || onGoingGame) return; // exit
    // rest of the click handler...
});  

var playerTurn = function() {
    if(currentPlayerTurn != "human" || onGoingGame) return; // exit
    playerArray = [];
    setTimeout(function () {
        // etc....
    }, 5000);
}

// ... etc

NB: The variable onGoingGame seems unnecessary, as it always corresponds to the expression currentPlayerTurn == "computer" 注意:变量onGoingGame似乎是不必要的,因为它始终与表达式currentPlayerTurn == "computer"相对应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM