Loading gamelogic.c +19 −3 Original line number Diff line number Diff line Loading @@ -107,52 +107,68 @@ void clear_held_values(game_context *ctx) { } } // actual game logic will go in here // todo: this should only map the selection to the method, and then yaht.c will handle the scoreboard assignment (I think -- revisit to confirm) unsigned int calculate_selection_score(game_context *ctx, char *selection) { // discern score action based on the scoring selection // we pass in a whole game_context because we need to write to the scorecard and read the dice values int points_awarded = 0; if (strcasecmp(selection, "aces") == 0) { ctx->game_state.scorecard.aces = sum_dice_matching_value(ctx->game_state.dice, 1); points_awarded = ctx->game_state.scorecard.aces; } else if (strcasecmp(selection, "twos") == 0) { ctx->game_state.scorecard.twos = sum_dice_matching_value(ctx->game_state.dice, 2); points_awarded = ctx->game_state.scorecard.twos; } else if (strcasecmp(selection, "threes") == 0) { ctx->game_state.scorecard.threes = sum_dice_matching_value(ctx->game_state.dice, 3); points_awarded = ctx->game_state.scorecard.threes; } else if (strcasecmp(selection, "fours") == 0) { ctx->game_state.scorecard.fours = sum_dice_matching_value(ctx->game_state.dice, 4); points_awarded = ctx->game_state.scorecard.fours; } else if (strcasecmp(selection, "fives") == 0) { ctx->game_state.scorecard.fives = sum_dice_matching_value(ctx->game_state.dice, 5); points_awarded = ctx->game_state.scorecard.fives; } else if (strcasecmp(selection, "sixes") == 0) { ctx->game_state.scorecard.sixes = sum_dice_matching_value(ctx->game_state.dice, 6); points_awarded = ctx->game_state.scorecard.sixes; } 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"); ctx->game_state.scorecard.three_of_a_kind = 0; 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 printf("not implemented, scoring 0 for now"); ctx->game_state.scorecard.four_of_a_kind = 0; 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"); 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 printf("not implemented, scoring 0 for now"); 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"); 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 printf("not implemented, scoring 0 for now"); ctx->game_state.scorecard.yahtc = 0; points_awarded = ctx->game_state.scorecard.yahtc; } else if (strcasecmp(selection, "chance") == 0) { // sum of all dice printf("not implemented, scoring 0 for now"); ctx->game_state.scorecard.chance = 0; points_awarded = ctx->game_state.scorecard.chance; } printf("for selection %s, points awarded: %d\n", selection, points_awarded); // default return 0; } Loading Loading @@ -245,8 +261,8 @@ char *get_player_score_selection_with_menu(struct scorecard_st *scorecard) { int sum_dice_matching_value(struct die_st *dice, unsigned int value) { int sum = 0; for (unsigned int i = 0; i < NUM_DICE; i++) { if (dice->value == value) { sum += (int)dice->value; if (dice[i].value == value) { sum += (int)dice[i].value; } } return sum; Loading yaht.c +4 −3 Original line number Diff line number Diff line Loading @@ -72,19 +72,20 @@ unsigned int take_turn(game_context *ctx, unsigned int num_dice) { // ask if holding dice, scoring, or rolling again // (put this in a while loop so user keeps getting prompted with choice until choosing to roll again) // (also don't allow user to roll again if on the third roll) unsigned int can_roll_again = 0; while (!can_roll_again) { unsigned int ready_for_next_roll = 0; while (!ready_for_next_roll) { const char *player_action = menu_prompt(in_turn_menu, num_menu_items); 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); } else if (strcasecmp(player_action, "roll") == 0) { printf("Rolling again...\n"); can_roll_again = 1; ready_for_next_roll = 1; } else if (strcasecmp(player_action, "score") == 0) { // enter the score menu char *selection = get_player_score_selection_with_menu(&ctx->game_state.scorecard); calculate_selection_score(ctx, selection); return 0; } 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 +19 −3 Original line number Diff line number Diff line Loading @@ -107,52 +107,68 @@ void clear_held_values(game_context *ctx) { } } // actual game logic will go in here // todo: this should only map the selection to the method, and then yaht.c will handle the scoreboard assignment (I think -- revisit to confirm) unsigned int calculate_selection_score(game_context *ctx, char *selection) { // discern score action based on the scoring selection // we pass in a whole game_context because we need to write to the scorecard and read the dice values int points_awarded = 0; if (strcasecmp(selection, "aces") == 0) { ctx->game_state.scorecard.aces = sum_dice_matching_value(ctx->game_state.dice, 1); points_awarded = ctx->game_state.scorecard.aces; } else if (strcasecmp(selection, "twos") == 0) { ctx->game_state.scorecard.twos = sum_dice_matching_value(ctx->game_state.dice, 2); points_awarded = ctx->game_state.scorecard.twos; } else if (strcasecmp(selection, "threes") == 0) { ctx->game_state.scorecard.threes = sum_dice_matching_value(ctx->game_state.dice, 3); points_awarded = ctx->game_state.scorecard.threes; } else if (strcasecmp(selection, "fours") == 0) { ctx->game_state.scorecard.fours = sum_dice_matching_value(ctx->game_state.dice, 4); points_awarded = ctx->game_state.scorecard.fours; } else if (strcasecmp(selection, "fives") == 0) { ctx->game_state.scorecard.fives = sum_dice_matching_value(ctx->game_state.dice, 5); points_awarded = ctx->game_state.scorecard.fives; } else if (strcasecmp(selection, "sixes") == 0) { ctx->game_state.scorecard.sixes = sum_dice_matching_value(ctx->game_state.dice, 6); points_awarded = ctx->game_state.scorecard.sixes; } 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"); ctx->game_state.scorecard.three_of_a_kind = 0; 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 printf("not implemented, scoring 0 for now"); ctx->game_state.scorecard.four_of_a_kind = 0; 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"); 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 printf("not implemented, scoring 0 for now"); 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"); 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 printf("not implemented, scoring 0 for now"); ctx->game_state.scorecard.yahtc = 0; points_awarded = ctx->game_state.scorecard.yahtc; } else if (strcasecmp(selection, "chance") == 0) { // sum of all dice printf("not implemented, scoring 0 for now"); ctx->game_state.scorecard.chance = 0; points_awarded = ctx->game_state.scorecard.chance; } printf("for selection %s, points awarded: %d\n", selection, points_awarded); // default return 0; } Loading Loading @@ -245,8 +261,8 @@ char *get_player_score_selection_with_menu(struct scorecard_st *scorecard) { int sum_dice_matching_value(struct die_st *dice, unsigned int value) { int sum = 0; for (unsigned int i = 0; i < NUM_DICE; i++) { if (dice->value == value) { sum += (int)dice->value; if (dice[i].value == value) { sum += (int)dice[i].value; } } return sum; Loading
yaht.c +4 −3 Original line number Diff line number Diff line Loading @@ -72,19 +72,20 @@ unsigned int take_turn(game_context *ctx, unsigned int num_dice) { // ask if holding dice, scoring, or rolling again // (put this in a while loop so user keeps getting prompted with choice until choosing to roll again) // (also don't allow user to roll again if on the third roll) unsigned int can_roll_again = 0; while (!can_roll_again) { unsigned int ready_for_next_roll = 0; while (!ready_for_next_roll) { const char *player_action = menu_prompt(in_turn_menu, num_menu_items); 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); } else if (strcasecmp(player_action, "roll") == 0) { printf("Rolling again...\n"); can_roll_again = 1; ready_for_next_roll = 1; } else if (strcasecmp(player_action, "score") == 0) { // enter the score menu char *selection = get_player_score_selection_with_menu(&ctx->game_state.scorecard); calculate_selection_score(ctx, selection); return 0; } 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