Advertisement
Josif_tepe

Untitled

Apr 28th, 2023
934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let playerText = document.getElementById("playerText")
  2. let restartButton = document.getElementById("restartButton")
  3. let boxes = Array.from(document.getElementsByClassName("box"))
  4. const xText = "X"
  5. const oText = "O"
  6. let currentPlayer = xText
  7. let cells = Array(9).fill(null)
  8. function startGame() {
  9.     for(let i = 0; i < 9; i++) {
  10.         boxes[i].addEventListener('click', boxClicked)
  11.     }
  12. }
  13. function boxClicked(e) {
  14.     let cellsNotEmpty = 0;
  15.     let idx = e.target.id;
  16.     for(let i = 0; i < 9; i++) {
  17.         if(cells[i]) {
  18.             cellsNotEmpty++;
  19.         }
  20.     }
  21.     if(cellsNotEmpty == 9) {
  22.         playerText.innerText = "It is a draw!"
  23.         return
  24.     }
  25.     if(!cells[idx]) {
  26.         cells[idx] = currentPlayer
  27.         e.target.innerText = currentPlayer
  28.         if(playerWon()) {
  29.  
  30.         }
  31.  
  32.         if(currentPlayer == xText) {
  33.             currentPlayer = oText
  34.         }
  35.         else {
  36.             currentPlayer = xText
  37.         }
  38.     }
  39. }
  40. function playerWon() {
  41.    
  42. }
  43. /**
  44.  * winning combos
  45.  * 0, 1, 2
  46.  * 3, 4, 5
  47.  * 6, 7, 8
  48.  * 0, 3, 6
  49.  * 1, 4, 7
  50.  * 2, 5, 8
  51.  * 0, 4, 8
  52.  * 2, 4, 6
  53.  */
  54.  
  55. startGame();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement