Advertisement
Guest User

minesweeper.lua

a guest
Jun 15th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.33 KB | None | 0 0
  1. local m = peripheral.wrap("front")
  2.  
  3. local mines = {}
  4. local numbers = {}
  5. local revealed = {}
  6.  
  7. local mineColors = {colors.white,colors.yellow,colors.orange,colors.red,colors.purple,colors.blue,colors.cyan}
  8.  
  9. local xm, ym = m.getSize()
  10.  
  11. local ignoreTimer = 0
  12.  
  13. function generateMines()
  14.     ignoreTimer = -1
  15.     for x = 1,xm do
  16.         mines[x] = {}
  17.         revealed[x] = {}
  18.         for y = 1,ym do
  19.             mines[x][y] = math.max(0,math.random(-8,1))
  20.             revealed[x][y] = 0
  21.         end
  22.     end
  23.    
  24.     for x = 1,xm do
  25.         numbers[x] = {}
  26.         for y = 1,ym do
  27.             numbers[x][y] = getNumber(x,y)
  28.         end
  29.     end
  30. end
  31.  
  32. function getNumber(x,y)
  33.     local total = 0
  34.     local xu = x+1
  35.     local xl = x-1
  36.     local yu = y+1
  37.     local yl = y-1
  38.     if xu > xm then xu = xm end
  39.     if xl < 1 then xl = 1 end
  40.     if yu > ym then yu = ym end
  41.     if yl < 1 then yl = 1 end
  42.     for x = xl,xu do
  43.         for y = yl,yu do
  44.             total = total + mines[x][y]
  45.         end
  46.     end
  47.     return total
  48. end
  49.  
  50. titleScreen = true
  51.  
  52. minesweeperIcon = {
  53. {0,0,0,0,0,0,0,0,0,0,0,0,0},
  54. {0,0,0,0,1,1,1,1,1,0,0,0,0},
  55. {0,0,0,1,1,1,1,1,1,1,0,0,0},
  56. {0,0,0,1,1,1,1,1,1,1,0,0,0},
  57. {0,0,0,0,1,1,1,1,1,0,0,0,0},
  58. {0,0,0,0,0,0,0,0,0,0,0,0,0}
  59. }
  60.  
  61. colorTable = {
  62.     [0] = colors.black,
  63.     [1] = colors.gray
  64. }
  65.  
  66. function render()
  67.     if titleScreen then
  68.         ignoreTimer = 9999
  69.         m.setBackgroundColor(colors.black)
  70.         m.clear()
  71.         for y, data in pairs(minesweeperIcon) do
  72.             for x, color in pairs(data) do
  73.                 m.setCursorPos(x,y)
  74.                 m.setBackgroundColor(colorTable[color])
  75.                 m.write(" ")
  76.             end
  77.         end
  78.         sleep(3)
  79.     else
  80.         for x = 1,xm do
  81.             for y = 1,ym do
  82.                 m.setBackgroundColor(colors.black)
  83.                 if revealed[x][y] == 0 then
  84.                     m.setBackgroundColor(colors.gray)
  85.                     m.setCursorPos(x,y)
  86.                     m.write(" ")
  87.                 elseif mines[x][y] == 1 then
  88.                     m.setBackgroundColor(colors.red)
  89.                     m.setCursorPos(x,y)
  90.                     m.write(" ")
  91.                 else
  92.                     m.setCursorPos(x,y)
  93.                     local text = numbers[x][y]
  94.                     if text ~= 0 then m.setTextColor(mineColors[text]) end
  95.                     if text == 0 then text = " " end
  96.                     m.write(text)
  97.                 end
  98.             end
  99.         end
  100.     end
  101. end
  102.  
  103. generateMines()
  104.  
  105. function reveal(x,y)
  106.     if revealed[x][y] == 0 then
  107.         revealed[x][y] = 1
  108.         if numbers[x][y] == 0 then
  109.             reveal(math.min(x+1,xm),y)
  110.             reveal(math.max(1,x-1),y)
  111.             reveal(x,math.min(y+1,ym))
  112.             reveal(x,math.max(1,y-1))
  113.         end
  114.     end
  115. end
  116.  
  117. function checkWin()
  118.     for x = 1,xm do
  119.         for y = 1,ym do
  120.             if mines[x][y] == 0 and revealed[x][y] == 0 then
  121.                 return false
  122.             end
  123.         end
  124.     end
  125.     return true
  126. end
  127.  
  128. while true do
  129.     render()
  130.     local event, side, x, y = os.pullEvent()
  131.     if event == "monitor_touch" then
  132.         if not titleScreen then
  133.             reveal(x,y)
  134.             if mines[x][y] == 1 then
  135.                 lose = true
  136.             elseif checkWin() then
  137.                 win = true
  138.             end
  139.         else
  140.             titleScreen = false
  141.             generateMines()
  142.         end
  143.         ignoreTimer = ignoreTimer + 1
  144.         os.startTimer(15)
  145.     elseif event == "timer" then
  146.         if ignoreTimer > 0 then
  147.             ignoreTimer = ignoreTimer - 1
  148.         else
  149.             titleScreen = true
  150.         end
  151.     end
  152.     if win then
  153.         ignoreTimer = ignoreTimer + 1
  154.         render()
  155.         m.setBackgroundColor(colors.lime)
  156.         for x = 1,xm do
  157.             for y = 1,ym do
  158.                 if mines[x][y] == 1 then
  159.                     m.setCursorPos(x,y)
  160.                     m.write(" ")
  161.                 end
  162.             end
  163.         end
  164.         sleep(3)
  165.         generateMines()
  166.         win = false
  167.         os.startTimer(0.1)
  168.         titleScreen = true
  169.     elseif lose then
  170.         ignoreTimer = ignoreTimer + 1
  171.         render()
  172.         sleep(3)
  173.         generateMines()
  174.         lose = false
  175.         os.startTimer(0.1)
  176.         titleScreen = true
  177.     end
  178. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement