1 # .tlv file generated by https://github.com/akkartik/teliva
2 # You may edit it if you are careful; however, you may see cryptic errors if you
3 # violate Teliva's assumptions.
5 # .tlv files are representations of Teliva programs. Teliva programs consist of
6 # sequences of definitions. Each definition is a table of key/value pairs. Keys
7 # and values are both strings.
9 # Lines in .tlv files always follow exactly one of the following forms:
10 # - comment lines at the top of the file starting with '#' at column 0
11 # - beginnings of definitions starting with '- ' at column 0, followed by a
13 # - key/value pairs consisting of ' ' at column 0, containing either a
14 # spaceless value on the same line, or a multi-line value
15 # - multiline values indented by more than 2 spaces, starting with a '>'
17 # If these constraints are violated, Teliva may unceremoniously crash. Please
18 # report bugs at http://akkartik.name/contact
19 - __teliva_timestamp: original
21 >function check(x, msg)
26 > print(' '..str(x)..' is false/nil')
27 > teliva_num_test_failures = teliva_num_test_failures + 1
28 > -- overlay first test failure on editors
29 > if teliva_first_failure == nil then
30 > teliva_first_failure = msg
34 - __teliva_timestamp: original
36 >function check_eq(x, expected, msg)
37 > if eq(x, expected) then
41 > print(' expected '..str(expected)..' but got '..str(x))
42 > teliva_num_test_failures = teliva_num_test_failures + 1
43 > -- overlay first test failure on editors
44 > if teliva_first_failure == nil then
45 > teliva_first_failure = msg
49 - __teliva_timestamp: original
52 > if type(a) ~= type(b) then return false end
53 > if type(a) == 'table' then
54 > if #a ~= #b then return false end
55 > for k, v in pairs(a) do
60 > for k, v in pairs(b) do
69 - __teliva_timestamp: original
72 >-- slow; used only for debugging
74 > if type(x) == 'table' then
76 > result = result..#x..'{'
77 > for k, v in pairs(x) do
78 > result = result..str(k)..'='..str(v)..', '
80 > result = result..'}'
82 > elseif type(x) == 'string' then
87 - __teliva_timestamp: original
89 >function render(window)
91 > local lines, cols = window:getmaxyx()
92 > local line = math.floor(lines/2)
93 > local col = math.floor(cols/4)
94 > for i,t in ipairs(tower) do
95 > render_tower(window, line, i*col, i, t)
99 - __teliva_timestamp: original
101 >function lines(window)
102 > local lines, cols = window:getmaxyx()
105 - __teliva_timestamp: original
108 > return table.remove(array)
110 - __teliva_timestamp: original
112 >Window = curses.stdscr()
113 - __teliva_timestamp: original
115 >function render_tower(window, line, col, tower_index, tower)
116 > window:attron(curses.A_BOLD)
117 > window:mvaddch(line+2, col, string.char(96+tower_index))
118 > window:attroff(curses.A_BOLD)
119 > window:attron(curses.color_pair(15))
120 > window:mvaddstr(line+1, col-6, " ")
121 > window:attroff(curses.color_pair(15))
122 > for i, n in ipairs(tower) do
123 > render_disk(window, line, col, n)
126 > for i=1,5-len(tower)+1 do
127 > window:attron(curses.color_pair(15))
128 > window:mvaddstr(line, col, " ")
129 > window:attroff(curses.color_pair(15))
133 - __teliva_timestamp: original
135 >tower = {{6, 5, 4, 3, 2}, {}, {}}
136 - __teliva_timestamp: original
138 >function render_disk(window, line, col, size)
141 > window:attron(curses.color_pair(size))
142 > window:mvaddstr(line, col, " ")
143 > window:attroff(curses.color_pair(size))
147 - __teliva_timestamp: original
150 > curses.assume_default_colors(238, 139)
152 > curses.init_pair(i, 0, i)
154 > curses.init_pair(15, 0, 250) -- tower frames
161 - __teliva_timestamp: original
165 > for k in pairs(array) do
170 - __teliva_timestamp: original
172 >function update(window)
173 > window:mvaddstr(lines(window)-2, 5, "tower to remove top disk from? ")
174 > local from = window:getch() - 96
175 > window:mvaddstr(lines(window)-1, 5, "tower to stack it on? ")
176 > local to = window:getch() - 96
177 > make_move(from, to)
179 - __teliva_timestamp: original
181 >function make_move(from, to)
182 > local disk = pop(tower[from])
183 > table.insert(tower[to], disk)
185 - __teliva_timestamp: original
187 >function cols(window)
188 > local lines, cols = window:getmaxyx()
191 - __teliva_timestamp:
192 >Thu Feb 17 20:07:06 2022
194 >Single-player game: the Towers of Hanoi
196 >Move disks around from one tower A to tower B, under one constraint: a disk can never lie above a smaller disk.
198 >https://en.wikipedia.org/wiki/Tower_of_Hanoi