main.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. This is a simple card game simulator, where you try to beat the computer by
  3. obtaining a stronger card. Sort of an "indian poker" kind of game, I guess.
  4. Copyright 2022 - kzimmermann - https://tilde.town/~kzimmermann
  5. This program is Free Software licensed and released under the GNU GPL v3.
  6. See <https://www.gnu.org/licenses> for more information
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <time.h> // for pseudorandom number
  11. #include <unistd.h> // for sleep()
  12. #include <string.h>
  13. #include "cards.h"
  14. #define BUFF 25 // buffer size for reading a line
  15. #define MAX_SCORE 500 // how much is the winning threshold of the game?
  16. #define MIN_BET 10 // minimum bet!
  17. int
  18. main(int argc, char* argv[]) {
  19. // seed the RNG:
  20. srand(time(NULL));
  21. // player's "hand"
  22. struct playingcard mine;
  23. // opponent's hand:
  24. struct playingcard theirs;
  25. // Initial purse money:
  26. int purse = 100;
  27. // bet container
  28. int bet = 0;
  29. // are ya winning, son?
  30. int win = 0;
  31. // limits for betting:
  32. int bet_max, bet_min = 0;
  33. // placeholder string
  34. char line[BUFF];
  35. while(1) {
  36. mine = draw_card();
  37. theirs = draw_card();
  38. // clear screen:
  39. printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  40. printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  41. printf("\n------------------------------------\n");
  42. printf("Your current purse: %d\n", purse);
  43. if (purse > MIN_BET) {
  44. bet_min = MIN_BET;
  45. bet_max = purse;
  46. }
  47. else {
  48. // uh-oh you have less than the minimum amount!
  49. bet_min = purse;
  50. bet_max = purse;
  51. }
  52. printf("Your hand: ");
  53. int status = describe_card(mine);
  54. if (status != 0) {
  55. printf("Error: malformed card.\n");
  56. return 1;
  57. }
  58. printf("Proceed? (1=proceed, 0=fold) ");
  59. int decision = 0;
  60. fgets(line, BUFF - 1, stdin);
  61. sscanf(line, "%d", &decision);
  62. if (decision == 1) {
  63. while(1) {
  64. printf("Your bet? (%d ~ %d) ", bet_min, purse);
  65. fgets(line, BUFF - 1, stdin);
  66. sscanf(line, "%d", &bet);
  67. if (bet < bet_min || bet > bet_max) {
  68. printf("Choose a bet between your limits!\n");
  69. }
  70. else
  71. break;
  72. }
  73. }
  74. printf("Opponent's hand: ");
  75. status = describe_card(theirs);
  76. if (status != 0) {
  77. printf("Error: malformed card.\n");
  78. return 1;
  79. }
  80. if (decision == 0) {
  81. printf("However, you've folded, so you lose %d!\n", MIN_BET);
  82. purse -= MIN_BET; // no more cheating!
  83. }
  84. else {
  85. if (mine.value > theirs.value) {
  86. printf("You win!\n");
  87. win = 1;
  88. } else if (mine.value == theirs.value && mine.suit > theirs.suit) {
  89. printf("You win!\n");
  90. win = 1;
  91. } else {
  92. printf("You lose the round.\n");
  93. win = 0;
  94. }
  95. if (win == 1) {
  96. printf("You gain %d\n", bet);
  97. purse += bet;
  98. }
  99. else {
  100. printf("You lose %d\n", bet);
  101. purse -= bet;
  102. }
  103. if (purse <= 0) {
  104. printf("\n***Game over***\n");
  105. break;
  106. } else if (purse >= MAX_SCORE) {
  107. printf("\n*** You've made over %d in the game. Congrats, you win!\n", MAX_SCORE);
  108. break;
  109. }
  110. }
  111. // add a little suspense >:)
  112. sleep(1);
  113. }
  114. return 0;
  115. }