Loading gamelogic.c +22 −6 Original line number Diff line number Diff line // dicelogic.c #include <malloc.h> #include <stdio.h> #include <time.h> #include <string.h> Loading Loading @@ -193,28 +194,43 @@ unsigned int can_continue_playing(struct scorecard_st *scorecard) { void hold_dice_with_menu(struct die_st *dice, unsigned int num_dice) { menu_item dice_hold_selections[num_dice]; printf("initializing hold dice menu\n"); for (unsigned int i = 0; i < num_dice; i++) { unsigned int human_readable_i = i + 1; sprintf(dice_hold_selections[i].title, "%d", human_readable_i); 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); dice_hold_selections[i].multi_selected = dice[i].held; dice_hold_selections[i].enabled = 1; sprintf(dice_hold_selections[i].command, "toggle_hold_%d", i); dice_hold_selections[i].command = malloc(13 * sizeof(char)); sprintf(dice_hold_selections[i].command, "toggle_hold_%1d", i); } printf("prompting hold dice menu\n"); menu_prompt_multi_select(dice_hold_selections, num_dice); // menu_prompt(dice_hold_selections, num_dice); printf("done prompting hold dice menu; saving selections\n"); for (unsigned int i = 0; i < num_dice; i++) { dice[i].held = dice_hold_selections[i].multi_selected; } printf("hold_dice_with_menu() done\n"); for (int i = 0; i < num_dice; i++) { if (dice_hold_selections[i].title) { free(dice_hold_selections[i].title); } if (dice_hold_selections[i].command) { free(dice_hold_selections[i].command); } } } char *get_player_score_selection() { return ""; // placeholder } int sum_dice_matching_value(struct die_st *dice, unsigned int value) { int sum = 0; unsigned int sum_dice_matching_value(struct die_st *dice, unsigned int value) { unsigned int sum = 0; for (unsigned int i = 0; i < NUM_DICE; i++) { if (dice->value == value) { sum += dice->value; Loading gamelogic.h +2 −2 Original line number Diff line number Diff line Loading @@ -11,7 +11,7 @@ extern const unsigned int ROLLS_PER_TURN; extern const unsigned int ROLL_NUMBER_OFFSET; struct die_st { int value; unsigned int value; unsigned int held; }; void init_die_st(struct die_st *die); Loading Loading @@ -59,6 +59,6 @@ unsigned int calculate_selection_score(game_context *ctx, char *selection); unsigned int calculate_total_score(struct scorecard_st *scorecard); void initialize_scorecard(struct scorecard_st *scorecard); char *get_player_score_selection(); int sum_dice_matching_value(struct die_st *dice, unsigned int value); unsigned int sum_dice_matching_value(struct die_st *dice, unsigned int value); #endif //DICELOGIC_H menu.c +1 −0 Original line number Diff line number Diff line Loading @@ -114,6 +114,7 @@ void menu_prompt_multi_select(menu_item *items, const unsigned int num_menu_item case SPACE: items[hovered_selection_index].multi_selected = !items[hovered_selection_index].multi_selected; hovered_item_changed = 1; break; case ENTER: if (items[hovered_selection_index].enabled) { user_done_selecting = 1; Loading menu.h +1 −1 Original line number Diff line number Diff line Loading @@ -14,10 +14,10 @@ typedef enum { } MENU_DIRECTION; typedef struct { char *title; char *command; unsigned int enabled; unsigned int multi_selected; char *title; } menu_item; char *menu_prompt(const menu_item *items, unsigned int num_menu_items); Loading utils.c +1 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // timespec struct #include "utils.h" Loading Loading
gamelogic.c +22 −6 Original line number Diff line number Diff line // dicelogic.c #include <malloc.h> #include <stdio.h> #include <time.h> #include <string.h> Loading Loading @@ -193,28 +194,43 @@ unsigned int can_continue_playing(struct scorecard_st *scorecard) { void hold_dice_with_menu(struct die_st *dice, unsigned int num_dice) { menu_item dice_hold_selections[num_dice]; printf("initializing hold dice menu\n"); for (unsigned int i = 0; i < num_dice; i++) { unsigned int human_readable_i = i + 1; sprintf(dice_hold_selections[i].title, "%d", human_readable_i); 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); dice_hold_selections[i].multi_selected = dice[i].held; dice_hold_selections[i].enabled = 1; sprintf(dice_hold_selections[i].command, "toggle_hold_%d", i); dice_hold_selections[i].command = malloc(13 * sizeof(char)); sprintf(dice_hold_selections[i].command, "toggle_hold_%1d", i); } printf("prompting hold dice menu\n"); menu_prompt_multi_select(dice_hold_selections, num_dice); // menu_prompt(dice_hold_selections, num_dice); printf("done prompting hold dice menu; saving selections\n"); for (unsigned int i = 0; i < num_dice; i++) { dice[i].held = dice_hold_selections[i].multi_selected; } printf("hold_dice_with_menu() done\n"); for (int i = 0; i < num_dice; i++) { if (dice_hold_selections[i].title) { free(dice_hold_selections[i].title); } if (dice_hold_selections[i].command) { free(dice_hold_selections[i].command); } } } char *get_player_score_selection() { return ""; // placeholder } int sum_dice_matching_value(struct die_st *dice, unsigned int value) { int sum = 0; unsigned int sum_dice_matching_value(struct die_st *dice, unsigned int value) { unsigned int sum = 0; for (unsigned int i = 0; i < NUM_DICE; i++) { if (dice->value == value) { sum += dice->value; Loading
gamelogic.h +2 −2 Original line number Diff line number Diff line Loading @@ -11,7 +11,7 @@ extern const unsigned int ROLLS_PER_TURN; extern const unsigned int ROLL_NUMBER_OFFSET; struct die_st { int value; unsigned int value; unsigned int held; }; void init_die_st(struct die_st *die); Loading Loading @@ -59,6 +59,6 @@ unsigned int calculate_selection_score(game_context *ctx, char *selection); unsigned int calculate_total_score(struct scorecard_st *scorecard); void initialize_scorecard(struct scorecard_st *scorecard); char *get_player_score_selection(); int sum_dice_matching_value(struct die_st *dice, unsigned int value); unsigned int sum_dice_matching_value(struct die_st *dice, unsigned int value); #endif //DICELOGIC_H
menu.c +1 −0 Original line number Diff line number Diff line Loading @@ -114,6 +114,7 @@ void menu_prompt_multi_select(menu_item *items, const unsigned int num_menu_item case SPACE: items[hovered_selection_index].multi_selected = !items[hovered_selection_index].multi_selected; hovered_item_changed = 1; break; case ENTER: if (items[hovered_selection_index].enabled) { user_done_selecting = 1; Loading
menu.h +1 −1 Original line number Diff line number Diff line Loading @@ -14,10 +14,10 @@ typedef enum { } MENU_DIRECTION; typedef struct { char *title; char *command; unsigned int enabled; unsigned int multi_selected; char *title; } menu_item; char *menu_prompt(const menu_item *items, unsigned int num_menu_items); Loading
utils.c +1 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // timespec struct #include "utils.h" Loading