Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Minesweeper --
- local dir = fs.getDir(shell.getRunningProgram())
- local w,h = term.getSize() h = h-1
- assert(term.isColor(), "Advanced Computer Required.")
- local run,result = true,"Lost"
- local cols = {"white", "orange", "magenta", "lightBlue", "yellow", "lime", "pink", "gray", "lightGray", "cyan", "purple", "blue", "brown", "green", "red", "black"}
- --[[
- local writ = function(t) write(tostring(t)) end
- local write = function(t) writ(t) end
- --]]
- local number_colors = {
- colors.green,
- colors.green,
- colors.yellow,
- colors.orange,
- colors.magenta,
- colors.purple,
- colors.lightGray,
- colors.red,
- }
- local data = {
- display = {
- text = {
- flag = "orange",
- masked = "gray",
- foreground = "black",
- bomb = "red",
- highlight = "lightGray",
- invalidflag = "red",
- validflag = "cyan",
- },
- sprites = {
- numbs = {"1","2","3","4","5","6","7","8"},
- flag = "^",
- bomb = "!",
- masked = "#",
- foreground = " ",
- highlight = "-",
- invalidflag = "^",
- validflag = "^",
- },
- background = {
- foreground = "black",
- masked = "black",
- flag = "lightGray",
- bomb = "black",
- highlight = "black",
- invalidflag = "black",
- validflag = "black",
- },
- },
- settings = {
- enableHighlight = false,
- },
- number_colors = {
- "lime",
- "green",
- "yellow",
- "orange",
- "magenta",
- "purple",
- "lightGray",
- "red",
- }
- }
- local user_data = nil
- if fs.exists(dir.."/theme.lua") then local file = fs.open(dir.."/theme.lua", "r") user_data = textutils.unserialize(file.readAll()) file.close() end
- if user_data then
- if user_data.display then
- if user_data.text then data.text = user_data.text end
- if user_data.sprites then data.sprites = user_data.sprites end
- if user_data.background then data.background = user_data.background end
- end
- if user_data.settings then data.settings = user_data.settings end
- if user_data.number_colors then data.number_colors = user_data.number_colors end
- end
- local map,time,tbombs = {}, {true, 0},math.ceil(((w*h)/math.random(70,100)))*10
- local lookup = {
- {-1, 0},
- {1, 0},
- {0, -1},
- {0, 1},
- {-1, -1},
- {1, 1},
- {1, -1},
- {-1, 1},
- }
- local function getBombs(locationx,locationy)
- local tmp = 0
- for i=1, #lookup do
- if map[locationy+lookup[i][1]] then
- if map[locationy+lookup[i][1]][locationx+lookup[i][2]] then
- if map[locationy+lookup[i][1]][locationx+lookup[i][2]][1] == 10 then
- tmp = tmp + 1
- end
- end
- end
- end
- return tmp
- end
- local function generateMap()
- map = {}
- map.x,map.y = w,h
- map.totalBombs = tbombs
- map.flags = 0
- map.correctFlags = 0
- result = "Lost"
- for i=1, map.y do
- map[i] = {}
- for ii=1, map.x do
- map[i][ii] = {9, 0}
- end
- end
- local tmpx,tmpy
- for i=1, map.totalBombs do
- local l = true
- while l do
- tmpy = math.random(1,map.y)
- for ii=1, #map[tmpy] do
- if map[tmpy][ii][1] == 9 then l = false end
- end
- end
- repeat tmpx = math.random(1,map.x) until map[tmpy][tmpx][1] == 9
- map[tmpy][tmpx][1] = 10
- end
- for i=1, #map do
- for ii=1, #map[i] do
- if map[i][ii][1] == 9 then
- map[i][ii][1] = getBombs(ii,i)
- end
- end
- end
- end
- local function drawMapPixel(x,y)
- term.setCursorPos(x,y)
- if map[y][x][2] == 0 then
- term.setBackgroundColor(colors[data.display.background.masked] or colors.black)
- term.setTextColor(colors[data.display.text.masked] or colors.gray)
- write(data.display.sprites.masked or "#")
- elseif map[y][x][2] == 1 then
- if map[y][x][1] == 10 then
- term.setBackgroundColor(colors[data.display.background.bomb] or colors.black)
- term.setTextColor(colors[data.display.text.bomb] or colors.red)
- write(data.display.sprites.bomb or "!")
- elseif map[y][x][1] == 0 then
- term.setBackgroundColor(colors[data.display.background.foreground] or colors.black)
- term.setTextColor(colors[data.display.text.foreground] or colors.black)
- write(data.display.sprites.foreground or " ")
- else
- term.setBackgroundColor(colors[data.display.background.foreground] or colors.black)
- term.setTextColor(colors[data.number_colors[map[y][x][1]] or 1] or number_colors[map[y][x][1] or 1])
- write(data.display.sprites.numbs[map[y][x][1]] or "0")
- end
- elseif map[y][x][2] == 2 then
- term.setBackgroundColor(colors[data.display.background.masked] or colors.black)
- term.setTextColor(colors[data.display.text.flag] or colors.orange)
- write(data.display.sprites.flag or "^")
- elseif map[y][x][2] == 3 then
- term.setBackgroundColor(colors[data.display.background.validflag] or colors.black)
- term.setTextColor(colors[data.display.text.validflag] or colors.cyan)
- write(data.display.sprites.validflag or "^")
- elseif map[y][x][2] == 4 then
- term.setBackgroundColor(colors[data.display.background.invalidflag] or colors.black)
- term.setTextColor(colors[data.display.text.invalidflag] or colors.red)
- write(data.display.sprites.invalidflag or "^")
- else
- write(map[y][x][1])
- end
- end
- local function drawMap()
- term.setCursorPos(1,1)
- for i=1, #map do
- term.setCursorPos(1,i)
- for ii=1, #map[i] do
- drawMapPixel(ii,i)
- end
- end
- end
- local function runSettings()
- local loop, sel = true, 1
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- local menu = {
- --
- }
- while loop do
- for i=1, #menu do
- term.setCursorPos(1,i)
- if i == sel then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- write("< ")
- write(menu[i][1])
- write(" >")
- else
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.gray)
- write(" ")
- write(menu[i][1])
- write(" ")
- end
- end
- local a,i = os.pullEvent("key")
- if i == keys.w or i == keys.up then
- if sel ~= 1 then sel = sel - 1 end
- elseif i == keys.s or i == keys.down then
- if sel ~= #menu then sel = sel + 1 end
- elseif i == keys.enter or i == keys.e then
- if type(menu[sel][2]) ~= "table" then
- menu[sel][2]()
- end
- elseif i == keys.q then
- loop = false
- end
- end
- term.clear()
- end
- local function drawDisplay()
- if data.display.sprites.flag then
- term.setCursorPos(w-string.len(" "..data.display.sprites.flag..map.totalBombs-map.flags.." Time: "..time[2]), h+1)
- else
- term.setCursorPos(w-string.len(" ^"..map.totalBombs-map.flags.." Time: "..time[2]), h+1)
- end
- term.setBackgroundColor(colors[data.display.background.masked] or colors.black)
- term.setTextColor(colors[data.display.text.flag] or colors.orange)
- write(" "..data.display.sprites.flag or "^")
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- write(map.totalBombs-map.flags.." ")
- end
- local function runMenu()
- local menu = {
- {"Reset", function() generateMap() time[2] = 0 drawMap() drawDisplay() end},
- {"Clr Flags", function() for i=1, #map do for ii=1, #map[i] do if map[i][ii][2] == 2 then map[i][ii][2] = 0 end end end drawMap() end},
- --{"Settings", function() runSettings() drawMap() drawDisplay() end},
- {"Exit", function() result = "Exit" run = false end},
- }
- term.setTextColor(colors.gray)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,h+1)
- write("[Menu]")
- while run do
- sleep(.001)
- a,i = os.pullEvent("key")
- if i == keys.leftCtrl or i == keys.rightCtrl then
- local active,sel = true,1
- time[1] = false
- while active do
- term.setCursorPos(1,h+1)
- for i=1, #menu do
- if sel == i then term.setTextColor(colors.yellow) write("["..menu[i][1].."]") else term.setTextColor(colors.gray) write(" "..menu[i][1].." ") end
- end
- a,i = os.pullEvent("key")
- if i == keys.d or i == keys.right then
- if sel ~= #menu then sel = sel + 1 end
- elseif i == keys.a or i == keys.left then
- if sel ~= 1 then sel = sel - 1 end
- elseif i == keys.enter or i == keys.e then
- menu[sel][2]()
- active = false
- elseif i == keys.q or i == keys.leftCtrl or i == keys.rightCtrl then
- active = false
- end
- end
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,h+1)
- for i=1, #menu do
- write(" "..menu[i][1].." ")
- end
- term.setTextColor(colors.gray)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,h+1)
- write("[Menu]")
- time[1] = true
- end
- end
- end
- local function runGame()
- local discover_chart = {}
- local function discover(x,y)
- --discover_chart = {}
- for i=1, #lookup do
- if map[y+lookup[i][1]] then
- if map[y+lookup[i][1]][x+lookup[i][2]] then
- if map[y+lookup[i][1]][x+lookup[i][2]][2] == 0 and map[y+lookup[i][1]][x+lookup[i][2]][1] ~= 9 and map[y+lookup[i][1]][x+lookup[i][2]][2] ~= 10 then
- map[y+lookup[i][1]][x+lookup[i][2]][2] = 1
- if map[y+lookup[i][1]][x+lookup[i][2]][1] == 0 then
- discover_chart[#discover_chart+1] = {x+lookup[i][2], y+lookup[i][1]}
- end
- drawMapPixel(x+lookup[i][2],y+lookup[i][1])
- end
- end
- end
- end
- end
- local function highlight(x,y,status)
- for i=1, #lookup do
- if map[y+lookup[i][1]] then
- if map[y+lookup[i][1]][x+lookup[i][2]] then
- if status then
- term.setCursorPos(x+lookup[i][2],y+lookup[i][1])
- term.setTextColor(colors[data.display.text.highlight] or colors.lightGray)
- term.setBackgroundColor(colors[data.display.background.highlight] or colors.black)
- write(data.display.sprites.highlight or "-")
- else
- drawMapPixel(x+lookup[i][2],y+lookup[i][1])
- end
- end
- end
- end
- end
- while run do
- a,i,x,y = os.pullEvent("mouse_click")
- if i == 1 then
- if map[y][x][2] ~= 2 then
- if data.settings.enableHighlight then
- highlight(x,y,true)
- os.pullEvent("mouse_up")
- highlight(x,y,false)
- end
- map[y][x][2] = 1
- if map[y][x][1] == 10 then
- run = false
- elseif map[y][x][1] == 0 then
- discover(x,y)
- repeat
- local tmp = #discover_chart
- for i=1, #discover_chart do
- discover(discover_chart[i][1],discover_chart[i][2])
- end
- until #discover_chart == tmp
- end
- drawMapPixel(x,y)
- end
- elseif i == 2 or i == 3 then
- if map[y][x][2] ~= 2 and map[y][x][2] ~= 1 and map.flags ~= map.totalBombs then
- map[y][x][2] = 2 map.flags = map.flags+1
- if map[y][x][1] == 10 then map.correctFlags = map.correctFlags+1 end
- drawDisplay()
- elseif map[y][x][2] == 2 then
- map[y][x][2] = 0 map.flags = map.flags-1
- if map[y][x][1] == 10 then map.correctFlags = map.correctFlags-1 end
- drawDisplay()
- end
- if map.correctFlags == map.totalBombs then result = "Win" run = false end
- drawMapPixel(x,y)
- end
- --drawMap()
- end
- end
- local function runTime()
- while true do
- if time[2] > 0 then time[2] = 0 end
- while run do
- if time[1] then
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.setCursorPos((w-string.len("Time: "..time[2])),h+1)
- write("Time: "..time[2])
- sleep(1)
- time[2] = time[2] + 1
- else
- sleep(.00001)
- end
- end
- sleep(.00001)
- end
- end
- local function runMinesweeper()
- generateMap()
- term.clear()
- drawMap()
- drawDisplay() run = true
- parallel.waitForAny(runGame, runMenu, runTime)
- if result ~= "Exit" then
- for i=1, #map do
- for ii=1, #map[i] do
- if map[i][ii][1] == 10 and map[i][ii][2] == 2 then
- map[i][ii][2] = 3
- elseif map[i][ii][1] ~= 10 and map[i][ii][2] == 2 then
- map[i][ii][2] = 4
- else
- map[i][ii][2] = 1
- end
- end
- end
- drawMap()
- term.setCursorPos(1,h+1)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black) sleep(1)
- write(result..". Press any key to exit...") sleep(.2) os.pullEvent()
- end
- term.clear() term.setCursorPos(1,1)
- end
- local function main()
- local r, m_sel, sel = true, 1,1
- local m = {
- {
- {"Play Game", function() runMinesweeper() end},
- {"Set Bombs", function() term.clear() term.setCursorPos(1,1) term.setTextColor(colors.white) write("Bombs: ") local tmp = tonumber(read()) if type(tmp) == "number" then tbombs = math.min(math.max(tmp,1),(w*h)-1) end end},
- {"Create a Theme file", function() term.setTextColor(colors.white) if not fs.exists(dir.."/theme.lua") then local file = fs.open(dir.."/theme.lua", "w") if file then file.write(textutils.serialize(data)) file.close() term.clear() term.setCursorPos(1,1) write("Theme file created at '"..dir.."/theme.lua'.") else term.clear() term.setCursorPos(1,1) write("Failed to create File at '"..dir.."/theme.lua'.") end else term.clear() term.setCursorPos(1,1) write("Theme file Already exists at '"..dir.."/theme.lua'.") sleep(.5) end sleep(1) loop = false end},
- {"Exit", function() r = false end},
- },
- }
- term.clear()
- while r do
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.lime)
- print("Minesweeper")
- term.setCursorPos(1,3)
- for i=1, #m[m_sel] do
- if i == sel then
- term.setTextColor(colors.white)
- print("> "..m[m_sel][i][1].." ")
- else
- term.setTextColor(colors.gray)
- print(" "..m[m_sel][i][1].." ")
- end
- end
- term.setCursorPos(1,h+1)
- term.setTextColor(colors.gray)
- write("Bombs: "..tbombs)
- a,i = os.pullEvent("key")
- if i == keys.w or i == keys.up then
- if sel ~= 1 then sel = sel - 1 end
- elseif i == keys.s or i == keys.down then
- if sel ~= #m[m_sel] then sel = sel + 1 end
- elseif i == keys.enter or i == keys.e then
- m[m_sel][sel][2]() term.clear()
- end
- end
- end
- main()
- term.setBackgroundColor(colors.black) term.setTextColor(colors.white) term.clear() term.setCursorPos(1,1) sleep(.1)
Advertisement
Add Comment
Please, Sign In to add comment