chopstyix

Wordle

Feb 5th, 2022 (edited)
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.22 KB | None | 0 0
  1. local lib = require("lib")
  2. local pd = peripheral.find("playerDetector")
  3. local filename = "db.txt"
  4. local dictionary = require("dictionary")
  5. local mon = peripheral.find("monitor")
  6. local speaker = peripheral.find("speaker")
  7. local console = term.redirect(mon)
  8. --local prize = 1
  9. local playerDetectorSensitivity = 3
  10.  
  11. local debug = 0
  12. local username = "Bob"
  13.  
  14. local referenceDate = os.time{day=2,year=2022,month=2}
  15. local refConvert = referenceDate/24/60/60
  16.  
  17. local wotd, wotd_num
  18.  
  19. local tbl_alphabet = { -- 0:Unknown,1: Non-present, 2:Present, 3:Confirmed
  20.   ["A"] = 0,
  21.   ["B"] = 0,
  22.   ["C"] = 0,
  23.   ["D"] = 0,
  24.   ["E"] = 0,
  25.   ["F"] = 0,
  26.   ["G"] = 0,
  27.   ["H"] = 0,
  28.   ["I"] = 0,
  29.   ["J"] = 0,
  30.   ["K"] = 0,
  31.   ["L"] = 0,
  32.   ["M"] = 0,
  33.   ["N"] = 0,
  34.   ["O"] = 0,
  35.   ["P"] = 0,
  36.   ["Q"] = 0,
  37.   ["R"] = 0,
  38.   ["S"] = 0,
  39.   ["T"] = 0,
  40.   ["U"] = 0,
  41.   ["V"] = 0,
  42.   ["W"] = 0,
  43.   ["X"] = 0,
  44.   ["Y"] = 0,
  45.   ["Z"] = 0
  46. }
  47.  
  48. local tbl_wordle_userInput = {
  49.   [1] = {" "," "," "," "," "},  -- 10 coins
  50.   [2] = {" "," "," "," "," "},  -- 5 coins
  51.   [3] = {" "," "," "," "," "},  -- 4 coins
  52.   [4] = {" "," "," "," "," "},  -- 3 coins
  53.   [5] = {" "," "," "," "," "},  -- 2 coins
  54.   [6] = {" "," "," "," "," "}   -- 1 coins
  55. }
  56.  
  57. local tbl_color = {
  58.   [1] = {colors.black,colors.black,colors.black,colors.black,colors.black},
  59.   [2] = {colors.black,colors.black,colors.black,colors.black,colors.black},
  60.   [3] = {colors.black,colors.black,colors.black,colors.black,colors.black},
  61.   [4] = {colors.black,colors.black,colors.black,colors.black,colors.black},
  62.   [5] = {colors.black,colors.black,colors.black,colors.black,colors.black},
  63.   [6] = {colors.black,colors.black,colors.black,colors.black,colors.black}
  64. }
  65.  
  66. local tbl_wotd_lock = {false,false,false,false,false,false}
  67.  
  68. function getWotd()
  69.   local _todaysDate = os.date("*t")
  70.   local _year = _todaysDate["year"]
  71.   local _yday = _todaysDate["yday"]
  72.   local _todaysDateConvert = (((_year-1970)*365)+((_year-1970)*.25)+_yday)
  73.   local _daysGap = math.floor(_todaysDateConvert - refConvert)
  74.   local _wotd = string.upper(dictionary.words[_daysGap])
  75.   return _wotd, _daysGap
  76. end
  77.  
  78. function getPlayerName()
  79.   local _player = pd.getPlayersInRange(playerDetectorSensitivity)
  80.   -- Get the name of the player in index one of table 'player'
  81.   return _player[1]
  82. end
  83.  
  84. function findPlayer(_player)
  85.   local _file = fs.open(filename,"r")
  86.   local _data = textutils.unserialize(_file.readAll())
  87.   for k,v in pairs(_data) do
  88.     --print(k,v)
  89.     if _player == k then return true,k,v end
  90.   end
  91.   _file.close()
  92.   return false
  93. end
  94.  
  95. function updatePlayer(_player)
  96.   local _DBfile = fs.open(filename,"r")
  97.   local database = textutils.unserialize(_DBfile.readAll())
  98.   _DBfile.close()
  99.   local _file = fs.open(filename,"w")
  100.   database[_player] = lib.getCurrentDate()
  101.   local _tbl = textutils.serialize(database)
  102.   _file.writeLine(_tbl)
  103.   _file.close()
  104. end
  105.  
  106. function debug(x)
  107.   term.redirect(console)
  108.   print(x)
  109.   term.redirect(mon)
  110. end
  111.  
  112. function isValidWord(attempt)
  113.   local word = table.concat(tbl_wordle_userInput[attempt])
  114.   for k,v in ipairs(dictionary.words) do
  115.     if word == string.upper(v) then debug("Valid Word") return true end
  116.   end
  117.  
  118.   for k,v in ipairs(dictionary.words_ok) do
  119.     if word == string.upper(v) then debug("Valid Word") return true end
  120.   end
  121.   debug("Invalid Word")
  122.   speaker.playNote("bass",1,4)
  123.   sleep(.3)
  124.   speaker.playNote("bass",1,0)
  125.   return false
  126. end
  127.  
  128. function convertTableToString()
  129.   return table.concat(tbl_wordle_userInput[attempt])
  130. end  
  131.  
  132. function convertStringToTable(str)
  133.   local t = {}
  134.   for i = 1, #str do
  135.     t[i] = str:sub(i,i)
  136.   end
  137.   return t
  138. end
  139.  
  140. function switch(c)
  141.   local swtbl = {
  142.     casevar = c,
  143.     caseof = function (self,code)
  144.       local f
  145.       if (self.casevar) then
  146.         f = code[self.casevar] or code.default
  147.       else
  148.         f = code.missing or code.default
  149.       end
  150.       if f then
  151.         if type(f) == "function" then
  152.           return f(self.casevar,self)
  153.         else
  154.           error("case "..tostring(self.casevar).."not a function")
  155.         end
  156.       end
  157.     end
  158.   }
  159.   return swtbl
  160. end  
  161.  
  162. function alpha(letter)
  163.   local x = tbl_alphabet[letter]
  164.   if x == 0 then
  165.     term.write(letter.." ")
  166.   elseif x == 1 then
  167.     term.setTextColor(colors.red)
  168.     term.write(letter.." ")
  169.     term.setTextColor(colors.white)
  170.   elseif x == 2 then
  171.     term.setTextColor(colors.yellow)
  172.     term.write(letter.." ")
  173.     term.setTextColor(colors.white)
  174.   elseif x == 3 then
  175.     term.setTextColor(colors.green)
  176.     term.write(letter.." ")
  177.     term.setTextColor(colors.white)
  178.   else
  179.     debug("What")
  180.   end
  181. end
  182.  
  183. function drawKeyboard()
  184.   mon.setCursorPos(5,21)
  185.   alpha("Q") alpha("W") alpha("E") alpha("R") alpha("T") alpha("Y") alpha("U") alpha("I") alpha("O") alpha("P")
  186.   mon.setCursorPos(6,22)
  187.   alpha("A") alpha("S") alpha("D") alpha("F") alpha("G") alpha("H") alpha("J") alpha("K") alpha("L")
  188.   mon.setCursorPos(8,23)
  189.   alpha("Z") alpha("X") alpha("C") alpha("V") alpha("B") alpha("N") alpha("M")
  190.   newLine()
  191.   newLine()
  192.   centerText("Erase   Enter")
  193.   mon.setCursorPos(1,1)
  194. end
  195.  
  196. function hideKeyboard()
  197.   mon.setCursorPos(5,21)
  198.   mon.setCursorPos(6,22)
  199.   mon.setCursorPos(8,23)
  200. end
  201.  
  202. function center(x)
  203.   local x,y = term.getSize()
  204. end
  205.  
  206. function centerText(text,fontColor,bgColor)
  207.   if bgColor == nil then
  208.     bgColor = colors.black
  209.   end
  210.   if fontColor == nil then
  211.     fontColor = colors.white
  212.   end
  213.   local x,y = term.getSize()
  214.   local curPosX, curPosY = mon.getCursorPos()
  215.   local length = string.len(text)
  216.   mon.setCursorPos(x/2-length/2,curPosY)
  217.   local oldTxt = term.getTextColor()
  218.   local oldBg = term.getBackgroundColor()
  219.   term.setTextColor(fontColor)
  220.   term.setBackgroundColor(bgColor)
  221.   mon.write(text)
  222.   term.setBackgroundColor(oldBg)
  223.   term.setTextColor(oldTxt)
  224. end
  225.  
  226. function drawBox(x,y,color)
  227.   local oldColor = term.getBackgroundColor()
  228.   paintutils.drawPixel(x,y,color)
  229.   term.setBackgroundColor(oldColor)
  230. end
  231.  
  232. function drawWord(attempt,yPos)
  233.   local oldColor = term.getBackgroundColor()
  234.   local xPos = 10
  235.   for k,v in ipairs(tbl_wordle_userInput[attempt]) do
  236.     if v == " " then
  237.       term.setCursorPos(xPos,yPos)
  238.       drawBox(xPos,yPos,colors.white)
  239.     else
  240.       local oldColor = term.getBackgroundColor()
  241.       term.setBackgroundColor(tbl_color[attempt][k])
  242.       term.setCursorPos(xPos,yPos)
  243.       term.write(v)
  244.       term.setBackgroundColor(oldColor)
  245.     end
  246.     xPos = xPos + 2
  247.   end
  248.   tbl_wotd_lock = {false,false,false,false,false,false} -- Reset
  249. end
  250.  
  251. function newLine()
  252.   local curPosX, curPosY = mon.getCursorPos()
  253.   mon.setCursorPos(1,curPosY+1)
  254. end
  255.  
  256. function cashout(_attempt)
  257.   local prize
  258.   if _attempt == 1 then prize = 10
  259.   elseif _attempt == 2 then prize = 5
  260.   elseif _attempt == 3 then prize = 4
  261.   elseif _attempt == 4 then prize = 3
  262.   elseif _attempt == 5 then prize = 2
  263.   elseif _attempt == 6 then prize = 1
  264.   else print("Cashout Error!") end
  265.   for i=1,prize do
  266.     redstone.setOutput("bottom",true)
  267.     sleep(.15)
  268.     redstone.setOutput("bottom",false)
  269.     sleep(.15)
  270.   end
  271. end
  272.  
  273. function compareInput(attempt)
  274.   local tbl = convertStringToTable(wotd)
  275.   --Debug
  276.   -- term.redirect(console)
  277.   -- for k,v in ipairs(tbl_wordle_userInput[attempt]) do
  278.     -- print(k,v)
  279.   -- end
  280.   -- term.redirect(mon)
  281.  
  282.   -- First do all correct letters -- GREEN
  283.   for k,v in ipairs(tbl_wordle_userInput[attempt]) do -- v: character of user guess
  284.     if (v == tbl[k]) and tbl_wotd_lock[k] == false then  -- tbl[k]: character of wotd
  285.       if (tbl_alphabet[v] == 0) or (tbl_alphabet[v] == 2) then
  286.         tbl_alphabet[v] = 3
  287.       end
  288.       term.redirect(console)
  289.       print("Letter",v,"is in the correct index. Assigning green")
  290.       term.redirect(mon)
  291.       tbl_color[attempt][k] = colors.green
  292.       tbl_wotd_lock[k] = true
  293.     end
  294.   end
  295.  
  296.   for k,v in ipairs(tbl_wordle_userInput[attempt]) do -- # 5 : A
  297.     term.redirect(console)
  298.     --print(tbl_wotd_lock[k])
  299.     term.redirect(mon)
  300.     for wotd_k,wotd_v in ipairs(tbl) do -- # 4 : A
  301.       --term.redirect(console)
  302.       --print(wotd_k,wotd_v)
  303.       --term.redirect(mon)
  304.       if (wotd_v == v) and (tbl_wotd_lock[wotd_k] == false) then -- If 's' is present in wotd and square index 1 is not locked
  305.         if tbl_alphabet[v] == 0 then
  306.           tbl_alphabet[v] = 2
  307.         end
  308.         if tbl_color[attempt][k] == colors.black then
  309.           term.redirect(console)
  310.           print("Letter",v,"is NOT in correct index but present. Assigning yellow")
  311.           term.redirect(mon)
  312.           tbl_color[attempt][k] = colors.orange
  313.           tbl_wotd_lock[wotd_k] = true -- I feel like this should me 'wotd_k' instead of 'k'
  314.         end
  315.         --tbl_wotd_lock[wotd_k] = true -- I feel like this should me 'wotd_k' instead of 'k'
  316.       end
  317.     end
  318.   end
  319.  
  320.   for k,v in ipairs(tbl_wordle_userInput[attempt]) do
  321.     --debug(tbl_color[attempt][k])
  322.     if (v ~= tbl[k]) and (tbl_color[attempt][k] == colors.black) then
  323.       if tbl_alphabet[v] == 0 then
  324.         tbl_alphabet[v] = 1
  325.       end
  326.       if tbl_color[attempt][k] == colors.black then
  327.         term.redirect(console)
  328.         if tbl_color[attempt][k] == colors.black then
  329.           print("Letter",v,"is NOT present. Assigning red")
  330.           term.redirect(mon)
  331.           tbl_color[attempt][k] = colors.red
  332.         end
  333.       end
  334.     end
  335.   end
  336.  
  337.   -- Debug
  338.   -- term.redirect(console)
  339.   -- for k,v in ipairs(tbl_wordle_userInput[attempt]) do
  340.     -- print(tbl_wotd_lock[k])
  341.   -- end
  342.   -- term.redirect(mon)
  343. end
  344.  
  345. function drawAlert(color,text)
  346.   local oldColor = term.getBackgroundColor()
  347.   paintutils.drawFilledBox(1,10,29,12,color)
  348.   mon.setCursorPos(1,11)
  349.   term.setBackgroundColor(oldColor)
  350.   centerText(text,colors.white,color)
  351. end
  352.  
  353. function parseKeyboard()
  354.   local letter
  355.   local event,side,touchX,touchY = os.pullEvent("monitor_touch")
  356.   speaker.playNote("hat",.5,0)
  357.   --mon.clearLine()
  358.   mon.setCursorPos(1,1)
  359.   mon.clearLine()
  360.   if debug == 1 then
  361.     mon.write("Debug: "..touchX.." "..touchY)
  362.   end
  363.   if touchY == 21 then
  364.     switch(touchX) : caseof {
  365.       [5] = function(x) action = "Q" end,
  366.       [7] = function(x) action = "W" end,
  367.       [9] = function(x) action = "E" end,
  368.       [11] = function(x) action = "R" end,
  369.       [13] = function(x) action = "T" end,
  370.       [15] = function(x) action = "Y" end,
  371.       [17] = function(x) action = "U" end,
  372.       [19] = function(x) action = "I" end,
  373.       [21] = function(x) action = "O" end,
  374.       [23] = function(x) action = "P" end,
  375.       default = function(x) action = nil end
  376.     }
  377.   elseif touchY == 22 then
  378.     switch(touchX) : caseof {
  379.       [6] = function(x) action = "A" end,
  380.       [8] = function(x) action = "S" end,
  381.       [10] = function(x) action = "D" end,
  382.       [12] = function(x) action = "F" end,
  383.       [14] = function(x) action = "G" end,
  384.       [16] = function(x) action = "H" end,
  385.       [18] = function(x) action = "J" end,
  386.       [20] = function(x) action = "K" end,
  387.       [22] = function(x) action = "L" end,
  388.       default = function(x) action = nil end
  389.     }
  390.   elseif touchY == 23 then
  391.     switch(touchX) : caseof {
  392.       [8] = function(x) action = "Z" end,
  393.       [10] = function(x) action = "X" end,
  394.       [12] = function(x) action = "C" end,
  395.       [14] = function(x) action = "V" end,
  396.       [16] = function(x) action = "B" end,
  397.       [18] = function(x) action = "N" end,
  398.       [20] = function(x) action = "M" end,
  399.       default = function(x) action = nil end
  400.     }
  401.   elseif touchY == 25 then
  402.     if touchX >= 8 and touchX <= 12 then
  403.       action = "delete"
  404.       -- Delete a character
  405.     elseif touchX >= 16 and touchX <= 20 then
  406.       action = "enter"
  407.       -- Check word if valid
  408.     end
  409.   else
  410.     action = nil
  411.   -- Do Nothing
  412.   end
  413.   return action
  414. end
  415.  
  416. -- Main Code Starts
  417. local old_mon = term.redirect(mon)
  418. local x,y = term.getSize()
  419. local player
  420.  
  421. while true do
  422.   mon.clear()
  423.   drawAlert(colors.blue,"Tap Screen To Play")
  424.   local event = os.pullEvent("monitor_touch")
  425.   speaker.playSound("entity.slime.squish")
  426.  
  427.   if debug == 1 then player = username else player = getPlayerName() end
  428.   debug(player)
  429.  
  430.   if findPlayer(player) then
  431.     local success,k,v = findPlayer(player)
  432.     if v < lib.getCurrentDate() then
  433.       --print("Player is able to play, updating date")
  434.       updatePlayer(player)
  435.       break
  436.       -- Update player in db
  437.     else
  438.       --print("You've already played today!")
  439.       drawAlert(colors.red,"You've played today!")
  440.       speaker.playNote("bass",1,4)
  441.       sleep(.3)
  442.       speaker.playNote("bass",1,0)
  443.       sleep(3)
  444.     end
  445.     --print(k,v)
  446.   else
  447.     --print("Player not found! Adding player to 'db'...")
  448.     updatePlayer(player)
  449.     break
  450.     -- Player is able to play
  451.   end
  452. end
  453.  
  454. mon.clear()
  455. mon.setCursorPos(1,1)
  456. mon.setTextScale(1)
  457.  
  458. wotd, wotd_num = getWotd()
  459. local lastLetter, correctLetters
  460. local tbl_index = 1
  461. local attempt = 1
  462. local yLine = attempt*2 + 6
  463.  
  464. term.clear()
  465. term.setCursorPos(1,1)
  466.  
  467. while true do
  468.   local yLine = attempt*2 + 6
  469.   term.setCursorPos(1,2)
  470.   mon.clearLine()
  471.   centerText("Good luck!")
  472.   term.setCursorPos(1,3)
  473.   mon.clearLine()
  474.   centerText(player)
  475.  
  476.   drawWord(attempt,yLine)
  477.   -- Draw Keyboard
  478.   drawKeyboard()
  479.   term.setCursorPos(1,26)
  480.   term.clearLine()
  481.   term.write("#"..wotd_num)
  482.   -- User selects up to 5 letters
  483.   local lastAction = parseKeyboard()
  484.  
  485.   -- Catches any nil characters
  486.   if lastAction == nil then
  487.     -- lastAction = ""
  488.   elseif lastAction == "delete" then
  489.     if tbl_index ~= 1 then
  490.       -- Delete last input
  491.       tbl_index = tbl_index - 1
  492.       tbl_wordle_userInput[attempt][tbl_index] = " "
  493.     elseif tbl_index == 1 then
  494.       -- Do Absolutely Nothing
  495.     else
  496.       -- Catch any errors
  497.     end
  498.   elseif lastAction == "enter" then
  499.     local check = 0
  500.     if tbl_index == 6 and isValidWord(attempt) then
  501.       compareInput(attempt) -- Check User Input
  502.       drawWord(attempt,yLine)   -- Draw Word on Screen
  503.       for k,v in ipairs(tbl_color[attempt]) do
  504.  
  505.         if v == colors.green then
  506.          check = check + 1
  507.         end
  508.       end
  509.       if check == 5 then
  510.         speaker.playSound("entity.player.levelup")
  511.         drawAlert(colors.green,"You  Win!")
  512.         debug("Winner!")
  513.         cashout(attempt)
  514.         sleep(5)
  515.         os.reboot()
  516.         --return false
  517.       end    
  518.       -- term.setCursorPos(1,1) -- For Debug
  519.       if attempt ~= 6 then
  520.         attempt = attempt + 1
  521.         tbl_index = 1
  522.       else
  523.         speaker.playSound("entity.creeper.primed")
  524.         drawAlert(colors.red,"You  Lose!")
  525.         sleep(5)
  526.         os.reboot()
  527.         --return false
  528.         -- Check if word is valid
  529.       end
  530.     else
  531.     end
  532.     -- Check if there are 5 letters in the tbl_color
  533.   elseif tbl_index > 5 then
  534.     speaker.playNote("bass",1,4)
  535.     sleep(.3)
  536.     speaker.playNote("bass",1,0)
  537.     -- Prevent player from adding new letters
  538.     -- Player either hits enter or delete
  539.   else
  540.     lastLetter = lastAction
  541.     tbl_wordle_userInput[attempt][tbl_index] = lastLetter
  542.     tbl_index = tbl_index + 1
  543.   end
  544.   --newLine()
  545.   if debug == 1 then
  546.     term.setCursorPos(1,2)
  547.     mon.clearLine()
  548.     print("Action: ",lastAction, "Index: ",tbl_index)
  549.     mon.clearLine()
  550.     --print("Word: ",attempt)
  551.     print("Attempt: ",attempt,"Match: ", correctLetters)
  552.   end
  553. end
  554.  
  555. --debug("Done")
Add Comment
Please, Sign In to add comment