在对象可以更新之前发生的递归函数

时间:2015-12-30 19:18:11

标签: javascript tic-tac-toe

我正在构建一个tic tac toe游戏,其中有一个玩家正在玩电脑并且只允许最好的平局。我的游戏防御功能应该发现玩家有两个相邻的行col并且第三个是空的,它应该自动选择该空间。然后更新游戏板对象以显示计算机控制的内容和玩家控制的内容。然后增加移动次数,检查获胜者,如果没有获胜者改变玩家转动。虽然单步执行代码,计算机首先播放就好了但是一旦玩家在游戏板更新之前选择了他们的方块,计算机已经选择了下一个选择。所以它不适应玩家并输掉。我不知道我已经用过了任何异步函数所以我不确定为什么它会在玩家游戏板选择更新之前发生。这是相关代码,但我认为通过下面的codepen链接可能更容易。

function computeMove(game, lastMove, move){
 if((game.a1==""&&game.a2=="C"&&game.a3=="C")||
 (game.a1==""&&game.b1=="C"&&game.c1=="C")||
 (game.a1==""&&game.b2=="C"&&game.c3=="C"))
 {
    $('#col-a1').html(computer.choose());
    var item = 'col-a1';
    changeValue(computer, item);
 }
 else if((game.a2=""&&game.a1=="C"&&game.a3=="C")||
     (game.a2==""&&game.b2=="C"&&game.c2=="C"))
  {
    $('#col-a2').html(computer.choose());
    var item = 'col-a2';
    changeValue(computer, item);
  }
else if((game.a1=="C" && game.a2=="C" && game.a3=="")||
   (game.a3=="" && game.b2=="C" && game.c1=="C")||
   (game.a3=="" && game.b3=="C" && game.c3=="C"))
  {
    $('#col-a3').html(computer.choose());
    var item = 'col-a3';
    changeValue(computer, item);
  }
else if((game.b1=="" && game.b2=="C" && game.b3=="C")||
     (game.a1=="C" && game.b1=="" && game.c1=="C"))
  {
    $('#col-b1').html(computer.choose());
    var item = 'col-b1';
    changeValue(computer, item);
  }
else if((game.b2==""&&game.b1=="C"&&game.b3=="C")||
     (game.b2==""&&game.a2=="C"&&game.c2=="C")||
     (game.b2==""&&game.a1=="C"&&game.c3=="C")||
     (game.b2==""&&game.a3=="C"&&game.c1=="C"))
  {
    $('#col-b2').html(computer.choose());
    var item = 'col-b2';
    changeValue(computer, item);
  }
else if((game.b3==""&&game.b1=="C"&&game.b2=="C")||
     (game.b3==""&&game.a3=="C"&&game.c3=="C"))
  {
    $('#col-b3').html(computer.choose());
    var item = 'col-b3';
    changeValue(computer, item);
  }
else if((game.c1==""&&game.c2=="C"&&game.c3=="C")||
     (game.c1==""&&game.a1=="C"&&game.b1=="C")||
     (game.c1==""&&game.b2=="C"&&game.a3=="C"))
  {
    $('#col-c1').html(computer.choose());
    var item = 'col-c1';
    changeValue(computer, item);
  }
else if((game.c2==""&&game.c1=="C"&&game.c3=="C")||
     (game.c2==""&&game.b2=="C"&&game.a2=="C"))
  {
    $('#col-c2').html(computer.choose());
    var item = 'col-c2';
    changeValue(computer, item);
  }
else if((game.c3==""&&game.c1=="C"&&game.c2=="C")||
     (game.c3==""&&game.b3=="C"&&game.a3=="C")||
     (game.c3==""&&game.b2=="C"&&game.a1=="C"))
  {
    $('#col-c3').html(computer.choose());
    var item = 'col-c3';
    changeValue(computer, item);
  }
else
  {
    playDefense(game, lastMove, move)
  }
}

function playDefense(game, lastMove, move)
  {
    if((game.a1==""&&game.a2=="P"&&game.a3=="P")||
     (game.a1==""&&game.b1=="P"&&game.c1=="P")||
     (game.a1==""&&game.b2=="P"&&game.c3=="P"))
    {
      $('#col-a1').html(computer.choose());
      var item = 'col-a1';
      changeValue(computer, item);
    }
   else if((game.a2=""&&game.a1=="P"&&game.a3=="P")||
     (game.a2==""&&game.b2=="P"&&game.c2=="P"))
    {
      $('#col-a2').html(computer.choose());
      var item = 'col-a2';
      changeValue(computer, item);
    }
  else if((game.a1=="P" && game.a2=="P" && game.a3=="")||
     (game.a3=="" && game.b2=="P" && game.c1=="P")||
     (game.a3=="" && game.b3=="P" && game.c3=="P"))
  {
    $('#col-a3').html(computer.choose());
    var item = 'col-a3';
    changeValue(computer, item);
  }
 else if((game.b1=="" && game.b2=="P" && game.b3=="P")||
     (game.a1=="P" && game.b1=="" && game.c1=="P"))
  {
    $('#col-b1').html(computer.choose());
    var item = 'col-b1';
    changeValue(computer, item);
  }
 else if((game.b2==""&&game.b1=="P"&&game.b3=="P")||
     (game.b2==""&&game.a2=="P"&&game.c2=="P")||
     (game.b2==""&&game.a1=="P"&&game.c3=="P")||
     (game.b2==""&&game.a3=="P"&&game.c1=="P"))
 {
    $('#col-b2').html(computer.choose());
    var item = 'col-b2';
    changeValue(computer, item);
 }
else if((game.b3==""&&game.b1=="P"&&game.b2=="P")||
     (game.b3==""&&game.a3=="P"&&game.c3=="P"))
 {
    $('#col-b3').html(computer.choose());
    var item = 'col-b3';
    changeValue(computer, item);
 }
else if((game.c1==""&&game.c2=="P"&&game.c3=="P")||
     (game.c1==""&&game.a1=="P"&&game.b1=="P")||
     (game.c1==""&&game.b2=="P"&&game.a3=="P"))
 {
    $('#col-c1').html(computer.choose());
    var item = 'col-c1';
    changeValue(computer, item);
 }
else if((game.c2==""&&game.c1=="P"&&game.c3=="P")||
     (game.c2==""&&game.b2=="P"&&game.a2=="P"))
 {
    $('#col-c2').html(computer.choose());
    var item = 'col-c2';
    changeValue(computer, item);
 }
else if((game.c3==""&&game.c1=="P"&&game.c2=="P")||
     (game.c3==""&&game.b3=="P"&&game.a3=="P")||
     (game.c3==""&&game.b2=="P"&&game.a1=="P"))
 {
    $('#col-c3').html(computer.choose());
    var item = 'col-c3';
    changeValue(computer, item);
 }
else
 {
  findPlayerMove(game, lastMove, move)
 }
}

function findPlayerMove(game, lastMove, move)
 {
  if(move==2)
    {
      if(lastMove=="col-a1")
        {
          $('#col-c3').html(computer.choose());
         var item = 'col-c3';
         changeValue(computer, item);
        }
      else if(lastMove=="col-a3")
        {
          $('#col-c1').html(computer.choose());
           var item = 'col-c1';
           changeValue(computer, item);
        }
      else if(lastMove=="col-c1")
        {
          $('#col-a3').html(computer.choose());
          var item = 'col-a3';
          changeValue(computer, item);
        }
      else if(lastMove=="col-c3")
        {
         $('#col-a1').html(computer.choose());
         var item = 'col-a1';
         changeValue(computer, item);
       }
      else if(lastMove=="col-a2")
        {
         $('#col-c3').html(computer.choose());
         var item = 'col-c3';
         changeValue(computer, item);
        }
      else if(lastMove=="col-b1")
        {
          $('#col-c3').html(computer.choose());
           var item = 'col-c3';
           changeValue(computer, item);
        }
      else if(lastMove=="col-b3")
        {
          $('#col-c1').html(computer.choose());
          var item = 'col-c1';
          changeValue(computer, item);
        }
      else if(lastMove=="col-c2")
        {
          $('#col-a3').html(computer.choose());
          var item = 'col-a3';
          changeValue(computer, item);
        }
    }
  else if(move==4)
  {
    if(lastMove=="col-a2")
      {
        if(game.c3=="")
          {
            $('#col-c3').html(computer.choose());
            var item = 'col-c3';
            changeValue(computer, item);
          }
        else if(game.c1=="")
          {
            $('#col-c1').html(computer.choose());
            var item = 'col-c1';
            changeValue(computer, item);
          }
      }
    else if(lastMove=="col-b1")
      {
        if(game.a3=="P"&&game.a1=="")
          {
            $('#col-a1').html(computer.choose());
            var item = 'col-a1';
            changeValue(computer, item);
          }
        else if(game.c3=="P"&&game.c1=="")
          {
            $('#col-c1').html(computer.choose());
            var item = 'col-c1';
            changeValue(computer, item);
          }
      }
    else if(lastMove=="col-b3")
      {
        if(game.a1=="P"&&game.c1=="")
          {
            $('#col-c1').html(computer.choose());
            var item = 'col-c1';
            changeValue(computer, item);
          }
        else if(game.c1=="P"&&game.c3=="")
          {
            $('#col-c3').html(computer.choose());
            var item = 'col-c3';
            changeValue(computer, item);
          }
      }
    else if(lastMove=="col-c2")
      {
        if(game.a1=="P"&&game.c1=="")
          {
            $('#col-c1').html(computer.choose());
            var item = 'col-c1';
            changeValue(computer, item);
          }
        else if(game.c1=="P"&&game.c3=="")
          {
            $('#col-c3').html(computer.choose());
            var item = 'col-c3';
            changeValue(computer, item);
          }
        else if(game.a3=="P"&&game.c3=="")
          {
            $('#col-c3').html(computer.choose());
            var item = 'col-c3';
            changeValue(computer, item);
          }
      }
  }
if(move==6)
  {
    if(lastMove=="col-a2")
      {
        if(game.b1=="")
          {
            $('#col-b1').html(computer.choose());
            var item = 'col-b1';
            changeValue(computer, item);
          }
      }
    else if(lastMove=="col-b1")
      {
        if(game.a2=="")
          {
            $('#col-a2').html(computer.choose());
            var item = 'col-a2';
            changeValue(computer, item);
          }
      }
    else if(lastMove=="col-b3")
      {
         if(game.a2=="")
          {
            $('#col-a2').html(computer.choose());
            var item = 'col-a2';
            changeValue(computer, item);
          }
      }
    else if(lastMove=="col-c2")
      {
        if(game.b1=="")
          {
            $('#col-b1').html(computer.choose());
            var item = 'col-b1';
            changeValue(computer, item);
          }
      }
  }
if(move==8)
  {
    if(game.a1=="")
      {
        $('#col-a1').html(computer.choose());
        var item = 'col-a1';
        changeValue(computer, item);
      }
    if(game.a2=="")
      {
        $('#col-a2').html(computer.choose());
        var item = 'col-a2';
        changeValue(computer, item);
      }
    if(game.a3=="")
      {
        $('#col-a3').html(computer.choose());
        var item = 'col-a3';
        changeValue(computer, item);
      }
    if(game.b1=="")
      {
        $('#col-b1').html(computer.choose());
        var item = 'col-b1';
        changeValue(computer, item);
      }
    if(game.b3=="")
      {
        $('#col-b3').html(computer.choose());
        var item = 'col-b3';
        changeValue(computer, item);
      }
    if(game.c1=="")
      {
        $('#col-c1').html(computer.choose());
        var item = 'col-c1';
        changeValue(computer, item);
      }
    if(game.c2=="")
      {
        $('#col-c2').html(computer.choose());
        var item = 'col-c2';
        changeValue(computer, item);
      }
    if(game.c3=="")
      {
        $('#col-c3').html(computer.choose());
        var item = 'col-c3';
        changeValue(computer, item);
      }
  }
}

