Neui

minccweeper

Jun 26th, 2015
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.56 KB | None | 0 0
  1. --[[
  2.   +---=== minccweeper ===---+
  3.   | This is a 'recreation'  |
  4.   | of minesweeper for the  |
  5.   | Minecraft modification  |
  6.   | computercraft.          |
  7.   +-------------------------+
  8.           Made by Neui
  9. ]]
  10.  
  11. local currVer = 0 -- Version of this program
  12. local w, h = term.getSize() -- Size of the screen
  13. local running = true -- Program runs
  14.  
  15. local f = {} -- Functions
  16. f.draw = {} -- Drawing functions
  17.  
  18. local v = {} -- Variables
  19. v.state = 1 -- State 0 = startup, 1 = Main menu, 2 = Ingame
  20. v.sel = { ["title"] = 1; ["customField"] = 0; }
  21. v.titleSelection = {}
  22. v.field = { ["width"] = 9; ["height"] = 9; ["mines"] = 10; }
  23. v.time = 0
  24. v.draw = {}
  25. v.draw.fieldMoved = true
  26. v.draw.redraw = true
  27.  
  28. local c = {} -- constants
  29. c.name = "minCCweeper"
  30. c.title = "~[ " .. c.name .. " ]~"
  31. c.field = { ["maxwidth"] = 30; ["maxheight"] = 24; ["minmines"] = 10; ["maxmines"] = 668; }
  32. c.field.draw = {}
  33. c.field.draw.unknown = { ["char"] = '#'; ["fgmono"] = colors.white; ["bgadv"] = colors.gray; ["bgmono"] = colors.black; }
  34. c.field.draw.nothing = { ["char"] = ' '; }
  35. c.field.draw.mine = { ["char"] = 'O'; ["fgadv"] = colors.red; }
  36. c.field.draw.mightMine = { ["char"] = 'P'; ["fgadv"] = colors.lightGray; }
  37. c.field.draw.numbers = { ["fg"] = { colors.blue, colors.green, colors.red, colors.blue, colors.brown, colors.lightBlue, colors.gray, colors.lightGray }; }
  38. c.field.presets = {} -- Also taken form windows minesweeper
  39. c.field.presets.beginner = { ["name"] = "Beginner"; ["width"] =  9; ["height"] =  9; ["mines"] = 10; }
  40. c.field.presets.advanced = { ["name"] = "Advanced"; ["width"] = 16; ["height"] = 16; ["mines"] = 40; }
  41. c.field.presets.pro =  { ["name"] = "Professional"; ["width"] = 30; ["height"] = 16; ["mines"] = 99; }
  42.  
  43. local playfield = {} -- Contains all of the bombs etc.
  44. local playerfiled = {} -- What the players sees
  45.  
  46. local function csleep(sec)
  47.   local timer, p1 = os.startTimer(tonumber(sec or 0))
  48.   repeat
  49.     local _, p1 = os.pullEventRaw("timer")
  50.   until p1 == timer
  51. end
  52.  
  53. -- Main Drawing Loop
  54. function f.drawLoop()
  55.   while running do
  56.     if v.draw.redraw then
  57.       f.draw.setTBGColor(term, colors.white, colors.black)
  58.       term.clear()
  59.     end
  60.     if v.state == 1 or v.state == 2 then -- Main Selection
  61.       f.draw.title(term, w, h)
  62.       f.draw.titleSelection(term, w, h)
  63.       f.draw.fieldSizeAndWidth(term, w, h)
  64.       if v.state == 2 then
  65.         term.setCursorPos(w, h-1)
  66.         term.setCursorBlink(true)
  67.       else
  68.        
  69.       end
  70.     elseif v.state == 3 then -- ingame
  71.       if v.draw.fieldMoved or v.draw.redraw then
  72.         f.draw.field(term, w, h, field)
  73.       end
  74.       f.draw.footer(term, w, h)
  75.     end
  76.     csleep(0)
  77.   end
  78. end
  79.  
  80. function f.draw.field(term, w, h, field)
  81.  
  82. end
  83.  
  84. function f.draw.footer(term, w, h)
  85.   -- [FHxFW MI     TIMEs]
  86.   f.draw.setBGColor(term, colors.black)
  87.   term.setCursorPos(1, h)
  88.   f.draw.setTextColor(term, colors.green)
  89.   term.write(tostring(v.field.width))
  90.   f.draw.setTextColor(term, colors.white)
  91.   term.write("x")
  92.   f.draw.setTextColor(term, colors.green)
  93.   term.write(tostring(v.field.height))
  94.   f.draw.setTextColor(term, colors.brown)
  95.   term.write(" " .. tostring(v.field.mines))
  96. end
  97.  
  98. function f.draw.title(term, w, h)
  99.   term.setCursorPos((w/2)-(#c.title/2), h/#v.titleSelection)
  100.   term.write(c.title)
  101. end
  102.  
  103. function f.draw.titleSelection(term, w, h)
  104.   for k, val in ipairs(v.titleSelection) do
  105.     term.setCursorPos((w/2)-((#val.name+2)/2), (h/2)-(#v.titleSelection/2)+k)
  106.     if k == v.sel.title then
  107.       f.draw.setTBGColor(term, colors.black, colors.lightGray, colors.white)
  108.     else
  109.       f.draw.setTBGColor(term, colors.white, colors.black)
  110.     end
  111.     term.write(" " .. val.name .. " ")
  112.   end
  113. end
  114.  
  115. function f.draw.fieldSizeAndWidth(term, w, h)
  116.   term.setCursorPos(w/2-7, h-1)
  117.   f.draw.setTBGColor(term, colors.white, colors.black)
  118.   term.write("  x       Mines")
  119.  
  120.   term.setCursorPos(w/2-7, h-1)
  121.   f.draw.setTBGColor(term, colors.orange, colors.black, colors.white)
  122.   if v.field.width < 10 then
  123.     term.write("0")
  124.   end
  125.   term.write(tostring(v.field.width))
  126.  
  127.   term.setCursorPos(w/2-4, h-1)
  128.   if v.field.height < 10 then
  129.     term.write("0")
  130.   end
  131.   term.write(tostring(v.field.height))
  132.  
  133.   term.setCursorPos(w/2-1, h-1)
  134.   if v.field.mines < 100 then
  135.     term.write("0")
  136.   elseif v.field.mines < 10 then
  137.     term.write("00")
  138.   end
  139.   term.write(tostring(v.field.mines))
  140. end
  141.  
  142. function f.draw.setTextColor(term, adv, mono)
  143.   local mono, adv = tonumber(mono) or tonumber(adv) or 1, tonumber(adv) or 1
  144.   if term.isColor and term.isColor() then
  145.     term.setTextColor(adv)
  146.   else
  147.     term.setTextColor(mono)
  148.   end
  149. end
  150.  
  151. function f.draw.setBGColor(term, adv, mono)
  152.   local mono, adv = tonumber(mono) or tonumber(adv) or colors.black, tonumber(adv) or colors.black
  153.   if term.isColor and term.isColor() then
  154.     term.setBackgroundColor(adv)
  155.   else
  156.     term.setBackgroundColor(mono)
  157.   end
  158. end
  159.  
  160. function f.draw.setTBGColor(term, advt, advbg, monot, monobg)
  161.   f.draw.setTextColor(term, advt, monot)
  162.   f.draw.setBGColor(term, advbg, monobg)
  163. end
  164.  
  165. function f.getFromFieldPos(x, y, field)
  166.   return field[x*y]
  167. end
  168.  
  169. function f.doSelection(sel, tsel, field)
  170.   if tsel[sel]["function"] == "exit" then
  171.     running = false
  172.   elseif tsel[sel]["function"] == "custom" then
  173.      
  174.   else
  175.     v.state = 3
  176.   end
  177. end
  178.  
  179. function f.inputLoop()
  180.   while running do
  181.     local ev, p1, p2, p3, p4, p5 = os.pullEventRaw()
  182.     if ev == "terminate" then
  183.       running = false
  184.     elseif ev == "term.resize" then
  185.       w, h = term.getSize()
  186.     elseif ev == "key" then
  187.       if v.state == 1 then -- Main menu
  188.         if p1 == 200 then -- Up Arrow
  189.           v.sel.title = ((v.sel.title - 2) % #v.titleSelection) + 1
  190.         elseif p1 == 208 then -- Down Arrow
  191.           v.sel.title = (v.sel.title % #v.titleSelection) + 1
  192.         elseif p1 == 28 then -- Enter
  193.           f.doSelection(v.sel.title, v.titleSelection, v.field)
  194.         end
  195.         if not v.titleSelection[v.sel.title]["function"] then
  196.           v.field.width = v.titleSelection[v.sel.title]["width"]
  197.           v.field.height = v.titleSelection[v.sel.title]["height"]
  198.           v.field.mines = v.titleSelection[v.sel.title]["mines"]
  199.         end
  200.       elseif v.mode == 2 then -- Custom selection
  201.        
  202.       elseif v.mode == 3 then -- Ingame
  203.      
  204.       end
  205.     elseif ev == "mouse_click" or ev == "mouse_drag" then
  206.       if v.state == 1 then -- Main menu
  207.         for k = 1, #v.titleSelection do
  208.           local hpos = math.floor((h/2)-(#v.titleSelection/2)+k)
  209.           if hpos == p3 then
  210.             if p1 == 1 and ev == "mouse_click" then
  211.               v.sel.title = k
  212.               os.queueEvent("key", 28) -- Enter, i am too lazy
  213.             else
  214.               if v.sel.title == k and ev == "mouse_click" then
  215.                 os.queueEvent("key", 28) -- Enter
  216.               else
  217.                 v.sel.title = k
  218.               end
  219.             end
  220.            
  221.             break
  222.           end
  223.         end
  224.       elseif v.mode == 2 then -- Custom selection
  225.        
  226.       elseif v.mode == 3 then -- Ingame
  227.        
  228.       end
  229.     end
  230.   end
  231. end
  232.  
  233. -- Main Program
  234. local function main()
  235.   parallel.waitForAny(f.inputLoop, f.drawLoop)
  236. end
  237.  
  238. -- Initalise
  239. local function init()
  240.   local notice = false
  241.   f.draw.setTextColor(term, colors.white)
  242.  
  243.   -- Setup the titlescreen selection
  244.   for k, val in pairs(c.field.presets) do
  245.     v.titleSelection[#v.titleSelection + 1] = val
  246.   end
  247.   v.titleSelection[#v.titleSelection + 1] = {["name"] = "Custom"; ["function"] = "custom";}
  248.   v.titleSelection[#v.titleSelection + 1] = {["name"] = "Exit"; ["function"] = "exit"; }
  249.  
  250.  
  251.   if notice and running then
  252.     write("\nPress a key to continue... (or wait 10s)")
  253.     local tim, ev, p1 = os.startTimer(10)
  254.     repeat
  255.       ev, p1 = os.pullEventRaw()
  256.     until (ev == "key") or (ev == "timer" and p1 == tim)
  257.   end
  258. end
  259.  
  260. local est, emsg = pcall(init)
  261. if not running or not est then -- error
  262.   if not est then
  263.    write(string.format("\nAn error happend during init:\n%s\n", tostring(emsg)))
  264.   else
  265.    write("\nProgram terminated.\n")
  266.   end
  267. else
  268.   local _gsStart = os.clock()
  269.   local est, emsg = pcall(main)
  270.   term.setTextColor(1)
  271.   term.setBackgroundColor(32768)
  272.   term.clear()
  273.   term.setCursorPos(1,1)
  274.   if not est then
  275.    write(string.format("An error happend:\n%s\n", tostring(emsg)))
  276.   else
  277.    write("Program terminated.\n")
  278.   end
  279.   local _gs = math.floor(os.clock() - _gsStart + 0.5)
  280.   local _h, _m, _s = _gs / 3600, (_gs / 60) % 60, _gs % 60
  281.   write(string.format("Ran for %uh%um%us (%us)\n", _h, _m, _s, _gs))
  282. end
Advertisement
Add Comment
Please, Sign In to add comment