Commit e4995be5 authored by Chris's avatar Chris
Browse files

created functions for setting dice held; main function emulates roll 1, holding some dice, roll 2

parent b6f9f207
Loading
Loading
Loading
Loading
+27 −3
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@
// Chris Morris
// MIT License

// todo move into header file
// TODO move these structs, functions, etc. into header file

const int NUM_DICE = 5;

@@ -29,6 +29,7 @@ typedef struct game_context_st {
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 set_die_held(game_context *ctx, unsigned int index, unsigned int held);

int main() {
	 game_context ctx;
@@ -45,13 +46,24 @@ int main() {

	// first roll
	roll_multiple_dice(&ctx, next_roll, 1);

	print_multiple_dice(next_roll, NUM_DICE, 0);

	sleep(1);

	printf("\n");

	// set some dice held (debug)
	set_die_held(&ctx, 0, 1);
	set_die_held(&ctx, 1, 1);
	set_die_held(&ctx, 4, 1);
	// set_die_held(&ctx, 5, 1);


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

	// free the next_roll var (mallon)
	if (next_roll) {
		free(next_roll);
	}
@@ -60,7 +72,6 @@ int main() {
}

// get random value from device

FILE *get_urandom() {
	FILE *urandom = fopen("/dev/urandom", "rb");
	return urandom;
@@ -87,3 +98,16 @@ unsigned int roll_single_die(FILE *urandom_file) {
	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;
}
+40 B (16.8 KiB)

File changed.

No diff preview for this file type.