Commit 4ac0f0cd authored by Chris's avatar Chris
Browse files

refactor to organize functions into their own source/header files

parent 2ff243fe
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -3,4 +3,8 @@ project(yahtc C)

set(CMAKE_C_STANDARD 11)

add_executable(yahtc yaht.c graphics.c)
add_executable(yahtc yaht.c graphics.c
        dicelogic.h
        dicelogic.c
        utils.h
        utils.c)

dicelogic.c

0 → 100644
+58 −0
Original line number Diff line number Diff line
// dicelogic.c

#include "dicelogic.h"
#include "graphics.h"
#include "utils.h"

const int NUM_DICE = 5;

void roll_and_print(game_context *ctx, unsigned int *out_dice_values, int animation_seconds) {
    unsigned int temporary_next_roll[NUM_DICE];
    // do a little dice animation
    for (int i = 0; i < 30; i++) {
        generate_dice_roll(ctx, temporary_next_roll, 0);
        print_multiple_dice(temporary_next_roll, NUM_DICE);
        sleep_ms(50);
        clear_dice();
    }
    // the last roll will be the one that is saved in game state
    generate_dice_roll(ctx, out_dice_values, 1);
    print_multiple_dice(out_dice_values, NUM_DICE);

    printf("\n");
}

void generate_dice_roll(game_context *ctx, unsigned int *out_dice_values, int save_in_game_state) {
    for (int i = 0; i < NUM_DICE; i++) {
        if (ctx->game_state.dice[i].held) {
            out_dice_values[i] = ctx->game_state.dice[i].value;
        } else {
            out_dice_values[i] = generate_single_die_roll(ctx->random_device_fd);
        }

        if (save_in_game_state) {
            ctx->game_state.dice[i].value = out_dice_values[i];
        }
    }
}

unsigned int generate_single_die_roll(FILE *urandom_file) {
    unsigned int next_random;
    fread(&next_random, sizeof(next_random), 1, urandom_file);
    next_random = (next_random % 6) + 1;
    return next_random;

}

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);
        error = 1;
    } else {
        ctx->game_state.dice[index].held = held;
    }

    return error;
}

dicelogic.h

0 → 100644
+30 −0
Original line number Diff line number Diff line
// dicelogic.h

#pragma once

#ifndef DICELOGIC_H
#define DICELOGIC_H
#include <stdio.h>

extern const int NUM_DICE;

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

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

typedef struct game_context_st {
    FILE *random_device_fd;
    struct game_state_st game_state;
} game_context;

unsigned int generate_single_die_roll(FILE *urandom_file);
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);

#endif //DICELOGIC_H

utils.c

0 → 100644
+18 −0
Original line number Diff line number Diff line
// utils.c

#include "utils.h"

#include <time.h> // timespec struct

// get random value from device
FILE *get_urandom() {
    FILE *urandom = fopen("/dev/urandom", "rb");
    return urandom;
}

void sleep_ms(const unsigned int ms) {
    struct timespec ts;
    ts.tv_sec = ms / 1000;
    ts.tv_nsec = (ms % 1000) * 1000000;
    nanosleep(&ts, NULL);
}

utils.h

0 → 100644
+12 −0
Original line number Diff line number Diff line
// utils.h

#pragma once

#ifndef UTILS_H
#define UTILS_H
#include <stdio.h>

FILE *get_urandom();
void sleep_ms(unsigned int ms);

#endif //UTILS_H
Loading