Advertisement
Hazem3529

Test

Sep 9th, 2016
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function () {
  2.     var arrWinners = [[7, 8, 9], [4, 5, 6], [1, 2, 3], [3, 6, 9], [2, 5, 8], [1, 4, 7], [1, 5, 9], [3, 5, 7]];
  3.     var arrMoves = [[7, 8, 9], [4, 5, 6], [1, 2, 3], [3, 6, 9], [2, 5, 8], [1, 4, 7], [1, 5, 9], [3, 5, 7]
  4.              , [2, 4, 1], [2, 6, 3], [4, 8, 7], [6, 8, 9], [1, 9, 6], [4, 9, 7], [2, 7, 1]];
  5.     var xMoves = [];
  6.     var oMoves = []
  7.     var human;
  8.     var dirty = [];
  9.     var xTurn = false;
  10.     $(".pick").hide();
  11.     $(".pick").fadeIn(1000);
  12.     $("table").hide();
  13.     $(".x").click(function () {
  14.         $(".pick").hide()
  15.         $("#picked").text("You Are playing with X")
  16.         $(".game").fadeIn(3000);
  17.         xTurn = true;
  18.         human = "X";
  19.     })
  20.     $(".o").click(function () {
  21.         $(".pick").hide()
  22.         $("#picked").text("You Are playing with O")
  23.         $(".game").fadeIn(3000);
  24.         human = "O";
  25.     })
  26.     $("td").click(function (event) {
  27.         console.log(dirty);
  28.         if (xTurn) {
  29.             xTurn = false;
  30.        
  31.                 moveHuman(xMoves);
  32.                 var value = "#" + event.target.id;
  33.                 $(value).text("X").fadeIn("1000");
  34.                 pcPlay("O");
  35.            
  36.         }
  37.         else {
  38.             xTurn = true;
  39.             if (human === "O") {
  40.                 moveHuman(oMoves);
  41.                 var value = "#" + event.target.id;
  42.                 $(value).text("O").fadeIn("1000");
  43.                 pcPlay("X");
  44.             }
  45.         }
  46.     });
  47.  
  48.     function moveHuman(arr) {
  49.         arr.push(parseInt(event.target.id));
  50.         dirty.push(parseInt(event.target.id));
  51.         for (var i = 0; i < arrWinners.length; i++) {
  52.             var count = 0;
  53.             for (var j = 0; j < 3; j++) {
  54.                 if ((arr.indexOf(arrWinners[i][j])) !== -1) count++;
  55.                 if (count == 3) alert("win");
  56.             }
  57.         }
  58.     }
  59.  
  60.     function movePc(arr, random) {
  61.         arr.push(parseInt(event.target.id));
  62.         dirty.push(parseInt(random));
  63.         for (var i = 0; i < arrWinners.length; i++) {
  64.             var count = 0;
  65.             for (var j = 0; j < 3; j++) {
  66.                 if ((arr.indexOf(arrWinners[i][j])) !== -1) count++;
  67.                 if (count == 3) alert("win");
  68.             }
  69.         }
  70.     }
  71.  
  72.     function pcPlay(type) {
  73.         var random = Math.floor(Math.random() * 10)+1;
  74.    
  75.         while (dirty.indexOf(random) !== -1) {
  76.                 console.log(random);
  77.             random = Math.floor(Math.random * 10);
  78.         }
  79.    
  80.         movePc(xMoves,random);
  81.         var value = "#" + random;
  82.         $(value).text(type).fadeIn("1000");
  83.     }
  84. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement