<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[CE Lua Крестики и нолики]]></title><description><![CDATA[<p dir="auto"><a href="/assets/uploads/files/1684622558663-%D0%BA%D1%80%D0%B5%D1%81%D1%82%D0%B8%D0%BA%D0%B8-%D0%B8-%D0%BD%D0%BE%D0%BB%D0%B8%D0%BA%D0%B8.ct">Крестики и нолики.CT</a></p>
<p dir="auto"><img src="/assets/uploads/files/1684622446687-c7c664ad-b109-47d7-a1f0-3c8882243941-image.png" alt="c7c664ad-b109-47d7-a1f0-3c8882243941-image.png" class=" img-fluid img-markdown" /><br />
Игра с рандомом. Есть счет.<br />
Запускается через таблицу в аттаче.<br />
Здесь мог быть AI на Lua, возвращающий номер клетки от 1 до 9.</p>
<pre><code>function InputAI()
 -- рандом
 return math.random (1, 9)
end
</code></pre>
<pre><code>player1Symbol = '1'
player2Symbol = '0'
whoStep = 0
player1Score = 0
player2Score = 0

function InputAI()
 -- рандом
 return math.random (1, 9)
end

\-- Проверка правил
function caheckRules(symbol)

  -- Комбинации выигрыша
  local tableRules =
  {
    {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 mask = [[
    return UDF1.CEButton%s.Caption == 'symbol' and
           UDF1.CEButton%s.Caption == 'symbol' and
           UDF1.CEButton%s.Caption == 'symbol'
  ]]

  for i = 1, #tableRules do

    local luaStringCode = mask:format(tableRules[i][1],
      tableRules[i][2], tableRules[i][3]):gsub('symbol', symbol)

    if loadstring (luaStringCode)() then
      return true
    end

  end

  return false
end

function StartGame()
  whoStep = 0
  for i = 1, 9 do
    loadstring ('UDF1.CEButton'..i..'.Caption = ""')()
  end
end

\-- 1 - ход сделан, 0 - ход не удался
function Input(indexInput)

  if loadstring ('return UDF1.CEButton'..indexInput..'.Caption')() ~= '' then
    return 0
  end

  local writeSymbol = player1Symbol
  if whoStep == 0 then
    whoStep = 1
  else
    whoStep = 0
    writeSymbol = player2Symbol
  end

  local s = 'UDF1.CEButton'..indexInput..'.Caption = '..writeSymbol
  loadstring (s)()

  local somebodyWinner = false

  if caheckRules(player1Symbol) then
    player1Score = player1Score + 1
    UDF1.CELabelScore.Caption = player1Score..':'..player2Score
    ShowMessage('Player1 is winner!')
    somebodyWinner = true

  elseif caheckRules(player2Symbol) then
    player2Score = player2Score + 1
    UDF1.CELabelScore.Caption = player1Score..':'..player2Score
    ShowMessage('Player2 is winner!')
    somebodyWinner = true
  end

  -- Если кто-то выиграл, то очистить поле
  if somebodyWinner then
    StartGame()
    return 1
  end

  --  Проверка ничьи
  local countEmpty = 9
  for i = 1, 9 do
    if loadstring ('return UDF1.CEButton'..i..'.Caption ~= ""')() then
      countEmpty = countEmpty - 1
    end
  end

  if countEmpty &lt;= 0 then
    UDF1.CELabelScore.Caption = player1Score..':'..player2Score
    ShowMessage('Friendship!')
    StartGame()
    return 1
  end

  -- Ходит IA
  if whoStep == 1 then
    ::repeat1::
    local index = InputAI()
    if Input(index) == 0 then
      goto repeat1
    end
  end
end


function CEButtonClick(sender)
  Input(tonumber(sender.name:match('%d')))
end

function MenuItem1Click(sender)
  player1Score = 0
  player2Score = 0
  UDF1.CELabelScore.Caption = player1Score..':'..player2Score
  StartGame()
  ShowMessage('Restart!')
end

UDF1.show()
</code></pre>
]]></description><link>https://celua.ru/topic/118/ce-lua-крестики-и-нолики</link><generator>RSS for Node</generator><lastBuildDate>Tue, 18 Aug 2026 11:24:04 GMT</lastBuildDate><atom:link href="https://celua.ru/topic/118.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 20 May 2023 22:42:40 GMT</pubDate><ttl>60</ttl></channel></rss>