Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local m = peripheral.wrap("front")
- local mines = {}
- local numbers = {}
- local revealed = {}
- local mineColors = {colors.white,colors.yellow,colors.orange,colors.red,colors.purple,colors.blue,colors.cyan}
- local xm, ym = m.getSize()
- local ignoreTimer = 0
- function generateMines()
- ignoreTimer = -1
- for x = 1,xm do
- mines[x] = {}
- revealed[x] = {}
- for y = 1,ym do
- mines[x][y] = math.max(0,math.random(-8,1))
- revealed[x][y] = 0
- end
- end
- for x = 1,xm do
- numbers[x] = {}
- for y = 1,ym do
- numbers[x][y] = getNumber(x,y)
- end
- end
- end
- function getNumber(x,y)
- local total = 0
- local xu = x+1
- local xl = x-1
- local yu = y+1
- local yl = y-1
- if xu > xm then xu = xm end
- if xl < 1 then xl = 1 end
- if yu > ym then yu = ym end
- if yl < 1 then yl = 1 end
- for x = xl,xu do
- for y = yl,yu do
- total = total + mines[x][y]
- end
- end
- return total
- end
- titleScreen = true
- minesweeperIcon = {
- {0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,1,1,1,1,1,0,0,0,0},
- {0,0,0,1,1,1,1,1,1,1,0,0,0},
- {0,0,0,1,1,1,1,1,1,1,0,0,0},
- {0,0,0,0,1,1,1,1,1,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0}
- }
- colorTable = {
- [0] = colors.black,
- [1] = colors.gray
- }
- function render()
- if titleScreen then
- ignoreTimer = 9999
- m.setBackgroundColor(colors.black)
- m.clear()
- for y, data in pairs(minesweeperIcon) do
- for x, color in pairs(data) do
- m.setCursorPos(x,y)
- m.setBackgroundColor(colorTable[color])
- m.write(" ")
- end
- end
- sleep(3)
- else
- for x = 1,xm do
- for y = 1,ym do
- m.setBackgroundColor(colors.black)
- if revealed[x][y] == 0 then
- m.setBackgroundColor(colors.gray)
- m.setCursorPos(x,y)
- m.write(" ")
- elseif mines[x][y] == 1 then
- m.setBackgroundColor(colors.red)
- m.setCursorPos(x,y)
- m.write(" ")
- else
- m.setCursorPos(x,y)
- local text = numbers[x][y]
- if text ~= 0 then m.setTextColor(mineColors[text]) end
- if text == 0 then text = " " end
- m.write(text)
- end
- end
- end
- end
- end
- generateMines()
- function reveal(x,y)
- if revealed[x][y] == 0 then
- revealed[x][y] = 1
- if numbers[x][y] == 0 then
- reveal(math.min(x+1,xm),y)
- reveal(math.max(1,x-1),y)
- reveal(x,math.min(y+1,ym))
- reveal(x,math.max(1,y-1))
- end
- end
- end
- function checkWin()
- for x = 1,xm do
- for y = 1,ym do
- if mines[x][y] == 0 and revealed[x][y] == 0 then
- return false
- end
- end
- end
- return true
- end
- while true do
- render()
- local event, side, x, y = os.pullEvent()
- if event == "monitor_touch" then
- if not titleScreen then
- reveal(x,y)
- if mines[x][y] == 1 then
- lose = true
- elseif checkWin() then
- win = true
- end
- else
- titleScreen = false
- generateMines()
- end
- ignoreTimer = ignoreTimer + 1
- os.startTimer(15)
- elseif event == "timer" then
- if ignoreTimer > 0 then
- ignoreTimer = ignoreTimer - 1
- else
- titleScreen = true
- end
- end
- if win then
- ignoreTimer = ignoreTimer + 1
- render()
- m.setBackgroundColor(colors.lime)
- for x = 1,xm do
- for y = 1,ym do
- if mines[x][y] == 1 then
- m.setCursorPos(x,y)
- m.write(" ")
- end
- end
- end
- sleep(3)
- generateMines()
- win = false
- os.startTimer(0.1)
- titleScreen = true
- elseif lose then
- ignoreTimer = ignoreTimer + 1
- render()
- sleep(3)
- generateMines()
- lose = false
- os.startTimer(0.1)
- titleScreen = true
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement