Loading gamelogic.c +9 −2 Original line number Diff line number Diff line Loading @@ -151,7 +151,7 @@ unsigned int calculate_selection_score(game_context *ctx, char *selection) { 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); two_of_a_kind_value = n_of_a_kind(ctx->game_state.dice, NUM_DICE, 2, three_of_a_kind_value); } if (three_of_a_kind_value && two_of_a_kind_value) { ctx->game_state.scorecard.full_house = 25; Loading Loading @@ -300,6 +300,14 @@ 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 value_to_avoid) { // this should NOT score as a full house dice[0].value = 6; dice[1].value = 6; dice[2].value = 6; dice[3].value = 6; dice[4].value = 3; if (n > num_dice) { printf("n_of_a_kind(): error: n is greater than num_dice"); } Loading Loading @@ -338,7 +346,6 @@ unsigned int n_in_a_row(struct die_st *dice, unsigned int num_dice, unsigned int // dice[3].value = 4; // dice[4].value = 1; if (n > num_dice) { printf("n_in_a_row(): error: n is greater than num_dice"); } Loading yaht.c +4 −4 Original line number Diff line number Diff line Loading @@ -35,10 +35,11 @@ int main(void) { const char *selected_action = menu_prompt(items, num_menu_items, "Main menu", ""); if (strcasecmp(selected_action, "start") == 0) { unsigned int user_quit = 0; while (can_continue_playing(&ctx.game_state.scorecard) && !user_quit) { user_quit = take_turn(&ctx, NUM_DICE); unsigned int game_over = 0; while (can_continue_playing(&ctx.game_state.scorecard) && !game_over) { game_over = take_turn(&ctx, NUM_DICE); } printf("Your final score was %d.\nThank you for playing!\n", calculate_total_score(&ctx.game_state.scorecard)); } else if (strcasecmp(selected_action, "demo") == 0) { do_demo_dice_rolls(&ctx, NUM_DICE); } Loading Loading @@ -92,7 +93,6 @@ unsigned int take_turn(game_context *ctx, unsigned int num_dice) { } 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 printf("Your final score was %d.\nThank you for playing!\n", calculate_total_score(&ctx->game_state.scorecard)); return 1; // 1 signifies user_quit == true, which will end the game } } Loading Loading
gamelogic.c +9 −2 Original line number Diff line number Diff line Loading @@ -151,7 +151,7 @@ unsigned int calculate_selection_score(game_context *ctx, char *selection) { 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); two_of_a_kind_value = n_of_a_kind(ctx->game_state.dice, NUM_DICE, 2, three_of_a_kind_value); } if (three_of_a_kind_value && two_of_a_kind_value) { ctx->game_state.scorecard.full_house = 25; Loading Loading @@ -300,6 +300,14 @@ 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 value_to_avoid) { // this should NOT score as a full house dice[0].value = 6; dice[1].value = 6; dice[2].value = 6; dice[3].value = 6; dice[4].value = 3; if (n > num_dice) { printf("n_of_a_kind(): error: n is greater than num_dice"); } Loading Loading @@ -338,7 +346,6 @@ unsigned int n_in_a_row(struct die_st *dice, unsigned int num_dice, unsigned int // dice[3].value = 4; // dice[4].value = 1; if (n > num_dice) { printf("n_in_a_row(): error: n is greater than num_dice"); } Loading
yaht.c +4 −4 Original line number Diff line number Diff line Loading @@ -35,10 +35,11 @@ int main(void) { const char *selected_action = menu_prompt(items, num_menu_items, "Main menu", ""); if (strcasecmp(selected_action, "start") == 0) { unsigned int user_quit = 0; while (can_continue_playing(&ctx.game_state.scorecard) && !user_quit) { user_quit = take_turn(&ctx, NUM_DICE); unsigned int game_over = 0; while (can_continue_playing(&ctx.game_state.scorecard) && !game_over) { game_over = take_turn(&ctx, NUM_DICE); } printf("Your final score was %d.\nThank you for playing!\n", calculate_total_score(&ctx.game_state.scorecard)); } else if (strcasecmp(selected_action, "demo") == 0) { do_demo_dice_rolls(&ctx, NUM_DICE); } Loading Loading @@ -92,7 +93,6 @@ unsigned int take_turn(game_context *ctx, unsigned int num_dice) { } 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 printf("Your final score was %d.\nThank you for playing!\n", calculate_total_score(&ctx->game_state.scorecard)); return 1; // 1 signifies user_quit == true, which will end the game } } Loading