samedi 24 décembre 2016

JavaScript Tic Tac Toe - If...Else

I'm having an issue with my Tic Tac Toe's AI. The AI doesn't always make a move after the human player makes a move. My guess is that it has something to do with my If...Else statements inside of the AI function. Maybe it's the order they are in? Does that matter? Here's a link to the game: http://ift.tt/2hCeYtJ

You might have to play around to see it in action. Basically, though, if you don't make 2 moves that are right next to each other, the AI doesn't make a move.

$(document).ready(function() {

  var userPick, compPick, cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8, cell9, aI, boardCheck, divTableCell;
  var turn = 0;

  $('#option1, #option2').click(function() {
    if (this.id == 'option1') {
      userPick = "X";
      compPick = "O";
    } else if (this.id == 'option2') {
      userPick = "O";
      compPick = "X";
    }

  });

  $("#option1, #option2").on("click", function() {
    $("#option1, #option2").hide()
  })

  $(".reset").click(function() {

    $("button").show();
    $(".divTableCell").html(" ");
    userPick = "";
    compPick = "";

  })

  $(".divTableCell").click(function() {
    if (turn == 0) {

      $(this).html(userPick);
      boardCheck();
      turn == 1;
      aI();
      boardCheck();

    }

  })

  var aI = function() {

    if (cell2 == userPick && cell3 == userPick || cell4 == userPick && cell7 == userPick || cell5 == userPick && cell9 == userPick) {
      $(".cell1").html(compPick)
      turn = 0;
    } else {
      if (cell1 == userPick && cell3 == userPick || cell5 == userPick && cell8 == userPick) {
        $(".cell2").html(compPick)
        turn = 0;
      } else {
        if (cell1 == userPick && cell2 == userPick || cell6 == userPick && cell9 == userPick || cell7 == userPick && cell5 == userPick) {
          $(".cell3").html(compPick)
          turn = 0;
        } else {
          if (cell7 == userPick && cell8 == userPick || cell3 == userPick && cell6 == userPick || cell1 == userPick && cell5 == userPick) {
            $(".cell9").html(compPick)
            turn = 0;
          } else {
            if (cell1 == userPick && cell4 == userPick || cell8 == userPick && cell9 == userPick) {
              $(".cell7").html(compPick)
              turn = 0;
            } else {
              if (cell7 == userPick && cell9 == userPick || cell2 == userPick && cell5 == userPick) {
                $(".cell8").html(compPick)
                turn = 0;
              } else {
                if (cell5 == userPick && cell6 == userPick || cell1 == userPick && cell7 == userPick) {
                  $(".cell4").html(compPick)
                  turn = 0;
                } else {
                  if (cell4 == userPick && cell5 == userPick || cell3 == userPick && cell9 == userPick) {
                    $(".cell6").html(compPick)
                    turn = 0;
                  } else {
                    if (cell4 == userPick && cell6 == userPick || cell3 == userPick && cell7 == userPick || cell1 == userPick && cell9 == userPick) {
                      $(".cell5").html(compPick)
                      turn = 0;
                    } else {
                      if (cell3 == compPick && cell5 == compPick && (cell7 == "" || cell7 == " " || cell7 !== userPick)) {
                        $(".cell7").html(compPick)
                        turn = 0;
                      } else {
                        if (cell5 !== userPick || cell5 == "" || cell5 == " ") {
                          $(".cell5").html(compPick)
                          turn = 0;
                        } else {
                          if (cell1 !== userPick || cell1 == "" || cell1 == " ") {
                            $(".cell1").html(compPick)
                            turn = 0;
                          } else {
                            if (cell9 !== userPick || cell9 == "" || cell9 == " ") {
                              $(".cell9").html(compPick)
                              turn = 0;
                            } else {
                              if (cell8 !== userPick || cell8 == "" || cell8 == " ") {
                                $(".cell8").html(compPick)
                                turn = 0;
                              } else {
                                if (cell4 !== userPick || cell4 == "" || cell4 == " ") {
                                  $(".cell4").html(compPick)
                                  turn = 0;
                                }
                              }

                            }
                          }
                        }

                      }
                    }

                  }

                }
              }
            }
          }

        }

      }
    } }



  boardCheck = function() {
    cell1 = $('.cell1').html();
    cell2 = $('.cell2').html();
    cell3 = $('.cell3').html();
    cell4 = $('.cell4').html();
    cell5 = $('.cell5').html();
    cell6 = $('.cell6').html();
    cell7 = $('.cell7').html();
    cell8 = $('.cell8').html();
    cell9 = $('.cell9').html();
  };

});

Aucun commentaire:

Enregistrer un commentaire