local board = {" ", " ", " ", " ", " ", " ", " ", " ", " "} local game_active = false local function PrintBoard(player) local b = {} for i = 1, 9 do if board[i] == "X" then b[i] = "\x82X\x80" elseif board[i] == "O" then b[i] = "\x85O\x80" else b[i] = "\x83" .. i .. "\x80" end end CONS_Printf(player, "\x83 " .. b[1] .. " | " .. b[2] .. " | " .. b[3]) CONS_Printf(player, "\x83 -----------") CONS_Printf(player, "\x83 " .. b[4] .. " | " .. b[5] .. " | " .. b[6]) CONS_Printf(player, "\x83 -----------") CONS_Printf(player, "\x83 " .. b[7] .. " | " .. b[8] .. " | " .. b[9]) end local function CheckWinner() local win_conditions = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, -- горизонталь {1, 4, 7}, {2, 5, 8}, {3, 6, 9}, -- вертикаль {1, 5, 9}, {3, 5, 7} -- диагональ } for _, cond in ipairs(win_conditions) do if board[cond[1]] ~= " " and board[cond[1]] == board[cond[2]] and board[cond[1]] == board[cond[3]] then return board[cond[1]] end end local open_spots = 0 for i = 1, 9 do if board[i] == " " then open_spots = open_spots + 1 end end if open_spots == 0 then return "Draw" end return nil end -- ход бота local function BotMove() local win_conditions = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, -- горизонталь {1, 4, 7}, {2, 5, 8}, {3, 6, 9}, -- вертикаль {1, 5, 9}, {3, 5, 7} -- диагональ } local function countInLine(cond, symbol) local count = 0 local empty_index = nil for _, idx in ipairs(cond) do if board[idx] == symbol then count = count + 1 elseif board[idx] == " " then empty_index = idx end end return count, empty_index end for _, cond in ipairs(win_conditions) do local count, empty_idx = countInLine(cond, "O") if count == 2 and empty_idx then board[empty_idx] = "O" return end end for _, cond in ipairs(win_conditions) do local count, empty_idx = countInLine(cond, "X") if count == 2 and empty_idx then board[empty_idx] = "O" return end end if board[5] == " " then board[5] = "O" return end local corners = {1, 3, 7, 9} local free_corners = {} for _, idx in ipairs(corners) do if board[idx] == " " then table.insert(free_corners, idx) end end if #free_corners > 0 then local rand_idx = (math.floor((os.time() * 1000) % #free_corners) + 1) board[free_corners[rand_idx]] = "O" return end local remaining = {} for i = 1, 9 do if board[i] == " " then table.insert(remaining, i) end end if #remaining > 0 then local rand_idx = (math.floor((os.time() * 1000) % #remaining) + 1) board[remaining[rand_idx]] = "O" end end COM_AddCommand("ttt_start", function(player) board = {" ", " ", " ", " ", " ", " ", " ", " ", " "} game_active = true CONS_Printf(player, "\x82--- Tic-Tac-Toe Game Started! ---") CONS_Printf(player, "Use command: \x82ttt_move <1-9>\x80 to place your X.") PrintBoard(player) end) COM_AddCommand("ttt_move", function(player, cell_str) if not game_active then CONS_Printf(player, "\x85No active game. Type \x82ttt_start\x85 to begin!") return end local cell = tonumber(cell_str) if not cell or cell < 1 or cell > 9 then CONS_Printf(player, "\x85Invalid input! Choose a number between 1 and 9.") return end if board[cell] ~= " " then CONS_Printf(player, "\x85That cell is already taken!") return end board[cell] = "X" local winner = CheckWinner() if winner then PrintBoard(player) if winner == "X" then CONS_Printf(player, "\x82=== YOU WIN! Congratulations! ===") else CONS_Printf(player, "\x83=== IT'S A DRAW! ===") end game_active = false return end BotMove() winner = CheckWinner() PrintBoard(player) if winner then if winner == "O" then CONS_Printf(player, "\x85=== BOT WINS! Better luck next time. ===") else CONS_Printf(player, "\x83=== IT'S A DRAW! ===") end game_active = false end end)