Advertisement
CaptainSpaceCat

Crossword!

Jun 22nd, 2016
112
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ----------------------------------------------MISC FUNCS
  2. local tArgs = {...}
  3.  
  4. function printWords(words)
  5.     for i, v in ipairs(words) do
  6.         print(v)
  7.     end
  8. end
  9.  
  10. function printPuzzle(crossword)  --word, coords, up or down, possibly length (just use #word)
  11.     for i, v in ipairs(crossword) do
  12.         if v[3] then
  13.             term.setCursorPos(v[2][1], v[2][2])
  14.             write(v[1])
  15.         else
  16.             for i = 1, #v[1] do
  17.                 term.setCursorPos(v[2][1]+i-1, v[2][2])
  18.                 write(v[1]:sub(i, i))
  19.             end
  20.         end
  21.     end
  22. end
  23.  
  24. ----------------------------------------------GENERATION
  25.  
  26. function initializeWords(words)
  27.     while #words < tonumber(tArgs[1]) do
  28.         local str = io.read():gsub(" ", "")
  29.         if validateWord(str) then
  30.             table.insert(words, str)
  31.         end
  32.     end
  33. end
  34.  
  35. function validateWord(word)
  36.     local t = word:gsub("[%a]", "")
  37.     if #t == 0 then
  38.         return true
  39.     end
  40.     return false
  41. end
  42.  
  43. function generatePuzzle(words)
  44.     local crossword = {}
  45.     local updown = true
  46.     if math.random(0, 1) == 0 then updown = false end
  47.     table.insert(crossword, {words[1], {1, 1}, updown})
  48.     table.remove(words, 1)
  49.     while #words > 0 do
  50.         local wordBase = getAvailableExistingWords(crossword)
  51.         local ok = false
  52.         while #wordBase > 0 do
  53.             local baseChoice = table.remove(wordBase, math.random(1, #wordBase))
  54.             local availableWords = getAvailableWords(words, baseChoice[1])
  55.             if #availableWords > 0 then
  56.                 local wordChoice = availableWords[math.random(1, #availableWords)]
  57.                 local xOff, yOff
  58.                 if baseChoice[2] then
  59.                     xOff = wordChoice[2][math.random(1, #wordChoice[2])]
  60.                     yOff = wordChoice[3] * -1
  61.                 else
  62.                     yOff = wordChoice[2][math.random(1, #wordChoice[2])]
  63.                     xOff = wordChoice[3] * -1
  64.                 end
  65.                 table.insert(crossword, {wordChoice[1], {baseChoice[2][1] + xOff, baseChoice[2][2] + yOff}, not baseChoice[3]})
  66.                 ok = true
  67.                 break;
  68.             end
  69.         end
  70.         --if ok then break; end
  71.     end
  72.     return crossword
  73. end
  74.  
  75. function getAvailableWords(words, base)
  76.     local wordList = {}
  77.     for i, v in ipairs(words) do
  78.         local ok = false;
  79.         local connections = {}
  80.         for i = 1, #base do
  81.             if v:find(base:sub(i, i)) then
  82.                 ok = true
  83.                 table.insert(connections, i)
  84.             end
  85.         end
  86.         if ok then
  87.             table.insert(wordList, {v, connections, v:find(base:sub(connections[1], connections[1]))})
  88.         end
  89.     end
  90. end
  91.  
  92. function getAvailableExistingWords(crossword)
  93.     local existing = {}
  94.     for i, v in ipairs(crossword) do
  95.         table.insert(existing, {v[1], v[2], v[3]})
  96.     end
  97.     return existing
  98. end
  99.  
  100. ----------------------------------------------ENGINE
  101. if not tonumber(tArgs[1]) or tonumber(tArgs[1]) < 2 then
  102.    
  103. else
  104.     local words = {}
  105.     initializeWords(words)
  106.     --printWords(words)
  107.     local crossword = generatePuzzle(words)
  108.     printPuzzle(crossword)
  109. end
Advertisement
RAW Paste Data Copied
Advertisement