Loading gamelogic.c +13 −9 Original line number Diff line number Diff line Loading @@ -229,17 +229,20 @@ void hold_dice_with_menu(struct die_st *dice, unsigned int num_dice) { for (int i = 0; i < num_dice; i++) { int human_readable_i = i + 1; dice_hold_selections[i].title = malloc(21 * sizeof(char)); snprintf(dice_hold_selections[i].title, 20, "die = %1d: value = %1d", human_readable_i, dice[i].value); snprintf(dice_hold_selections[i].title, 20, "die %1d: value = %1d", human_readable_i, dice[i].value); dice_hold_selections[i].multi_selected = dice[i].held; dice_hold_selections[i].enabled = 1; dice_hold_selections[i].command = malloc(SCORECARD_NUM_ITEMS * sizeof(char)); sprintf(dice_hold_selections[i].command, "toggle_hold_%1d", i); // todo cm delete: you don't even need a command for this because we go by the "multi_selected" value for dice hold assignments // dice_hold_selections[i].command = malloc(SCORECARD_NUM_ITEMS * sizeof(char)); // sprintf(dice_hold_selections[i].command, "toggle_hold_%1d", i); dice_hold_selections[i].command = ""; } menu_prompt_multi_select(dice_hold_selections, num_dice); // menu_prompt(dice_hold_selections, num_dice); char *action = menu_prompt_multi_select(dice_hold_selections, num_dice, "Hold dice", "held"); for (unsigned int i = 0; i < num_dice; i++) { dice[i].held = dice_hold_selections[i].multi_selected; } Loading @@ -249,9 +252,9 @@ void hold_dice_with_menu(struct die_st *dice, unsigned int num_dice) { if (dice_hold_selections[i].title) { free(dice_hold_selections[i].title); } if (dice_hold_selections[i].command) { free(dice_hold_selections[i].command); } // if (dice_hold_selections[i].command) { // free(dice_hold_selections[i].command); // } } } Loading @@ -270,9 +273,10 @@ char *get_player_score_selection_with_menu(struct scorecard_st *scorecard) { {.title = "full house", .command = "full house", .multi_selected = 0, .enabled = scorecard->full_house == SCORE_UNSCORED}, {.title = "yahtc", .command = "yahtc", .multi_selected = 0, .enabled = scorecard->yahtc == SCORE_UNSCORED}, {.title = "chance", .command = "chance", .multi_selected = 0, .enabled = scorecard->chance == SCORE_UNSCORED}, {.title = "{BACK}", .command = "back", .multi_selected = 0, .enabled = 1} }; unsigned int num_menu_items = sizeof(scoring_menu) / sizeof(menu_item); return menu_prompt(scoring_menu, num_menu_items); return menu_prompt(scoring_menu, num_menu_items, "Scoring", "already played"); } int sum_dice_matching_value(struct die_st *dice, unsigned int value) { Loading menu.c +9 −4 Original line number Diff line number Diff line Loading @@ -6,7 +6,9 @@ MENU_DIRECTION get_direction(void); char *menu_prompt(const menu_item *items, const unsigned int num_menu_items) { char *menu_prompt(const menu_item *items, unsigned int num_menu_items, const char *title, const char *item_disabled_label) { printf("%s\n", title); for (unsigned int i = 0; i < num_menu_items; i++) { printf("\n"); // make downward room for the menu } Loading @@ -28,7 +30,7 @@ char *menu_prompt(const menu_item *items, const unsigned int num_menu_items) { printf("> "); } if (!items[i].enabled) { printf(" [disabled] "); printf(" [%s] ", item_disabled_label); } printf("%s\n", items[i].title); } Loading Loading @@ -67,7 +69,10 @@ char *menu_prompt(const menu_item *items, const unsigned int num_menu_items) { return selected_action; } void menu_prompt_multi_select(menu_item *items, const unsigned int num_menu_items) { char *menu_prompt_multi_select(menu_item *items, unsigned int num_menu_items, const char *title, const char *item_disabled_label) { printf("%s\n", title); printf("Press SPACE to select an item. Press ENTER to make your selection.\n"); for (unsigned int i = 0; i < num_menu_items; i++) { printf("\n"); // make downward room for the menu } Loading @@ -92,7 +97,7 @@ void menu_prompt_multi_select(menu_item *items, const unsigned int num_menu_item printf(" [selected] "); } if (!items[i].enabled) { printf(" [disabled] "); printf(" [%s] ", item_disabled_label); } printf("%s\n", items[i].title); } Loading menu.h +2 −3 Original line number Diff line number Diff line Loading @@ -20,8 +20,7 @@ typedef struct { char *title; } menu_item; char *menu_prompt(const menu_item *items, unsigned int num_menu_items); void menu_prompt_multi_select(menu_item *items, unsigned int num_menu_items); MENU_DIRECTION get_menu_direction(int selection); char *menu_prompt(const menu_item *items, unsigned int num_menu_items, const char *title, const char *item_disabled_label); char *menu_prompt_multi_select(menu_item *items, unsigned int num_menu_items, const char *title, const char *item_disabled_label); #endif //MENU_H yaht.c +7 −5 Original line number Diff line number Diff line Loading @@ -32,7 +32,7 @@ int main(void) { }; const unsigned int num_menu_items = sizeof(items) / sizeof(menu_item); const char *selected_action = menu_prompt(items, num_menu_items); const char *selected_action = menu_prompt(items, num_menu_items, "Main menu", ""); if (strcasecmp(selected_action, "start") == 0) { unsigned int user_quit = 0; Loading Loading @@ -74,7 +74,7 @@ unsigned int take_turn(game_context *ctx, unsigned int num_dice) { {.title = "End game", .command = "quit", .enabled = 1}, }; const unsigned int num_menu_items = sizeof(in_turn_menu) / sizeof(menu_item); const char *player_action = menu_prompt(in_turn_menu, num_menu_items); const char *player_action = menu_prompt(in_turn_menu, num_menu_items, "Choose from the following:", "disabled"); if (strcasecmp(player_action, "enter_hold_selection") == 0) { // enter the menu for which dice to hold hold_dice_with_menu(ctx->game_state.dice, NUM_DICE); Loading @@ -84,9 +84,11 @@ unsigned int take_turn(game_context *ctx, unsigned int num_dice) { } else if (strcasecmp(player_action, "score") == 0) { // enter the score menu char *selection = get_player_score_selection_with_menu(&ctx->game_state.scorecard); if (strcasecmp(selection, "back") != 0) { unsigned int points_awarded = calculate_selection_score(ctx, selection); // todo we should use the return value for this to assign the point value, not do it within get_player_score_selection() printf("for selection %s, points awarded: %d\n", selection, points_awarded); return 0; // 0 signifies user_quit == false, which will move on to the next turn } } else if (strcasecmp(player_action, "quit") == 0) { // todo this could be more robust or interactive. maybe add the final score? // also you could maybe ask to start a new game or to quit the program Loading Loading
gamelogic.c +13 −9 Original line number Diff line number Diff line Loading @@ -229,17 +229,20 @@ void hold_dice_with_menu(struct die_st *dice, unsigned int num_dice) { for (int i = 0; i < num_dice; i++) { int human_readable_i = i + 1; dice_hold_selections[i].title = malloc(21 * sizeof(char)); snprintf(dice_hold_selections[i].title, 20, "die = %1d: value = %1d", human_readable_i, dice[i].value); snprintf(dice_hold_selections[i].title, 20, "die %1d: value = %1d", human_readable_i, dice[i].value); dice_hold_selections[i].multi_selected = dice[i].held; dice_hold_selections[i].enabled = 1; dice_hold_selections[i].command = malloc(SCORECARD_NUM_ITEMS * sizeof(char)); sprintf(dice_hold_selections[i].command, "toggle_hold_%1d", i); // todo cm delete: you don't even need a command for this because we go by the "multi_selected" value for dice hold assignments // dice_hold_selections[i].command = malloc(SCORECARD_NUM_ITEMS * sizeof(char)); // sprintf(dice_hold_selections[i].command, "toggle_hold_%1d", i); dice_hold_selections[i].command = ""; } menu_prompt_multi_select(dice_hold_selections, num_dice); // menu_prompt(dice_hold_selections, num_dice); char *action = menu_prompt_multi_select(dice_hold_selections, num_dice, "Hold dice", "held"); for (unsigned int i = 0; i < num_dice; i++) { dice[i].held = dice_hold_selections[i].multi_selected; } Loading @@ -249,9 +252,9 @@ void hold_dice_with_menu(struct die_st *dice, unsigned int num_dice) { if (dice_hold_selections[i].title) { free(dice_hold_selections[i].title); } if (dice_hold_selections[i].command) { free(dice_hold_selections[i].command); } // if (dice_hold_selections[i].command) { // free(dice_hold_selections[i].command); // } } } Loading @@ -270,9 +273,10 @@ char *get_player_score_selection_with_menu(struct scorecard_st *scorecard) { {.title = "full house", .command = "full house", .multi_selected = 0, .enabled = scorecard->full_house == SCORE_UNSCORED}, {.title = "yahtc", .command = "yahtc", .multi_selected = 0, .enabled = scorecard->yahtc == SCORE_UNSCORED}, {.title = "chance", .command = "chance", .multi_selected = 0, .enabled = scorecard->chance == SCORE_UNSCORED}, {.title = "{BACK}", .command = "back", .multi_selected = 0, .enabled = 1} }; unsigned int num_menu_items = sizeof(scoring_menu) / sizeof(menu_item); return menu_prompt(scoring_menu, num_menu_items); return menu_prompt(scoring_menu, num_menu_items, "Scoring", "already played"); } int sum_dice_matching_value(struct die_st *dice, unsigned int value) { Loading
menu.c +9 −4 Original line number Diff line number Diff line Loading @@ -6,7 +6,9 @@ MENU_DIRECTION get_direction(void); char *menu_prompt(const menu_item *items, const unsigned int num_menu_items) { char *menu_prompt(const menu_item *items, unsigned int num_menu_items, const char *title, const char *item_disabled_label) { printf("%s\n", title); for (unsigned int i = 0; i < num_menu_items; i++) { printf("\n"); // make downward room for the menu } Loading @@ -28,7 +30,7 @@ char *menu_prompt(const menu_item *items, const unsigned int num_menu_items) { printf("> "); } if (!items[i].enabled) { printf(" [disabled] "); printf(" [%s] ", item_disabled_label); } printf("%s\n", items[i].title); } Loading Loading @@ -67,7 +69,10 @@ char *menu_prompt(const menu_item *items, const unsigned int num_menu_items) { return selected_action; } void menu_prompt_multi_select(menu_item *items, const unsigned int num_menu_items) { char *menu_prompt_multi_select(menu_item *items, unsigned int num_menu_items, const char *title, const char *item_disabled_label) { printf("%s\n", title); printf("Press SPACE to select an item. Press ENTER to make your selection.\n"); for (unsigned int i = 0; i < num_menu_items; i++) { printf("\n"); // make downward room for the menu } Loading @@ -92,7 +97,7 @@ void menu_prompt_multi_select(menu_item *items, const unsigned int num_menu_item printf(" [selected] "); } if (!items[i].enabled) { printf(" [disabled] "); printf(" [%s] ", item_disabled_label); } printf("%s\n", items[i].title); } Loading
menu.h +2 −3 Original line number Diff line number Diff line Loading @@ -20,8 +20,7 @@ typedef struct { char *title; } menu_item; char *menu_prompt(const menu_item *items, unsigned int num_menu_items); void menu_prompt_multi_select(menu_item *items, unsigned int num_menu_items); MENU_DIRECTION get_menu_direction(int selection); char *menu_prompt(const menu_item *items, unsigned int num_menu_items, const char *title, const char *item_disabled_label); char *menu_prompt_multi_select(menu_item *items, unsigned int num_menu_items, const char *title, const char *item_disabled_label); #endif //MENU_H
yaht.c +7 −5 Original line number Diff line number Diff line Loading @@ -32,7 +32,7 @@ int main(void) { }; const unsigned int num_menu_items = sizeof(items) / sizeof(menu_item); const char *selected_action = menu_prompt(items, num_menu_items); const char *selected_action = menu_prompt(items, num_menu_items, "Main menu", ""); if (strcasecmp(selected_action, "start") == 0) { unsigned int user_quit = 0; Loading Loading @@ -74,7 +74,7 @@ unsigned int take_turn(game_context *ctx, unsigned int num_dice) { {.title = "End game", .command = "quit", .enabled = 1}, }; const unsigned int num_menu_items = sizeof(in_turn_menu) / sizeof(menu_item); const char *player_action = menu_prompt(in_turn_menu, num_menu_items); const char *player_action = menu_prompt(in_turn_menu, num_menu_items, "Choose from the following:", "disabled"); if (strcasecmp(player_action, "enter_hold_selection") == 0) { // enter the menu for which dice to hold hold_dice_with_menu(ctx->game_state.dice, NUM_DICE); Loading @@ -84,9 +84,11 @@ unsigned int take_turn(game_context *ctx, unsigned int num_dice) { } else if (strcasecmp(player_action, "score") == 0) { // enter the score menu char *selection = get_player_score_selection_with_menu(&ctx->game_state.scorecard); if (strcasecmp(selection, "back") != 0) { unsigned int points_awarded = calculate_selection_score(ctx, selection); // todo we should use the return value for this to assign the point value, not do it within get_player_score_selection() printf("for selection %s, points awarded: %d\n", selection, points_awarded); return 0; // 0 signifies user_quit == false, which will move on to the next turn } } else if (strcasecmp(player_action, "quit") == 0) { // todo this could be more robust or interactive. maybe add the final score? // also you could maybe ask to start a new game or to quit the program Loading