From feae9f442000cf218c249e6e163e8b5137771857 Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 28 Jun 2020 08:34:16 +0200 Subject: Added player controls --- src/main.c | 82 ++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 29 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 57b8179..a7ed674 100644 --- a/src/main.c +++ b/src/main.c @@ -1,10 +1,22 @@ #include "hanoi.h" -int main() +int main(int argc, char **argv) { + t_tower towers[3]; + size_t disk_num = 3; + if (argc == 2) + if (sscanf(argv[1], "%lu", &disk_num) != 1) + { + fprintf(stderr, "%s: error: `%s` is not a valid number of disk", argv[0], argv[1]); + return (1); + } + towers_init(towers, disk_num); + initscr(); noecho(); cbreak(); + keypad(stdscr, TRUE); + curs_set(0); int width, height; getmaxyx(stdscr, height, width); @@ -13,64 +25,76 @@ int main() int win_width = width / 3; WINDOW *wins[3]; + refresh(); for (int i = 0; i < 3; i++) { - refresh(); if ((wins[i] = newwin(0, win_width, 0, i * win_width)) == NULL) abort(); - box(wins[i], 0, 0); - /* wrefresh(wins[i]); */ } - t_tower towers[3]; - towers_init(towers, 3); - + enum e_mode mode = MODE_SELECT_FROM; int from_selection = 0; - int to_selection = 1; - bool mode_from = true; + int current = 0; bool running = true; while (running) { for (int i = 0; i < 3; i++) { - int highlight_level = 0; - if (i == from_selection) - highlight_level = 1; - tower_put(&towers[i], wins[i], highlight_level); + enum e_highlight highlight = HIGHLIGHT_NONE; + if (mode == MODE_SELECT_TO && i == from_selection) + highlight |= HIGHLIGHT_FROM; + if (i == current) + highlight |= HIGHLIGHT_CURRENT; + tower_put(&towers[i], wins[i], highlight); } - char c; + int c; c = getch(); - int selection = mode_from ? from_selection : to_selection; switch (c) { case 'q': running = false; break; case 'j': - selection--; + case KEY_LEFT: + current--; break; case 'k': - selection++; + case KEY_RIGHT: + current++; break; case '\n': - if (mode_from) - from_selection = selection; - else - to_selection = selection; - if (!mode_from) - towers_move(towers, from_selection, to_selection); - mode_from = !mode_from; + if (mode == MODE_SELECT_FROM) + { + if (towers[current].len == 0) + mvprintw(0, 0, "Tower %d is empty"); + else + { + from_selection = current; + mode = MODE_SELECT_TO; + } + } + else if (mode == MODE_SELECT_TO) + { + if (!towers[current].len == 0 && + tower_peek(&towers[current]) < tower_peek(&towers[from_selection])) + { + mvprintw(0, 0, "Top disk of %d is smaller than top disk of %d", + current, from_selection); + } + else + towers_move(towers, from_selection, current); + mode = MODE_SELECT_FROM; + } break; } - selection %= 3; - mvprintw(0, 0, "%d", selection); - refresh(); + if (current < 0) + current = 2; + else if (current > 2) + current = 0; } - /* refresh(); */ - for (int i = 0; i < 3; i++) delwin(wins[3]); endwin(); -- cgit