Advertisement
Bolodefchoco_LUAXML

[Script] #mastermind

Mar 17th, 2019
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.53 KB | None | 0 0
  1. table.remove = function(list, pos)
  2.     local len, out = #list
  3.  
  4.     if not pos or pos == len then
  5.         out = list[len]
  6.         list[len] = nil
  7.     elseif pos < len then
  8.         out = list[pos]
  9.         list[pos] = nil
  10.  
  11.         for i = pos, len do
  12.             list[i] = list[i + 1]
  13.         end
  14.     end
  15.     return out
  16. end
  17.  
  18. local secToDate = function(s)
  19.     local m = (s / 60) % 60
  20.     local h = (s / 3600) % 24
  21.  
  22.     s = s % 60
  23.  
  24.     return string.format("%02dh%02dm%02ds", h, m, s)
  25. end
  26.  
  27. local info = { }
  28.  
  29. local genData
  30. do
  31.     genData = function()
  32.         local numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
  33.  
  34.         local number, n = { _set = { } }
  35.         for i = 1, 4 do
  36.             n = table.remove(numbers, math.random((i == 1 and 2 or 1), #numbers))
  37.             number[i] = n
  38.             number._set[n] = true
  39.         end
  40.  
  41.         return {
  42.             time = 0,
  43.             tentative = 1,
  44.             digit = 1,
  45.             sequence = { _set = { } },
  46.             number = number,
  47.             restart = false
  48.         }
  49.     end
  50. end
  51.  
  52. local input = function(playerName, id, highlighted)
  53.     ui.addTextArea(id, "<p align='center'><font size='20'>" .. (type(highlighted) ~= "boolean" and highlighted or ''), playerName, 243 + ((id - 1) * 60), 60, 40, 30, (highlighted == true and 0x323232 or 1), 1, .7, true)
  54. end
  55.  
  56. local str = "<p align='center'><font size='20'>"
  57.  
  58. local displayInterface = function(playerName)
  59.     ui.addTextArea(-2, "<p align='center'><font size='16'>  Guessed    Exist   Match", playerName, 5, 25, 220, 365, 1, 1, .5, true)
  60.     ui.addTextArea(-1, "<p align='center'><font size='16'>Enter number [0-9]", playerName, 233, 25, 243, 365, 1, 1, .5, true)
  61.     ui.addTextArea(0, str, playerName, 233, 100, 243, nil, 1, 1, 0, true)
  62.  
  63.     local displayFields, y = 4
  64.     for i = 1, 4 do
  65.         input(playerName, i, (i == 1))
  66.     end
  67.     for i = 0, 6 do
  68.         displayFields = displayFields + 3
  69.         y = 60 + (i * 48)
  70.         ui.addTextArea(displayFields - 2, str, playerName, 15, y, 80, 30, 1, 1, .7, true)
  71.         ui.addTextArea(displayFields - 1, str, playerName, 115, y, 40, 30, 1, 1, .7, true)
  72.         ui.addTextArea(displayFields    , str, playerName, 175, y, 40, 30, 1, 1, .7, true)
  73.     end
  74. end
  75.  
  76. local newPlayer = function(playerName)
  77.     info[playerName] = genData()
  78.     displayInterface(playerName)
  79. end
  80.  
  81. local bytes = { 16, 46, string.byte(" 0123456789abcdefghij`", 1, -1) }
  82. eventNewPlayer = function(playerName)
  83.     newPlayer(playerName)
  84.  
  85.     for i = 1, #bytes do
  86.         system.bindKeyboard(playerName, bytes[i], true, true)
  87.     end
  88.     tfm.exec.setPlayerScore(playerName, 0)
  89.  
  90.     tfm.exec.chatMessage("<O>Type <B>!help</B> to learn how play.</O>\n<D>Thanks to <O><B>Blank#3495</B></O> and his great ideas</D>", playerName)
  91. end
  92. for playerName in next, tfm.get.room.playerList do
  93.     eventNewPlayer(playerName)
  94. end
  95.  
  96. eventNewGame = function()
  97.     for playerName in next, tfm.get.room.playerList do
  98.         tfm.exec.killPlayer(playerName)
  99.     end
  100. end
  101.  
  102. eventLoop = function()
  103.     for playerName, data in next, info do
  104.         if not data.restart then
  105.             data.time = data.time + .5
  106.             if data.time % 1 == 0 then
  107.                 ui.updateTextArea(0, "<D><B>" .. secToDate(data.time) .. "</B></D>\n\n\nPress <D><B>Space</B></D> to send the number;\n\nPress <D><B>Shift</B></D> to erase the last number;\n\nPress <D><B>Delete</B></D> to start a new game.", playerName)
  108.             end
  109.         end
  110.     end
  111. end
  112.  
  113. eventKeyboard = function(playerName, key)
  114.     local k = key
  115.     key = tonumber(string.char(key))
  116.  
  117.     if k == 46 then
  118.         return newPlayer(playerName)
  119.     elseif k == 32 then
  120.         if info[playerName].restart then
  121.             return newPlayer(playerName)
  122.         end
  123.  
  124.         if info[playerName].digit < 5 then return end
  125.  
  126.         local exist, match = 0, 0
  127.         for i = 1, 4 do
  128.             if info[playerName].number._set[info[playerName].sequence[i]] then
  129.                 exist = exist + 1
  130.             end
  131.             if info[playerName].number[i] == info[playerName].sequence[i] then
  132.                 match = match + 1
  133.             end
  134.         end
  135.  
  136.         local tentative = info[playerName].tentative * 3
  137.         ui.updateTextArea(4 + tentative - 2, table.concat(info[playerName].sequence, ''), playerName)
  138.         ui.updateTextArea(4 + tentative - 1, exist, playerName)
  139.         ui.updateTextArea(4 + tentative, match, playerName)
  140.  
  141.         info[playerName].tentative = info[playerName].tentative + 1
  142.         if info[playerName].tentative < 8 then
  143.             info[playerName].digit = 1
  144.             info[playerName].sequence = { _set = { } }
  145.             for i = 1, 4 do
  146.                 input(playerName, i, (i == 1))
  147.             end
  148.         end
  149.  
  150.         if match == 4 then
  151.             tfm.exec.chatMessage("<O>You <B>won</B> (<B>" .. secToDate(info[playerName].time) .. "</B>)! The number was <D><B>" .. table.concat(info[playerName].number, '') .. "</B></D>. Press <B>Space</B> to play again.</O>", playerName)
  152.             tfm.exec.setPlayerScore(playerName, 1, true)
  153.             info[playerName].restart = true
  154.         else
  155.             if info[playerName].tentative == 8 then
  156.                 tfm.exec.chatMessage("<O>You <B>lose</B>! The number was <D><B>" .. table.concat(info[playerName].number, '') .. "</B></D>. Press <B>Space</B> to play again.</O>", playerName)
  157.                 info[playerName].restart = true
  158.             end
  159.         end
  160.     else
  161.         if info[playerName].restart then return end
  162.         if k == 16 then
  163.             info[playerName].digit = math.max(1, info[playerName].digit - 1)
  164.             if not info[playerName].sequence[info[playerName].digit] then return end
  165.  
  166.             info[playerName].sequence._set[info[playerName].sequence[info[playerName].digit]] = nil
  167.             info[playerName].sequence[info[playerName].digit] = nil
  168.            
  169.             input(playerName, math.min(4, info[playerName].digit + 1))
  170.             input(playerName, info[playerName].digit, true)
  171.         else
  172.             if not tonumber(key) then
  173.                 key = tonumber(string.char(k - 48))
  174.             end
  175.             if info[playerName].digit == 1 and key == 0 then return end
  176.             if info[playerName].sequence._set[key] then return end
  177.             if info[playerName].digit == 5 then return end
  178.  
  179.             info[playerName].sequence._set[key] = true
  180.             info[playerName].sequence[info[playerName].digit] = key
  181.             input(playerName, info[playerName].digit, key)
  182.  
  183.             info[playerName].digit = info[playerName].digit + 1
  184.             if info[playerName].digit < 5 then
  185.                 input(playerName, info[playerName].digit, true)
  186.             end
  187.         end
  188.     end
  189. end
  190.  
  191. eventChatCommand = function(playerName, command)
  192.     if command == "help" then
  193.         tfm.exec.chatMessage("<O>Computer selects a four digit number [0-9], all four digits are different. Number may not begin with 0. Any number can be guessed in 7 tries or less.\n\t- <B>Exist</B> column displays total number of digits you guessed right.\n\t- <B>Match</B> shows how many of those that exists were placed at the right spots.</O>", playerName)
  194.     end
  195. end
  196.  
  197. eventPlayerLeft = function(playerName)
  198.     info[playerName] = nil
  199. end
  200.  
  201. system.disableChatCommandDisplay()
  202. tfm.exec.disableAutoNewGame()
  203. tfm.exec.disableAutoScore()
  204. tfm.exec.disableAutoShaman()
  205. tfm.exec.newGame('<C><P /><Z><S /><D /><O /></Z></C>')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement