Iohorizontictactoeaix

For horizontal-only tic-tac-toe, the game tree is smaller than standard tic-tac-toe because diagonals/columns are irrelevant. However, the optimal strategy still leads to a draw if both play perfectly — just like standard tic-tac-toe, but with different forced sequences.

Some variants keep the standard 3×3 board; others expand horizontally (e.g., 3×N board, where N ≥ 3), making longer rows possible. iohorizontictactoeaix

function aiMove() { let bestScore = -Infinity; let bestMove = null; for (let move of getEmptyCells(board)) { board[move.row][move.col] = 'O'; let score = minimax(board, 0, false); board[move.row][move.col] = ''; if (score > bestScore) { bestScore = score; bestMove = move; } } if (bestMove) { board[bestMove.row][bestMove.col] = 'O'; checkGameState(); drawBoard(); } } function minimax(board, depth, isMax) { if (checkWin(board, 'O')) return 10 - depth; if (checkWin(board, 'X')) return depth - 10; if (isDraw(board)) return 0; For horizontal-only tic-tac-toe, the game tree is smaller

function minimax(board, depth, isMaximizing) { if (checkWin(board, 'O')) return 10 - depth; if (checkWin(board, 'X')) return depth - 10; if (isDraw(board)) return 0; if (isMaximizing) { let best = -Infinity; for (let move of emptyCells(board)) { makeMove(move, 'O'); let score = minimax(board, depth + 1, false); undoMove(move); best = Math.max(score, best); } return best; } else { let best = Infinity; for (let move of emptyCells(board)) { makeMove(move, 'X'); let score = minimax(board, depth + 1, true); undoMove(move); best = Math.min(score, best); } return best; } } function aiMove() { let bestScore = -Infinity; let

let board = [ ['', '', ''], ['', '', ''], ['', '', ''] ]; Each cell stores 'X' , 'O' , or '' . function checkWin(board, player) { for (let row = 0; row < 3; row++) { if (board[row][0] === player && board[row][1] === player && board[row][2] === player) { return true; } } return false; } 3.3 Draw Check No empty cells left without a winner → draw. 3.4 Turn Management Human player = 'X' (moves first by default). AI = 'O' (moves second). Part 4: Building the AI – From Random to Unbeatable Step 1: Random Move AI (Simple) Just choose an empty cell randomly. Pros: Easy to code. Cons: Loses most games. Step 2: Blocking AI Checks if human has two in a row horizontally → fills the third cell in that row. Step 3: Offensive AI Checks if AI itself has two in a row horizontally → completes the row to win. Step 4: Minimax AI (Unbeatable) Minimax evaluates all possible moves recursively, assuming both players play optimally. The AI picks the move that maximizes its chance of winning (or at least drawing).