home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 305_01 / blackjac.c < prev    next >
Text File  |  1990-02-14  |  10KB  |  481 lines

  1. /*
  2. TITLE:        BLACKJACK.C;
  3. DATE:        1/10/89;
  4. DESCRIPTION:    "Blackjack.";
  5. VERSION:    1.03;
  6. KEYWORDS:    blackjack, cards;
  7. FILENAME:    BLACKJACK.C;
  8. WARNINGS:    "Requires IBM or compatible.";
  9. SEE-ALSO:    CARDS.DOC, POKER.C;
  10. SYSTEM:        MS-DOS;
  11. COMPILERS:    Aztec;
  12. AUTHORS:    Dan Schechter;
  13.  */
  14.  
  15. /*
  16. This program is copyright 1988, 1989 by Dan Schechter.
  17. You may copy and distribute it only on a not-for-profit basis.
  18. I provide this source code mainly for informational purposes but 
  19. you may modify it providing that you give attribution to the original author
  20. and document your modifications and distribute the modified version
  21. only on a not-for-profit basis. Compiles under small model.
  22.  */
  23. #include <stdio.h>
  24. char *version = "\
  25. BLACKJACK from Kittensoft. Version 1.03 1/10/89\n\
  26. Copyright (c) 1987, 1988, 1989, by Dan Schechter\n\
  27. Route 1 Box 19\n\
  28. Amenia, North Dakota 58004\n\n\
  29. This program is shareware. Pass copies along to your friends.\n\
  30. a single $5 contribution covers both BLACKJACK and POKER.\n" ;
  31.  
  32. #define KLINE 14
  33. #define SLINE 2
  34. #define BOFF 4
  35. #define TOFF 5
  36. #define DEALERSTAND 17
  37. #define PROMPTLINE 22
  38. #define MINCARD 15    /* Minimum number of cards needed to play a hand. */
  39. int deck[52];
  40. int *numused,except,color,oldattrib;
  41. char far *shmaddr;
  42. extern int _attrib;
  43.  
  44. char *txt[53]={
  45.     "2S",    /* 0 */
  46.     "2H",    /* 1 */
  47.     "2C",    /* 2 */
  48.     "2D",    /* 3 */
  49.     "3S",    /* 4 */
  50.     "3H",    /* 5 */
  51.     "3C",    /* 6 */
  52.     "3D",    /* 7 */
  53.     "4S",    /* 8 */
  54.     "4H",    /* 9 */
  55.     "4C",    /* 10 */
  56.     "4D",    /* 11 */
  57.     "5S",    /* 12 */
  58.     "5H",    /* 13 */
  59.     "5C",    /* 14 */
  60.     "5D",    /* 15 */
  61.     "6S",    /* 16 */
  62.     "6H",    /* 17 */
  63.     "6C",    /* 18 */
  64.     "6D",    /* 19 */
  65.     "7S",    /* 20 */
  66.     "7H",    /* 21 */
  67.     "7C",    /* 22 */
  68.     "7D",    /* 23 */
  69.     "8S",    /* 24 */
  70.     "8H",    /* 25 */
  71.     "8C",    /* 26 */
  72.     "8D",    /* 27 */
  73.     "9S",    /* 28 */
  74.     "9H",    /* 29 */
  75.     "9C",    /* 30 */
  76.     "9D",    /* 31 */
  77.     "TS",    /* 32 */
  78.     "TH",    /* 33 */
  79.     "TC",    /* 34 */
  80.     "TD",    /* 35 */
  81.     "JS",    /* 36 */
  82.     "JH",    /* 37 */
  83.     "JC",    /* 38 */
  84.     "JD",    /* 39 */
  85.     "QS",    /* 40 */
  86.     "QH",    /* 41 */
  87.     "QC",    /* 42 */
  88.     "QD",    /* 43 */
  89.     "KS",    /* 44 */
  90.     "KH",    /* 45 */
  91.     "KC",    /* 46 */
  92.     "KD",    /* 47 */
  93.     "AS",    /* 48 */
  94.     "AH",    /* 49 */
  95.     "AC",    /* 50 */
  96.     "AD",    /* 51 */
  97.     "  "
  98. };
  99.  
  100. main()
  101. {
  102.     int     i,bet=0,payoff,st,kt,kcard[15],scard[15],kcol,scol,
  103.         cardcnt,kcount,scount,bjk;
  104.     
  105.     oldattrib=_attrib;
  106.     
  107.     if (( getmode() &15 ) ==7){
  108.         shmaddr=(char far *)0xb0000000;
  109.         color='m';
  110.     }
  111.     else {
  112.         shmaddr=(char far *)0xb8000000;
  113.         _attrib=10;
  114.         color='c';
  115.     }
  116.     payoff=0;
  117.     numused= &cardcnt;
  118.     scr_clear();
  119.     tprintf("%s\n",version);
  120.     tprintf("\
  121. Bet limit is 999 dollars.\n\
  122. Dealer stands on 17.\n\
  123. Dealer wins all draws.\n\
  124. The deck is shuffled if fewer than %d cards remain.\n\
  125. Good luck!\n\n\
  126. Please place your bet. ",MINCARD);    
  127.     shuffle();
  128.     cardcnt=0;
  129.     goto gb;
  130.     
  131. shuf:    shuffle();
  132.     cardcnt=0;
  133.     tprintf("New Deck.");
  134. bett:    ;
  135.     tprintf("\nYour balance is %d dollars.\n\
  136. Please place your bet. <CR> to repeat previous bet. ",payoff);
  137. gb:    bet=getbet(bet);
  138.     scr_clear();
  139.     scr_curs(0,25);
  140.     tprintf("Your Balance is %d. ",payoff);
  141.     if (bet) tprintf("Your bet is now %d.",bet);
  142.     else tprintf("No bet on this hand.");
  143.     kcol=scol=scount=kcount=0;
  144.     scr_curs(0,0);
  145.     tprintf("YOUR CARDS");
  146.     scr_curs(KLINE-2,0);
  147.     tprintf("DEALER'S CARDS");
  148.     scard[scount++]=deck[cardcnt++];
  149.     kcard[kcount++]=deck[cardcnt++];
  150.     scard[scount++]=deck[cardcnt++];
  151.     except=cardcnt;
  152.     kcard[kcount++]=deck[cardcnt++];
  153.  
  154.     writecard(SLINE,scol,scard[0]);
  155.     scol+=6;
  156.     writecard(SLINE,scol,scard[1]);
  157.     scol+=6;
  158.     writecard(KLINE,kcol,kcard[0]);
  159.     kcol+=6;
  160.     
  161.     st=countup(scard,scount);
  162.     bjk=0;
  163.     if (st==21){
  164.         scr_curs(SLINE+BOFF,0);
  165.         tprintf("Blackjack!");
  166.         bjk=1;
  167.     }
  168.     kt=countup(kcard,kcount);
  169.     if (kt==21){
  170.         writecard(KLINE,kcol,kcard[1]);
  171.         bjk=2;
  172.     }
  173.     if (bjk) goto bust;
  174.     scr_curs(PROMPTLINE,0);
  175.     tprintf("(H)it or (S)tand? ");  
  176.     scr_eol();
  177. htst:    if (getreply("hs")=='h'){
  178.         scard[scount]=deck[cardcnt++];
  179.         writecard(SLINE,scol,scard[scount++]);
  180.         scol+=6;
  181.         if ((st=countup(scard,scount))>21) goto bust;
  182.         goto htst;
  183.     }
  184. bust:    if (bjk==2) {
  185.         scr_curs(KLINE+BOFF,0);
  186.         tprintf("Blackjack!");
  187.     }
  188.     else {
  189.         writecard(KLINE,kcol,kcard[1]);
  190.         kcol+=6;
  191.     }
  192.     if (bjk) goto bkjk;
  193.     if (st<=21){
  194.         while ((kt=countup(kcard,kcount))<DEALERSTAND){
  195.             kcard[kcount]=deck[cardcnt++];
  196.             writecard(KLINE,kcol,kcard[kcount++]);
  197.             kcol+=6;
  198.         }
  199.     }
  200.     else kt=countup(kcard,kcount);
  201.     scr_curs(SLINE+TOFF,0);
  202.     tprintf("Total: %d",st);
  203.     if (st>21) tprintf("     You bust.");
  204.     scr_eol();
  205.  
  206.     scr_curs(KLINE+TOFF,0);
  207.     tprintf("Total: %d",kt);
  208.     if (kt>21) tprintf("     I bust.");
  209. bkjk:    ;
  210.     scr_curs(PROMPTLINE,0);
  211.     scr_eol();
  212.     if ((bjk==2)|((bjk==0)&((st>21)|((kt>=st)&(kt<=21))))){
  213.         payoff-=bet;
  214.         tprintf("You lose.\n");
  215.     }
  216.     else {
  217.         payoff+=bet;
  218.         tprintf("You win.\n");
  219.     }
  220.     if (payoff>30000){
  221.         tprintf("You have won %d dollars. The casino is busted. Sob!",payoff);
  222.         _attrib=oldattrib;
  223.         exit(0);
  224.     }
  225.     if (payoff<-30000){
  226.         tprintf("You have lost %d dollars. No more credit!",payoff*-1);
  227.         _attrib=oldattrib;
  228.         exit(0);
  229.     }
  230.     tprintf("Your balance is %d dollars. Another hand? ",payoff);
  231.     scr_eol();
  232.     except= 0;
  233.     if (getreply("yn")=='y'){
  234.         scr_clear();
  235.         if (cardcnt>52-MINCARD) goto shuf;
  236.         goto bett;
  237.     }
  238.     if (payoff<0) tprintf("\r\
  239. %d dollars will be electronically deducted from your bank account.",payoff*-1);
  240.     if (payoff>0) tprintf("\r\
  241. I'll mail you an I.O.U. for %d dollars from Argentina.",payoff);
  242.     if (payoff==0) tprintf("\rGosh. You came out even!");
  243.     scr_eol();
  244.     _attrib=oldattrib;
  245.     exit(0);
  246. }
  247. countup(hand,n)
  248. int hand[],n;
  249. {
  250.     int i,aces,c,total;
  251.     
  252.     for (i=aces=total=0;i<n;i++){
  253.         c=txt[hand[i]][0];
  254.         if (c>'B') c=':';  /* for ten */
  255.         if (c=='A'){
  256.             c='1';
  257.             aces++;
  258.         }
  259.         total+=(c-48);
  260.     }
  261.     for (i=0;i<aces;i++) if (total<=11) total+=10;
  262.     return(total);
  263. }
  264. /* -------------------------------- SHUFFLE */
  265. shuffle()
  266. {
  267.     int i,j,k,num,hold[52];
  268.     
  269.     for (i=0;i<52;i++) deck[i]=i;
  270.     
  271.     for (k=0;k<3;k++) {
  272.         for (num=51,i=0;i<52;i++,num--){
  273.             if (num==0) j=0;
  274.             else j=rando()%num+1;
  275.             hold[i]=deck[j];
  276.             deldeck(j);
  277.         }
  278.         for (i=0;i<52;i++) deck[i]=hold[i];
  279.     }
  280.     j=rando()%45+3;
  281.     for (i=0;i<52;i++,j++) {
  282.         if (j==52) j=0;
  283.         deck[i]=hold[j];
  284.     }
  285. }
  286. deldeck(n)
  287. int n;
  288. {
  289.     while (n<51) {
  290.         deck[n]=deck[n+1];
  291.         n++;
  292.     }
  293. }
  294. int rando()
  295. {
  296.     static long zi;
  297.     static int mark=1;
  298.     long time();
  299.     
  300.     if (mark) {
  301.         mark=0;
  302.         zi = time((long *)0);
  303.         zi = zi & 16383;
  304.     }
  305.     zi*=23;
  306.     zi+=17;
  307.     zi%=32749;
  308.     return((int)zi);
  309. }
  310. int getreply(s)
  311. char *s;
  312. {
  313.     int i,c;
  314.     
  315.     for(;;){
  316.         c=scr_getc();
  317.         if ((c==3)||(c==-2)) {
  318.             _attrib=oldattrib; 
  319.             tprintf("\nQuitter!\n");
  320.             exit(1);
  321.         }
  322.         c=tlr(c);
  323.         for (i=0;s[i];i++) if (c==s[i]) {
  324.             scr_putc(c);
  325.             return(c);
  326.         }
  327.     }
  328. }
  329. writecard(v,h,card)
  330. int v,h,card;
  331. {
  332.     int n,s;
  333.     
  334.     n=txt[card][0];
  335.     s=txt[card][1];
  336.     
  337.     scr_color(v,h,218,0);
  338.     scr_color(v,h+1,196,0);
  339.     scr_color(v,h+2,196,0);
  340.     scr_color(v,h+3,196,0);
  341.     scr_color(v,h+4,191,0);
  342.     scr_color(v+1,h,179,0);
  343.     colorchar(v+1,h+1,s,' ');
  344.     colorchar(v+1,h+2,s,' ');
  345.     colorchar(v+1,h+3,s,' ');
  346.     scr_color(v+1,h+4,179,0);
  347.     scr_color(v+2,h,179,0);
  348.     colorchar(v+2,h+1,s,' ');
  349.     colorchar(v+2,h+2,s,' ');
  350.     colorchar(v+2,h+3,s,' ');
  351.     scr_color(v+2,h+4,179,0);
  352.     scr_color(v+3,h,192,0);
  353.     scr_color(v+3,h+1,196,0);
  354.     scr_color(v+3,h+2,196,0);
  355.     scr_color(v+3,h+3,196,0);
  356.     scr_color(v+3,h+4,217,0);
  357.     if (n=='T'){
  358.         colorchar(v+1,h+1,s,'1');
  359.         colorchar(v+1,h+2,s,'0');
  360.     }
  361.     else colorchar(v+1,h+2,s,n);  
  362.     switch(s){
  363.         case 'H': colorchar(v+2,h+2,s,3); break;
  364.         case 'D': colorchar(v+2,h+2,s,4); break;
  365.         case 'C': colorchar(v+2,h+2,s,5); break;
  366.         case 'S': colorchar(v+2,h+2,s,6); break;
  367.     }
  368. }
  369. colorchar(v,h,suit,c)
  370. int v,h,suit,c;
  371. {
  372.     switch (suit) {
  373.         case 'H': 
  374.         case 'D': scr_color(v,h,c,1); break;
  375.         default:  scr_color(v,h,c,0);
  376.     }
  377. }
  378. scr_color(v,h,c,q)
  379. int v,h,c,q;
  380. {
  381.     int off;
  382.  
  383.     off = 160*v+ 2*h;
  384.     shmaddr[off++]=c;
  385.     if (color=='m') shmaddr[off] = q ? 112 : 7 ;
  386.     else shmaddr[off] = q ? 116 : 112 ;
  387. }
  388. int tlr(c)
  389. int c;
  390. {
  391.     if ((c>='A')&&(c<='Z')) c+=32;
  392.     return(c);
  393. }
  394. getmode()
  395. {
  396.     struct { unsigned ax,bx,cx,dx,si,di,ds,es; } regs;
  397.     
  398.     regs.bx=regs.cx=regs.dx=regs.si=regs.di=regs.ds=regs.es=0;
  399.     regs.ax = 15<<8;
  400.     sysint(0x10,®s,®s);
  401.     
  402.     return (regs.ax&255);
  403. }
  404. int tprintf(ctrl,parml)
  405. char *ctrl;
  406. unsigned parml;
  407. {
  408.     char *p;
  409.     
  410.     p= (char *)(&parml);
  411.     while (*ctrl) {
  412.         if ((*ctrl)=='%') {
  413.             ctrl++;
  414.             switch(*ctrl++){
  415.                 case 's': 
  416.                     _kstrput(p);
  417.                     p += sizeof(char *);
  418.                     break;
  419.                 case 'd': 
  420.                     _mydput(p);
  421.                     p += sizeof(int);
  422.                     break;
  423.                 case 0:
  424.                     return;
  425.             }
  426.             continue;
  427.         }
  428.         if (*ctrl=='\n') scr_putc('\r');
  429.         scr_putc( *(ctrl++));
  430.     }
  431.     return;
  432. }
  433. int _kstrput(p)
  434. char **p;
  435. {
  436.     int i=0;
  437.     
  438.     while ((*p)[i]){
  439.         if ( (*p)[i]=='\n' ) scr_putc('\r');
  440.         scr_putc((*p)[i++]) ;
  441.     }
  442. }
  443. _mydput(p)
  444. char *p;
  445. {
  446.     int t,n,mask;
  447.     
  448.     n= *((int *)p);
  449.     if (n<0){
  450.         scr_putc('-');
  451.         n = -n;
  452.     }
  453.     for(mask = 10000;mask!=1;mask/=10)
  454.         if (mask<=n) break;
  455.     for(;mask;mask/=10){
  456.         t = n/mask;
  457.         n -= t*mask;
  458.         scr_putc(t+48);
  459.     }
  460. }
  461. getbet(b)
  462. int b;
  463. {
  464.     int a=0,c,q=1;
  465.     
  466.     for(;;){
  467.         c=getreply("1234567890\r");
  468.         if (c=='\r') {
  469.             if (q) return b;
  470.             return a;
  471.         }
  472.         q=0;
  473.         a *= 10;
  474.         a += c-48;
  475.         if (a>999) {
  476.             tprintf("\nLimit is 999. Please start over: ");
  477.             a=0;
  478.         }
  479.     }
  480. }
  481.