function changeValue(pl, col)
    {
        console.log("This is the current player: ")
  console.log(pl);
        console.log("This is the current col: " + col);
  if(pl.name == "Player One")
    {
      if (col=="col-a1") {
          cGameBoard.a1='P';
      } else if (col=="col-a2") {
          cGameBoard.a2='P';
      } else if (col=="col-a3") {
          cGameBoard.a3='P';
      } else if (col=="col-b1") {
          cGameBoard.b1='P';
      } else if (col=="col-b2") {
          cGameBoard.b2='P';
      } else if (col=="col-b3") {
          cGameBoard.b3='P';
      } else if (col=="col-c1") {
          cGameBoard.c1='P';
      } else if (col=="col-c2") {
          cGameBoard.c2='P';
      } else if (col=="col-c3") {
          cGameBoard.c3='P';
      } else {
          alert("ChangeValue not working");
      }
    }
  else
    {
      if (col=="col-a1") {
                cGameBoard.a1='C';
      } else if (col=="col-a2") {
          cGameBoard.a2='C';
      } else if (col=="col-a3") {
          cGameBoard.a3='C';
      } else if (col=="col-b1") {
          cGameBoard.b1='C';
      } else if (col=="col-b2") {
          cGameBoard.b2='C';
      } else if (col=="col-b3") {
          cGameBoard.b3='C';
      } else if (col=="col-c1") {
          cGameBoard.c1='C';
      } else if (col=="col-c2") {
          cGameBoard.c2='C';
      } else if (col=="col-c3") {
          cGameBoard.c3='C';
      } else {
          alert("ChangeValue not working");
      }
    }
            console.log("gameboard before winnercheck");
  console.log(cGameBoard);
    }

  var move = 0;
  function playGame(){
    if($('#well-info').text()=='')
     {
       if(computer.turn)
        {
          if(move == 0)
            {
              $('#col-b2').html(computer.choose());
              var item = 'col-b2';
              changeValue(computer, item);
              move++;
              winnerCheck(cGameBoard);
              player1.turn = true;
              computer.turn = false;
              playGame();
            }
          else
           {
             computeMove(cGameBoard, lastMove, move);
             move++;
             winnerCheck(cGameBoard);
             player1.turn = true;
             computer.turn = false;
             playGame();
           }
       }
     else if(player1.turn)
      {
        console.log("It is player 1 turn")
        $('.status').click(function(event){
        console.log("Got the click event");
        var $item = $(this).attr('id');
          console.log("This is $item");
          console.log($item);
        if($.trim($('#'+$item).html())=='')
          {
            console.log("The box was empty");
            $('#' + $item).html(player1.choose());
            lastMove = $item;
            changeValue(player1, $item);
            move++;
            winnerCheck(cGameBoard);
            player1.turn = false;
            computer.turn = true;
            playGame();
          }
        else
          {
            $('#' + $item).toggleClass("highlighted");
            setTimeout(function() {
               $('#' + $item).removeClass("highlighted")
             }, 1000);
          }
         });
        }
     }
 }

http://codepen.io/RawleJuglal/pen/mVOMyd

0 个答案:

没有答案