diff --git a/src/C/tamagoshi_cow.c b/src/C/tamagoshi_cow.c new file mode 100644 index 0000000..31a33f0 --- /dev/null +++ b/src/C/tamagoshi_cow.c @@ -0,0 +1,125 @@ +#include +#include +#include + +enum State { + byebyelife = 0, + lifesucks = 1, + liferocks = 2 +}; + +int stock = 5; +int fitness = 5; + +int clamp(int x, int min, int max) { + if (x < min) return min; + if (x > max) return max; + return x; +} + +enum State get_state(void) { + if (fitness == 0 || fitness == 10) { + return byebyelife; + } else if ((fitness >= 1 && fitness <= 3) || (fitness >= 7 && fitness <= 9)) { + return lifesucks; + } else { + return liferocks; + } +} + +void affiche_vache(enum State s) { + if (s == liferocks) { + printf(" _______________________________________\n"); + printf("< You are a good sla... I mean player ! >\n"); + printf("-----------------------------------------\n"); + printf(" \\ ^__^\n"); + printf(" \\ ($$)\\_______\n"); + printf(" ( _)\\ )\\/\\\n"); + printf(" U ||----w |\n"); + printf(" || ||\n"); + } else if (s == lifesucks) { + printf(" _______________________\n"); + printf("< You SUCK at this game >\n"); + printf("-------------------------\n"); + printf(" \\ ^__^\n"); + printf(" \\ (@@)\\_______\n"); + printf(" (__)\\ )\\/\\\n"); + printf(" | ||----w |\n"); + printf(" ' /| ||\n"); + } else { + printf(" _______________________________\n"); + printf("< Where is my warranty ticket :( >\n"); + printf(" ----------------------------------\n"); + printf(" \\ ||\n"); + printf(" \\ ||\n"); + printf(" \\ ||\n"); + printf(" \\||\n"); + printf(" ~__~\n"); + printf(" (xx)\\_______\n"); + printf(" (__)\\ )\\\n"); + printf(" o o||----w | \\\n"); + printf(" o o|| ||\n"); + } +} + +int fitness_update(int lunchfood) { + int digestion = -(rand() % 4); // valeur entre 0 et -3 + + fitness = fitness + lunchfood + digestion; + fitness = clamp(fitness, 0, 10); + + return fitness; +} + +int stock_update(int lunchfood) { + int crop = (rand() % 7) - 3; // valeur entre -3 et +3, il y a probablement le biais avec le modulo comme parler dans les fichiers bash mais la n'est pas le sujet.... + + stock = stock - lunchfood + crop; + stock = clamp(stock, 0, 10); + + return stock; +} + +int main(void) { + srand((unsigned) time(NULL)); + + int duree_vie = 0; + int lunchfood; + enum State state = get_state(); + + while (state != byebyelife) { + printf("\n============================\n"); + printf("TAMAGOSHI COW\n"); + printf("============================\n\n"); + + affiche_vache(state); + + printf("\nStock actuel : %d / 10\n", stock); + + do { + printf("Quantite de nourriture a donner : "); + scanf("%d", &lunchfood); + + if (lunchfood < 0 || lunchfood > stock) { + printf("Valeur invalide. Entrez une valeur entre 0 et %d.\n", stock); + } + + } while (lunchfood < 0 || lunchfood > stock); + + fitness_update(lunchfood); + stock_update(lunchfood); + + duree_vie++; + state = get_state(); + } + + printf("\n============================\n"); + printf("GAME OVER\n"); + printf("============================\n\n"); + + affiche_vache(byebyelife); + + printf("\nScore final : %d\n", duree_vie); + + return 0; +}