AndreSoYeah

MineSweeper

Feb 17th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. numbcolors={
  2. ["1"] = colors.lightBlue,
  3. ["2"] = colors.lime,
  4. ["3"] = colors.magenta,
  5. ["4"] = colors.blue,
  6. ["5"] = colors.red,
  7. ["6"] = colors.green,
  8. ["7"] = colors.black,
  9. ["8"] = colors.gray
  10. }
  11. local boardX=9
  12. local boardY=9
  13. local mines=10
  14. local function gen()
  15. local function genMain()
  16. board = {}
  17. for i=1,boardY do
  18. board[i] = {}
  19. for j=1,boardX do
  20. --print("X: "..#board.." Y: "..#board[i].." ")
  21. table.insert(board[i],"safe")
  22. end
  23. end
  24. mineLocations = board
  25. for k=1,mines do
  26. f = true
  27. while f do
  28. local x=math.random(1,boardX)
  29. local y=math.random(1,boardY)
  30. if mineLocations[x][y] ~= true then
  31. board[x][y] = "mine"
  32. mineLocations[x][y] = "mine"
  33. f=false
  34. end
  35. end
  36. end
  37. x,y=0,0
  38. function getPlotStat(x,y)
  39. --print("X: "..x.." Y: "..y)
  40. local l=0
  41. touching = {}
  42. mineGets = {false,false,false,false,false,false,false,false}
  43. if board[x-1] then
  44. if board[x-1][y-1] == "mine" then
  45. l = l+1
  46. mineGets[1] = true
  47. elseif board[x-1][y-1] ~= "S" then
  48. table.insert(touching,textutils.serialize({x-1,y-1}))
  49. end
  50. end
  51. if board[x][y-1] == "mine" then
  52. l = l+1
  53. mineGets[2] = true
  54. elseif board[1][y-1] ~= "S" then
  55. table.insert(touching,textutils.serialize({x,y-1}))
  56. end
  57. if board[x+1] then
  58. if board[x+1][y-1] == "mine" then
  59. l = l+1
  60. mineGets[3] = true
  61. elseif board[x+1][y-1] ~= "S" then
  62. table.insert(touching,textutils.serialize({x+1,y-1}))
  63. end
  64. if board[x+1][y] == "mine" then
  65. l = l+1
  66. mineGets[4] = true
  67. elseif board[x+1][y] ~= "S" then
  68. table.insert(touching,textutils.serialize({x+1,y}))
  69. end
  70. if board[x+1][y+1] == "mine" then
  71. l = l+1
  72. mineGets[5] = true
  73. elseif board[x+1][y+1] ~= "S" then
  74. table.insert(touching,textutils.serialize({x+1,y+1}))
  75. end
  76. end
  77. if board[x][y+1] == "mine" then
  78. l = l+1
  79. mineGets[6] = true
  80. elseif board[x][y+1] ~= "S" then
  81. table.insert(touching,textutils.serialize({x,y+1}))
  82. end
  83. if board[x-1] then
  84. if board[x-1][y+1] == "mine" then
  85. l = l+1
  86. mineGets[7] = true
  87. elseif board[x-1][y+1] ~= "S" then
  88. table.insert(touching,textutils.serialize({x-1,y+1}))
  89. end
  90. if board[x-1][y] == "mine" then
  91. l=l+1
  92. mineGets[8] = true
  93. elseif board[x-1][y] ~= "S" then
  94. table.insert(touching,textutils.serialize({x-1,y}))
  95. end
  96. end
  97. if l==0 then
  98. l="S"
  99. end
  100. return l,mineGets,touching
  101. end
  102. mineAmount=0
  103. print(" ")
  104. for i=1,boardY do
  105. for j=1,boardX do
  106. if board[j][i] == "mine" then
  107. board[j][i]="M"
  108. mineAmount=mineAmount+1
  109. else
  110. board[j][i],_=getPlotStat(j,i)
  111. end
  112. end
  113. print()
  114. end
  115. return board
  116. end
  117. while true do
  118. genMain()
  119. if mineAmount == mines then
  120. break
  121. end
  122. end
  123. return board
  124. end
  125. local function click()
  126. while true do
  127. _,button,xPos,yPos = os.pullEvent("mouse_click")
  128. if xPos > 1 and xPos < boardX+2 and yPos > 1 and yPos < boardY+2 then
  129. break
  130. end
  131. end
  132. return xPos-1,yPos-1,button,xPos,yPos
  133. end
  134. local board = gen()
  135. term.setBackgroundColor(colors.black)
  136. term.clear()
  137. term.setCursorPos(1,1)
  138. term.setBackgroundColor(colors.lightGray)
  139. for a=1,boardY+2 do
  140. for b=1,boardX+2 do
  141. if b==1 or b==boardX+2 or a==1 or a==boardY+2 then
  142. term.setBackgroundColor(colors.orange)
  143. write(" ")
  144. else
  145. term.setBackgroundColor(colors.lightGray)
  146. write(" ")
  147. end
  148. end
  149. print("")
  150. end
  151. function checkTable(lookFor,inside)
  152. toReturn = false
  153. toReturnTwo=1
  154. for j=1,#inside do
  155. if inside[j] == lookFor then
  156. toReturn = true
  157. toReturnTwo=j
  158. break
  159. end
  160. end
  161. return toReturn,toReturnTwo
  162. end
  163. discovered = {}
  164. function discover(disX,disY)
  165. discovered = {}
  166. discovering = {}
  167. toDiscover = {}
  168. lookX,lookY = disX,disY
  169. while true do
  170. t,p,touching = getPlotStat(lookX,lookY)
  171. for i=1,#touching do
  172. coords = textutils.unserialize(touching[i])
  173. boolee,amo = checkTable(coords,discovered)
  174. if not boolee then
  175. print(coords[amo][1])
  176. print("test")
  177. term.setCursorPos(coords[amo][1],coords[amo][2])
  178. term.setTextColor(numbcolors[tostring(t)])
  179. write(t)
  180. else
  181. term.setTextColor(colors.white)
  182. end
  183. end
  184. end
  185. end
  186. while true do
  187. xPos,yPos,button,sxPos,syPos= click()
  188. if button == 1 then
  189. term.setCursorPos(1,15)
  190. write("X: "..xPos.." Y: "..yPos)
  191. if board[xPos][yPos] == "M" then
  192. write("GAME OVER")
  193. elseif type(board[xPos][yPos]) == type(2) then
  194. x=board[xPos][yPos]
  195. term.setCursorPos(sxPos,syPos)
  196. term.setBackgroundColor(colors.yellow)
  197. term.setTextColor(numbcolors[tostring(x)])
  198. write(x)
  199. elseif board[xPos][yPos] == "S" then
  200. x=board[xPos][yPos]
  201. discover(xPos,yPos)
  202. end
  203. end
  204. end
Advertisement
Add Comment
Please, Sign In to add comment