Advertisement
Weegee

17 und 4

May 31st, 2011
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.29 KB | None | 0 0
  1. //      siebzehnundvier.c
  2. //      
  3. //      This program is free software; you can redistribute it and/or modify
  4. //      it under the terms of the GNU General Public License as published by
  5. //      the Free Software Foundation; either version 2 of the License, or
  6. //      (at your option) any later version.
  7. //      
  8. //      This program is distributed in the hope that it will be useful,
  9. //      but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. //      GNU General Public License for more details.
  12. //      
  13. //      You should have received a copy of the GNU General Public License
  14. //      along with this program; if not, write to the Free Software
  15. //      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. //      MA 02110-1301, USA.
  17. //      
  18. //      
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <time.h>
  23. #include <string.h>
  24. #define DECKSIZE 52
  25. #define NAMELENGTH 30
  26. #define MAIN 0
  27. #define DEALER 1
  28. #define PLAYER 2
  29. // bullshit
  30. #define FANCYSHIT "\n====================\n"
  31.  
  32. struct deck {
  33.     int count;
  34.     int card[DECKSIZE];
  35.     int type;
  36.     char owner[NAMELENGTH];
  37. };
  38.  
  39. int deckvalue(struct deck *target_deck) {
  40.     int value = 0;
  41.     for(int i = 0; i < target_deck->count; i++) {
  42.         value += target_deck->card[i];
  43.     }
  44.     return value;
  45. }
  46.  
  47. void showdeck(struct deck *target_deck) {
  48.     if(target_deck->type == PLAYER) {
  49.         printf("\nYour deck: ");
  50.         for(int i = 0; i < target_deck->count; i++) {
  51.             printf("%d ",target_deck->card[i]);
  52.         }
  53.         printf("\nHand value: %d\n",deckvalue(target_deck));
  54.     }
  55.     else if(target_deck->type == DEALER) {
  56.         printf("Dealer's deck: ");
  57.         for(int i = 0; i < target_deck->count; i++) {
  58.             printf("%d ",target_deck->card[i]);
  59.         }
  60.     }
  61.     else if(target_deck->type == MAIN) {
  62.         printf("\n(DEBUG) Okay, so here's the full main deck: ");
  63.         for(int i = 0; i < DECKSIZE; i++) {
  64.             printf("%d ",target_deck->card[i]);
  65.         }
  66.     }
  67.     else {
  68.         printf("\n(ERROR) This is fucking impossible. Exiting ...\n");
  69.         exit(EXIT_FAILURE);
  70.     }
  71. }
  72.  
  73. struct deck *createdeck(int type, struct deck *mdeck) {
  74.     if(type == MAIN) {
  75.         struct deck *maindeck;
  76.         maindeck = malloc(sizeof(struct deck));
  77.         for(int i = 0; i < DECKSIZE; i++) {
  78.             maindeck->card[i] = rand () % (11 - 2 + 1) + 2; // (max - min + 1) + min
  79.         }
  80.         maindeck->count = 0;
  81.         strcpy(maindeck->owner,"nobody");
  82.         maindeck->type = MAIN;
  83.         printf("\n(DEBUG) Created main deck\n");
  84.         return maindeck;
  85.     }
  86.     else if(type == DEALER) {
  87.         struct deck *dealerdeck;
  88.         dealerdeck = malloc(sizeof(struct deck));
  89.         // dealer's first card = third card in the main deck
  90.         dealerdeck->card[0] = mdeck->card[2];
  91.         dealerdeck->count = 1;
  92.         // now the dealer has the third card, so set its value to 0
  93.         mdeck->card[2] = 0;
  94.         mdeck->count++;
  95.         strcpy(dealerdeck->owner,"dealer");
  96.         dealerdeck->type = DEALER;
  97.         printf("\n(DEBUG) Created dealer's deck\n");
  98.         return dealerdeck;
  99.     }
  100.     else if(type == PLAYER) {
  101.         struct deck *playerdeck;
  102.         playerdeck = malloc(sizeof(struct deck));
  103.         // the player gets the first two cards out of the main deck
  104.         memcpy(playerdeck->card,mdeck->card,2 * sizeof(int));
  105.         playerdeck->count = 2;
  106.         // now the player has the first two cards from the main deck, so set their value to 0
  107.         mdeck->card[0] = 0;
  108.         mdeck->card[1] = 0;
  109.         mdeck->count += 2;
  110.         playerdeck->type = PLAYER;
  111.         strcpy(playerdeck->owner,"player");
  112.         printf("\n(DEBUG) Created player's deck\n");
  113.         return playerdeck;
  114.     }
  115.     else {
  116.         printf("\n(ERROR) No deck type given, exit ...\n");
  117.         exit(EXIT_FAILURE);
  118.     }
  119. }
  120.  
  121. void give(struct deck *target_deck,struct deck *maindeck) {
  122.     if(maindeck->count < DECKSIZE - 1) {
  123.         if(strcmp(target_deck->owner,"nobody") == 0) {
  124.             printf("\n(ERROR) So, who's touching the main deck?\n");
  125.             exit(EXIT_FAILURE);
  126.         }
  127.         else {
  128.             target_deck->card[target_deck->count] = maindeck->card[maindeck->count];
  129.             //printf("\n(DEBUG) Deck: %s; Card value: %d\n",target_deck->owner,target_deck->card[target_deck->count]);
  130.             target_deck->count++;
  131.             //printf("\n(DEBUG) Deck: %s; Number of cards before/after draw: %d/%d\n",target_deck->owner,target_deck->count - 1,target_deck->count);
  132.             maindeck->card[maindeck->count] = 0;
  133.             maindeck->count++;
  134.             if(target_deck->type == PLAYER) {
  135.                 printf("You drew a card: %d. New hand value: %d\n",target_deck->card[target_deck->count - 1],deckvalue(target_deck));
  136.             }
  137.             else if(target_deck->type == DEALER) {
  138.                 printf("\nThe dealer drew a card: %d.\n",target_deck->card[target_deck->count - 1]);
  139.                 showdeck(target_deck);
  140.                 printf("\nDealer's new hand value: %d\n",deckvalue(target_deck));
  141.             }
  142.             else {
  143.                 printf("\n(ERROR) Unknown deck type: %d, exiting ...\n",target_deck->type);
  144.             }
  145.         }
  146.     }
  147.     else {
  148.         printf("\n(ERROR) Main deck is ... emtpy?\n");
  149.         exit(EXIT_FAILURE);
  150.     }
  151. }
  152.  
  153. void dealer(struct deck *dealerdeck, struct deck *playerdeck, struct deck *maindeck) {
  154.     // I'LL WIN LOLOLO
  155.     if(deckvalue(dealerdeck) < 21) {
  156.         give(dealerdeck,maindeck);
  157.         dealer(dealerdeck,playerdeck,maindeck);
  158.     }
  159.     else if(deckvalue(dealerdeck) == 21) {
  160.         if(deckvalue(dealerdeck) > deckvalue(playerdeck)) {
  161.             printf(FANCYSHIT);
  162.             printf("Your hand value (%d) is less than the dealer's hand value (%d), you lose!\n",deckvalue(playerdeck),deckvalue(dealerdeck));
  163.             showdeck(dealerdeck);
  164.             exit(EXIT_SUCCESS);
  165.         }
  166.     }
  167.     else {
  168.         printf(FANCYSHIT);
  169.         printf("fuck this shit I lost THE GAME ;_;\n");
  170.         printf("The dealer's hand value (%d) ist greater than 21, you win!\n",deckvalue(dealerdeck));
  171.         showdeck(dealerdeck);
  172.         exit(EXIT_SUCCESS);
  173.     }
  174. }
  175.  
  176. void game(struct deck *dealerdeck, struct deck *playerdeck, struct deck *maindeck) {
  177.     if(deckvalue(playerdeck) > 21) {
  178.         printf(FANCYSHIT);
  179.         printf("Your hand value (%d) is greater than 21, you lose!\n",deckvalue(playerdeck));
  180.         showdeck(dealerdeck);
  181.         printf("\nThe dealer's hand value is %d\n",deckvalue(dealerdeck));
  182.         exit(EXIT_SUCCESS);
  183.     }
  184.     else {
  185.         //printf("\n(press enter)\n");
  186.         int choice;
  187.         while ((choice = getchar()) != '\n' && choice != EOF);
  188.         printf(FANCYSHIT);
  189.         printf("Choose:\n");
  190.         printf("\t(1) Show your deck\n\t(2) Draw a card\n\t(3) Finish drawing\n");
  191.         printf(FANCYSHIT);
  192.        
  193.         char input[3];
  194.         fgets(input,3,stdin);
  195.         choice = (int) strtol(input,NULL,10);
  196.        
  197.         switch(choice) {
  198.             case 1: showdeck(playerdeck);
  199.                     game(dealerdeck,playerdeck,maindeck);
  200.                     break;
  201.             case 2: give(playerdeck,maindeck);
  202.                     game(dealerdeck,playerdeck,maindeck);
  203.                     break;
  204.             case 3: printf("Dealer's first card: %d\n",dealerdeck->card[0]); // AI goes here :3
  205.                     dealer(dealerdeck,playerdeck,maindeck);
  206.                     break;
  207.             default:    printf("Something went wrong, please choose again!\n");
  208.                         game(dealerdeck,playerdeck,maindeck);
  209.                         break;
  210.         }
  211.     }
  212. }
  213.  
  214. int main(void) {
  215.     // random numbers
  216.     srand((unsigned)time(NULL));
  217.    
  218.     struct deck *maindeck;
  219.     struct deck *playerdeck;
  220.     struct deck *dealerdeck;
  221.    
  222.     // create decks
  223.     maindeck = createdeck(MAIN,NULL);
  224.     playerdeck = createdeck(PLAYER,maindeck);
  225.     showdeck(playerdeck);
  226.     dealerdeck = createdeck(DEALER,maindeck);
  227.    
  228.     //showdeck(maindeck);
  229.    
  230.     // start the game :3
  231.     game(dealerdeck,playerdeck,maindeck);
  232.    
  233.     // clean up (oh hai jerry)
  234.     free(dealerdeck);
  235.     free(playerdeck);
  236.     free(maindeck);
  237.    
  238.     printf("THE END (actually you should never see this)\n");
  239.    
  240.     return EXIT_SUCCESS;
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement