gpt-4-1106-preview· OpenAI
Arena Human Preference 55K
Hi, can you write me a tic Tac toe game that will run in a browser?
- User
Hi, can you write me a tic Tac toe game that will run in a browser?
- Assistant
Certainly! Below is a simple implementation of a Tic Tac Toe game that you can paste into an HTML file and run in your web browser. This game is written in HTML, CSS, and JavaScript.
<!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</title> <style> .board { display: grid; grid-template-columns: repeat(3, 100px); grid-gap: 10px; margin: 20px auto; } .cell { width: 100px; height: 100px; background-color: #f0f0f0; display: flex; align-items: center; justify-content: center; font-size: 2em; cursor: pointer; } </style> </head> <body> <div id="game"> <div class="board"> <div class="cell" data-cell-index="0"></div> <div class="cell" data-cell-index="1"></div> <div class="cell" data-cell-index="2"></div> <div class="cell" data-cell-index="3"></div> <div class="cell" data-cell-index="4"></div> <div class="cell" data-cell-index="5"></div> <div class="cell" data-cell-index="6"></div> <div class="cell" data-cell-index="7"></div> <div class="cell" data-cell-index="8"></div> </div> <p id="status"></p> </div> <script> const statusDisplay = document.getElementById('status'); let gameActive = true; let currentPlayer = 'X'; let gameState = ['', '', '', '', '', '', '', '', '']; const winningConditions = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6] ]; function handleCellPlayed(clickedCell, clickedCellIndex) { gameState[clickedCellIndex] = currentPlayer; clickedCell.innerHTML = currentPlayer; } function handlePlayerChange() { currentPlayer = currentPlayer === 'X' ? 'O' : 'X'; statusDisplay.innerHTML = `It's ${currentPlayer}'s turn`; } function handleResultValidation() { let roundWon = false; for (let i = 0; i <= 7; i++) { const winCondition = winningConditions[i]; let a = gameState[winCondition[0]]; let b = gameState[winCondition[1]]; let c = gameState[winCondition[2]]; if (a === '' || b === '' || c === '') { continue; } if (a === b && b === c) { roundWon = true; break; } } if (roundWon) { statusDisplay.innerHTML = `Player ${currentPlayer} has won!`; gameActive = false; return; } let roundDraw = !gameState.includes(''); if (roundDraw) { statusDisplay.innerHTML = `Game ended in a draw!`; gameActive = false; return; } handlePlayerChange(); } function handleCellClick(clickedCellEvent) { const clickedCell = clickedCellEvent.target; const clickedCellIndex = parseInt(clickedCell.getAttribute('data-cell-index')); if (gameState[clickedCellIndex] !== '' || !gameActive) { return; } handleCellPlayed(clickedCell, clickedCellIndex); handleResultValidation(); } function restartGame() { gameActive = true; currentPlayer = 'X'; gameState = ['', '', '', '', '', '', '', '', '']; statusDisplay.innerHTML = `It's ${currentPlayer}'s turn`; document.querySelectorAll('.cell').forEach(cell => cell.innerHTML = ''); } document.querySelectorAll('.cell').forEach(cell => cell.addEventListener('click', handleCellClick)); statusDisplay.innerHTML = `It's ${currentPlayer}'s turn`; </script> </body> </html>To play the game:
- Copy the above code and paste it into a new text file.
- Save the file with a
.htmlextension (for example,tic-tac-toe.html). - Locate the file on your computer and open it with your web browser.
Click on the cells to play the game. The status display will show you which player's turn it is and
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.