From 39359f73687269178f10da8db11c69e91dc1ff3b Mon Sep 17 00:00:00 2001 From: Louie S Date: Wed, 1 Nov 2023 17:18:18 -0400 Subject: Create win screen and ability to restart --- src/main.c | 109 +++++++++++++++++++++++++++++++++++++++---------------------- src/ring.c | 10 ++++++ src/ring.h | 1 + 3 files changed, 81 insertions(+), 39 deletions(-) diff --git a/src/main.c b/src/main.c index 56fc9d7..eb159a1 100644 --- a/src/main.c +++ b/src/main.c @@ -9,10 +9,13 @@ #define TERMINAL_MIN_HEIGHT 14 #define TERMINAL_MIN_WIDTH 39 +void init_rings(); +void init_towers(); bool check_terminal(); void draw_title(); void draw_body(); void draw_tower(int ring_index, int startx, int starty); +void draw_win(); void draw_move_count(); void input_loop(); @@ -26,22 +29,8 @@ int move_count = 0; int main() { - int i; - - // initialize rings - strcpy(rings[0].ascii, " ======= "); - strcpy(rings[1].ascii, " ===== "); - strcpy(rings[2].ascii, " === "); - for(i = 2; i >= 0; --i) { - rings[i].size = i; - rings[i].location = 0; - rings[i].held = false; - } - - // initialize towers - memset(towers[0].rings, true, 3); - memset(towers[1].rings, false, 3); - memset(towers[2].rings, false, 3); + init_rings(); + init_towers(); wmain = initscr(); cbreak(); @@ -69,6 +58,27 @@ int main() return 0; } +void init_rings() { + int i; + + // initialize rings + strcpy(rings[0].ascii, " ======= "); + strcpy(rings[1].ascii, " ===== "); + strcpy(rings[2].ascii, " === "); + for(i = 2; i >= 0; --i) { + rings[i].size = i; + rings[i].location = 0; + rings[i].held = false; + } +} + +void init_towers() { + // initialize towers + memset(towers[0].rings, true, 3); + memset(towers[1].rings, false, 3); + memset(towers[2].rings, false, 3); +} + bool check_terminal() { // check that terminal is big enough if(getmaxx(wmain) < TERMINAL_MIN_WIDTH) return false; @@ -127,6 +137,8 @@ void draw_body() { mvprintw(16, 2, "size: %d", rings[2].size); mvprintw(17, 2, "ascii: %s", rings[2].ascii); } + + if(held == NULL && check_win_condition()) draw_win(); } void draw_tower(int ring_index, int startx, int starty) { @@ -158,6 +170,11 @@ void draw_tower(int ring_index, int startx, int starty) { wattroff(wbody, A_BOLD); } +void draw_win() { + char *message = "WINNER (Return Key to play again)"; + mvwprintw(wbody, getmaxy(wbody)/2, getmaxx(wbody)/2 - strlen(message)/2, message); +} + void draw_move_count() { // tried to use log10, but this created weird problems int local_move_count = move_count; @@ -176,35 +193,49 @@ void input_loop() { while(input != 'q') { input = getch(); - switch(input) { - case KEY_RIGHT: - hover_move_right(); - draw_body(); - wrefresh(wbody); - break; - - case KEY_LEFT: - hover_move_left(); - draw_body(); - wrefresh(wbody); - break; - - // pickup a ring - case ' ': // spacebar - if(held == NULL) { - pickup_ring(); - if(held != NULL) ++move_count; + if(held != NULL || !check_win_condition()) { + switch(input) { + case KEY_RIGHT: + hover_move_right(); + draw_body(); + wrefresh(wbody); + break; + + case KEY_LEFT: + hover_move_left(); draw_body(); wrefresh(wbody); - } - else { - if(drop_ring()) { + break; + + // pickup a ring + case ' ': // spacebar + if(held == NULL) { + pickup_ring(); + if(held != NULL) ++move_count; draw_body(); wrefresh(wbody); } - } - break; + else { + if(drop_ring()) { + draw_body(); + wrefresh(wbody); + } + } + break; + } + } + else { + switch(input) { + case 10: // enter/return key + init_rings(); + init_towers(); + hover = 0; + move_count = 0; + draw_body(); + wrefresh(wbody); + break; + } } } } diff --git a/src/ring.c b/src/ring.c index e73d477..909a90f 100644 --- a/src/ring.c +++ b/src/ring.c @@ -44,3 +44,13 @@ bool drop_ring() { held = NULL; return true; } + +bool check_win_condition() { + int i; + + for(i = 0; i < 3; ++i) { + if(rings[i].location != 2) return false; + } + + return true; +} diff --git a/src/ring.h b/src/ring.h index 0e146ff..c7fde98 100644 --- a/src/ring.h +++ b/src/ring.h @@ -15,5 +15,6 @@ extern Ring *held; void pickup_ring(); bool drop_ring(); +bool check_win_condition(); #endif -- cgit