Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //
  3. // DOM Variables
  4. const X_CLASS = 'x';
  5. const CIRCLE_CLASS = 'o';
  6. const restartButton = document.querySelector('#restartButton');
  7. const cellElements = document.querySelectorAll('.cell');
  8. const WINNING_MESSAGE = document.querySelector('.winning-message');
  9. const BOARD = document.querySelector('.board');
  10. const ENDGAME_MESSAGE = document.querySelector('[data-winning-message]');
  11.  
  12. // GAME Variables
  13. let winner = false;
  14. let draw = false;
  15. let circleTurn = false;
  16. const WINNING_COMBINATIONS = [
  17.     [0, 1, 2],
  18.     [3, 4, 5],
  19.     [6, 7, 8],
  20.     [0, 3, 6],
  21.     [1, 4, 7],
  22.     [2, 5, 8],
  23.     [0, 4, 8],
  24.     [2, 4, 6]
  25. ];
  26.  
  27. restartButton.addEventListener('click', startGame);
  28. startGame();
  29.  
  30. function startGame() {
  31.     cellElements.forEach(cell => {
  32.         cell.classList.remove(X_CLASS);
  33.         cell.classList.remove(CIRCLE_CLASS);
  34.         cell.removeEventListener('click', handleClick);
  35.         cell.addEventListener('click', handleClick, { once: true });
  36.     });
  37.     WINNING_MESSAGE.classList.remove('show');
  38.     circleTurn = false;
  39.  
  40.     setHoverTurn();
  41.  
  42. }
  43.  
  44. function setHoverTurn() {
  45.     BOARD.classList.remove(X_CLASS);
  46.     BOARD.classList.remove(CIRCLE_CLASS);
  47.  
  48.     if (circleTurn) {
  49.         BOARD.classList.add(CIRCLE_CLASS);
  50.     } else {
  51.         BOARD.classList.add(X_CLASS);
  52.     }
  53. }
  54.  
  55. function handleClick(event) {
  56.     const cell = event.target;
  57.     const currentClass = BOARD.classList[1];
  58.  
  59.     cell.classList.add(currentClass);
  60.  
  61.  
  62.     if (checkWin(currentClass)) {
  63.         winner = true;
  64.         addEndGameData(winner);
  65.     } else if (checkDraw()) {
  66.         draw = true;
  67.         addEndGameData(draw);
  68.     }
  69.     circleTurn = !circleTurn;
  70.  
  71.     // vs computer   / computer turn
  72.     if (circleTurn) {
  73.         let possibleWin = predictWin(currentClass);
  74.     }
  75.  
  76.  
  77.  
  78.     setHoverTurn();
  79.  
  80. }
  81.  
  82. function checkWin(currentClass) {
  83.     return WINNING_COMBINATIONS.some(combination => {
  84.         return combination.every(index => {
  85.             return cellElements[index].classList.contains(currentClass);
  86.         });
  87.     });
  88.  
  89. }
  90.  
  91. function checkDraw() {
  92.     return [...cellElements].every(cell => {
  93.         return cell.classList.contains(X_CLASS) || cell.classList.contains(CIRCLE_CLASS);
  94.     });
  95.  
  96. }
  97.  
  98. function addEndGameData() {
  99.  
  100.     if (draw) {
  101.         ENDGAME_MESSAGE.innerText = 'No one wins!';
  102.     } else {
  103.         let result = circleTurn ? ENDGAME_MESSAGE.innerText = 'Circle Wins!' : ENDGAME_MESSAGE.innerText = 'X\'s wins!';
  104.     }
  105.  
  106.     WINNING_MESSAGE.classList.add('show');
  107. }
  108.  
  109. function predictWin(currentClass) {
  110.     debugger;
  111.     let countPossibleWin = 0;
  112.     let possibleWin = false;
  113.  
  114.     let check = WINNING_COMBINATIONS.forEach(combination => {
  115.        return combination.forEach(index => {
  116.             if (cellElements[index].classList.contains(currentClass)) {
  117.                 countPossibleWin++;
  118.             }
  119.         });
  120.         if (countPossibleWin === 2) {
  121.             return true;
  122.         }
  123.         countPossibleWin = 0;
  124.         return false;
  125.     });
  126.  
  127.     if (check) {
  128.         console.log('possible win true');
  129.     } else {
  130.         console.log('not possible')
  131.     }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement