Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------------------------------MISC FUNCS
- local tArgs = {...}
- function printWords(words)
- for i, v in ipairs(words) do
- print(v)
- end
- end
- function printPuzzle(crossword) --word, coords, up or down, possibly length (just use #word)
- for i, v in ipairs(crossword) do
- if v[3] then
- term.setCursorPos(v[2][1], v[2][2])
- write(v[1])
- else
- for i = 1, #v[1] do
- term.setCursorPos(v[2][1]+i-1, v[2][2])
- write(v[1]:sub(i, i))
- end
- end
- end
- end
- ----------------------------------------------GENERATION
- function initializeWords(words)
- while #words < tonumber(tArgs[1]) do
- local str = io.read():gsub(" ", "")
- if validateWord(str) then
- table.insert(words, str)
- end
- end
- end
- function validateWord(word)
- local t = word:gsub("[%a]", "")
- if #t == 0 then
- return true
- end
- return false
- end
- function generatePuzzle(words)
- local crossword = {}
- local updown = true
- if math.random(0, 1) == 0 then updown = false end
- table.insert(crossword, {words[1], {1, 1}, updown})
- table.remove(words, 1)
- while #words > 0 do
- local wordBase = getAvailableExistingWords(crossword)
- local ok = false
- while #wordBase > 0 do
- local baseChoice = table.remove(wordBase, math.random(1, #wordBase))
- local availableWords = getAvailableWords(words, baseChoice[1])
- if #availableWords > 0 then
- local wordChoice = availableWords[math.random(1, #availableWords)]
- local xOff, yOff
- if baseChoice[2] then
- xOff = wordChoice[2][math.random(1, #wordChoice[2])]
- yOff = wordChoice[3] * -1
- else
- yOff = wordChoice[2][math.random(1, #wordChoice[2])]
- xOff = wordChoice[3] * -1
- end
- table.insert(crossword, {wordChoice[1], {baseChoice[2][1] + xOff, baseChoice[2][2] + yOff}, not baseChoice[3]})
- ok = true
- break;
- end
- end
- --if ok then break; end
- end
- return crossword
- end
- function getAvailableWords(words, base)
- local wordList = {}
- for i, v in ipairs(words) do
- local ok = false;
- local connections = {}
- for i = 1, #base do
- if v:find(base:sub(i, i)) then
- ok = true
- table.insert(connections, i)
- end
- end
- if ok then
- table.insert(wordList, {v, connections, v:find(base:sub(connections[1], connections[1]))})
- end
- end
- end
- function getAvailableExistingWords(crossword)
- local existing = {}
- for i, v in ipairs(crossword) do
- table.insert(existing, {v[1], v[2], v[3]})
- end
- return existing
- end
- ----------------------------------------------ENGINE
- if not tonumber(tArgs[1]) or tonumber(tArgs[1]) < 2 then
- else
- local words = {}
- initializeWords(words)
- --printWords(words)
- local crossword = generatePuzzle(words)
- printPuzzle(crossword)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement