Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

Write a tic-tac-toe game using HTML

  1. User

    Write a tic-tac-toe game using HTML

  2. Assistant

    Here's a simple Tic-Tac-Toe game using HTML, CSS, and JavaScript. You can copy and paste the entire code into a new HTML file and open it in a browser to see the game in action.

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Tic Tac Toe Game</title>
      <style>
        body {
          font-family: Arial, sans-serif;
          display: flex;
          justify-content: center;
          align-items: center;
          height: 100vh;
          margin: 0;
          background-color: #282c34;
        }
        h1 {
          color: white;
          margin-bottom: 1rem;
        }
        .game-container {
          display: flex;
          flex-direction: column;
          align-items: center;
        }
        .game-grid {
          display: grid;
          grid-template-columns: repeat(3, 100px);
          grid-template-rows: repeat(3, 100px);
          gap: 5px;
        }
        .game-cell {
          display: flex;
          justify-content: center;
          align-items: center;
          width: 100%;
          height: 100%;
          background-color: white;
          cursor: pointer;
          font-size: 2rem;
          font-weight: bold;
          color: #282c34;
        }
      </style>
    </head>
    <body>
      <div class="game-container">
        <h1>Tic Tac Toe</h1>
        <div class="game-grid">
          <div class="game-cell" onclick="makeMove(0)"></div>
          <div class="game-cell" onclick="makeMove(1)"></div>
          <div class="game-cell" onclick="makeMove(2)"></div>
          <div class="game-cell" onclick="makeMove(3)"></div>
          <div class="game-cell" onclick="makeMove(4)"></div>
          <div class="game-cell" onclick="makeMove(5)"></div>
          <div class="game-cell" onclick="makeMove(6)"></div>
          <div class="game-cell" onclick="makeMove(7)"></div>
          <div class="game-cell" onclick="makeMove(8)"></div>
        </div>
      </div>
      
      <script>
        const cells = document.querySelectorAll('.game-cell');
        let currentPlayer = 'X';
        let gameBoard = ['', '', '', '', '', '', '', '', ''];
        let gameOver = false;
    
        function makeMove(index) {
          if (gameBoard[index] === '' && !gameOver) {
            gameBoard[index] = currentPlayer;
            cells[index].textContent = currentPlayer;
            cells[index].style.pointerEvents = 'none';
            checkWinner();
            switchPlayer();
          }
        }
    
        function checkWinner() {
          const winningCombos = [
            [0, 1, 2],
            [3, 4, 5],
            [6, 7, 8],
            [0, 3, 6],
            [1, 4, 7],
            [2, 5, 8],
            [0, 4, 8],
            [2, 4, 6]
          ];
    
          for (const combo of winningCombos) {
            const [a, b, c] = combo;
    
            if (gameBoard[a] && gameBoard[a] === gameBoard[b] && gameBoard[a] === gameBoard[c]) {
              gameOver = true;
              alert(currentPlayer + ' wins!');
              return;
            }
          }
    
          if (!gameBoard.includes('')) {
            gameOver = true;
            alert('It\'s a tie!');
          }
        }
    
        function switchPlayer() {
          currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
        }
      </script>
    </body>
    </html>
    

    This HTML file contains a basic Tic-Tac-Toe game grid using CSS Grid and JavaScript to handle gameplay. To play the game, simply click on the cells to place either an 'X' or an 'O', alternating each turn. The game will alert the winner or a tie when either condition occurs.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.