diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-06-27 16:23:31 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-06-27 16:23:31 +0200 |
| commit | b9c8bbac74bb828eeae033a54c1f25d1711cf66a (patch) | |
| tree | 4483b62c30c9f9be46a9c1f699c4c6076f69a8e7 /src/tower.c | |
| parent | e2c1a8b40787516b9f8f3697a73ac406eae05e6f (diff) | |
| download | hanoi-b9c8bbac74bb828eeae033a54c1f25d1711cf66a.tar.gz hanoi-b9c8bbac74bb828eeae033a54c1f25d1711cf66a.tar.bz2 hanoi-b9c8bbac74bb828eeae033a54c1f25d1711cf66a.zip | |
Added tower display
Diffstat (limited to 'src/tower.c')
| -rw-r--r-- | src/tower.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/tower.c b/src/tower.c new file mode 100644 index 0000000..a92a201 --- /dev/null +++ b/src/tower.c @@ -0,0 +1,56 @@ +#include "tower.h" + +static uint8_t st_tower_pop(t_tower tower) +{ + if (tower.len == 0) + abort(); + uint8_t top = tower.data[tower.len - 1]; + tower.len--; + return top; +} + +static void st_tower_push(t_tower tower, uint8_t top) +{ + if (tower.len == MAX_HEIGHT) + abort(); + tower.data[tower.len] = top; + tower.len++; +} + +void towers_init(t_tower towers[3], size_t disk_num) +{ + memset(towers[0].data, 0, sizeof(t_tower)); + memset(towers[1].data, 0, sizeof(t_tower)); + memset(towers[2].data, 0, sizeof(t_tower)); + for (size_t i = 0; i < disk_num; i++) + towers[0].data[i] = disk_num - i; + towers[0].len = disk_num; + towers[1].len = 0; + towers[2].len = 0; +} + +void towers_move(t_tower towers[3], size_t from, size_t to) +{ + if (from > 2 || to > 2) + abort(); + st_tower_push(towers[to], st_tower_pop(towers[from])); +} + +void tower_put(t_tower *tower, WINDOW *win, int highlight_level) +{ + int height, width; + getmaxyx(win, height, width); + + if (highlight_level == 1) + wattron(win, A_BOLD); + mvwvline(win, 1, width / 2, '|', height - 2); + + for (size_t i = 0; i < tower->len; i++) + { + int disk_width = tower->data[i] * 2 + 1; + mvwhline(win, height - i - 2, width / 2 - disk_width / 2, '#', disk_width); + } + if (highlight_level == 1) + wattroff(win, A_BOLD); + wrefresh(win); +} |
