Advertisement
Pinkishu

Untitled

Feb 19th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.04 KB | None | 0 0
  1. local field = {}
  2. local cX,cY = 1,1
  3. local width,height,mines
  4. local running = true
  5. local cursorSwap = true
  6. local updateTimer
  7.  
  8. local function getField(x,y)
  9.     if field[x] == nil then field[x] = {} end
  10.     if field[x][y] == nil then field[x][y] = { fType = "unknown", x=x, y=y } end
  11.     return field[x][y]
  12. end
  13.  
  14. local function getNum(cField)
  15.     if not cField.x or not cField.y then return " " end
  16.     local nums = 0
  17.     for dX=-1,1,1 do
  18.         for dY=-1,1,1 do
  19.             if dX ~= 0 or dY ~= 0 then
  20.                 if field[cField.x+dX] and field[cField.x+dX][cField.y+dY] then
  21.                     if field[cField.x+dX][cField.y+dY].mine then
  22.                         nums=nums+1
  23.                     end
  24.                 end
  25.             end
  26.         end
  27.     end
  28.     return (nums==0 and " " or string.format("%d",nums))
  29. end
  30.  
  31. local function getChar(field)
  32.     if field == nil then field = { fType = "unknown" } end
  33.     local tab = { flag="F", unknown="#", openMine = "X" }
  34.     if field.fType == "open" then
  35.         return getNum(field)
  36.     else
  37.         return tab[field.fType]
  38.     end
  39. end
  40.  
  41. local function readNum(min,max,intOnly)
  42.     local tX,tY = term.getCursorPos()
  43.  
  44.     while true do
  45.         term.setCursorPos(tX,tY)
  46.         term.clearLine()
  47.  
  48.         local inp = tonumber(read())
  49.         if inp and (intOnly == false or (math.abs(inp)==inp)) then
  50.             if (min == nil or inp >= min) and (max == nil or inp <= max) then
  51.                 return inp
  52.             end
  53.         end
  54.  
  55.     end
  56. end
  57.  
  58. local function putMine(x,y)
  59.     local field = getField(x,y)
  60.     if field.mine == true then return false end
  61.     field.mine = true
  62.     return true
  63. end
  64.  
  65. local function drawSpace(x,y)
  66.     term.setCursorPos(x,y)
  67.     if field[x] and field[x][y] then
  68.         term.write(getChar(getField(x,y)))
  69.     else
  70.         term.write(getChar(nil))
  71.     end    
  72. end
  73.  
  74. local function drawField()
  75.     term.clear()
  76.     for x=1,width,1 do
  77.         for y=1,height,1 do
  78.             drawSpace(x,y)
  79.         end
  80.     end
  81. end
  82.  
  83. term.clear()
  84. term.setCursorPos(1,1)
  85. print("Width of field?")
  86. width = readNum(1,30,true)
  87. print("Height of field?")
  88. height = readNum(1,30,true)
  89. print("Number of mines?")
  90. mines = readNum(1,width*height,true)
  91. term.setCursorBlink(false)
  92.  
  93. do
  94.     local minesPut = 0
  95.     while minesPut < mines do
  96.         if putMine(math.random(1,width),math.random(1,height)) then
  97.             minesPut = minesPut + 1
  98.         end
  99.     end
  100. end
  101.  
  102. local function updateCursor()
  103.     cursorSwap = not cursorSwap
  104.     if cursorSwap then
  105.         term.setCursorPos(cX,cY)
  106.         term.write("|")
  107.     else
  108.         drawSpace(cX,cY)
  109.     end
  110.     updateTimer = os.startTimer(0.3)
  111. end
  112.  
  113. local function moveCursor(nX,nY)
  114.     nX = math.min(width,math.max(1,nX))
  115.     nY = math.min(height,math.max(1,nY))
  116.     drawSpace(cX,cY)
  117.     cX,cY = nX,nY
  118.     cursorSwap = false
  119.     updateCursor()    
  120. end
  121.  
  122. local function sweep(originField)
  123.     local sweepList = { originField }
  124.     local function sweepCheck(x,y)
  125.         if x >= 1 and x <= width and y >= 1 and y <= height then
  126.             local pField = getField(x,y)
  127.             if pField.fType == "unknown" then
  128.                 pField.fType = "open"
  129.                 if getNum(pField) == " " then
  130.                     table.insert(sweepList,pField)
  131.                 end
  132.             end
  133.         end
  134.     end
  135.     while #sweepList > 0 do
  136.         local sweepField = table.remove(sweepList,1)
  137.         for dX=-1,1,1 do
  138.             for dY=-1,1,1 do
  139.                 if dX ~= 0 or dY ~= 0 then
  140.                     sweepCheck(sweepField.x+dX,sweepField.y+dY)
  141.                 end
  142.             end
  143.         end
  144.     end
  145.     drawField()
  146. end
  147.  
  148. local function drawMines()
  149.     for x=1,width,1 do
  150.         for y=1,height,1 do
  151.             if field[x] and field[x][y] then
  152.                 local fld = getField(x,y)
  153.                 if fld.mine then
  154.                     fld.fType = "openMine"
  155.                 end
  156.             end
  157.         end
  158.     end
  159. end
  160.  
  161. local function checkWin()
  162.     for x=1,width,1 do
  163.         for y=1,height,1 do
  164.             if not field[x] or not field[x][y] or (field[x][y].fType == "unknown" and field[x][y].mine == false) then
  165.                 return false
  166.             end
  167.         end
  168.     end
  169.     return true
  170. end
  171.  
  172. drawField()
  173. updateTimer = os.startTimer(0.3)
  174. while running do
  175.     local evData = {os.pullEvent()}
  176.  
  177.     if evData[1] == "timer" and evData[2] == updateTimer then
  178.         updateCursor()
  179.     elseif evData[1] == "key" then
  180.         if evData[2] == keys.f4 then
  181.             running = false
  182.         elseif evData[2] == keys.left then
  183.             moveCursor(cX-1,cY)
  184.         elseif evData[2] == keys.right then
  185.             moveCursor(cX+1,cY)
  186.         elseif evData[2] == keys.down then
  187.             moveCursor(cX,cY+1)
  188.         elseif evData[2] == keys.up then
  189.             moveCursor(cX,cY-1)
  190.         elseif evData[2] == keys.f then
  191.             local field = getField(cX,cY)
  192.             if field.fType == "unknown" then
  193.                 field.fType = "flag"
  194.             elseif field.fType == "flag" then
  195.                 field.fType = "unkown"
  196.             end
  197.             cursorSwap = true
  198.             updateCursor()
  199.         elseif evData[2] == keys.enter then
  200.             local field = getField(cX,cY)
  201.             if field.fType == "unknown" or field.fType == "flag" then
  202.                 if field.mine then
  203.                     drawMines()
  204.                     drawField()
  205.                     print("GAME OVER")
  206.                     sleep(5)
  207.                     running = false
  208.                 else
  209.                     field.fType = "open"
  210.                     if getNum(field) == " " then
  211.                         sweep(field)
  212.                     end
  213.                     if checkWin() then
  214.                         drawField()
  215.                         print("YOU WON")
  216.                         sleep(5)
  217.                         running = false
  218.                     end
  219.                 end
  220.             end
  221.         end
  222.     end
  223. end
  224.  
  225. term.clear()
  226. term.setCursorPos(1,1)
  227. term.setCursorBlink(true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement