Loading gamelogic.c +21 −7 Original line number Diff line number Diff line Loading @@ -133,7 +133,7 @@ unsigned int calculate_selection_score(game_context *ctx, char *selection) { } else if (strcasecmp(selection, "three of a kind") == 0) { // sum all dice if 3 of a kind match; 0 otherwise // printf("not implemented, scoring 0 for now"); if (n_of_a_kind(ctx->game_state.dice, NUM_DICE, 3)) { if (n_of_a_kind(ctx->game_state.dice, NUM_DICE, 3, 0)) { ctx->game_state.scorecard.three_of_a_kind = sum_dice(ctx->game_state.dice); } else { ctx->game_state.scorecard.three_of_a_kind = 0; Loading @@ -141,7 +141,7 @@ unsigned int calculate_selection_score(game_context *ctx, char *selection) { points_awarded = ctx->game_state.scorecard.three_of_a_kind; } else if (strcasecmp(selection, "four of a kind") == 0) { // sum all dice if 4 of a kind match; 0 otherwise if (n_of_a_kind(ctx->game_state.dice, NUM_DICE, 4)) { if (n_of_a_kind(ctx->game_state.dice, NUM_DICE, 4, 0)) { ctx->game_state.scorecard.four_of_a_kind = sum_dice(ctx->game_state.dice); } else { ctx->game_state.scorecard.four_of_a_kind = 0; Loading @@ -149,8 +149,16 @@ unsigned int calculate_selection_score(game_context *ctx, char *selection) { points_awarded = ctx->game_state.scorecard.four_of_a_kind; } else if (strcasecmp(selection, "full house") == 0) { // 25 points for a combination of three of one number + two of another; 0 otherwise printf("not implemented, scoring 0 for now"); unsigned int three_of_a_kind_value = n_of_a_kind(ctx->game_state.dice, NUM_DICE, 3, 0); unsigned int two_of_a_kind_value = 0; if (three_of_a_kind_value) { two_of_a_kind_value = n_of_a_kind(ctx->game_state.dice, NUM_DICE, 2, 0); } if (three_of_a_kind_value && two_of_a_kind_value) { ctx->game_state.scorecard.full_house = 25; } else { ctx->game_state.scorecard.full_house = 0; } points_awarded = ctx->game_state.scorecard.full_house; } else if (strcasecmp(selection, "small straight") == 0) { // 30 points for a sequence of four consecutive numbers; 0 otherwise Loading @@ -171,7 +179,7 @@ unsigned int calculate_selection_score(game_context *ctx, char *selection) { } else if (strcasecmp(selection, "yahtc") == 0) { // 50 points for five of a kind; 0 otherwise printf("not implemented, scoring 0 for now"); if (n_of_a_kind(ctx->game_state.dice, NUM_DICE, NUM_DICE)) { if (n_of_a_kind(ctx->game_state.dice, NUM_DICE, NUM_DICE, 0)) { ctx->game_state.scorecard.yahtc = 50; } else { ctx->game_state.scorecard.yahtc = 0; Loading Loading @@ -289,18 +297,24 @@ int sum_dice(struct die_st *dice) { // I know this is uglier than it could be, but I don't have a hash map implementation in C and don't want to allocate the memory to track which values we've seen before. // so this will be okay unsigned int n_of_a_kind(struct die_st *dice, unsigned int num_dice, unsigned int n) { unsigned int n_of_a_kind(struct die_st *dice, unsigned int num_dice, unsigned int n, unsigned int value_to_avoid) { if (n > num_dice) { printf("n_of_a_kind(): error: n is greater than num_dice"); } for (unsigned int i = 0; i < num_dice - 1; i++) { if (dice[i].value == value_to_avoid) { continue; } int num_dice_with_same_value = 1; for (unsigned int j = i + 1; j < num_dice; j++) { if (dice[j].value == value_to_avoid) { continue; } if (dice[i].value == dice[j].value) { num_dice_with_same_value++; } if (num_dice_with_same_value == n) { return 1; return dice[i].value; } } } Loading gamelogic.h +1 −1 Original line number Diff line number Diff line Loading @@ -61,7 +61,7 @@ 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_with_menu(struct scorecard_st *scorecard); unsigned int n_of_a_kind(struct die_st *dice, unsigned int num_dice, unsigned int n); unsigned int n_of_a_kind(struct die_st *dice, unsigned int num_dice, unsigned int n, unsigned int value_to_avoid); unsigned int n_in_a_row(struct die_st *dice, unsigned int num_dice, unsigned int n); #endif //DICELOGIC_H Loading
gamelogic.c +21 −7 Original line number Diff line number Diff line Loading @@ -133,7 +133,7 @@ unsigned int calculate_selection_score(game_context *ctx, char *selection) { } else if (strcasecmp(selection, "three of a kind") == 0) { // sum all dice if 3 of a kind match; 0 otherwise // printf("not implemented, scoring 0 for now"); if (n_of_a_kind(ctx->game_state.dice, NUM_DICE, 3)) { if (n_of_a_kind(ctx->game_state.dice, NUM_DICE, 3, 0)) { ctx->game_state.scorecard.three_of_a_kind = sum_dice(ctx->game_state.dice); } else { ctx->game_state.scorecard.three_of_a_kind = 0; Loading @@ -141,7 +141,7 @@ unsigned int calculate_selection_score(game_context *ctx, char *selection) { points_awarded = ctx->game_state.scorecard.three_of_a_kind; } else if (strcasecmp(selection, "four of a kind") == 0) { // sum all dice if 4 of a kind match; 0 otherwise if (n_of_a_kind(ctx->game_state.dice, NUM_DICE, 4)) { if (n_of_a_kind(ctx->game_state.dice, NUM_DICE, 4, 0)) { ctx->game_state.scorecard.four_of_a_kind = sum_dice(ctx->game_state.dice); } else { ctx->game_state.scorecard.four_of_a_kind = 0; Loading @@ -149,8 +149,16 @@ unsigned int calculate_selection_score(game_context *ctx, char *selection) { points_awarded = ctx->game_state.scorecard.four_of_a_kind; } else if (strcasecmp(selection, "full house") == 0) { // 25 points for a combination of three of one number + two of another; 0 otherwise printf("not implemented, scoring 0 for now"); unsigned int three_of_a_kind_value = n_of_a_kind(ctx->game_state.dice, NUM_DICE, 3, 0); unsigned int two_of_a_kind_value = 0; if (three_of_a_kind_value) { two_of_a_kind_value = n_of_a_kind(ctx->game_state.dice, NUM_DICE, 2, 0); } if (three_of_a_kind_value && two_of_a_kind_value) { ctx->game_state.scorecard.full_house = 25; } else { ctx->game_state.scorecard.full_house = 0; } points_awarded = ctx->game_state.scorecard.full_house; } else if (strcasecmp(selection, "small straight") == 0) { // 30 points for a sequence of four consecutive numbers; 0 otherwise Loading @@ -171,7 +179,7 @@ unsigned int calculate_selection_score(game_context *ctx, char *selection) { } else if (strcasecmp(selection, "yahtc") == 0) { // 50 points for five of a kind; 0 otherwise printf("not implemented, scoring 0 for now"); if (n_of_a_kind(ctx->game_state.dice, NUM_DICE, NUM_DICE)) { if (n_of_a_kind(ctx->game_state.dice, NUM_DICE, NUM_DICE, 0)) { ctx->game_state.scorecard.yahtc = 50; } else { ctx->game_state.scorecard.yahtc = 0; Loading Loading @@ -289,18 +297,24 @@ int sum_dice(struct die_st *dice) { // I know this is uglier than it could be, but I don't have a hash map implementation in C and don't want to allocate the memory to track which values we've seen before. // so this will be okay unsigned int n_of_a_kind(struct die_st *dice, unsigned int num_dice, unsigned int n) { unsigned int n_of_a_kind(struct die_st *dice, unsigned int num_dice, unsigned int n, unsigned int value_to_avoid) { if (n > num_dice) { printf("n_of_a_kind(): error: n is greater than num_dice"); } for (unsigned int i = 0; i < num_dice - 1; i++) { if (dice[i].value == value_to_avoid) { continue; } int num_dice_with_same_value = 1; for (unsigned int j = i + 1; j < num_dice; j++) { if (dice[j].value == value_to_avoid) { continue; } if (dice[i].value == dice[j].value) { num_dice_with_same_value++; } if (num_dice_with_same_value == n) { return 1; return dice[i].value; } } } Loading
gamelogic.h +1 −1 Original line number Diff line number Diff line Loading @@ -61,7 +61,7 @@ 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_with_menu(struct scorecard_st *scorecard); unsigned int n_of_a_kind(struct die_st *dice, unsigned int num_dice, unsigned int n); unsigned int n_of_a_kind(struct die_st *dice, unsigned int num_dice, unsigned int n, unsigned int value_to_avoid); unsigned int n_in_a_row(struct die_st *dice, unsigned int num_dice, unsigned int n); #endif //DICELOGIC_H