jay5476

Minesweeper v0.2

Aug 30th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.95 KB | None | 0 0
  1. w,h = term.getSize()
  2. move = 0
  3. --mColor = colors.black -- mine color: hidden
  4. mColor = colors.red --show mines
  5. rColor = colors.red -- mine color: revealed
  6. bColor = colors.black -- background color
  7. cColor = colors.blue -- color when clicked on
  8. ocColor = colors.green
  9. fColor = colors.lime -- color when square is flagged
  10. --[[default colors
  11. mColor = colors.black
  12. rColor = colors.red
  13. bColor = colors.black
  14. cColor = colors.blue
  15. ]]
  16. start = os.clock()
  17. statement = "Lose!"
  18. mines = 5 -- number of mines per row
  19. win = false
  20. map = {}
  21. lose = false
  22. sMap = {}
  23. function clear()
  24. term.setBackgroundColor(2^15)
  25. term.clear()
  26. term.setCursorPos(1,1)
  27. end
  28. clear()
  29. function intro()
  30. clear()
  31. print("Welcome To Minesweeper v0.1")
  32. print("Made By Jadengregory00")
  33. print("\nLeft Click On The Screen Once In Game")
  34. print("This Will Turn The Spot Blue(defualt)")
  35. print("It Will Also Show Number Of Surronding Mines")
  36. print("\nRight Click On The Screen Once In Game")
  37. print("This Will Flag A Spot That You Think Has A Mine")
  38. print("If You Flag All Mines You Win")
  39. print("\nPress Any Key To Continue")
  40. os.pullEvent("key")
  41. clear()
  42. end
  43. intro()
  44. function checkWin()
  45.   win = true
  46.     for k = 1, w do
  47.       for v = 1, h do
  48.         if map[k][v] == "mine" then
  49.         win = false
  50.         statement = "Lose!"
  51.         end
  52.       end
  53.     end
  54. end
  55.  
  56.  
  57.  
  58. function setField()
  59. -- blank field
  60.   for x = 1, w do
  61.   map[x] = {}
  62.     for y = 1, h do
  63.     map[x][y] = "noclick"
  64.     end
  65.   end
  66. -- enter mine positions
  67.   for l = 1, h do
  68.     for m = 1, mines do
  69.     minepos = math.random(1,w)
  70.     -- PRINT MINE POSITIONS
  71.     --paintutils.drawPixel(minepos, l, 32)
  72.     map[minepos][l] = "mine"
  73.    
  74.     end
  75.   end
  76.  
  77. end
  78. function insertSurround()
  79. -- blank field
  80.   for x = 1, w do
  81.   sMap[x] = {}
  82.     for y = 1, h do
  83.     sMap[x][y] = countSurround(x,y)
  84.     end
  85.   end
  86. end
  87.  
  88.  
  89. function countSurround(x,y)
  90. s = 0
  91. if x > 1 then if map[x-1][y] == "mine" then s = s + 1 end end
  92. -- to the left
  93. if x < w then if map[x+1][y] == "mine" then s = s + 1 end end
  94. -- to the right
  95. if y < h then if map[x][y+1] == "mine" then s = s + 1 end end
  96. -- below
  97. if y > 1 then if map[x][y-1] == "mine" then s = s + 1 end end
  98. -- above
  99. if y > 1 and x > 1 then if map[x-1][y-1] == "mine" then s = s + 1 end end
  100. -- above, to the left
  101. if y < h and x > 1 then if map[x-1][y+1] == "mine" then s = s + 1 end end
  102. -- below, to left
  103. if y > 1 and x < w then if map[x+1][y-1] == "mine" then s = s + 1 end end
  104. -- above, to right
  105. if y < h and x < w then if map[x+1][y+1] == "mine" then s = s + 1 end end
  106. --below, to right
  107. return s
  108. end
  109.  
  110. function fillSurround(x,y)
  111. s = 0
  112.  
  113. -- to the left
  114.   if x > 1 then
  115.     t = 1
  116.      os.queueEvent("fake") os.pullEvent("fake")
  117.     if map[x-t][y] == "mine" then
  118.     s = s + 1
  119.   elseif map[x-t][y] == "noclick" and countSurround(x-t,y) == 0 then
  120.  
  121.     while countSurround(x-t,y) < 1 and x-t > 1 do
  122.     map[x-t][y] = "click"
  123.     --fillSurround(x-t,y)
  124.     t = t + 1
  125.     end
  126.   end
  127.  
  128. end
  129.  
  130. -- to the right
  131.   if x < w then
  132.    os.queueEvent("fake") os.pullEvent("fake")
  133.     t = 1
  134.     if map[x+t][y] == "mine" then
  135.     s = s + 1
  136.   elseif map[x+t][y] == "noclick" and countSurround(x+t,y) == 0 then
  137.  
  138.     while countSurround(x+t,y) < 1 and x-t < w do
  139.     map[x+t][y] = "click"
  140.     --fillSurround(x+t,y)
  141.     t = t + 1
  142.     end
  143.   end
  144. end
  145.  
  146. -- below
  147.   if y < h then
  148.    os.queueEvent("fake") os.pullEvent("fake")
  149.     t = 1
  150.     if map[x][y+t] == "mine" then
  151.     s = s + 1
  152.   elseif map[x][y+t] == "noclick" and countSurround(x,y+t) == 0 then
  153.  
  154.     while countSurround(x,y+t) < 1 and y+t < h do
  155.     map[x][y+t] = "click"
  156.     --fillSurround(x,y+t)
  157.     t = t + 1
  158.     end
  159.   end
  160. end
  161.  
  162. -- above
  163.   if y > 1 then
  164.    os.queueEvent("fake") os.pullEvent("fake")
  165.     t = 1
  166.     if map[x][y-t] == "mine" then
  167.     s = s + 1
  168.   elseif (map[x][y-t] == "noclick" or map[x][y-t] == "click") and countSurround(x,y-t) == 0 then
  169.  
  170.     while countSurround(x,y-t) < 1 and y-t > 0 do
  171.     map[x][y-t] = "click"  
  172.     fillSurround(x,y-t)
  173.     t = t + 1
  174.     end
  175.   end
  176. end
  177.  
  178. if y > 1 and x > 1 then if map[x-1][y-1] == "mine" then s = s + 1 end end
  179. -- above, to the left
  180. if y < h and x > 1 then if map[x-1][y+1] == "mine" then s = s + 1 end end
  181. -- below, to left
  182. if y > 1 and x < w then if map[x+1][y-1] == "mine" then s = s + 1 end end
  183. -- above, to right
  184. if y < h and x < w then if map[x+1][y+1] == "mine" then s = s + 1 end end
  185. --below, to right
  186. return s
  187. end
  188.  
  189. function checkClick()
  190. e, button, xPos, yPos = os.pullEvent("mouse_click")
  191. if e == "mouse_click" or e == "mouse_drag" then
  192. move = move + 1
  193.   if button == 1 then
  194.     if map[xPos][yPos] == "mine" then
  195.       lose = true
  196.       drawMap()
  197.     else
  198.       map[xPos][yPos] = "click"
  199.       sMap[xPos][yPos] = countSurround(xPos,yPos)
  200.       if sMap[xPos][yPos] == 0 then
  201.       fillSurround(xPos,yPos)
  202.       end
  203.     end
  204.    else
  205.     map[xPos][yPos] = "flag"
  206.     end
  207. end
  208. end
  209. c = true
  210. function drawMap()
  211.   for a = 1, w do
  212.     for b = 1, h do
  213.       if map[a][b] == "noclick" then
  214.       paintutils.drawPixel(a,b,bColor)
  215.       elseif map[a][b] == "click" then
  216.       if c == true then paintutils.drawPixel(a,b,cColor) else paintutils.drawPixel(a,b,ocColor) c = not c end
  217.        
  218.       term.setCursorPos(a,b)
  219.       write(sMap[a][b])
  220.      
  221.       elseif map[a][b] == "mine" then
  222.       if lose == false then paintutils.drawPixel(a,b,mColor) end
  223.       if lose == true then paintutils.drawPixel(a,b,rColor) end
  224.       elseif map[a][b] == "flag" then
  225.       sMap[a][b] = "F"
  226.       paintutils.drawPixel(a,b,fColor)
  227.       term.setCursorPos(a,b)
  228.       write(sMap[a][b])
  229.       end
  230.      
  231.     end
  232.     os.queueEvent("pull")
  233.     os.pullEvent("pull")
  234.     term.setBackgroundColor(2^15)
  235.     --sleep(0)
  236.   end
  237.    
  238. end
  239.  
  240. setField()
  241.  
  242. while not lose and not win do
  243. insertSurround()
  244. drawMap()
  245. checkClick()
  246. checkWin()
  247. end
  248. stop = os.clock()
  249. endTime = stop-start
  250. sleep(5)
  251.  
  252. clear()
  253.  
  254. print("You "..statement)
  255. print("Program Made By Jadengregory00")
  256. print("You Made "..move.." Moves In "..endTime.." Seconds")
Advertisement
Add Comment
Please, Sign In to add comment