西蒙游戏机制(Javascript) -- 序列输入

Simon game mechanism (Javascript) -- Sequence input

基本上我有以下内容:

1) 游戏会随机预选20个随机序列

2) 游戏会关闭点击事件监听

3) 游戏将显示每个关卡的灯光序列(起始关卡 1)

4) 游戏会开启点击事件监听,让用户可以通过.buttons输入

5) 游戏会评估用户的输入

我在第 5 步遇到问题...

如何在游戏还在的时候评估用户的输入运行?我考虑过使用 while 循环,但似乎效果不佳...

Board.prototype.getUserInput = function(){
    board.userInput = []; //reset the user input
    $(".button").click(function(){
       var currentLevel = board.sequence.slice(0,board.index);
       var color = $(this).attr('id');
       board.userInput.push(color)
       board.evaluateInput(currentLevel);
})

}

这是我的代码笔:http://codepen.io/neotriz/pen/RRxOJb

这是我对游戏所做的旧实现。 http://codepen.io/antoniskamamis/pen/pJAxq?editors=0010#0

基本上,因为您已经预定义了顺序,所以您所要做的就是在每次单击颜色时增加一个计数器,并检查单击的颜色是否是预定义颜色中第 n 个位置的颜色

解决方案的伪代码

var predefinedColors = ['red', 'yellow',...]
var turn = 0;
buttons.addEventListener("click", ev => {
  if(ev.target.classList.contain(predefinedColors[turn]){
    //right guess
    turn++
  } else {
   // wrong guess reset game
  }

})