CE Lua Крестики и нолики
Игра с рандомом. Есть счет.
Запускается через таблицу в аттаче.
Здесь мог быть AI на Lua, возвращающий номер клетки от 1 до 9.
function InputAI() -- рандом return math.random (1, 9) end
Извиняюсь, лучше рандома пока не могу предложить.
Скрытый текст
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 <= 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()
-
1
0 Комментариев
Рекомендуемые комментарии
Комментариев нет
Пожалуйста, войдите, чтобы комментировать
Вы сможете оставить комментарий после входа в
Войти