-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
109 lines (88 loc) · 2.49 KB
/
Copy pathutils.lua
File metadata and controls
109 lines (88 loc) · 2.49 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
--[[
utils.lua
General-purpose utility functions for data manipulation and logic.
Contains:
- emptyGrid: Creates a new, empty 2D grid table.
- deepCopy: Performs a deep copy of a 2D grid table.
- getRuleTable: Parses a "B/S" rulestring into a rule table.
- center: Centers a string by adding padding.
- nextListValue: Cycles to the next value in a list (for speeds, grid sizes).
]]
-- Returns empty grid of given size
function emptyGrid(size)
local data = {}
for i = 1, size do
data[i] = {}
for j = 1, size do
data[i][j] = 0
end
end
return data
end
-- Returns a deep copy of a square table
function deepCopy(data)
local size = #data
local copy = emptyGrid(size)
for i = 1, size do
for j = 1, size do
copy[i][j] = data[i][j]
end
end
return copy
end
-- @rulestr: String containing ruleString
-- Returns table with B/S values and generated ruleString if input is valid
-- else returns nil
function getRuleTable(rulestr)
rulestr = rulestr:gsub('\n', '')
rulestr = rulestr:gsub('%s', '')
local _, _, b,s = rulestr:find("^B([0-8]+)/S([0-8]*)$")
if not b and not s then
return nil
end
local ruleTable={["B"]={},["S"]={}}
for ch in b:gmatch"." do
ruleTable["B"][tonumber(ch)]=true
end
for ch in s:gmatch"." do
ruleTable["S"][tonumber(ch)]=true
end
-- Build up ruleString
local bNums = 'B'
local sNums = 'S'
for var = 0, 8 do
bNums= bNums .. (ruleTable['B'][var] and var or '')
sNums= sNums .. (ruleTable['S'][var] and var or '')
end
local ruleString = bNums .. '/' .. sNums
return ruleTable, ruleString
end
-- @text: Text to be centered
-- @width: Width of text area
-- Takes in a string and adds spaces to the left of it
-- to center it in an area of specified width
function center(str, width)
local half = math.floor(#str/2)
return string.rep(' ', width/2 - half)..str
end
-- @list: Table containing unique values in increasing order
-- @value: Current value in the table
-- Returns the value in the table after the value passed in
function nextListValue(list, value)
-- Find what the index of the current value is
local index = 0
for i = 1, #list do
index = (value == list[i]) and i or index
end
-- Return the next value or wrap around
index = (index == #list) and 1 or index + 1
value = list[index]
return value
end
return {
emptyGrid = emptyGrid,
deepCopy = deepCopy,
getRuleTable = getRuleTable,
center = center,
nextListValue = nextListValue
}