Advertisement
Guest User

HighOrLow.lua

a guest
Aug 11th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.57 KB | None | 0 0
  1. --[[
  2. High or low
  3. By Konlab
  4.  
  5. ]]
  6. --init
  7. local version = 1.1
  8. local w,h = term.getSize()
  9. local maxnum = 20
  10. local theme = 2
  11. local themes = {{"Old",colors.orange,colors.black},{"New",colors.lightBlue,colors.black},{"Dark & Blue",colors.blue,colors.black},{"Green",colors.green,colors.white},{"White",colors.white,colors.black}}
  12. local money = 0
  13. local configpath = "/.hilodata"
  14. --functions
  15. local function cl()
  16.   term.setBackgroundColor(themes[theme][2])
  17.   term.setTextColor(themes[theme][3])
  18.   term.clear()
  19.   term.setCursorPos(1,1)
  20. end
  21. local function loaddata()
  22.     local file = fs.open(configpath,"r")
  23.     maxnum = tonumber(file.readLine())
  24.     theme = tonumber(file.readLine())
  25.     money = tonumber(file.readLine())
  26.     file.close()
  27. end
  28. local function savedata()
  29.     local file = fs.open(configpath,"w")
  30.     file.writeLine(maxnum)
  31.     file.writeLine(theme)
  32.     file.writeLine(money)
  33.     file.close()
  34. end
  35. local function init()
  36.     if fs.exists(configpath) then
  37.         loaddata() 
  38.     else
  39.         savedata() 
  40.     end
  41. end
  42. local function loadscreen()
  43.   term.setBackgroundColor(colors.orange)
  44.   term.setTextColor(colors.black)
  45.   term.clear()
  46.   local text = {"High or Low","By Konlab","version: "..version}
  47.   for i=1,#text do
  48.     term.setCursorPos(math.floor(w/2-#text[i]/2),math.floor(h/2-#text/2+i))
  49.     term.write(text[i])
  50.   end
  51.   init()
  52. end
  53. local function choosetheme()
  54. cl()
  55. i = 0
  56. for k,v in pairs(themes) do
  57.     i = i + 1
  58.     term.setBackgroundColor(v[2])
  59.     term.setTextColor(v[3])
  60.     term.setCursorPos(2,i)
  61.     term.write(v[1])
  62.     if k == theme then
  63.       term.write(" - selected")
  64.     end
  65. end
  66. local _,_,_,y = os.pullEvent("mouse_click")
  67. if y < #themes + 1 then
  68. theme = y  
  69. end
  70. end
  71. local function settings()
  72.         cl()
  73.         print([[Settings:
  74.            
  75.             Max number:
  76.             ]])
  77.         term.setCursorPos(1,h)
  78.         term.write("click anywhere else to go back")
  79.         term.setTextColor(colors.black)
  80.         term.setCursorPos(1,4)
  81.         term.setBackgroundColor(colors.white)
  82.         term.clearLine()
  83.         term.setCursorPos(2,4)
  84.         term.write(maxnum)
  85.         term.setCursorPos(1,6)
  86.         term.clearLine()
  87.         term.setCursorPos(1,6)
  88.         term.write(" Choose a theme")
  89.         local _,_,_,y = os.pullEvent("mouse_click")
  90.         if y == 4 then
  91.             term.setCursorPos(1,4)
  92.             term.clearLine()
  93.           term.setCursorPos(2,4)
  94.           maxnum = math.floor(tonumber(read()))
  95.             if maxnum < 5 then
  96.                 maxnum = 5
  97.             elseif maxnum > 100 then
  98.                 maxnum = 100
  99.             end
  100.         elseif y == 6 then
  101.           choosetheme()
  102.         end
  103.         savedata()
  104. end
  105. local function dIntf(currentNumber)
  106.         cl()
  107.         term.setBackgroundColor(themes[theme][2])
  108.         term.setCursorPos(1,3)
  109.         term.write("Number range: 1-" .. maxnum)
  110.         term.setCursorPos(1,5)
  111.         term.write("Current number: "..currentNumber)
  112.         term.setCursorPos(1,h-1)
  113.         term.write("Probability: "..(maxnum-currentNumber)* (math.floor(10000/maxnum)/100) .."% / " .. math.floor(10000/maxnum)/100 .. "% / " ..((currentNumber-1)*(math.floor(10000/maxnum)/100)).."%")
  114.         term.setCursorPos(1,h-2)
  115.         term.write("Money won: " .. tostring(money))
  116.         term.setCursorPos(1,math.floor(h/2)-1)
  117.         term.write("\\/")
  118.         term.setCursorPos(1,math.floor(h/2)+1)
  119.         term.write("/\\")
  120.         term.clearLine()
  121.                 for j=0,math.floor(w/5) do
  122.                         term.setCursorPos(1+j*math.floor(w/5),math.floor(h/2))
  123.                         local k
  124.                         if currentNumber+j > maxnum then k = currentNumber+j - maxnum else k = currentNumber+j end
  125.                         term.write(k)
  126.                 end
  127.         --not the same background
  128.         term.setTextColor(colors.black)
  129.         term.setBackgroundColor(colors.white)
  130.         term.setCursorPos(1,1)
  131.         term.clearLine()
  132.         term.setCursorPos(1,1)
  133.         term.write("High or Low")
  134.         term.setCursorPos(w-2,1)
  135.         term.write("S x")
  136.         term.setCursorPos(1,h)
  137.         term.setBackgroundColor(colors.green)
  138.         term.write("High")
  139.         term.setBackgroundColor(colors.red)
  140.         term.setCursorPos(w-2,h)
  141.         term.write("Low")
  142.         term.setBackgroundColor(colors.white)
  143.         term.setCursorPos(math.floor(w/2)-3,h)
  144.         term.write("Equal")
  145.        
  146. end
  147. local function animateNums(wonNumber,preNumber)
  148.         term.setBackgroundColor(themes[theme][2])
  149.         term.setTextColor(themes[theme][3])
  150.         for i=preNumber,wonNumber+maxnum do
  151.                 term.clearLine()
  152.                 for j=0,math.floor(w/5) do
  153.                   term.setCursorPos(1+j*math.floor(w/5),math.floor(h/2))
  154.                         local k
  155.                         if i+j > maxnum then k = i+j - maxnum else k = i+j end
  156.                         term.write(k)
  157.                 end
  158.                 sleep(0.1)
  159.         end
  160. end
  161. local function calculate(prevNumber,state)
  162.         local nextNumber = math.random(1,maxnum)
  163.         animateNums(nextNumber,prevNumber)
  164.         if nextNumber < prevNumber then
  165.             if state == 3 then
  166.                 money = money + math.floor(math.random(1,10000)^0.5)
  167.             else
  168.                 money = math.floor(money - money/2)
  169.             end
  170.         elseif nextNumber > prevNumber then
  171.            
  172.             if state == 1 then
  173.                 money = money + math.floor(math.random(1,10000)^0.5)
  174.             else
  175.                 money = math.floor(money - money/2)
  176.             end
  177.       else
  178.         --equal
  179.         if state == 2 then
  180.                 money = money + math.floor(math.random(10,100000)^0.5)--equal bonus :)
  181.             else
  182.                 money = math.floor(money - money/2)
  183.         end
  184.       end
  185.         return nextNumber
  186. end
  187. local function waitForActions(currentNumber)
  188.     while true do
  189.         dIntf(currentNumber)
  190.          local _,b,x,y = os.pullEvent("mouse_click")
  191.          local state
  192.          if b == 1 and y == h then
  193.               if x < 5 then state = 1 elseif x > w-3 then state = 3 elseif x > math.floor(w/2) - 3 and x < math.floor(w/2) + 3 then state = 2 end
  194.                      
  195.          end
  196.          if b == 1 and y == 1 and x == w then
  197.             savedata()
  198.                  term.setBackgroundColor(colors.black)
  199.                          term.setTextColor(colors.white)
  200.                   term.setCursorPos(1,1)
  201.                  term.clear()
  202.                  error("Bye",0)
  203.               end
  204.           if b == 1 and y == 1 and x == w-2 then
  205.                 settings()
  206.                 currentNumber = math.ceil(maxnum/2)
  207.           end
  208.           if b == 2 then
  209.                 settings()
  210.                 currentNumber = math.ceil(maxnum/2)
  211.           end
  212.           if type(state) == "number"  then currentNumber = calculate(currentNumber,state) end
  213.           return currentNumber
  214. end
  215. end
  216. --main loop
  217. loadscreen()
  218. sleep(3)
  219. local currentNumber = math.ceil(maxnum/2)
  220. while true do
  221.    currentNumber = waitForActions(currentNumber)
  222.    sleep(0.5)
  223. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement