Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - local function split(str)
 - local t = {}
 - for match in str:gmatch(".") do
 - t[#t+1] = match
 - end
 - return t
 - end
 - local function replace(str,what,with)
 - return string.gsub(str,what,with)
 - end
 - local function loadFile(path)
 - local f = assert(io.open(path,"r"))
 - local content = f:read("*all")
 - f:close()
 - return content
 - end
 - local function parseLevel(raw)
 - local height = tonumber(raw[1])
 - local width = tonumber(raw[3])
 - local level = {}
 - local curLevel
 - for i = 4,#raw do
 - local current = raw[i]
 - if((i-4)%width==0) then
 - level[#level+1] = {}
 - curLevel = level[#level]
 - end
 - curLevel[#curLevel+1] = current
 - end
 - return level
 - end
 - local function getRelatives(level,x1,y1)
 - local relatives = {}
 - if(y1-1~=0) then
 - table.insert(relatives,{
 - x=x1,
 - y=y1-1,
 - })
 - end
 - if(x1+1<=#level[1]) then
 - table.insert(relatives,{
 - x=x1+1,
 - y=y1,
 - })
 - end
 - if(y1+1<=#level) then
 - table.insert(relatives,{
 - x=x1,
 - y=y1+1,
 - })
 - end
 - if(x1-1~=0) then
 - table.insert(relatives,{
 - x=x1-1,
 - y=y1,
 - })
 - end
 - return relatives
 - end
 - local function findKassiopeia(level)
 - for y = 1,#level do
 - for x = 1,#level[1] do
 - if(level[y][x]=="K") then
 - return x,y
 - end
 - end
 - end
 - end
 - local function getWhitespaces(level)
 - local i = 0
 - for y = 1,#level do
 - for x = 1,#level[1] do
 - if(level[y][x] == " ") then
 - i = i+1
 - end
 - end
 - end
 - return i+1
 - end
 - local marked = {}
 - local found = 0
 - local function canReachEveryField(level,startX,startY)
 - if(level[startY][startX]=="#") then
 - return
 - elseif(marked[startX.." "..startY]) then
 - return
 - else --Alles was nicht markiert ist, und auch nicht # ist, muss ein begehbares feld sein.
 - marked[startX.." "..startY] = true
 - found = found+1
 - end
 - local relatives = getRelatives(level,startX,startY)
 - for k,v in pairs(relatives) do
 - canReachEveryField(level,v.x,v.y)
 - end
 - end
 - --Ist nur zum debuggen. Hat keinen wirklichen nutzen.
 - local function printLevel(level)
 - for k,v in pairs(level) do
 - print(table.concat(v,""))
 - end
 - end
 - local arg = {...}
 - --Das muss man nicht machen, aber so finde ich das ein wenig ordentlicher.
 - local function main()
 - local rawData = loadFile(arg[1] and arg[1] or "currentLevel.txt")
 - print(rawData)
 - rawData = replace(rawData,"\n","")
 - rawData = replace(rawData,"\r","")
 - rawData = split(rawData)
 - local level = parseLevel(rawData)
 - local startX,startY = findKassiopeia(level)
 - canReachEveryField(level,startX,startY)
 - if(found==getWhitespaces(level)) then
 - print("Kassiopeia kann alle felder erreichen.")
 - else
 - print("Kassiopeia kann nicht alle felder erreichen.")
 - end
 - end
 - main()
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment