Commit f93cb14c authored by Chris's avatar Chris
Browse files

building up the procedure for the game sequence

parent 3bc033e8
Loading
Loading
Loading
Loading
+20 −2
Original line number Diff line number Diff line
@@ -20,7 +20,8 @@ void roll_and_print(game_context *ctx, unsigned int *out_dice_values, unsigned i
        cursor_to_dice_start(0);
    }
    // the last roll will be the one that is saved in game state
    generate_dice_roll(ctx, out_dice_values, 1); // the IO read + storing to ctx cause a stutter here (I believe -- need to investigate)
    generate_dice_roll(ctx, out_dice_values,
                       1); // the IO read + storing to ctx cause a stutter here (I believe -- need to investigate)
    print_multiple_dice(out_dice_values, NUM_DICE);

    printf("\n");
@@ -67,7 +68,24 @@ void clear_held_values(game_context *ctx) {
    }
}

unsigned int calculate_score(struct scores_st *scorecard, char *scoring_selection) {
unsigned int calculate_score(struct scorecard_st *scorecard, char *scoring_selection) {
    // discern score action based on the scoring selection

}

void initialize_scorecard(struct scorecard_st *scorecard) {
    scorecard->aces
            = scorecard->twos
            = scorecard->threes
            = scorecard->fours
            = scorecard->fives
            = scorecard->sixes
            = scorecard->three_of_a_kind
            = scorecard->four_of_a_kind
            = scorecard->full_house
            = scorecard->small_straight
            = scorecard->large_straight
            = scorecard->yahtc
            = scorecard->chance
            = SCORE_UNSCORED;
}
+7 −3
Original line number Diff line number Diff line
@@ -8,12 +8,15 @@
extern const int NUM_DICE;
extern const int SCORE_UNSCORED;

//const unsigned int MAX_TURNS = 3;
//const unsigned int TURN_NUMBER_OFFSET = 1;

struct die_st {
    unsigned int value;
    unsigned int held;
};

struct scores_st {
struct scorecard_st {
    int aces;
    int twos;
    int threes;
@@ -31,7 +34,7 @@ struct scores_st {

struct game_state_st {
    struct die_st dice[5];
    struct scores_st scorecard;
    struct scorecard_st scorecard;
};

typedef struct game_context_st {
@@ -48,6 +51,7 @@ void roll_and_print(game_context *ctx, unsigned int *out_dice_values, unsigned i
void clear_held_values(game_context *ctx);

// scorecard functions
unsigned int calculate_score(struct scores_st *scorecard, char *scoring_selection);
unsigned int calculate_score(struct scorecard_st *scorecard, char *scoring_selection);
void initialize_scorecard(struct scorecard_st *scorecard);

#endif //DICELOGIC_H
+10 −0
Original line number Diff line number Diff line
@@ -100,3 +100,13 @@ MENU_DIRECTION get_direction(void) {


}

menu_item *get_menu_item_by_command(menu_item *items, const unsigned int num_menu_items, char *command_to_find) {
    for (unsigned int i = 0; i < num_menu_items; i++) {
        if (strcasecmp(items[i].command, command_to_find) == 0) {
            return &items[i];
        }
    }
    printf("get_menu_item_by_command(): item %s not found", command_to_find);
    return NULL;
}
+1 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ typedef enum {
typedef struct {
    char *title;
    char *command;
    unsigned int enabled;
} menu_item;

char *menu_prompt(const menu_item *items, unsigned int num_menu_items);
+26 −10
Original line number Diff line number Diff line
@@ -10,12 +10,13 @@
// MIT License

void do_demo_dice_rolls(game_context *ctx, unsigned int num_dice); // this is specifically to show 2 demo rolls, with one hold
void start_game(game_context *ctx);
void game_loop(game_context *ctx, unsigned int num_dice);

int main(void) {
    game_context ctx;
    // initialize random device for game state
    ctx.random_device_fd = get_urandom();
    initialize_scorecard(&ctx.game_state.scorecard);

    // set terminal to raw mode so we have more control over single keypress user inputs
    set_raw_terminal_configuration();
@@ -25,16 +26,16 @@ int main(void) {
    printf("yaht.c\n");

    menu_item items[] = {
            {.title = "New Game", .command = "start"},
            {.title = "Demo", .command = "demo"},
            {.title = "Exit", .command = "exit"},
            {.title = "New Game", .command = "start", .enabled = 1},
            {.title = "Demo", .command = "demo", .enabled = 1},
            {.title = "Exit", .command = "exit", .enabled = 1},
    };
    const unsigned int num_menu_items = sizeof(items) / sizeof(menu_item);

    const char *selected_action = menu_prompt(items, num_menu_items);

    if (strcasecmp(selected_action, "start") == 0) {
        start_game(&ctx);
        game_loop(&ctx, NUM_DICE);
    } else if (strcasecmp(selected_action, "demo") == 0) {
        do_demo_dice_rolls(&ctx, NUM_DICE);
    }
@@ -47,9 +48,17 @@ int main(void) {
    return 0;
}

void start_game(game_context *ctx) {
    unsigned int next_roll[NUM_DICE];
void game_loop(game_context *ctx, unsigned int num_dice) {
//    clear_held_values(ctx); // todo fix the struct so that this isn't needed before the first roll_and_print()
    unsigned int next_roll[num_dice];
    const menu_item in_turn_menu[] = {
            {.title = "Hold dice", .command = "enter_hold_selection", .enabled = 1},
            {.title = "Roll again", .command = "roll", .enabled = 1},
            {.title = "Score", .command = "score", .enabled = 1},
    };
    const unsigned int num_menu_items = sizeof(in_turn_menu) / sizeof(menu_item);
    for (int i = 1; i < 4; i++) {
        clear_held_values(ctx);
        // print Roll 1.
        printf("Roll %d\n", i);

@@ -59,12 +68,19 @@ void start_game(game_context *ctx) {
        // 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)

        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
        } else if (strcasecmp(player_action, "roll") == 0) {
            printf("Rolling again...");
        } else if (strcasecmp(player_action, "score") == 0) {
            // enter the score menu
        }
    }
}

void do_demo_dice_rolls(game_context *ctx, unsigned int num_dice) {

    clear_held_values(ctx);
    unsigned int next_roll[num_dice];

@@ -84,7 +100,7 @@ void do_demo_dice_rolls(game_context *ctx, unsigned int num_dice) {
    roll_and_print(ctx, next_roll, 3);

    // sanity check that we're actually saving to game state in the roll_and_print()
    for (int i = 0; i < NUM_DICE; i++) {
    for (int i = 0; i < num_dice; i++) {
        printf("%d ", ctx->game_state.dice[i].value);
    }
    printf("\n");