Advertisement
Manhydra

Black Jack

Jun 14th, 2013
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.36 KB | None | 0 0
  1. /*
  2.  *      bj_main.c
  3.  *
  4.  *      Copyright 2002 Marc Sylvestre <marc.sylvestre@manhydra.com>
  5.  *
  6.  *      This program is free software; you can redistribute it and/or modify
  7.  *      it under the terms of the GNU General Public License as published by
  8.  *      the Free Software Foundation; either version 3 of the License, or
  9.  *      (at your option) any later version.
  10.  *
  11.  *      This program is distributed in the hope that it will be useful,
  12.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *      GNU General Public License for more details.
  15.  *
  16.  *      You should have received a copy of the GNU General Public License
  17.  *      along with this program; if not, see <http://www.gnu.org/licenses/>,
  18.  *      or if prefer good old fashion postal mail, write to the Free Software
  19.  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20.  *      MA 02110-1301, USA.
  21.  */
  22.  
  23. /*
  24.  * This is a Black Jack game I wrote when I was learning C (and C++, of which a version of this
  25.  * exist). In it, you'll find examples of pointers, structs, and gambling addictions, lol.
  26.  * Have fun!
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <time.h>
  33.  
  34. typedef struct
  35. {
  36.     char *playerName;
  37.     int hand;
  38.     int card;
  39.     int bet;
  40.     int loot;
  41. } bjPlayer;
  42.  
  43. void Compare(bjPlayer*, bjPlayer*, int);
  44. void Game(bjPlayer*, bjPlayer*);
  45.  
  46. void Compare(bjPlayer * bjp, bjPlayer * bjd, int additionalCards)
  47. {
  48.     int play;
  49.     printf(
  50.         "\n===================================================="
  51.         "\n========= %s: %d =============  %s: %d ============="
  52.         "\n====================================================\n",
  53.         bjp->playerName,bjp->hand,bjd->playerName,bjd->hand);
  54.     if ((bjp->hand <= 21)&&(additionalCards==3))
  55.     {
  56.         printf("You have 5 cards without going over! You Win!\t");
  57.         bjp->loot *= 3;
  58.         printf("$%d\n\n",bjp->loot);
  59.     }
  60.     else if (bjp->hand > 21)
  61.     {
  62.         printf("\t\tYou Lose! \t");
  63.         bjp->loot -= bjp->bet;
  64.         printf("$%d\n\n",bjp->loot);
  65.     }
  66.     else if (bjd->hand > 21)
  67.     {
  68.         printf("\t\tYou Win! \t");
  69.         bjp->loot += bjp->bet;
  70.         printf("$%d\n\n",bjp->loot);
  71.     }
  72.     else if (bjp->hand > bjd->hand)
  73.     {
  74.         printf("\t\tYou Win! \t");
  75.         bjp->loot += bjp->bet;
  76.         printf("$%d\n\n",bjp->loot);
  77.     }
  78.     else if (bjp->hand < bjd->hand)
  79.     {
  80.         printf("\t\tYou Lose! \t");
  81.         bjp->loot -= bjp->bet;
  82.         printf("$%d\n\n",bjp->loot);
  83.     }
  84.     else printf("\t\tIt's a draw!\n\n");
  85.  
  86.     if (bjp->loot<=0)
  87.     {
  88.         printf("\t\tYou've lost all your winnings. Game over!\n");
  89.     }
  90.     else
  91.     {
  92.         printf("Press key \"1\" only to play again: ");
  93.         scanf("%d",&play);
  94.         if (play==1) Game(bjp,bjd);
  95.         else printf("Thanks for playing!\n");
  96.      }
  97. }
  98.  
  99. void Game(bjPlayer * bjp, bjPlayer * bjd)
  100. {
  101.     int deal, x;
  102.     srand( (unsigned)time( NULL ) );
  103.     bjp->hand = (rand() % 18)+4;
  104.     bjd->hand = (rand() % 18)+4;
  105.     for (x=0; x<3; x++)
  106.     {
  107.         bjp->card = (rand() % 11)+1;
  108.         bjd->card = (rand() % 11)+1;
  109.         if (bjd->hand < 17) bjd->hand += bjd->card;
  110.  
  111.         /* system("cls"); <-- See this? Yes, Even I was using this.
  112.         This let's you know how old this game is, lol */
  113.         printf(
  114.             "\n===================================================="
  115.             "\n============= %s, your hand reads: %d =============="
  116.             "\n===================================================="
  117.             "\n===================================================="
  118.             "\n=============== Hit[1] or Stand[2]: ================"
  119.             "\n===================================================="
  120.             "\n>>>>>>>>>>>>>>>>>>>> ",bjp->playerName,bjp->hand);
  121.         scanf("%d",&deal);
  122.         if (deal==2)
  123.         {
  124.             break;
  125.             Compare(bjp,bjd,x);
  126.         }
  127.         else bjp->hand += bjp->card;
  128.         if (bjp->hand > 21)
  129.         {
  130.             break;
  131.             Compare(bjp,bjd,x);
  132.         }
  133.     }
  134.     Compare(bjp,bjd,x);
  135. }
  136.  
  137. int main(int argc, char *argv[])
  138. {
  139.     int antiup;
  140.     char player_name[20];
  141.     printf(
  142.         "****************************************************\n"
  143.         "***********          BLACK JACK          ***********\n"
  144.         "****************************************************\n"
  145.         "****************************************************\n"
  146.         "***** by Marc Sylvestre (manhydra@gmail.com) *******\n"
  147.         "****************************************************\n"
  148.         "======>>>> What's your name? ");
  149.     scanf("%s", player_name);
  150.     printf("Hello, %s\n",player_name);
  151.     printf("======>>>> Place your bet: $");
  152.     scanf("%d",&antiup);
  153.  
  154.     bjPlayer player;
  155.     bjPlayer dealer;
  156.  
  157.     player.playerName = player_name;
  158.     player.bet = antiup;
  159.  
  160.     dealer.playerName = "DEADLER";
  161.  
  162.     while(player.bet <= 0)
  163.     {
  164.         printf("\nPlease enter an amount greater than 0. ");
  165.         scanf("%d",&antiup);
  166.         player.bet = antiup;
  167.     }
  168.  
  169.     printf("\n\nLet the games begin!\n\n");
  170.     player.loot = player.bet;
  171.  
  172.     Game(&player,&dealer);
  173.  
  174.     /* system("pause"); <-- See this? Yes, Even I back then was
  175.     using this. But only on Windows. */
  176.     return EXIT_SUCCESS;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement