Commit 3bc033e8 authored by Chris's avatar Chris
Browse files

dice animation now goes based on time

parent a4bc5f39
Loading
Loading
Loading
Loading
+13 −5
Original line number Diff line number Diff line
// dicelogic.c

#include <stdio.h>
#include <time.h>
#include "gamelogic.h"
#include "graphics.h"
#include "utils.h"

const int NUM_DICE = 5;
const int SCORE_UNSCORED = -1;

void roll_and_print(game_context *ctx, unsigned int *out_dice_values, int animation_seconds) {
void roll_and_print(game_context *ctx, unsigned int *out_dice_values, unsigned int animation_seconds) {
    unsigned int temporary_next_roll[NUM_DICE];
    time_t start_time = time(NULL);
    // do a little dice animation
    for (int i = 0; i < 30; i++) {
    for (time_t current_time = time(NULL); current_time - start_time < animation_seconds; current_time = time(NULL)) {
        sleep_ms(50);
        generate_dice_roll(ctx, temporary_next_roll, 0);
        print_multiple_dice(temporary_next_roll, NUM_DICE);
        sleep_ms(50);
        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);
    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");
@@ -49,7 +52,7 @@ int set_die_held(game_context *ctx, unsigned int index, unsigned int held) {
    int error = 0;
    // check bounds
    if (index > 4) {
        printf("hold_die(): index %d is out of bounds\n", index);
        printf("set_die_held(): index %d is out of bounds\n", index);
        error = 1;
    } else {
        ctx->game_state.dice[index].held = held;
@@ -63,3 +66,8 @@ void clear_held_values(game_context *ctx) {
        ctx->game_state.dice[i].held = 0;
    }
}

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

}
+24 −1
Original line number Diff line number Diff line
@@ -6,14 +6,32 @@
#define DICELOGIC_H

extern const int NUM_DICE;
extern const int SCORE_UNSCORED;

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

struct scores_st {
    int aces;
    int twos;
    int threes;
    int fours;
    int fives;
    int sixes;
    int three_of_a_kind;
    int four_of_a_kind;
    int full_house;
    int small_straight;
    int large_straight;
    int yahtc; // five of a kind
    int chance;
};

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

typedef struct game_context_st {
@@ -21,10 +39,15 @@ typedef struct game_context_st {
    struct game_state_st game_state;
} game_context;

// dice functions
unsigned int generate_single_die_roll(FILE *urandom_file);
// todo disassemble: I don't want to reference the entire ctx in these, I want to reference direct struct members directly
void generate_dice_roll(game_context *ctx, unsigned int *out_dice_values, int save_in_game_state);
int set_die_held(game_context *ctx, unsigned int index, unsigned int held);
void roll_and_print(game_context *ctx, unsigned int *out_dice_values, int animation_seconds);
void roll_and_print(game_context *ctx, unsigned int *out_dice_values, unsigned int animation_seconds);
void clear_held_values(game_context *ctx);

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

#endif //DICELOGIC_H
+1 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@

#include <stdio.h>
#include <stdarg.h>
#include "utils.h"

static const int DIE_VERTICAL_LENGTH = 6;
static const int DICE_ARRAY_LENGTH = 7;
+33 −12
Original line number Diff line number Diff line
@@ -9,14 +9,13 @@
// Chris Morris
// MIT License

void do_demo_dice_rolls(game_context *ctx,
                        unsigned int *next_roll); // this is specifically to show 2 demo rolls, with one hold
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);

int main(void) {
    game_context ctx;
    // initialize random device for game state
    ctx.random_device_fd = get_urandom();
    unsigned int next_roll[NUM_DICE];

    // set terminal to raw mode so we have more control over single keypress user inputs
    set_raw_terminal_configuration();
@@ -34,8 +33,10 @@ int main(void) {

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

    if (strcasecmp(selected_action, "demo") == 0) {
        do_demo_dice_rolls(&ctx, next_roll);
    if (strcasecmp(selected_action, "start") == 0) {
        start_game(&ctx);
    } else if (strcasecmp(selected_action, "demo") == 0) {
        do_demo_dice_rolls(&ctx, NUM_DICE);
    }

    // cleanup items
@@ -46,21 +47,41 @@ int main(void) {
    return 0;
}

void do_demo_dice_rolls(game_context *ctx, unsigned int *next_roll) {
void start_game(game_context *ctx) {
    unsigned int next_roll[NUM_DICE];
    for (int i = 1; i < 4; i++) {
        // print Roll 1.
        printf("Roll %d\n", i);

        // do a roll
        roll_and_print(ctx, next_roll, 3);

        // 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)

    }
}

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

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

    // first roll
    roll_and_print(ctx, next_roll, 0);
    roll_and_print(ctx, next_roll, 3);

    sleep_ms(1000);

    // set some dice held (debug)
    if (num_dice < 6) { // safeguard
        set_die_held(ctx, 0, 1);
        set_die_held(ctx, 1, 1);
        set_die_held(ctx, 4, 1);
        // set_die_held(&ctx, 5, 1); // tests edge case
    }

    roll_and_print(ctx, next_roll, 0);
    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++) {