Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  window.onload = function () {
  3.      for(let i = 0; i < 9; i++){
  4.          document.getElementById('game').innerHTML +='<input type="button" value="" class="INPUT">';
  5.      }
  6.      let bool = true;
  7.      let counter = 0;
  8.      let x = 0;
  9.  
  10.      document.getElementById('game').onclick = function (event) {
  11.          console.log(event);
  12.         if(event.srcElement.value === ""){
  13.             if (x % 2 === 0){
  14.                 event.srcElement.value = "o";
  15.             }
  16.             else {
  17.                 event.srcElement.value = "x";
  18.             }
  19.            x++;
  20.             checkWinner();
  21.         }
  22.      }
  23.  
  24.      function XorO(char) {
  25.          bool === false;
  26.          if(char==='x'){
  27.              setTimeout(function() {alert("Крестики выиграли"), window.location.reload()}, 30);
  28.          }
  29.          else if (char==='o'){
  30.              setTimeout(function (){alert("Нолики выиграли"), window.location.reload()}, 30 );
  31.          }
  32.      }
  33.  
  34.      function CheckPosition(mas,char) {
  35.          if (mas[0].value===char && mas[1].value===char && mas[2].value===char){
  36.              mas[0].style.backgroundColor = "green";
  37.              mas[1].style.backgroundColor = "green";
  38.              mas[2].style.backgroundColor = "green";
  39.             XorO(char);
  40.          }
  41.  
  42.          if(mas[3].value===char && mas[4].value===char && mas[5].value===char){
  43.              mas[3].style.backgroundColor = "green";
  44.              mas[4].style.backgroundColor = "green";
  45.              mas[5].style.backgroundColor = "green";
  46.              XorO(char);
  47.          }
  48.          if (mas[6].value===char && mas[7].value===char && mas[8].value===char) {
  49.              mas[6].style.backgroundColor = "green";
  50.              mas[7].style.backgroundColor = "green";
  51.              mas[8].style.backgroundColor = "green";
  52.              XorO(char);
  53.          }
  54.  
  55.          if(mas[0].value===char && mas[3].value===char && mas[6].value===char){
  56.              mas[0].style.backgroundColor = "green";
  57.              mas[3].style.backgroundColor = "green";
  58.              mas[6].style.backgroundColor = "green";
  59.              XorO(char);
  60.          }
  61.  
  62.          if (mas[1].value===char && mas[4].value===char && mas[7].value===char){
  63.              mas[1].style.backgroundColor = "green";
  64.              mas[4].style.backgroundColor = "green";
  65.              mas[7].style.backgroundColor = "green";
  66.              XorO(char);
  67.          }
  68.  
  69.          if (mas[2].value===char && mas[5].value===char && mas[8].value===char){
  70.              mas[2].style.backgroundColor = "green";
  71.              mas[5].style.backgroundColor = "green";
  72.              mas[8].style.backgroundColor = "green";
  73.              XorO(char);
  74.          }
  75.          if (mas[0].value===char && mas[4].value===char && mas[8].value===char){
  76.              mas[0].style.backgroundColor = "green";
  77.              mas[4].style.backgroundColor = "green";
  78.              mas[8].style.backgroundColor = "green";
  79.              XorO(char);
  80.          }
  81.          if (mas[2].value===char && mas[4].value===char && mas[6].value===char){
  82.              mas[2].style.backgroundColor = "green";
  83.              mas[4].style.backgroundColor = "green";
  84.              mas[6].style.backgroundColor = "green";
  85.              XorO(char);
  86.          }
  87.      }
  88.  
  89.      function checkWinner() {
  90.          let allblock = document.getElementsByClassName('INPUT');
  91.          console.log(allblock);
  92.          CheckPosition(allblock, 'x');
  93.          CheckPosition(allblock, 'o');
  94.          counter++;
  95.          if (counter === 9 && bool){
  96.              setTimeout(function (){alert("Ничья"), window.location.reload()}, 100 );
  97.          }
  98.  
  99.      }
  100.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement