Skip to content

Instantly share code, notes, and snippets.

@cubapp
Last active September 23, 2020 05:25
Show Gist options
  • Save cubapp/30e8f6d11981c3ef2996627795c65701 to your computer and use it in GitHub Desktop.
Save cubapp/30e8f6d11981c3ef2996627795c65701 to your computer and use it in GitHub Desktop.
Dobble game card generator.
#include <stdio.h>
#include <stdlib.h>
// Dobblo generator
// Here is a C code inspired from @karinka's answer with a different arrangement of symbols.
// https://math.stackexchange.com/questions/1303497/what-is-the-algorithm-to-generate-the-cards-in-the-game-dobble-known-as-spo
//
// It works for n being a prime number (2, 3, 5, 7, 11, 13, 17, ...).
//
// Number of symbols in a given card = n+1
// Total number of cards = n^2 + n + 1
// Total number of symbols = n^2 + n +1
// Author: https://math.stackexchange.com/users/375691/urmil-parikh
#define PRINT(x) printf("%2d ", (x)+1)
void main(int argc, char *argv[]) {
int i, j, k, r = 0, n = 5; // n is important!
// first card
printf ("Card %2d: ", ++r);
for (i = 0; i <= n; i++) {
PRINT (i);
}
printf ("\n");
// n following cards
for (j = 0; j < n; j++) {
printf ("Card %2d: ", ++r);
PRINT (0);
for (k = 0; k < n; k++) {
PRINT (n+1 + n*j + k);
}
printf ("\n");
}
// n*n following cards
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf ("Card %2d: ", ++r);
PRINT (i+1);
for (k = 0; k < n; k++) {
PRINT (n+1 + n*k + (i*k+j)%n); // Good for n = prime number
}
printf ("\n");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment