use markdown syntax for images
[teliva.git] / hanoi.tlv
bloba78ac0d76c02084555a45888adbe45e4d50f92a4
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
12 #   key/value pair
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
20   check:
21     >function check(x, msg)
22     >  if x then
23     >    Window:addch('.')
24     >  else
25     >    print('F - '..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
31     >    end
32     >  end
33     >end
34 - __teliva_timestamp: original
35   check_eq:
36     >function check_eq(x, expected, msg)
37     >  if eq(x, expected) then
38     >    Window:addch('.')
39     >  else
40     >    print('F - '..msg)
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
46     >    end
47     >  end
48     >end
49 - __teliva_timestamp: original
50   eq:
51     >function eq(a, b)
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
56     >      if b[k] ~= v then
57     >        return false
58     >      end
59     >    end
60     >    for k, v in pairs(b) do
61     >      if a[k] ~= v then
62     >        return false
63     >      end
64     >    end
65     >    return true
66     >  end
67     >  return a == b
68     >end
69 - __teliva_timestamp: original
70   str:
71     >-- smarter tostring
72     >-- slow; used only for debugging
73     >function str(x)
74     >  if type(x) == 'table' then
75     >    local result = ''
76     >    result = result..#x..'{'
77     >    for k, v in pairs(x) do
78     >      result = result..str(k)..'='..str(v)..', '
79     >    end
80     >    result = result..'}'
81     >    return result
82     >  elseif type(x) == 'string' then
83     >    return '"'..x..'"'
84     >  end
85     >  return tostring(x)
86     >end
87 - __teliva_timestamp: original
88   render:
89     >function render(window)
90     >  window:clear()
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)
96     >  end
97     >  window:refresh()
98     >end
99 - __teliva_timestamp: original
100   lines:
101     >function lines(window)
102     >  local lines, cols = window:getmaxyx()
103     >  return lines
104     >end
105 - __teliva_timestamp: original
106   pop:
107     >function pop(array)
108     >  return table.remove(array)
109     >end
110 - __teliva_timestamp: original
111   Window:
112     >Window = curses.stdscr()
113 - __teliva_timestamp: original
114   render_tower:
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)
124     >    line = line - 1
125     >  end
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))
130     >    line = line - 1
131     >  end
132     >end
133 - __teliva_timestamp: original
134   tower:
135     >tower = {{6, 5, 4, 3, 2}, {}, {}}
136 - __teliva_timestamp: original
137   render_disk:
138     >function render_disk(window, line, col, size)
139     >  col = col-size+1
140     >  for i=1,size do
141     >    window:attron(curses.color_pair(size))
142     >    window:mvaddstr(line, col, "  ")
143     >    window:attroff(curses.color_pair(size))
144     >    col = col+2
145     >  end
146     >end
147 - __teliva_timestamp: original
148   main:
149     >function main()
150     >  curses.assume_default_colors(238, 139)
151     >  for i=1,7 do
152     >    curses.init_pair(i, 0, i)
153     >  end
154     >  curses.init_pair(15, 0, 250)  -- tower frames
155     >
156     >  while true do
157     >    render(Window)
158     >    update(Window)
159     >  end
160     >end
161 - __teliva_timestamp: original
162   len:
163     >function len(array)
164     >  local result = 0
165     >  for k in pairs(array) do
166     >    result = result+1
167     >  end
168     >  return result
169     >end
170 - __teliva_timestamp: original
171   update:
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)
178     >end
179 - __teliva_timestamp: original
180   make_move:
181     >function make_move(from, to)
182     >  local disk = pop(tower[from])
183     >  table.insert(tower[to], disk)
184     >end
185 - __teliva_timestamp: original
186   cols:
187     >function cols(window)
188     >  local lines, cols = window:getmaxyx()
189     >  return cols
190     >end
191 - __teliva_timestamp:
192     >Thu Feb 17 20:07:06 2022
193   doc:blurb:
194     >Single-player game: the Towers of Hanoi
195     >
196     >Move disks around from one tower A to tower B, under one constraint: a disk can never lie above a smaller disk.
197     >
198     >https://en.wikipedia.org/wiki/Tower_of_Hanoi