Freack100

Untitled

Sep 7th, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.65 KB | None | 0 0
  1. local function split(str)
  2.     local t = {}
  3.     for match in str:gmatch(".") do
  4.         t[#t+1] = match
  5.     end
  6.     return t
  7. end
  8.  
  9. local function replace(str,what,with)
  10.     return string.gsub(str,what,with)
  11. end
  12.  
  13. local function loadFile(path)
  14.     local f = assert(io.open(path,"r"))
  15.     local content = f:read("*all")
  16.     f:close()
  17.     return content
  18. end
  19.  
  20. local function parseLevel(raw)
  21.     local height = tonumber(raw[1])
  22.     local width = tonumber(raw[3])
  23.  
  24.     local level = {}
  25.     local curLevel
  26.  
  27.     for i = 4,#raw do
  28.         local current = raw[i]
  29.         if((i-4)%width==0) then
  30.             level[#level+1] = {}
  31.             curLevel = level[#level]
  32.         end
  33.         curLevel[#curLevel+1] = current
  34.     end
  35.     return level
  36. end
  37.  
  38. local function getRelatives(level,x1,y1)
  39.     local relatives = {}
  40.     if(y1-1~=0) then
  41.         table.insert(relatives,{
  42.             x=x1,
  43.             y=y1-1,
  44.             })
  45.     end
  46.     if(x1+1<=#level[1]) then
  47.         table.insert(relatives,{
  48.             x=x1+1,
  49.             y=y1,
  50.             })
  51.     end
  52.     if(y1+1<=#level) then
  53.         table.insert(relatives,{
  54.             x=x1,
  55.             y=y1+1,
  56.             })
  57.     end
  58.     if(x1-1~=0) then
  59.         table.insert(relatives,{
  60.             x=x1-1,
  61.             y=y1,
  62.             })
  63.     end
  64.     return relatives
  65. end
  66.  
  67. local function findKassiopeia(level)
  68.     for y = 1,#level do
  69.         for x = 1,#level[1] do
  70.             if(level[y][x]=="K") then
  71.                 return x,y
  72.             end
  73.         end
  74.     end
  75. end
  76.  
  77. local function getWhitespaces(level)
  78.     local i = 0
  79.     for y = 1,#level do
  80.         for x = 1,#level[1] do
  81.             if(level[y][x] == " ") then
  82.                 i = i+1
  83.             end
  84.         end
  85.     end
  86.     return i+1
  87. end
  88.  
  89. local marked = {}
  90. local found = 0
  91.  
  92. local function canReachEveryField(level,startX,startY)
  93.     if(level[startY][startX]=="#") then
  94.         return
  95.     elseif(marked[startX.." "..startY]) then
  96.         return
  97.     else --Alles was nicht markiert ist, und auch nicht # ist, muss ein begehbares feld sein.
  98.         marked[startX.." "..startY] = true
  99.         found = found+1
  100.     end
  101.     local relatives = getRelatives(level,startX,startY)
  102.     for k,v in pairs(relatives) do
  103.         canReachEveryField(level,v.x,v.y)
  104.     end
  105. end
  106.  
  107. --Ist nur zum debuggen. Hat keinen wirklichen nutzen.
  108. local function printLevel(level)
  109.     for k,v in pairs(level) do
  110.         print(table.concat(v,""))
  111.     end
  112. end
  113.  
  114. local arg = {...}
  115.  
  116. --Das muss man nicht machen, aber so finde ich das ein wenig ordentlicher.
  117. local function main()
  118.     local rawData = loadFile(arg[1] and arg[1] or "currentLevel.txt")
  119.     print(rawData)
  120.     rawData = replace(rawData,"\n","")
  121.     rawData = replace(rawData,"\r","")
  122.     rawData = split(rawData)
  123.     local level = parseLevel(rawData)
  124.     local startX,startY = findKassiopeia(level)
  125.     canReachEveryField(level,startX,startY)
  126.     if(found==getWhitespaces(level)) then
  127.         print("Kassiopeia kann alle felder erreichen.")
  128.     else
  129.         print("Kassiopeia kann nicht alle felder erreichen.")
  130.     end
  131. end
  132.  
  133. main()
Advertisement
Add Comment
Please, Sign In to add comment