teliva/hanoi.tlv

145 lines
3.0 KiB
Lua
Raw Normal View History

2021-11-14 07:19:07 +00:00
teliva_program = {
{
2021-11-27 05:56:22 +00:00
__teliva_timestamp = [==[
original]==],
2021-11-24 06:03:14 +00:00
render = [==[
2021-11-24 06:02:22 +00:00
function render(window)
2021-11-14 07:19:07 +00:00
window:clear()
local lines, cols = window:getmaxyx()
local line = math.floor(lines/2)
local col = math.floor(cols/4)
for i,t in ipairs(tower) do
render_tower(window, line, i*col, i, t)
end
curses.refresh()
end]==],
},
{
2021-11-27 05:56:22 +00:00
__teliva_timestamp = [==[
original]==],
2021-11-24 06:03:14 +00:00
lines = [==[
2021-11-24 06:02:22 +00:00
function lines(window)
2021-11-14 07:19:07 +00:00
local lines, cols = window:getmaxyx()
return lines
end]==],
},
{
2021-11-27 05:56:22 +00:00
__teliva_timestamp = [==[
original]==],
2021-11-24 06:03:14 +00:00
pop = [==[
2021-11-24 06:02:22 +00:00
function pop(array)
2021-11-14 07:19:07 +00:00
return table.remove(array)
end]==],
},
{
2021-11-27 05:56:22 +00:00
__teliva_timestamp = [==[
original]==],
window = [==[
window = curses.stdscr()]==],
},
{
2021-11-27 05:56:22 +00:00
__teliva_timestamp = [==[
original]==],
2021-11-24 06:03:14 +00:00
render_tower = [==[
2021-11-24 06:02:22 +00:00
function render_tower(window, line, col, tower_index, tower)
2021-11-14 07:19:07 +00:00
window:attron(curses.A_BOLD)
window:mvaddch(line+2, col, string.char(96+tower_index))
window:attroff(curses.A_BOLD)
window:attron(curses.color_pair(15))
2021-11-14 07:19:07 +00:00
window:mvaddstr(line+1, col-6, " ")
window:attroff(curses.color_pair(15))
2021-11-14 07:19:07 +00:00
for i, n in ipairs(tower) do
render_disk(window, line, col, n)
line = line - 1
end
for i=1,5-len(tower)+1 do
window:attron(curses.color_pair(15))
2021-11-14 07:19:07 +00:00
window:mvaddstr(line, col, " ")
window:attroff(curses.color_pair(15))
2021-11-14 07:19:07 +00:00
line = line - 1
end
end]==],
},
{
2021-11-27 05:56:22 +00:00
__teliva_timestamp = [==[
original]==],
tower = [==[
tower = {{6, 5, 4, 3, 2}, {}, {}}]==],
},
{
2021-11-27 05:56:22 +00:00
__teliva_timestamp = [==[
original]==],
2021-11-24 06:03:14 +00:00
render_disk = [==[
2021-11-24 06:02:22 +00:00
function render_disk(window, line, col, size)
2021-11-14 07:19:07 +00:00
col = col-size+1
for i=1,size do
window:attron(curses.color_pair(size))
window:mvaddstr(line, col, " ")
window:attroff(curses.color_pair(size))
col = col+2
end
end]==],
},
{
2021-11-27 05:56:22 +00:00
__teliva_timestamp = [==[
original]==],
2021-11-24 06:03:14 +00:00
main = [==[
2021-11-24 06:02:22 +00:00
function main()
curses.assume_default_colors(250, 139)
2021-11-14 07:19:07 +00:00
for i=1,7 do
curses.init_pair(i, 0, i)
end
curses.init_pair(15, 0, 250) -- tower frames
curses.init_pair(255, 15, 1) -- reserved for Teliva error messages
2021-11-14 07:19:07 +00:00
while true do
render(window)
update(window)
end
end
]==],
},
{
2021-11-27 05:56:22 +00:00
__teliva_timestamp = [==[
original]==],
2021-11-24 06:03:14 +00:00
len = [==[
2021-11-24 06:02:22 +00:00
function len(array)
2021-11-14 07:19:07 +00:00
local result = 0
for k in pairs(array) do
result = result+1
end
return result
end]==],
},
{
2021-11-27 05:56:22 +00:00
__teliva_timestamp = [==[
original]==],
2021-11-24 06:03:14 +00:00
update = [==[
2021-11-24 06:02:22 +00:00
function update(window)
2021-11-14 07:19:07 +00:00
window:mvaddstr(lines(window)-2, 5, "tower to remove top disk from? ")
local from = curses.getch() - 96
window:mvaddstr(lines(window)-1, 5, "tower to stack it on? ")
local to = curses.getch() - 96
make_move(from, to)
end]==],
},
{
2021-11-27 05:56:22 +00:00
__teliva_timestamp = [==[
original]==],
2021-11-24 06:03:14 +00:00
make_move = [==[
2021-11-24 06:02:22 +00:00
function make_move(from, to)
2021-11-14 07:19:07 +00:00
local disk = pop(tower[from])
table.insert(tower[to], disk)
end]==],
},
{
2021-11-27 05:56:22 +00:00
__teliva_timestamp = [==[
original]==],
2021-11-24 06:03:14 +00:00
cols = [==[
2021-11-24 06:02:22 +00:00
function cols(window)
2021-11-14 07:19:07 +00:00
local lines, cols = window:getmaxyx()
return cols
end]==],
},
2021-11-14 07:19:07 +00:00
}