Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local lib = require("lib")
- local pd = peripheral.find("playerDetector")
- local filename = "db.txt"
- local dictionary = require("dictionary")
- local mon = peripheral.find("monitor")
- local speaker = peripheral.find("speaker")
- local console = term.redirect(mon)
- --local prize = 1
- local playerDetectorSensitivity = 3
- local debug = 0
- local username = "Bob"
- local referenceDate = os.time{day=2,year=2022,month=2}
- local refConvert = referenceDate/24/60/60
- local wotd, wotd_num
- local tbl_alphabet = { -- 0:Unknown,1: Non-present, 2:Present, 3:Confirmed
- ["A"] = 0,
- ["B"] = 0,
- ["C"] = 0,
- ["D"] = 0,
- ["E"] = 0,
- ["F"] = 0,
- ["G"] = 0,
- ["H"] = 0,
- ["I"] = 0,
- ["J"] = 0,
- ["K"] = 0,
- ["L"] = 0,
- ["M"] = 0,
- ["N"] = 0,
- ["O"] = 0,
- ["P"] = 0,
- ["Q"] = 0,
- ["R"] = 0,
- ["S"] = 0,
- ["T"] = 0,
- ["U"] = 0,
- ["V"] = 0,
- ["W"] = 0,
- ["X"] = 0,
- ["Y"] = 0,
- ["Z"] = 0
- }
- local tbl_wordle_userInput = {
- [1] = {" "," "," "," "," "}, -- 10 coins
- [2] = {" "," "," "," "," "}, -- 5 coins
- [3] = {" "," "," "," "," "}, -- 4 coins
- [4] = {" "," "," "," "," "}, -- 3 coins
- [5] = {" "," "," "," "," "}, -- 2 coins
- [6] = {" "," "," "," "," "} -- 1 coins
- }
- local tbl_color = {
- [1] = {colors.black,colors.black,colors.black,colors.black,colors.black},
- [2] = {colors.black,colors.black,colors.black,colors.black,colors.black},
- [3] = {colors.black,colors.black,colors.black,colors.black,colors.black},
- [4] = {colors.black,colors.black,colors.black,colors.black,colors.black},
- [5] = {colors.black,colors.black,colors.black,colors.black,colors.black},
- [6] = {colors.black,colors.black,colors.black,colors.black,colors.black}
- }
- local tbl_wotd_lock = {false,false,false,false,false,false}
- function getWotd()
- local _todaysDate = os.date("*t")
- local _year = _todaysDate["year"]
- local _yday = _todaysDate["yday"]
- local _todaysDateConvert = (((_year-1970)*365)+((_year-1970)*.25)+_yday)
- local _daysGap = math.floor(_todaysDateConvert - refConvert)
- local _wotd = string.upper(dictionary.words[_daysGap])
- return _wotd, _daysGap
- end
- function getPlayerName()
- local _player = pd.getPlayersInRange(playerDetectorSensitivity)
- -- Get the name of the player in index one of table 'player'
- return _player[1]
- end
- function findPlayer(_player)
- local _file = fs.open(filename,"r")
- local _data = textutils.unserialize(_file.readAll())
- for k,v in pairs(_data) do
- --print(k,v)
- if _player == k then return true,k,v end
- end
- _file.close()
- return false
- end
- function updatePlayer(_player)
- local _DBfile = fs.open(filename,"r")
- local database = textutils.unserialize(_DBfile.readAll())
- _DBfile.close()
- local _file = fs.open(filename,"w")
- database[_player] = lib.getCurrentDate()
- local _tbl = textutils.serialize(database)
- _file.writeLine(_tbl)
- _file.close()
- end
- function debug(x)
- term.redirect(console)
- print(x)
- term.redirect(mon)
- end
- function isValidWord(attempt)
- local word = table.concat(tbl_wordle_userInput[attempt])
- for k,v in ipairs(dictionary.words) do
- if word == string.upper(v) then debug("Valid Word") return true end
- end
- for k,v in ipairs(dictionary.words_ok) do
- if word == string.upper(v) then debug("Valid Word") return true end
- end
- debug("Invalid Word")
- speaker.playNote("bass",1,4)
- sleep(.3)
- speaker.playNote("bass",1,0)
- return false
- end
- function convertTableToString()
- return table.concat(tbl_wordle_userInput[attempt])
- end
- function convertStringToTable(str)
- local t = {}
- for i = 1, #str do
- t[i] = str:sub(i,i)
- end
- return t
- end
- function switch(c)
- local swtbl = {
- casevar = c,
- caseof = function (self,code)
- local f
- if (self.casevar) then
- f = code[self.casevar] or code.default
- else
- f = code.missing or code.default
- end
- if f then
- if type(f) == "function" then
- return f(self.casevar,self)
- else
- error("case "..tostring(self.casevar).."not a function")
- end
- end
- end
- }
- return swtbl
- end
- function alpha(letter)
- local x = tbl_alphabet[letter]
- if x == 0 then
- term.write(letter.." ")
- elseif x == 1 then
- term.setTextColor(colors.red)
- term.write(letter.." ")
- term.setTextColor(colors.white)
- elseif x == 2 then
- term.setTextColor(colors.yellow)
- term.write(letter.." ")
- term.setTextColor(colors.white)
- elseif x == 3 then
- term.setTextColor(colors.green)
- term.write(letter.." ")
- term.setTextColor(colors.white)
- else
- debug("What")
- end
- end
- function drawKeyboard()
- mon.setCursorPos(5,21)
- alpha("Q") alpha("W") alpha("E") alpha("R") alpha("T") alpha("Y") alpha("U") alpha("I") alpha("O") alpha("P")
- mon.setCursorPos(6,22)
- alpha("A") alpha("S") alpha("D") alpha("F") alpha("G") alpha("H") alpha("J") alpha("K") alpha("L")
- mon.setCursorPos(8,23)
- alpha("Z") alpha("X") alpha("C") alpha("V") alpha("B") alpha("N") alpha("M")
- newLine()
- newLine()
- centerText("Erase Enter")
- mon.setCursorPos(1,1)
- end
- function hideKeyboard()
- mon.setCursorPos(5,21)
- mon.setCursorPos(6,22)
- mon.setCursorPos(8,23)
- end
- function center(x)
- local x,y = term.getSize()
- end
- function centerText(text,fontColor,bgColor)
- if bgColor == nil then
- bgColor = colors.black
- end
- if fontColor == nil then
- fontColor = colors.white
- end
- local x,y = term.getSize()
- local curPosX, curPosY = mon.getCursorPos()
- local length = string.len(text)
- mon.setCursorPos(x/2-length/2,curPosY)
- local oldTxt = term.getTextColor()
- local oldBg = term.getBackgroundColor()
- term.setTextColor(fontColor)
- term.setBackgroundColor(bgColor)
- mon.write(text)
- term.setBackgroundColor(oldBg)
- term.setTextColor(oldTxt)
- end
- function drawBox(x,y,color)
- local oldColor = term.getBackgroundColor()
- paintutils.drawPixel(x,y,color)
- term.setBackgroundColor(oldColor)
- end
- function drawWord(attempt,yPos)
- local oldColor = term.getBackgroundColor()
- local xPos = 10
- for k,v in ipairs(tbl_wordle_userInput[attempt]) do
- if v == " " then
- term.setCursorPos(xPos,yPos)
- drawBox(xPos,yPos,colors.white)
- else
- local oldColor = term.getBackgroundColor()
- term.setBackgroundColor(tbl_color[attempt][k])
- term.setCursorPos(xPos,yPos)
- term.write(v)
- term.setBackgroundColor(oldColor)
- end
- xPos = xPos + 2
- end
- tbl_wotd_lock = {false,false,false,false,false,false} -- Reset
- end
- function newLine()
- local curPosX, curPosY = mon.getCursorPos()
- mon.setCursorPos(1,curPosY+1)
- end
- function cashout(_attempt)
- local prize
- if _attempt == 1 then prize = 10
- elseif _attempt == 2 then prize = 5
- elseif _attempt == 3 then prize = 4
- elseif _attempt == 4 then prize = 3
- elseif _attempt == 5 then prize = 2
- elseif _attempt == 6 then prize = 1
- else print("Cashout Error!") end
- for i=1,prize do
- redstone.setOutput("bottom",true)
- sleep(.15)
- redstone.setOutput("bottom",false)
- sleep(.15)
- end
- end
- function compareInput(attempt)
- local tbl = convertStringToTable(wotd)
- --Debug
- -- term.redirect(console)
- -- for k,v in ipairs(tbl_wordle_userInput[attempt]) do
- -- print(k,v)
- -- end
- -- term.redirect(mon)
- -- First do all correct letters -- GREEN
- for k,v in ipairs(tbl_wordle_userInput[attempt]) do -- v: character of user guess
- if (v == tbl[k]) and tbl_wotd_lock[k] == false then -- tbl[k]: character of wotd
- if (tbl_alphabet[v] == 0) or (tbl_alphabet[v] == 2) then
- tbl_alphabet[v] = 3
- end
- term.redirect(console)
- print("Letter",v,"is in the correct index. Assigning green")
- term.redirect(mon)
- tbl_color[attempt][k] = colors.green
- tbl_wotd_lock[k] = true
- end
- end
- for k,v in ipairs(tbl_wordle_userInput[attempt]) do -- # 5 : A
- term.redirect(console)
- --print(tbl_wotd_lock[k])
- term.redirect(mon)
- for wotd_k,wotd_v in ipairs(tbl) do -- # 4 : A
- --term.redirect(console)
- --print(wotd_k,wotd_v)
- --term.redirect(mon)
- 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
- if tbl_alphabet[v] == 0 then
- tbl_alphabet[v] = 2
- end
- if tbl_color[attempt][k] == colors.black then
- term.redirect(console)
- print("Letter",v,"is NOT in correct index but present. Assigning yellow")
- term.redirect(mon)
- tbl_color[attempt][k] = colors.orange
- tbl_wotd_lock[wotd_k] = true -- I feel like this should me 'wotd_k' instead of 'k'
- end
- --tbl_wotd_lock[wotd_k] = true -- I feel like this should me 'wotd_k' instead of 'k'
- end
- end
- end
- for k,v in ipairs(tbl_wordle_userInput[attempt]) do
- --debug(tbl_color[attempt][k])
- if (v ~= tbl[k]) and (tbl_color[attempt][k] == colors.black) then
- if tbl_alphabet[v] == 0 then
- tbl_alphabet[v] = 1
- end
- if tbl_color[attempt][k] == colors.black then
- term.redirect(console)
- if tbl_color[attempt][k] == colors.black then
- print("Letter",v,"is NOT present. Assigning red")
- term.redirect(mon)
- tbl_color[attempt][k] = colors.red
- end
- end
- end
- end
- -- Debug
- -- term.redirect(console)
- -- for k,v in ipairs(tbl_wordle_userInput[attempt]) do
- -- print(tbl_wotd_lock[k])
- -- end
- -- term.redirect(mon)
- end
- function drawAlert(color,text)
- local oldColor = term.getBackgroundColor()
- paintutils.drawFilledBox(1,10,29,12,color)
- mon.setCursorPos(1,11)
- term.setBackgroundColor(oldColor)
- centerText(text,colors.white,color)
- end
- function parseKeyboard()
- local letter
- local event,side,touchX,touchY = os.pullEvent("monitor_touch")
- speaker.playNote("hat",.5,0)
- --mon.clearLine()
- mon.setCursorPos(1,1)
- mon.clearLine()
- if debug == 1 then
- mon.write("Debug: "..touchX.." "..touchY)
- end
- if touchY == 21 then
- switch(touchX) : caseof {
- [5] = function(x) action = "Q" end,
- [7] = function(x) action = "W" end,
- [9] = function(x) action = "E" end,
- [11] = function(x) action = "R" end,
- [13] = function(x) action = "T" end,
- [15] = function(x) action = "Y" end,
- [17] = function(x) action = "U" end,
- [19] = function(x) action = "I" end,
- [21] = function(x) action = "O" end,
- [23] = function(x) action = "P" end,
- default = function(x) action = nil end
- }
- elseif touchY == 22 then
- switch(touchX) : caseof {
- [6] = function(x) action = "A" end,
- [8] = function(x) action = "S" end,
- [10] = function(x) action = "D" end,
- [12] = function(x) action = "F" end,
- [14] = function(x) action = "G" end,
- [16] = function(x) action = "H" end,
- [18] = function(x) action = "J" end,
- [20] = function(x) action = "K" end,
- [22] = function(x) action = "L" end,
- default = function(x) action = nil end
- }
- elseif touchY == 23 then
- switch(touchX) : caseof {
- [8] = function(x) action = "Z" end,
- [10] = function(x) action = "X" end,
- [12] = function(x) action = "C" end,
- [14] = function(x) action = "V" end,
- [16] = function(x) action = "B" end,
- [18] = function(x) action = "N" end,
- [20] = function(x) action = "M" end,
- default = function(x) action = nil end
- }
- elseif touchY == 25 then
- if touchX >= 8 and touchX <= 12 then
- action = "delete"
- -- Delete a character
- elseif touchX >= 16 and touchX <= 20 then
- action = "enter"
- -- Check word if valid
- end
- else
- action = nil
- -- Do Nothing
- end
- return action
- end
- -- Main Code Starts
- local old_mon = term.redirect(mon)
- local x,y = term.getSize()
- local player
- while true do
- mon.clear()
- drawAlert(colors.blue,"Tap Screen To Play")
- local event = os.pullEvent("monitor_touch")
- speaker.playSound("entity.slime.squish")
- if debug == 1 then player = username else player = getPlayerName() end
- debug(player)
- if findPlayer(player) then
- local success,k,v = findPlayer(player)
- if v < lib.getCurrentDate() then
- --print("Player is able to play, updating date")
- updatePlayer(player)
- break
- -- Update player in db
- else
- --print("You've already played today!")
- drawAlert(colors.red,"You've played today!")
- speaker.playNote("bass",1,4)
- sleep(.3)
- speaker.playNote("bass",1,0)
- sleep(3)
- end
- --print(k,v)
- else
- --print("Player not found! Adding player to 'db'...")
- updatePlayer(player)
- break
- -- Player is able to play
- end
- end
- mon.clear()
- mon.setCursorPos(1,1)
- mon.setTextScale(1)
- wotd, wotd_num = getWotd()
- local lastLetter, correctLetters
- local tbl_index = 1
- local attempt = 1
- local yLine = attempt*2 + 6
- term.clear()
- term.setCursorPos(1,1)
- while true do
- local yLine = attempt*2 + 6
- term.setCursorPos(1,2)
- mon.clearLine()
- centerText("Good luck!")
- term.setCursorPos(1,3)
- mon.clearLine()
- centerText(player)
- drawWord(attempt,yLine)
- -- Draw Keyboard
- drawKeyboard()
- term.setCursorPos(1,26)
- term.clearLine()
- term.write("#"..wotd_num)
- -- User selects up to 5 letters
- local lastAction = parseKeyboard()
- -- Catches any nil characters
- if lastAction == nil then
- -- lastAction = ""
- elseif lastAction == "delete" then
- if tbl_index ~= 1 then
- -- Delete last input
- tbl_index = tbl_index - 1
- tbl_wordle_userInput[attempt][tbl_index] = " "
- elseif tbl_index == 1 then
- -- Do Absolutely Nothing
- else
- -- Catch any errors
- end
- elseif lastAction == "enter" then
- local check = 0
- if tbl_index == 6 and isValidWord(attempt) then
- compareInput(attempt) -- Check User Input
- drawWord(attempt,yLine) -- Draw Word on Screen
- for k,v in ipairs(tbl_color[attempt]) do
- if v == colors.green then
- check = check + 1
- end
- end
- if check == 5 then
- speaker.playSound("entity.player.levelup")
- drawAlert(colors.green,"You Win!")
- debug("Winner!")
- cashout(attempt)
- sleep(5)
- os.reboot()
- --return false
- end
- -- term.setCursorPos(1,1) -- For Debug
- if attempt ~= 6 then
- attempt = attempt + 1
- tbl_index = 1
- else
- speaker.playSound("entity.creeper.primed")
- drawAlert(colors.red,"You Lose!")
- sleep(5)
- os.reboot()
- --return false
- -- Check if word is valid
- end
- else
- end
- -- Check if there are 5 letters in the tbl_color
- elseif tbl_index > 5 then
- speaker.playNote("bass",1,4)
- sleep(.3)
- speaker.playNote("bass",1,0)
- -- Prevent player from adding new letters
- -- Player either hits enter or delete
- else
- lastLetter = lastAction
- tbl_wordle_userInput[attempt][tbl_index] = lastLetter
- tbl_index = tbl_index + 1
- end
- --newLine()
- if debug == 1 then
- term.setCursorPos(1,2)
- mon.clearLine()
- print("Action: ",lastAction, "Index: ",tbl_index)
- mon.clearLine()
- --print("Word: ",attempt)
- print("Attempt: ",attempt,"Match: ", correctLetters)
- end
- end
- --debug("Done")
Add Comment
Please, Sign In to add comment