Commit 768f2239 authored by Chris's avatar Chris
Browse files

refactored dice animation logic. todo next is to implement duration parameter

parent 8b64cf35
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -12,10 +12,10 @@ void print_single_die(unsigned int die_value) {
	}
}

void print_multiple_dice(int *die_value, int n, int animated) {
void print_multiple_dice(const unsigned int *die_value, const int n) {
	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];
			unsigned int which_die = die_value[j];
			printf("%s  ", DICE[which_die][which_row]);
		}
		printf("\n");
+1 −1
Original line number Diff line number Diff line
@@ -72,7 +72,7 @@ static const char *DICE[7][6] = {
};

void print_single_die(unsigned int die_value);
void print_multiple_dice(int *die_value, int n, int animated);
void print_multiple_dice(const unsigned int *die_value, const int n);
void clear_dice();

#endif //GRAPHICS_H_
+23 −18
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ const int NUM_DICE = 5;

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

struct game_state_st {
@@ -28,10 +28,11 @@ typedef struct game_context_st {
} 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);
unsigned int generate_single_die_roll(FILE *urandom_file);
void generate_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 sleep_ms(unsigned int ms);
void roll_and_print(game_context *ctx, unsigned int *out_dice_values, int animation_seconds);

int main() {
	 game_context ctx;
@@ -47,8 +48,7 @@ int main() {
	}

	// first roll
	roll_multiple_dice(&ctx, next_roll, 1);
	print_multiple_dice(next_roll, NUM_DICE, 0);
	roll_and_print(&ctx, next_roll, 0);

	sleep(1);

@@ -60,17 +60,8 @@ int main() {
	set_die_held(&ctx, 4, 1);
	// set_die_held(&ctx, 5, 1); // tests edge case

	// second roll
	roll_multiple_dice(&ctx, next_roll, 1);
	print_multiple_dice(next_roll, NUM_DICE, 0);
	roll_and_print(&ctx, next_roll, 0);

	// do a little dice animation
	for (int i = 0; i < 30; i++) {	
		roll_multiple_dice(&ctx, next_roll, 0);
		print_multiple_dice(next_roll, NUM_DICE, 0);
		sleep_ms(50);
		if (i < 30 - 1) clear_dice(); // display the last result
	}

	fclose(ctx.random_device_fd);
	return 0;
@@ -82,12 +73,26 @@ FILE *get_urandom() {
	return urandom;
}

void roll_multiple_dice(game_context *ctx, unsigned int *out_dice_values, int save_in_game_state) {
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_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_roll(ctx, out_dice_values, 1);
	print_multiple_dice(out_dice_values, NUM_DICE);
}

void generate_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] = roll_single_die(ctx->random_device_fd);
			out_dice_values[i] = generate_single_die_roll(ctx->random_device_fd);
		}

		if (save_in_game_state) {
@@ -96,7 +101,7 @@ void roll_multiple_dice(game_context *ctx, unsigned int *out_dice_values, int sa
	}
}

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