Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- w,h = term.getSize()
- move = 0
- --mColor = colors.black -- mine color: hidden
- mColor = colors.red --show mines
- rColor = colors.red -- mine color: revealed
- bColor = colors.black -- background color
- cColor = colors.blue -- color when clicked on
- ocColor = colors.green
- fColor = colors.lime -- color when square is flagged
- --[[default colors
- mColor = colors.black
- rColor = colors.red
- bColor = colors.black
- cColor = colors.blue
- ]]
- start = os.clock()
- statement = "Lose!"
- mines = 5 -- number of mines per row
- win = false
- map = {}
- lose = false
- sMap = {}
- function clear()
- term.setBackgroundColor(2^15)
- term.clear()
- term.setCursorPos(1,1)
- end
- clear()
- function intro()
- clear()
- print("Welcome To Minesweeper v0.1")
- print("Made By Jadengregory00")
- print("\nLeft Click On The Screen Once In Game")
- print("This Will Turn The Spot Blue(defualt)")
- print("It Will Also Show Number Of Surronding Mines")
- print("\nRight Click On The Screen Once In Game")
- print("This Will Flag A Spot That You Think Has A Mine")
- print("If You Flag All Mines You Win")
- print("\nPress Any Key To Continue")
- os.pullEvent("key")
- clear()
- end
- intro()
- function checkWin()
- win = true
- for k = 1, w do
- for v = 1, h do
- if map[k][v] == "mine" then
- win = false
- statement = "Lose!"
- end
- end
- end
- end
- function setField()
- -- blank field
- for x = 1, w do
- map[x] = {}
- for y = 1, h do
- map[x][y] = "noclick"
- end
- end
- -- enter mine positions
- for l = 1, h do
- for m = 1, mines do
- minepos = math.random(1,w)
- -- PRINT MINE POSITIONS
- --paintutils.drawPixel(minepos, l, 32)
- map[minepos][l] = "mine"
- end
- end
- end
- function insertSurround()
- -- blank field
- for x = 1, w do
- sMap[x] = {}
- for y = 1, h do
- sMap[x][y] = countSurround(x,y)
- end
- end
- end
- function countSurround(x,y)
- s = 0
- if x > 1 then if map[x-1][y] == "mine" then s = s + 1 end end
- -- to the left
- if x < w then if map[x+1][y] == "mine" then s = s + 1 end end
- -- to the right
- if y < h then if map[x][y+1] == "mine" then s = s + 1 end end
- -- below
- if y > 1 then if map[x][y-1] == "mine" then s = s + 1 end end
- -- above
- if y > 1 and x > 1 then if map[x-1][y-1] == "mine" then s = s + 1 end end
- -- above, to the left
- if y < h and x > 1 then if map[x-1][y+1] == "mine" then s = s + 1 end end
- -- below, to left
- if y > 1 and x < w then if map[x+1][y-1] == "mine" then s = s + 1 end end
- -- above, to right
- if y < h and x < w then if map[x+1][y+1] == "mine" then s = s + 1 end end
- --below, to right
- return s
- end
- function fillSurround(x,y)
- s = 0
- -- to the left
- if x > 1 then
- t = 1
- os.queueEvent("fake") os.pullEvent("fake")
- if map[x-t][y] == "mine" then
- s = s + 1
- elseif map[x-t][y] == "noclick" and countSurround(x-t,y) == 0 then
- while countSurround(x-t,y) < 1 and x-t > 1 do
- map[x-t][y] = "click"
- --fillSurround(x-t,y)
- t = t + 1
- end
- end
- end
- -- to the right
- if x < w then
- os.queueEvent("fake") os.pullEvent("fake")
- t = 1
- if map[x+t][y] == "mine" then
- s = s + 1
- elseif map[x+t][y] == "noclick" and countSurround(x+t,y) == 0 then
- while countSurround(x+t,y) < 1 and x-t < w do
- map[x+t][y] = "click"
- --fillSurround(x+t,y)
- t = t + 1
- end
- end
- end
- -- below
- if y < h then
- os.queueEvent("fake") os.pullEvent("fake")
- t = 1
- if map[x][y+t] == "mine" then
- s = s + 1
- elseif map[x][y+t] == "noclick" and countSurround(x,y+t) == 0 then
- while countSurround(x,y+t) < 1 and y+t < h do
- map[x][y+t] = "click"
- --fillSurround(x,y+t)
- t = t + 1
- end
- end
- end
- -- above
- if y > 1 then
- os.queueEvent("fake") os.pullEvent("fake")
- t = 1
- if map[x][y-t] == "mine" then
- s = s + 1
- elseif (map[x][y-t] == "noclick" or map[x][y-t] == "click") and countSurround(x,y-t) == 0 then
- while countSurround(x,y-t) < 1 and y-t > 0 do
- map[x][y-t] = "click"
- fillSurround(x,y-t)
- t = t + 1
- end
- end
- end
- if y > 1 and x > 1 then if map[x-1][y-1] == "mine" then s = s + 1 end end
- -- above, to the left
- if y < h and x > 1 then if map[x-1][y+1] == "mine" then s = s + 1 end end
- -- below, to left
- if y > 1 and x < w then if map[x+1][y-1] == "mine" then s = s + 1 end end
- -- above, to right
- if y < h and x < w then if map[x+1][y+1] == "mine" then s = s + 1 end end
- --below, to right
- return s
- end
- function checkClick()
- e, button, xPos, yPos = os.pullEvent("mouse_click")
- if e == "mouse_click" or e == "mouse_drag" then
- move = move + 1
- if button == 1 then
- if map[xPos][yPos] == "mine" then
- lose = true
- drawMap()
- else
- map[xPos][yPos] = "click"
- sMap[xPos][yPos] = countSurround(xPos,yPos)
- if sMap[xPos][yPos] == 0 then
- fillSurround(xPos,yPos)
- end
- end
- else
- map[xPos][yPos] = "flag"
- end
- end
- end
- c = true
- function drawMap()
- for a = 1, w do
- for b = 1, h do
- if map[a][b] == "noclick" then
- paintutils.drawPixel(a,b,bColor)
- elseif map[a][b] == "click" then
- if c == true then paintutils.drawPixel(a,b,cColor) else paintutils.drawPixel(a,b,ocColor) c = not c end
- term.setCursorPos(a,b)
- write(sMap[a][b])
- elseif map[a][b] == "mine" then
- if lose == false then paintutils.drawPixel(a,b,mColor) end
- if lose == true then paintutils.drawPixel(a,b,rColor) end
- elseif map[a][b] == "flag" then
- sMap[a][b] = "F"
- paintutils.drawPixel(a,b,fColor)
- term.setCursorPos(a,b)
- write(sMap[a][b])
- end
- end
- os.queueEvent("pull")
- os.pullEvent("pull")
- term.setBackgroundColor(2^15)
- --sleep(0)
- end
- end
- setField()
- while not lose and not win do
- insertSurround()
- drawMap()
- checkClick()
- checkWin()
- end
- stop = os.clock()
- endTime = stop-start
- sleep(5)
- clear()
- print("You "..statement)
- print("Program Made By Jadengregory00")
- print("You Made "..move.." Moves In "..endTime.." Seconds")
Advertisement
Add Comment
Please, Sign In to add comment