Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- numbcolors={
- ["1"] = colors.lightBlue,
- ["2"] = colors.lime,
- ["3"] = colors.magenta,
- ["4"] = colors.blue,
- ["5"] = colors.red,
- ["6"] = colors.green,
- ["7"] = colors.black,
- ["8"] = colors.gray
- }
- local boardX=9
- local boardY=9
- local mines=10
- local function gen()
- local function genMain()
- board = {}
- for i=1,boardY do
- board[i] = {}
- for j=1,boardX do
- --print("X: "..#board.." Y: "..#board[i].." ")
- table.insert(board[i],"safe")
- end
- end
- mineLocations = board
- for k=1,mines do
- f = true
- while f do
- local x=math.random(1,boardX)
- local y=math.random(1,boardY)
- if mineLocations[x][y] ~= true then
- board[x][y] = "mine"
- mineLocations[x][y] = "mine"
- f=false
- end
- end
- end
- x,y=0,0
- function getPlotStat(x,y)
- --print("X: "..x.." Y: "..y)
- local l=0
- touching = {}
- mineGets = {false,false,false,false,false,false,false,false}
- if board[x-1] then
- if board[x-1][y-1] == "mine" then
- l = l+1
- mineGets[1] = true
- elseif board[x-1][y-1] ~= "S" then
- table.insert(touching,textutils.serialize({x-1,y-1}))
- end
- end
- if board[x][y-1] == "mine" then
- l = l+1
- mineGets[2] = true
- elseif board[1][y-1] ~= "S" then
- table.insert(touching,textutils.serialize({x,y-1}))
- end
- if board[x+1] then
- if board[x+1][y-1] == "mine" then
- l = l+1
- mineGets[3] = true
- elseif board[x+1][y-1] ~= "S" then
- table.insert(touching,textutils.serialize({x+1,y-1}))
- end
- if board[x+1][y] == "mine" then
- l = l+1
- mineGets[4] = true
- elseif board[x+1][y] ~= "S" then
- table.insert(touching,textutils.serialize({x+1,y}))
- end
- if board[x+1][y+1] == "mine" then
- l = l+1
- mineGets[5] = true
- elseif board[x+1][y+1] ~= "S" then
- table.insert(touching,textutils.serialize({x+1,y+1}))
- end
- end
- if board[x][y+1] == "mine" then
- l = l+1
- mineGets[6] = true
- elseif board[x][y+1] ~= "S" then
- table.insert(touching,textutils.serialize({x,y+1}))
- end
- if board[x-1] then
- if board[x-1][y+1] == "mine" then
- l = l+1
- mineGets[7] = true
- elseif board[x-1][y+1] ~= "S" then
- table.insert(touching,textutils.serialize({x-1,y+1}))
- end
- if board[x-1][y] == "mine" then
- l=l+1
- mineGets[8] = true
- elseif board[x-1][y] ~= "S" then
- table.insert(touching,textutils.serialize({x-1,y}))
- end
- end
- if l==0 then
- l="S"
- end
- return l,mineGets,touching
- end
- mineAmount=0
- print(" ")
- for i=1,boardY do
- for j=1,boardX do
- if board[j][i] == "mine" then
- board[j][i]="M"
- mineAmount=mineAmount+1
- else
- board[j][i],_=getPlotStat(j,i)
- end
- end
- print()
- end
- return board
- end
- while true do
- genMain()
- if mineAmount == mines then
- break
- end
- end
- return board
- end
- local function click()
- while true do
- _,button,xPos,yPos = os.pullEvent("mouse_click")
- if xPos > 1 and xPos < boardX+2 and yPos > 1 and yPos < boardY+2 then
- break
- end
- end
- return xPos-1,yPos-1,button,xPos,yPos
- end
- local board = gen()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.lightGray)
- for a=1,boardY+2 do
- for b=1,boardX+2 do
- if b==1 or b==boardX+2 or a==1 or a==boardY+2 then
- term.setBackgroundColor(colors.orange)
- write(" ")
- else
- term.setBackgroundColor(colors.lightGray)
- write(" ")
- end
- end
- print("")
- end
- function checkTable(lookFor,inside)
- toReturn = false
- toReturnTwo=1
- for j=1,#inside do
- if inside[j] == lookFor then
- toReturn = true
- toReturnTwo=j
- break
- end
- end
- return toReturn,toReturnTwo
- end
- discovered = {}
- function discover(disX,disY)
- discovered = {}
- discovering = {}
- toDiscover = {}
- lookX,lookY = disX,disY
- while true do
- t,p,touching = getPlotStat(lookX,lookY)
- for i=1,#touching do
- coords = textutils.unserialize(touching[i])
- boolee,amo = checkTable(coords,discovered)
- if not boolee then
- print(coords[amo][1])
- print("test")
- term.setCursorPos(coords[amo][1],coords[amo][2])
- term.setTextColor(numbcolors[tostring(t)])
- write(t)
- else
- term.setTextColor(colors.white)
- end
- end
- end
- end
- while true do
- xPos,yPos,button,sxPos,syPos= click()
- if button == 1 then
- term.setCursorPos(1,15)
- write("X: "..xPos.." Y: "..yPos)
- if board[xPos][yPos] == "M" then
- write("GAME OVER")
- elseif type(board[xPos][yPos]) == type(2) then
- x=board[xPos][yPos]
- term.setCursorPos(sxPos,syPos)
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(numbcolors[tostring(x)])
- write(x)
- elseif board[xPos][yPos] == "S" then
- x=board[xPos][yPos]
- discover(xPos,yPos)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment