Created
December 29, 2024 18:50
-
-
Save maksverver/c3d9de3ec580cebd11bcd80d873f0475 to your computer and use it in GitHub Desktop.
CodeCup 2025 Box tile generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
static uint8_t random_data[240]; | |
static size_t random_size = 0; | |
uint8_t GetRandomByte() { | |
if (random_size == 0) { | |
fprintf(stderr, "Out of entropy!\n"); | |
exit(1); | |
} | |
return random_data[--random_size]; | |
} | |
/* Returns a random integer between 0 and n (exclusive). */ | |
int RandInt(int n) { | |
assert(n > 0 && n <= 256); | |
int lim = 256 / n * n; | |
uint8_t byte; | |
do byte = GetRandomByte(); while (byte >= lim); | |
return byte % n; | |
} | |
void ShuffleTile(char tile[6]) { | |
for (size_t i = 5; i > 0; --i) { | |
int j = RandInt(i + 1); | |
char tmp = tile[i]; | |
tile[i] = tile[j]; | |
tile[j] = tmp; | |
} | |
} | |
int main() { | |
random_size = sizeof(random_data); | |
if (getentropy(random_data, random_size) != 0) { | |
perror("getentropy"); | |
exit(1); | |
} | |
int color1 = RandInt(6) + 1; | |
int color2 = RandInt(6) + 1; | |
printf("%d %d\n", color1, color2); | |
char tile[7] = "123456"; | |
for (int n = 0; n < 40; ++n) { | |
ShuffleTile(tile); | |
printf("%s\n", tile); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment