Changed around line 1
+ let gameState = {
+ board: Array(9).fill(''),
+ currentPlayer: 'X',
+ moves: 0
+ };
+
+ const winPatterns = [
+ [0, 1, 2], [3, 4, 5], [6, 7, 8], // Rows
+ [0, 3, 6], [1, 4, 7], [2, 5, 8], // Columns
+ [0, 4, 8], [2, 4, 6] // Diagonals
+ ];
+
+ const initializeFrame = async () => {
+ try {
+ const frameContext = await window.frames.getContext();
+ const cells = document.querySelectorAll('.cell');
+
+ cells.forEach(cell => {
+ cell.addEventListener('click', () => handleCellClick(cell));
+ });
+
+ document.querySelector('.frameButton').addEventListener('click', async () => {
+ try {
+ await window.frames.postMessage({
+ action: 'post',
+ state: gameState
+ });
+ } catch (error) {
+ console.error('Frame action error:', error);
+ }
+ });
+
+ await window.frames.ready();
+ } catch (error) {
+ console.error('Frame initialization error:', error);
+ }
+ };
+
+ const handleCellClick = (cell) => {
+ const index = cell.dataset.index;
+ if (gameState.board[index] === '') {
+ gameState.board[index] = gameState.currentPlayer;
+ cell.textContent = gameState.currentPlayer;
+ cell.classList.add(gameState.currentPlayer.toLowerCase());
+ gameState.moves++;
+
+ if (checkWinner()) {
+ alert(`Player ${gameState.currentPlayer} wins!`);
+ resetGame();
+ } else if (gameState.moves === 9) {
+ alert("It's a draw!");
+ resetGame();
+ } else {
+ gameState.currentPlayer = gameState.currentPlayer === 'X' ? 'O' : 'X';
+ }
+ }
+ };
+
+ const checkWinner = () => {
+ return winPatterns.some(pattern => {
+ const line = pattern.map(index => gameState.board[index]);
+ return line.every(cell => cell === gameState.currentPlayer);
+ });
+ };
+
+ const resetGame = () => {
+ gameState = {
+ board: Array(9).fill(''),
+ currentPlayer: 'X',
+ moves: 0
+ };
+
+ document.querySelectorAll('.cell').forEach(cell => {
+ cell.textContent = '';
+ cell.classList.remove('x', 'o');
+ });
+ };
+
+ document.addEventListener('DOMContentLoaded', initializeFrame);