Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local events, lost, won
- local w, h = term.getSize()
- local midW, midH = math.floor(w/2), math.floor(h/2)
- local map = {}
- local flood = {}
- for i = 1, 15 do
- flood[i] = {}
- end
- local pos = {
- x = 8,
- y = 8
- }
- local dirsTab = {
- {-1, 0},
- {0, 1},
- {1, 0},
- {0, -1}
- }
- local function clearScreen()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- end
- local function genMap()
- for y = 1, 15 do
- map[y] = {}
- for x = 1, 15 do
- if not (y == 8 and x == 8) then
- map[y][x] = math.random(1, 10) == 1 and true or false
- end
- end
- end
- end
- local function floodFill(yPos, xPos)
- for y = 1, 15 do
- for x = 1, 15 do
- flood[y][x] = 225
- end
- end
- flood[yPos][xPos] = 0
- local flag = false
- for i = 1, 225 do
- flag = true
- for y = 1, 15 do
- for x = 1, 15 do
- if flood[y][x] < 225 then
- if y > 1 and not map[y - 1][x] and flood[y - 1][x] == 225 then
- flood[y - 1][x] = flood[y][x] + 1
- end
- if x < 15 and not map[y][x + 1] and flood[y][x + 1] == 225 then
- flood[y][x + 1] = flood[y][x] + 1
- end
- if y < 15 and not map[y + 1][x] and flood[y + 1][x] == 225 then
- flood[y + 1][x] = flood[y][x] + 1
- end
- if x > 1 and not map[y][x - 1] and flood[y][x - 1] == 225 then
- flood[y][x - 1] = flood[y][x] + 1
- end
- elseif not map[y][x] then
- flag = false
- end
- end
- end
- if flag then break end
- end
- end
- local function findPath()
- floodFill(pos.y, pos.x)
- local min = 225
- local minPos = {}
- for i = 2, 14 do
- if not map[1][i] and min > flood[1][i] then
- min = flood[1][i]
- minPos = {1, i}
- end
- if not map[i][1] and min > flood[i][1] then
- min = flood[i][1]
- minPos = {i, 1}
- end
- if not map[i][15] and min > flood[i][15] then
- min = flood[i][15]
- minPos = {i, 15}
- end
- if not map[15][i] and min > flood[15][i] then
- min = flood[15][i]
- minPos = {15, i}
- end
- end
- if min < 225 then
- floodFill(minPos[1], minPos[2])
- return false
- end
- return true
- end
- local function moveSquare()
- local min = 225
- local dir = 0
- if pos.y > 1 and not map[pos.y - 1][pos.x] and min > flood[pos.y - 1][pos.x] then --up
- min = flood[pos.y - 1][pos.x]
- dir = 1
- end
- if pos.x < 15 and not map[pos.y][pos.x + 1] and min > flood[pos.y][pos.x + 1] then --right
- min = flood[pos.y][pos.x + 1]
- dir = 2
- end
- if pos.y < 15 and not map[pos.y + 1][pos.x] and min > flood[pos.y + 1][pos.x] then --down
- min = flood[pos.y + 1][pos.x]
- dir = 3
- end
- if pos.x > 1 and not map[pos.y][pos.x - 1] and min > flood[pos.y][pos.x - 1] then --left
- min = flood[pos.y][pos.x - 1]
- dir = 4
- end
- if pos.y > 1 and pos.x < 15 and not map[pos.y - 1][pos.x + 1] and not (map[pos.y - 1][pos.x] and map[pos.y][pos.x + 1]) and min > flood[pos.y - 1][pos.x + 1] then --up right
- min = flood[pos.y - 1][pos.x + 1]
- dir = 5
- end
- if pos.y < 15 and pos.x < 15 and not map[pos.y + 1][pos.x + 1] and not (map[pos.y + 1][pos.x] and map[pos.y][pos.x + 1]) and min > flood[pos.y + 1][pos.x + 1] then --down right
- min = flood[pos.y + 1][pos.x + 1]
- dir = 6
- end
- if pos.y < 15 and pos.x > 1 and not map[pos.y + 1][pos.x - 1] and not (map[pos.y + 1][pos.x] and map[pos.y][pos.x - 1]) and min > flood[pos.y + 1][pos.x - 1] then --down left
- min = flood[pos.y + 1][pos.x - 1]
- dir = 7
- end
- if pos.y > 1 and pos.x > 1 and not map[pos.y - 1][pos.x - 1] and not (map[pos.y - 1][pos.x] and map[pos.y][pos.x - 1]) and min > flood[pos.y - 1][pos.x - 1] then --up left
- min = flood[pos.y - 1][pos.x - 1]
- dir = 8
- end
- if pos.x == 1 or pos.x == 15 or pos.y == 1 or pos.y == 15 then
- return true
- end
- if dir == 1 then
- pos.y = pos.y - 1
- elseif dir == 2 then
- pos.x = pos.x + 1
- elseif dir == 3 then
- pos.y = pos.y + 1
- elseif dir == 4 then
- pos.x = pos.x - 1
- elseif dir == 5 then
- pos.x = pos.x + 1
- pos.y = pos.y - 1
- elseif dir == 6 then
- pos.x = pos.x + 1
- pos.y = pos.y + 1
- elseif dir == 7 then
- pos.x = pos.x - 1
- pos.y = pos.y + 1
- elseif dir == 8 then
- pos.x = pos.x - 1
- pos.y = pos.y - 1
- end
- end
- local function updateScreen()
- for y = 1, 15 do
- for x = 1, 15 do
- if map[y][x] then
- if map[y][x] == "#" then
- term.setBackgroundColor(colors.red)
- else
- term.setBackgroundColor(colors.cyan)
- end
- elseif x == pos.x and y == pos.y and not lost then
- term.setBackgroundColor(colors.orange)
- elseif x % 2 == y % 2 then
- term.setBackgroundColor(colors.blue)
- else
- term.setBackgroundColor(colors.lightBlue)
- end
- term.setCursorPos(midW - 7 + x, midH - 7 + y)
- write(" ")
- end
- end
- end
- clearScreen()
- genMap()
- updateScreen()
- while true do
- repeat
- events = {coroutine.yield("mouse_click")}
- until (events[3] >= midW - 6 and events[3] <= midW + 8 and events[4] >= midH - 6 and events[4] <= midH + 8) and not map[events[4] - midH + 7][events[3] - midW + 7] and not (events[4] - midH + 7 == pos.y and events[3] - midW + 7 == pos.x)
- map[events[4] - midH + 7][events[3] - midW + 7] = true
- updateScreen()
- sleep(.2)
- won = findPath()
- if won then break end
- lost = moveSquare()
- updateScreen()
- if lost then break end
- end
- local endTime = os.startTimer(math.random(1, 10)/10)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(midW - 3, 1)
- if won then
- term.setTextColor(colors.green)
- write("YOU WON")
- floodFill(pos.y, pos.x)
- local trapTab = {}
- for y = 1, 15 do
- for x = 1, 15 do
- if flood[y][x] < 225 then
- trapTab[#trapTab + 1] = {y, x}
- end
- end
- end
- term.setBackgroundColor(colors.red)
- for i = 1, #trapTab do
- for n = 1, 4 do
- if map[trapTab[i][1] + dirsTab[n][2]][trapTab[i][2] + dirsTab[n][1]] then
- map[trapTab[i][1] + dirsTab[n][2]][trapTab[i][2] + dirsTab[n][1]] = "#"
- end
- end
- end
- while true do
- local choice = trapTab[math.random(1, #trapTab)]
- pos.y, pos.x = choice[1], choice[2]
- updateScreen()
- repeat
- events = {os.pullEvent()}
- until (events[1] == "timer" and events[2] == endTime) or events[1] == "key"
- if events[1] == "timer" then
- endTime = os.startTimer(math.random(1, 10)/10)
- elseif events[1] == "key" then break end
- end
- elseif lost then
- term.setTextColor(colors.red)
- write("YOU LOST")
- while true do
- for y = 1, 15 do
- for x = 1, 15 do
- if map[y][x] and math.random(1, 10) == 1 then
- map[y][x] = false
- end
- end
- end
- updateScreen()
- repeat
- events = {os.pullEvent()}
- until (events[1] == "timer" and events[2] == endTime) or events[1] == "key"
- if events[1] == "timer" then
- endTime = os.startTimer(math.random(1, 10)/10)
- elseif events[1] == "key" then break end
- end
- end
- sleep(.1)
- clearScreen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement