Commit 41793e9b authored by Chris's avatar Chris
Browse files

implemented small straight, large straight logic

parent 7f36cc04
Loading
Loading
Loading
Loading
+33 −4
Original line number Diff line number Diff line
@@ -154,13 +154,19 @@ unsigned int calculate_selection_score(game_context *ctx, char *selection) {
        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
        printf("not implemented, scoring 0 for now");
        if (n_in_a_row(ctx->game_state.dice, NUM_DICE, 4)) {
            ctx->game_state.scorecard.small_straight = 30;
        } else {
            ctx->game_state.scorecard.small_straight = 0;
        }
        points_awarded = ctx->game_state.scorecard.small_straight;
    } else if (strcasecmp(selection, "large straight") == 0) {
        // 40 points for a sequence of five consecutive numbers; 0 otherwise
        printf("not implemented, scoring 0 for now");
        if (n_in_a_row(ctx->game_state.dice, NUM_DICE, 5)) {
            ctx->game_state.scorecard.large_straight = 40;
        } else {
            ctx->game_state.scorecard.large_straight = 0;
        }
        points_awarded = ctx->game_state.scorecard.large_straight;
    } else if (strcasecmp(selection, "yahtc") == 0) {
        // 50 points for five of a kind; 0 otherwise
@@ -300,3 +306,26 @@ unsigned int n_of_a_kind(struct die_st *dice, unsigned int num_dice, unsigned in
    }
    return 0;
}

unsigned int n_in_a_row(struct die_st *dice, unsigned int num_dice, unsigned int n) {
    if (n > num_dice) {
        printf("n_in_a_row(): error: n is greater than num_dice");
    }
    for (unsigned int i = 0; i < num_dice - 1; i++) {
        unsigned int sequence_count = 1;
        unsigned int next_in_sequence = dice[i].value + 1;
        for (unsigned int j = 0; j < num_dice; j++) {
            if (j == i) continue; // don't factor in the current die

            if (dice[j].value == next_in_sequence) {
                sequence_count++;
                if (sequence_count == n) {
                    return 1;
                }
                next_in_sequence++;
                j = 0;
            }
        }
    }
    return 0;
}
+1 −0
Original line number Diff line number Diff line
@@ -62,5 +62,6 @@ 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_in_a_row(struct die_st *dice, unsigned int num_dice, unsigned int n);

#endif //DICELOGIC_H