Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local component = require("component")
- local term = require("term")
- local draw = require("draw")
- local event = require("event")
- local computer = require("computer")
- local color3 = require("color3")
- local gpu = component.gpu
- local sx,sy = gpu.getResolution()
- local mx,my = sx/2,sy/2
- local mines = {}
- local gridSizeX = 20
- local gridSizeY = 20
- local clicks = 0
- local ensureSafety = args[2] == "Y"
- local assist = args[3] == "Y"
- local assistFrequency = tonumber(args[4]) or 5
- local debug = args[5] == "Y"
- local numMines = tonumber(args[1] or 10)
- local running = true
- local openColor = color3.fromRGB(185, 185, 185)
- local closeColor = color3.fromRGB(129, 129, 129)
- local neighborMineColors = {
- color3.fromRGB(0,0,200),
- color3.fromRGB(0,200,0),
- color3.fromRGB(200,0,0),
- color3.fromRGB(200,0,200),
- color3.fromRGB(150,0,0),
- color3.fromRGB(0,150,200),
- color3.fromRGB(132,0,132),
- color3.fromRGB(117,117,117)
- }
- local function placeMine()
- local randX = math.random(1,gridSizeX)
- local randY = math.random(1,gridSizeY)
- local cell = mines[randX][randY]
- if cell.mine == true or cell.open == true then
- placeMine()
- return
- end
- mines[randX][randY].mine = true
- end
- local function setUpBoard()
- for x = 1,gridSizeX do
- mines[x] = {}
- for y = 1,gridSizeY do
- local state = {mine = false, open = false, flagged = false}
- mines[x][y] = state
- end
- end
- for i = 1,numMines do
- placeMine()
- end
- end
- local function drawCell(x,y,color,text,textcolor)
- local px, py = (mx - gridSizeX) + x*2, (my - gridSizeY/2) + y
- draw.rect(px,py,2,1,color)
- if text then
- draw.text(px,py,text,color,textcolor)
- end
- end
- local function drawBoard()
- local settingsString = ("Mines: %s / First Click Safety: %s / Assist Mode: %s"):format(tostring(numMines),tostring(ensureSafety and "Yes" or "No"),tostring(assist and "Yes" or "No"))
- draw.text((mx - gridSizeX), (my - gridSizeY/2) - 1,settingsString, color3.fromRGB(0,0,0),color3.fromHSV(0,0,1))
- draw.rect((mx - gridSizeX),(my - gridSizeY/2),gridSizeX*2 + 4,gridSizeY + 2,color3.fromHSV(0,0,0.1))
- for x = 1,gridSizeX do
- for y = 1,gridSizeY do
- local state = mines[x][y]
- drawCell(x,y,debug and (state.mine and openColor) or closeColor)
- end
- end
- end
- local function getNeighbors(x,y)
- local sum = 0
- for xoff = -1,1 do
- for yoff = -1,1 do
- if mines[x+xoff] then
- if mines[x+xoff][y+yoff] then
- sum = sum + (mines[x+xoff][y+yoff].mine == true and 1 or 0)
- end
- end
- end
- end
- return sum
- end
- local function flagCell(x,y)
- if mines[x] and mines[x][y] then
- local cell = mines[x][y]
- if cell.open == false then
- cell.flagged = not cell.flagged
- drawCell(x,y,closeColor,cell.flagged and "P" or " ",color3.fromRGB(0,0,0))
- computer.beep()
- end
- end
- end
- local function openCell(x,y,fill)
- local nearbyMines = getNeighbors(x,y)
- local cell = mines[x][y]
- cell.open = true
- drawCell(x,y,openColor,nearbyMines > 0 and nearbyMines or nil,neighborMineColors[nearbyMines])
- if cell.mine then
- drawCell(x,y,color3.fromHSV(0,1,1),"@",color3.fromRGB(0,0,0))
- computer.beep(500,2)
- running = false
- else
- if nearbyMines == 0 and cell.flagged == false and cell.open == true then
- --print("open neighbors")
- --openNeighbors(x,y)
- for xoff = -1,1 do
- for yoff = -1,1 do
- if mines[x+xoff] and mines[x+xoff][y+yoff] then
- local neighbor = mines[x+xoff][y+yoff]
- if neighbor.mine == false and neighbor.flagged == false and neighbor.open == false then
- openCell(x+xoff,y+yoff,true)
- end
- end
- end
- end
- end
- end
- if running and not fill then
- for i = 1,nearbyMines do
- computer.beep(nearbyMines*100,0.05)
- end
- end
- end
- print("Setting up a game of minesweeper with " .. numMines .. " mines")
- setUpBoard()
- drawBoard()
- while running do
- local _,_,x,y,button = event.pull("touch")
- x,y = math.floor(math.abs(((mx-x) - gridSizeX)/2)), math.abs((my-y) - gridSizeY/2)
- if button == 0 then
- if mines[x] and mines[x][y] and mines[x][y].open == false and mines[x][y].flagged == false then
- local cell = mines[x][y]
- if ensureSafety and clicks == 0 then
- for xoff = -1,1 do
- for yoff = -1,1 do
- if mines[x+xoff] and mines[x+xoff][y+yoff] then
- local cell = mines[x+xoff][y+yoff]
- if cell.mine == true then
- placeMine()
- cell.mine = false
- end
- end
- end
- end
- end
- openCell(x,y)
- clicks = clicks + 1
- if assist and clicks % assistFrequency == 0 then
- -- flag a random cell
- local actualMines = {}
- for x = 1,gridSizeX do
- for y = 1,gridSizeY do
- if mines[x][y].mine == true and mines[x][y].flagged == false then
- actualMines[#actualMines+1] = {x = x,y = y}
- end
- end
- end
- if #actualMines > 0 then
- local cell = actualMines[math.random(1,#actualMines)]
- flagCell(cell.x,cell.y)
- end
- end
- end
- else
- flagCell(x,y)
- end
- local count = 0
- for x = 1,gridSizeX do
- for y = 1,gridSizeY do
- local cell = mines[x][y]
- if cell.flagged == true and cell.mine == true then
- count = count + 1
- end
- end
- end
- if count == numMines then
- for x = 1,gridSizeX do
- for y = 1,gridSizeY do
- local cell = mines[x][y]
- if cell.mine == true then
- drawCell(x,y,color3.fromRGB(0,255,0),"#",color3.fromRGB(0,0,0))
- else
- if cell.flagged == true then
- drawCell(x,y,color3.fromRGB(255,0,0),"PX",color3.fromRGB(0,0,0))
- end
- end
- end
- end
- for i = 3,6 do
- computer.beep((i+2)*100)
- end
- computer.beep(1000)
- computer.beep(800)
- computer.beep(1000,0.7)
- running = false
- end
- end
- term.setCursor(1,1)
- term.clearLine()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement