-
Notifications
You must be signed in to change notification settings - Fork 0
/
words.lua
51 lines (42 loc) · 1.17 KB
/
words.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
local words = {list=nil, currentWord=nil, sentences=nil}
-- Generates a new map
function words:new(current)
nw = {}
setmetatable(nw, self)
self.__index = self
nw.list = lines_from("res/txt/dictionnary.txt")
for i=1, #nw.list do
if (nw.list[i] == string.lower(current)) then
nw.currentWord = current
break
end
end
if nw.currentWord == nil then
local index = math.random(#nw.list)
nw.currentWord = nw.list[index]
end
nw.sentences = lines_from("res/txt/sentences.txt")
return nw
end
function words:getNext()
for i=1, #self.list do
if (self.list[i] == string.lower(self.currentWord)) then
return self.list[i+1]
end
end
end
-- http://lua-users.org/wiki/FileInputOutput
function file_exists(file)
return love.filesystem.getInfo(file) ~= nil
end
-- get all lines from a file, returns an empty
-- list/table if the file does not exist
function lines_from(file)
if not file_exists(file) then print("file does not exist") return {} end
lines = {}
for line in love.filesystem.lines(file) do
lines[#lines + 1] = line
end
return lines
end
return words