Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local grid = {}
- local width, height = term.getSize()
- width = width/2
- height = height/2
- for x=1,width do grid[x] = {} for y=1,height do grid[x][y] = 0 end end
- grid[math.random(width)][math.random(height)] = 1
- local function getCell(x, y)
- if grid[x] ~= nil then if grid[x][y] ~= nil then return grid[x][y] end end
- return nil
- end
- local function checkCellNeigh(x, y)
- local n = 0
- for o=-1,1,2 do
- if getCell(x+o, y) == 1 then n = n + 1 end
- if getCell(x, y+o) == 1 then n = n + 1 end
- end
- return n
- end
- local function checkCell(x,y)
- local coords = { { {-1, 0}, {-1, -1}, {0, -1}}, { {-1, 0}, {-1, 1}, {0, 1} }, { {0, 1}, {1, 1}, {1, 0} }, { {1, 0}, {1, -1}, {0, -1} } }
- local ok = false
- if getCell(x, y) == 1 and checkCellNeigh(x, y) == 2 then
- for i=1,#coords do
- local n = 0
- local coord = coords[i]
- for j=1,#coord do
- local co = coord[j]
- --n = n + 1
- if getCell(co[1]+x, co[2]+y) == 1 then
- if checkCellNeigh(co[1]+x, co[2]+y) == 2 then
- n = n + 1
- end
- end
- end
- if n == #coord then
- ok = true
- end
- end
- end
- return ok
- end
- local function update()
- local possible = {}
- for x=1,width do for y=1,height do
- if getCell(x, y) == 1 and not checkCell(x, y) then
- for i=-1,1,2 do
- if getCell(x+i, y) == 0 then possible[#possible + 1] = {x+i, y} end
- if getCell(x, y+i) == 0 then possible[#possible + 1] = {x, y+i} end
- end
- end
- end end
- if #possible > 0 then
- local co = possible[math.random(#possible)]
- if getCell(unpack(co)) ~= nil then grid[co[1]][co[2]] = 1 end
- return true
- else return false end
- end
- local selected = nil
- local function draw()
- for x=1,width do for y=1,height do
- term.setBackgroundColor(colors.black) term.setTextColor(colors.white)
- if getCell(x, y) == 1 then term.setBackgroundColor(colors.white) end
- if checkCell(x, y) then term.setBackgroundColor(colors.red) end
- if selected ~= nil then if x == selected[1] and y == selected[2] then
- term.setBackgroundColor(colors.blue)
- end end
- for rx=((x-1)*2)+1,x*2 do for ry=((y-1)*2)+1,y*2 do
- term.setCursorPos(rx, ry)
- term.write(" ")
- end end
- end end
- end
- for i=1,7 do update() end
- local pause = 1.5
- local timer = os.startTimer(pause)
- draw()
- local on = true
- while on do
- local event, p1, p2, p3 = os.pullEvent()
- if event == "timer" and p1 == timer then
- on = update()
- draw()
- timer = os.startTimer(pause)
- elseif event == "mouse_click" then
- p2 = math.ceil(p2/2)
- p3 = math.ceil(p3/2)
- if p1 == 1 then
- if selected == nil then selected = {p2, p3} draw() else
- local c1 = getCell(p2, p3)
- grid[p2][p3] = grid[selected[1]][selected[2]]
- grid[selected[1]][selected[2]] = c1
- selected = nil
- draw()
- end
- elseif p1 == 2 then selected = nil end
- elseif event == "key" then
- if p1 == 29 then
- on = false
- end
- end
- end
- term.setCursorPos(1,1)
- print("GAME OVER, PRESS ENTER TO CONTINUE")
- local temp = read()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement