Loading graphics.c 0 → 100644 +36 −0 Original line number Diff line number Diff line // graphics.c #include "graphics.h" // void print_single_die(int die_value) { // // override invalid values with a blank die (DICE[0]) // if (die_value < 0 || die_value > DICE_ARRAY_LENGTH) { // die_value = 0; // } // // for (int i = 0; i < DIE_VERTICAL_LENGTH; i++) { // printf("%s\n", DICE[die_value][i]); // } // } // // void print_multiple_dice(int *die_values, int n, int animated) { // // if animation is not desired, just print out the die values // for (int which_row = 0; which_row < DIE_VERTICAL_LENGTH; which_row++) { // for (int j = 0; j < n; j++) { // int which_die = die_values[j]; // printf("%s ", DICE[which_die][which_row]); // } // printf("\n"); // } // } // // // to only be called after print_multiple_dice() // void clear_dice() { // // move cursor up // // for (int i = 0; i < DIE_VERTICAL_LENGTH + 1; i++) { // for (int i = 0; i < DIE_VERTICAL_LENGTH; i++) { // printf("\033[A"); // move cursor up one line // } // // move cursor all the way to the left // printf("\r"); // // printf("Hello world!\n"); // } graphics.h 0 → 100644 +111 −0 Original line number Diff line number Diff line // graphics.h #ifndef GRAPHICS_H_ #define GRAPHICS_H_ # pragma once #include <stdio.h> #include <stdarg.h> const int DIE_VERTICAL_LENGTH = 6; const int DICE_ARRAY_LENGTH = 7; char *DICE[7][6] = { { " +-------+ ", " | | ", " | | ", " | | ", " +-------+ ", " [ ] ", }, { " +-------+ ", " | | ", " | O | ", " | | ", " +-------+ ", " [1] ", }, { " +-------+ ", " | O | ", " | | ", " | O | ", " +-------+ ", " [2] ", }, { " +-------+ ", " | O | ", " | O | ", " | O | ", " +-------+ ", " [3] ", }, { " +-------+ ", " | O O | ", " | | ", " | O O | ", " +-------+ ", " [4] ", }, { " +-------+ ", " | O O | ", " | O | ", " | O O | ", " +-------+ ", " [5] ", }, { " +-------+ ", " | O O | ", " | O O | ", " | O O | ", " +-------+ ", " [6] ", } }; void print_single_die(int die_value); void print_multiple_dice(int *die_value, int n, int animated); void clear_dice(); // todo move the below to its own source file void print_single_die(int die_value) { // override invalid values with a blank die (DICE[0]) if (die_value < 0 || die_value > DICE_ARRAY_LENGTH) { die_value = 0; } for (int i = 0; i < DIE_VERTICAL_LENGTH; i++) { printf("%s\n", DICE[die_value][i]); } } void print_multiple_dice(int *die_value, int n, int animated) { for (int which_row = 0; which_row < DIE_VERTICAL_LENGTH; which_row++) { for (int j = 0; j < n; j++) { int which_die = die_value[j]; printf("%s ", DICE[which_die][which_row]); } printf("\n"); } } // to only be called after print_multiple_dice() void clear_dice() { // move cursor up // for (int i = 0; i < DIE_VERTICAL_LENGTH + 1; i++) { for (int i = 0; i < DIE_VERTICAL_LENGTH; i++) { printf("\033[A"); // move cursor up one line } // move cursor all the way to the left printf("\r"); } #endif //GRAPHICS_H_ yaht.c 0 → 100644 +58 −0 Original line number Diff line number Diff line #include <stdio.h> #include "graphics.h" // yaht.c // Chris Morris // MIT License // todo move into header file FILE *get_urandom(); unsigned int roll_die(FILE *urandom_file); const int NUM_DICE = 5; struct die_st { unsigned int value; int held; }; struct game_state_st { struct die_st dice[5]; }; struct context_st { FILE *random_device_fd; struct game_state_st game_state; }; int main() { struct context_st 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); // printf("%d\n", next_random); 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); } fclose(ctx.random_device_fd); return 0; } // get random value from device FILE *get_urandom() { FILE *urandom = fopen("/dev/urandom", "rb"); return urandom; } unsigned int roll_die(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; } yahtc 0 → 100755 +16.5 KiB File added.No diff preview for this file type. View file Loading
graphics.c 0 → 100644 +36 −0 Original line number Diff line number Diff line // graphics.c #include "graphics.h" // void print_single_die(int die_value) { // // override invalid values with a blank die (DICE[0]) // if (die_value < 0 || die_value > DICE_ARRAY_LENGTH) { // die_value = 0; // } // // for (int i = 0; i < DIE_VERTICAL_LENGTH; i++) { // printf("%s\n", DICE[die_value][i]); // } // } // // void print_multiple_dice(int *die_values, int n, int animated) { // // if animation is not desired, just print out the die values // for (int which_row = 0; which_row < DIE_VERTICAL_LENGTH; which_row++) { // for (int j = 0; j < n; j++) { // int which_die = die_values[j]; // printf("%s ", DICE[which_die][which_row]); // } // printf("\n"); // } // } // // // to only be called after print_multiple_dice() // void clear_dice() { // // move cursor up // // for (int i = 0; i < DIE_VERTICAL_LENGTH + 1; i++) { // for (int i = 0; i < DIE_VERTICAL_LENGTH; i++) { // printf("\033[A"); // move cursor up one line // } // // move cursor all the way to the left // printf("\r"); // // printf("Hello world!\n"); // }
graphics.h 0 → 100644 +111 −0 Original line number Diff line number Diff line // graphics.h #ifndef GRAPHICS_H_ #define GRAPHICS_H_ # pragma once #include <stdio.h> #include <stdarg.h> const int DIE_VERTICAL_LENGTH = 6; const int DICE_ARRAY_LENGTH = 7; char *DICE[7][6] = { { " +-------+ ", " | | ", " | | ", " | | ", " +-------+ ", " [ ] ", }, { " +-------+ ", " | | ", " | O | ", " | | ", " +-------+ ", " [1] ", }, { " +-------+ ", " | O | ", " | | ", " | O | ", " +-------+ ", " [2] ", }, { " +-------+ ", " | O | ", " | O | ", " | O | ", " +-------+ ", " [3] ", }, { " +-------+ ", " | O O | ", " | | ", " | O O | ", " +-------+ ", " [4] ", }, { " +-------+ ", " | O O | ", " | O | ", " | O O | ", " +-------+ ", " [5] ", }, { " +-------+ ", " | O O | ", " | O O | ", " | O O | ", " +-------+ ", " [6] ", } }; void print_single_die(int die_value); void print_multiple_dice(int *die_value, int n, int animated); void clear_dice(); // todo move the below to its own source file void print_single_die(int die_value) { // override invalid values with a blank die (DICE[0]) if (die_value < 0 || die_value > DICE_ARRAY_LENGTH) { die_value = 0; } for (int i = 0; i < DIE_VERTICAL_LENGTH; i++) { printf("%s\n", DICE[die_value][i]); } } void print_multiple_dice(int *die_value, int n, int animated) { for (int which_row = 0; which_row < DIE_VERTICAL_LENGTH; which_row++) { for (int j = 0; j < n; j++) { int which_die = die_value[j]; printf("%s ", DICE[which_die][which_row]); } printf("\n"); } } // to only be called after print_multiple_dice() void clear_dice() { // move cursor up // for (int i = 0; i < DIE_VERTICAL_LENGTH + 1; i++) { for (int i = 0; i < DIE_VERTICAL_LENGTH; i++) { printf("\033[A"); // move cursor up one line } // move cursor all the way to the left printf("\r"); } #endif //GRAPHICS_H_
yaht.c 0 → 100644 +58 −0 Original line number Diff line number Diff line #include <stdio.h> #include "graphics.h" // yaht.c // Chris Morris // MIT License // todo move into header file FILE *get_urandom(); unsigned int roll_die(FILE *urandom_file); const int NUM_DICE = 5; struct die_st { unsigned int value; int held; }; struct game_state_st { struct die_st dice[5]; }; struct context_st { FILE *random_device_fd; struct game_state_st game_state; }; int main() { struct context_st 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); // printf("%d\n", next_random); 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); } fclose(ctx.random_device_fd); return 0; } // get random value from device FILE *get_urandom() { FILE *urandom = fopen("/dev/urandom", "rb"); return urandom; } unsigned int roll_die(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; }