diff --git a/watch-faces/complication/blackjack_face.c b/watch-faces/complication/blackjack_face.c index 1497b7d6..3d549dca 100755 --- a/watch-faces/complication/blackjack_face.c +++ b/watch-faces/complication/blackjack_face.c @@ -60,6 +60,7 @@ typedef enum { BJ_DEALER_PLAYING, BJ_BUST, BJ_RESULT, + BJ_WIN_RATIO, } game_state_t; typedef enum { @@ -69,6 +70,8 @@ typedef enum { static game_state_t game_state; static uint8_t deck[DECK_SIZE] = {0}; static uint8_t current_card = 0; +static bool add_to_games_played = false; +static bool add_to_games_won = false; hand_info_t player; hand_info_t dealer; @@ -193,10 +196,15 @@ static void display_card_at_position(uint8_t card, uint8_t display_position) { } static void display_player_hand(void) { - uint8_t cards_to_display = player.idx_hand > MAX_PLAYER_CARDS_DISPLAY ? MAX_PLAYER_CARDS_DISPLAY : player.idx_hand; - for (uint8_t i=cards_to_display; i>0; i--) { - uint8_t card = player.hand[player.idx_hand-i]; - display_card_at_position(card, BOARD_DISPLAY_START + cards_to_display - i); + uint8_t card; + if (player.idx_hand <= MAX_PLAYER_CARDS_DISPLAY) { + card = player.hand[player.idx_hand - 1]; + display_card_at_position(card, BOARD_DISPLAY_START + player.idx_hand - 1); + } else { + for (uint8_t i=0; igames_played > 0) { // Avoid dividing by zero + win_ratio = (uint8_t)(100 * state->games_won) / state->games_played; + } + watch_display_text_with_fallback(WATCH_POSITION_TOP, "WINS ", "WR "); + sprintf(buf, "%3dPct", win_ratio); + watch_display_text(WATCH_POSITION_BOTTOM, buf); +} + static void begin_playing(bool tap_control_on) { watch_clear_display(); if (tap_control_on) { @@ -251,6 +276,7 @@ static void begin_playing(bool tap_control_on) { reset_hands(); // Give player their first 2 cards give_card(&player); + display_player_hand(); give_card(&player); display_player_hand(); display_score(player.score, WATCH_POSITION_SECONDS); @@ -304,6 +330,25 @@ static void see_if_dealer_hits(void) { } } +static void add_to_game_scores(blackjack_face_state_t *state) { + if (add_to_games_played) { + add_to_games_played = false; + state->games_played++; + if (state->games_played == 0) { + // Overflow + state->games_won = 0; + } + } + if (add_to_games_won) { + add_to_games_won = false; + state->games_won++; + if (state->games_won == 0) { + // Overflow + state->games_played = 0; + } + } +} + static void handle_button_presses(bool tap_control_on, bool hit) { switch (game_state) { @@ -324,6 +369,7 @@ static void handle_button_presses(bool tap_control_on, bool hit) { display_bust(); break; case BJ_RESULT: + case BJ_WIN_RATIO: display_title(); break; } @@ -362,6 +408,9 @@ void blackjack_face_activate(void *context) { bool blackjack_face_loop(movement_event_t event, void *context) { blackjack_face_state_t *state = (blackjack_face_state_t *) context; + if (game_state == BJ_RESULT) { + add_to_game_scores(state); + } switch (event.event_type) { case EVENT_ACTIVATE: if (state->tap_control_on) { @@ -390,6 +439,11 @@ bool blackjack_face_loop(movement_event_t event, void *context) { case EVENT_SINGLE_TAP: handle_button_presses(state->tap_control_on, true); break; + case EVENT_LIGHT_LONG_PRESS: + if (game_state == BJ_TITLE_SCREEN) { + display_win_ratio(state); + } + break; case EVENT_ALARM_LONG_PRESS: if (game_state == BJ_TITLE_SCREEN) { toggle_tap_control(state); diff --git a/watch-faces/complication/blackjack_face.h b/watch-faces/complication/blackjack_face.h index 84041517..c93143b9 100755 --- a/watch-faces/complication/blackjack_face.h +++ b/watch-faces/complication/blackjack_face.h @@ -71,6 +71,8 @@ typedef struct { bool tap_control_on; + uint16_t games_played; + uint16_t games_won; } blackjack_face_state_t; void blackjack_face_setup(uint8_t watch_face_index, void ** context_ptr);