RylofScripts

Tic Tac Toe

Sep 18th, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.25 KB | None | 0 0
  1. <h1>Tic-Tac-Toe</h1>
  2. <div id="board">
  3.     <div class="cell"></div>
  4.     <div class="cell"></div>
  5.     <div class="cell"></div>
  6.     <div class="cell"></div>
  7.     <div class="cell"></div>
  8.     <div class="cell"></div>
  9.     <div class="cell"></div>
  10.     <div class="cell"></div>
  11.     <div class="cell"></div>
  12. </div>
  13. <button id="reset">Reset Game</button>
  14. <script src="script.js"></script>
  15.  
  16. <style>
  17. #board {
  18.   width: 300px;
  19.   height: 300px;
  20.   display: grid;
  21.   grid-template-columns: 100px 100px 100px;
  22. }
  23.  
  24. .cell {
  25.   width: 100px;
  26.   height: 100px;
  27.   border: 1px solid black;
  28.   text-align: center;
  29.   vertical-align: middle;
  30.   font-size: 32px;
  31. }
  32. </style>
  33.  
  34. <script>
  35. let board = [
  36.   ['', '', ''],
  37.   ['', '', ''],
  38.   ['', '', '']
  39. ];
  40. let currentPlayer = 'X';
  41.  
  42. document.addEventListener('DOMContentLoaded', () => {
  43.   const cells = document.querySelectorAll('.cell');
  44.   cells.forEach((cell, index) => {
  45.     cell.addEventListener('click', function() {
  46.       const row = Math.floor(index / 3);
  47.       const col = index % 3;
  48.       makeMove(row, col, cell);
  49.     });
  50.   });
  51.  
  52.   document.getElementById('reset').addEventListener('click', resetGame);
  53. });
  54.  
  55. function makeMove(row, col, cell) {
  56.   if (board[row][col] === '') {
  57.     board[row][col] = currentPlayer;
  58.     cell.textContent = currentPlayer;
  59.     if (checkWinner(row, col)) {
  60.       alert(`${currentPlayer} wins!`);
  61.       resetGame();
  62.     } else {
  63.       currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
  64.     }
  65.   }
  66. }
  67.  
  68. function checkWinner(row, col) {
  69.   return (
  70.     checkRow(row) ||
  71.     checkCol(col) ||
  72.     checkDiagonalFromTopLeft() ||
  73.     checkDiagonalFromTopRight()
  74.   );
  75. }
  76.  
  77. function checkRow(row) {
  78.   return board[row].every(cell => cell === currentPlayer);
  79. }
  80.  
  81. function checkCol(col) {
  82.   return board.every(row => row[col] === currentPlayer);
  83. }
  84.  
  85. function checkDiagonalFromTopLeft() {
  86.   return board.every((row, index) => row[index] === currentPlayer);
  87. }
  88.  
  89. function checkDiagonalFromTopRight() {
  90.   return board.every((row, index) => row[2 - index] === currentPlayer);
  91. }
  92.  
  93. function resetGame() {
  94.   board = [
  95.     ['', '', ''],
  96.     ['', '', ''],
  97.     ['', '', '']
  98.   ];
  99.   currentPlayer = 'X';
  100.   document.querySelectorAll('.cell').forEach(cell => (cell.textContent = ''));
  101. }
  102.  
  103. </script>
  104.  
  105.  
Advertisement
Add Comment
Please, Sign In to add comment