| 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 |
| |
| \ |
| 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 |
| |
| |
| 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() |