Loading graphics.h +1 −1 Original line number Diff line number Diff line // graphics.h # pragma once #ifndef GRAPHICS_H_ #define GRAPHICS_H_ # pragma once #include <stdio.h> #include <stdarg.h> Loading yaht.c +41 −18 Original line number Diff line number Diff line #include <stdio.h> #include "graphics.h" #include <unistd.h> // sleep() #include <stdlib.h> // malloc() // yaht.c // Copyright (c) 2025 Loading @@ -8,8 +10,6 @@ // todo move into header file FILE *get_urandom(); unsigned int roll_die(FILE *urandom_file); const int NUM_DICE = 5; struct die_st { Loading @@ -21,31 +21,40 @@ struct game_state_st { struct die_st dice[5]; }; struct context_st { typedef struct game_context_st { FILE *random_device_fd; struct game_state_st game_state; }; } game_context; FILE *get_urandom(); unsigned int roll_single_die(FILE *urandom_file); void roll_multiple_dice(game_context *ctx, unsigned int *out_dice_values, int save_in_game_state); int main() { struct context_st ctx; game_context ctx; // initialize random device for game state ctx.random_device_fd = get_urandom(); unsigned int next_random = roll_die(ctx.random_device_fd); // the below were used to test single die roll functionality // print_single_die(next_random); unsigned int *next_roll = malloc(sizeof(unsigned int) * NUM_DICE); // first roll for (int i = 0; i < NUM_DICE; i++) { ctx.game_state.dice[i].value = roll_die(ctx.random_device_fd); // print_single_die(ctx.game_state.dice[i].value); } // extract die values into array int dice_values[NUM_DICE]; printf("yaht.c\n"); // un-hold all dice for (int i = 0; i < NUM_DICE; i++) { dice_values[i] = ctx.game_state.dice[i].value; ctx.game_state.dice[i].held = 0; } print_multiple_dice(dice_values, NUM_DICE, 0); // first roll roll_multiple_dice(&ctx, next_roll, 1); print_multiple_dice(next_roll, NUM_DICE, 0); sleep(1); printf("\n"); if (next_roll) { free(next_roll); } fclose(ctx.random_device_fd); return 0; } Loading @@ -57,7 +66,21 @@ FILE *get_urandom() { return urandom; } unsigned int roll_die(FILE *urandom_file) { void roll_multiple_dice(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] = roll_single_die(ctx->random_device_fd); } if (save_in_game_state) { ctx->game_state.dice[i].value = out_dice_values[i]; } } } unsigned int roll_single_die(FILE *urandom_file) { unsigned int next_random; fread(&next_random, sizeof(next_random), 1, urandom_file); next_random = (next_random % 6) + 1; Loading yahtc +208 B (16.7 KiB) File changed.No diff preview for this file type. View original file View changed file Loading
graphics.h +1 −1 Original line number Diff line number Diff line // graphics.h # pragma once #ifndef GRAPHICS_H_ #define GRAPHICS_H_ # pragma once #include <stdio.h> #include <stdarg.h> Loading
yaht.c +41 −18 Original line number Diff line number Diff line #include <stdio.h> #include "graphics.h" #include <unistd.h> // sleep() #include <stdlib.h> // malloc() // yaht.c // Copyright (c) 2025 Loading @@ -8,8 +10,6 @@ // todo move into header file FILE *get_urandom(); unsigned int roll_die(FILE *urandom_file); const int NUM_DICE = 5; struct die_st { Loading @@ -21,31 +21,40 @@ struct game_state_st { struct die_st dice[5]; }; struct context_st { typedef struct game_context_st { FILE *random_device_fd; struct game_state_st game_state; }; } game_context; FILE *get_urandom(); unsigned int roll_single_die(FILE *urandom_file); void roll_multiple_dice(game_context *ctx, unsigned int *out_dice_values, int save_in_game_state); int main() { struct context_st ctx; game_context ctx; // initialize random device for game state ctx.random_device_fd = get_urandom(); unsigned int next_random = roll_die(ctx.random_device_fd); // the below were used to test single die roll functionality // print_single_die(next_random); unsigned int *next_roll = malloc(sizeof(unsigned int) * NUM_DICE); // first roll for (int i = 0; i < NUM_DICE; i++) { ctx.game_state.dice[i].value = roll_die(ctx.random_device_fd); // print_single_die(ctx.game_state.dice[i].value); } // extract die values into array int dice_values[NUM_DICE]; printf("yaht.c\n"); // un-hold all dice for (int i = 0; i < NUM_DICE; i++) { dice_values[i] = ctx.game_state.dice[i].value; ctx.game_state.dice[i].held = 0; } print_multiple_dice(dice_values, NUM_DICE, 0); // first roll roll_multiple_dice(&ctx, next_roll, 1); print_multiple_dice(next_roll, NUM_DICE, 0); sleep(1); printf("\n"); if (next_roll) { free(next_roll); } fclose(ctx.random_device_fd); return 0; } Loading @@ -57,7 +66,21 @@ FILE *get_urandom() { return urandom; } unsigned int roll_die(FILE *urandom_file) { void roll_multiple_dice(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] = roll_single_die(ctx->random_device_fd); } if (save_in_game_state) { ctx->game_state.dice[i].value = out_dice_values[i]; } } } unsigned int roll_single_die(FILE *urandom_file) { unsigned int next_random; fread(&next_random, sizeof(next_random), 1, urandom_file); next_random = (next_random % 6) + 1; Loading
yahtc +208 B (16.7 KiB) File changed.No diff preview for this file type. View original file View changed file