home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / alt / sources / 2855 < prev    next >
Encoding:
Text File  |  1992-12-22  |  10.1 KB  |  321 lines

  1. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!zaphod.mps.ohio-state.edu!usc!ucla-cs!ucla-se!CAD.UCLA.EDU!jonathan
  2. From: jonathan@CAD.UCLA.EDU (Jonathan Katz)
  3. Newsgroups: alt.sources
  4. Subject: Chanukah stuff -- Dreidle simulator.
  5. Message-ID: <8970@lee.SEAS.UCLA.EDU>
  6. Date: 22 Dec 92 22:21:39 GMT
  7. Sender: news@SEAS.UCLA.EDU
  8. Organization: U.C.L.A. Computer Aided Design Laboratory
  9. Lines: 310
  10.  
  11. Not that I mind only seeing Christmas posts, but, here is a Chanukah
  12. post.  No, I dont feel that Chanukah is a major holiday (it isnt), but
  13. playing dreidle can be fun, hence this program....
  14.  
  15. Remember to link it with curses....
  16.  
  17. ----------------8<-----CUT HERE-----8<--------------------
  18.  
  19. /*  
  20.    dreidle.c
  21.    Dreidle Sim        Jonathan Katz 921221 
  22.    
  23.    Compile:
  24.       cc dreidle.c -lcurses
  25.  
  26.    History:
  27.       During the time of King Antiochus (sp?) IV, the Jews were not
  28. allowed to practice Judaism or too study their holy books.  Therefore,
  29. the wise men took to the practice of keeping little spinning tops
  30. on the table along with there holy books.  If, while they were studying
  31. in secret, some of the king's guards came to make sure that the
  32. king's wishes were being followed, the wise men would begin to
  33. play with their tops.  This is the origin of the Dreidle, and the origin of
  34. giving money (or Chanukah gelt) to children so that they may play.
  35.       This program is a little late for the start of Chanukah, but,
  36. still is early enough to be a little fun.  This program simulates a
  37. game of Dreidle.
  38.  
  39. The modern game is played as follows:
  40.    The spinning top can land on one of four sides; the four sides
  41. are labeled with the following Hebrew letters in counter-clockwise
  42. (from the top) order:  Nun, Gimmel, Hay, Shin.  These letters
  43. form an acronym for the Hebrew sentence "Nes Gadol Ha-ya Sham" which
  44. means "A great miracle happened there.  Dreidles from Israel have
  45. the Shin replaced with a Pay, for the Hebrew word "Po" which means
  46. "here".
  47.    The game is a form of gambling.  Whenever the pot is empty (including
  48. at the start of the game), each player places one penny, or dollar, or
  49. peanut, or candy, or article of clothing, etc. into the pot.  Players
  50. then take turns spinning the Dreidle and performing an action based
  51. on the Hebrew letter on which it lands.
  52. The actions are as follows:
  53.      Nun :  Nothing happens.  Next person spins.
  54.   Gimmel :  The player gets everything in the pot (REMEMBER, the
  55.               pot is now empty again!).  Next person spins.
  56.      Hay :  The player get 1/2 of everything in the pot.
  57.     Shin :  The player must add one penny, or dollar, or peanut, or
  58.                candy, or article of clothing, etc. into the pot.
  59. Play continues until there is only one person left with anything (or
  60. until the kids start crying...).
  61.  
  62.  
  63.    USAGE:
  64.       By default, this program plays a standard game of Dreidle with
  65. 4 players each starting with $10.  The game spins once every 100ms.
  66.  
  67. The following run-line options are valid:
  68.    -d NUMBER    (where NUMBER is the number of ms between spins)
  69.  
  70.    -p NUMBER    (where NUMBER is the number of starting players)
  71.  
  72.    -m NUMBER    (where NUMBER is the amount each person starts with)
  73.  
  74.    -s           (do not show the game, only the final stats.  This 
  75.                  option also disables the between spin delay)
  76.  
  77.    -S           (this option puts a special rule into effect where if
  78.                  a shin is rolled the player must MATCH the current
  79.                  pot, rather than just adding one unit)
  80.  
  81.    -?
  82.    -h           (provide a short help message on running the game)      
  83.  
  84. Other Chanukah facts:
  85.    This is the first recorded war fought for religious freedom (165 BCE).
  86.  
  87. Fun things to do with this program:
  88.    Run it with the -s options 1000 times, then do a statistical
  89.      analysis on the results. 
  90.    Laugh at the bad code.
  91.       
  92. */
  93.  
  94. #include <curses.h>
  95. #include <signal.h>
  96.  
  97.  
  98. /* define globals and set default values */
  99.  
  100. int    numplayers = 4;
  101. int    initbankroll = 10;
  102. int    *bankroll;
  103. int    pot;
  104. short  standardshin = 1;
  105. short  doprint = 1;
  106. long   delaytime = 100000;
  107. long   numrounds = 0;
  108. long   numspins = 0;
  109.  
  110.  
  111. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  112. /* Called before program exit (also pointed to by signal catcher) */
  113. void exitwell()
  114. {
  115.    int count, winner;
  116.  
  117.    if (doprint) endwin();
  118.    printf("Rounds: %ld     Spins: %ld     ",numrounds, numspins);
  119.    winner = 1;
  120.    for(count = 2; count <= numplayers; count++)
  121.       if(bankroll[count] > bankroll[winner]) winner = count;
  122.    printf("FinalPot: %d     Winner: %d with $%d\n",pot, winner,bankroll[winner]);
  123.    exit(0);
  124. }
  125.  
  126. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  127.  
  128. void updatescreen(int numplayers, int pot, int bankroll[])
  129. {
  130.    int count;
  131.  
  132.    for(count=1; count <= numplayers; count++)
  133.       mvprintw(5+( (count - 1) * 2), 20,"%d   ",bankroll[count]);
  134.    mvprintw(3,16, "%d   ",pot);
  135. }
  136.  
  137. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  138.  
  139. void playthegame()
  140. {
  141.    int  count,spin,curplayer,numplayercheck;
  142.    short keepgoing = 1;
  143.  
  144.    pot = 0;
  145.    curplayer = 0;
  146.    while(keepgoing)
  147.    {
  148.       if (pot == 0)
  149.       {
  150.          for(count = 1; count <= numplayers; count++) 
  151.             if(bankroll[count] >0 )
  152.             {
  153.                bankroll[count]--;
  154.                pot++;
  155.                if(doprint) mvprintw(5+( (count - 1) * 2), 28,"ANTE");
  156.             }
  157.          if(doprint)
  158.          {
  159.             updatescreen(numplayers, pot, bankroll);
  160.             refresh();
  161.             usleep(delaytime);
  162.             for(count = 1; count <= numplayers; count++) 
  163.                mvprintw(5+( (count - 1) * 2), 28,"    ");
  164.          }
  165.          if(pot == 1) keepgoing = 0;
  166.       }
  167.       if(!keepgoing) break;
  168.       curplayer++;
  169.       if(curplayer >numplayers) 
  170.       { 
  171.          curplayer = 1; 
  172.          numrounds++;
  173.       }
  174.       while(bankroll[curplayer] <= 0 && keepgoing)
  175.       {
  176.          curplayer++;
  177.          if(curplayer >numplayers) 
  178.          { 
  179.             curplayer = 1; 
  180.             numrounds++;
  181.          }
  182.       }
  183.       spin = rand()%4; numspins++;
  184.       switch (spin)
  185.       {
  186.          case 0:
  187.                 if(doprint) mvprintw(5+( (curplayer - 1) * 2), 28,"Nun");
  188.                 break;
  189.          case 1:
  190.                 if(doprint) mvprintw(5+( (curplayer - 1) * 2), 28,"Gimmel");
  191.                 bankroll[curplayer] += pot;
  192.                 pot = 0;
  193.                 if(doprint) updatescreen(numplayers, pot, bankroll);
  194.                 break;
  195.          case 2:
  196.                 if(doprint) mvprintw(5+( (curplayer - 1) * 2), 28,"Hay");
  197.                 bankroll[curplayer] += (int)pot/2;
  198.                 pot -= (int)pot/2;
  199.                 if(doprint) updatescreen(numplayers, pot, bankroll);
  200.                 break;
  201.          case 3:
  202.                 if(doprint) mvprintw(5+( (curplayer - 1) * 2), 28,"Shin");
  203.                 if(standardshin)
  204.                 {
  205.                    bankroll[curplayer]--;
  206.                    pot++;
  207.                 }
  208.                 else
  209.                 {
  210.                    bankroll[curplayer] -= pot;
  211.                    pot += pot;
  212.                    if(bankroll[curplayer] < 0)
  213.                    {
  214.                       pot += bankroll[curplayer];
  215.                       bankroll[curplayer] = 0;
  216.                    }
  217.                 }
  218.                 if(doprint) updatescreen(numplayers, pot, bankroll);
  219.                 break;
  220.       }
  221.       if(doprint) 
  222.       {
  223.          refresh();
  224.          usleep(delaytime);
  225.          mvprintw(5+( (curplayer - 1) * 2), 28,"         ");
  226.       }
  227.  
  228.       numplayercheck = 0;
  229.       for(count = 1; count <= numplayers; count++)
  230.          if(bankroll[count] > 0) numplayercheck++;
  231.       if(numplayercheck <= 1) keepgoing = 0;
  232.  
  233.    }
  234. }
  235.  
  236. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  237. void usage(char *progname)
  238. {
  239.    printf(
  240.       "%s: [-S] [-s] [-d delay] [-p numplayers] [-m init_money]\n", progname);
  241.    printf("   where '-S' makes shin match the pot rather than add 1 unit\n"); 
  242.    printf("   and '-s' means only print the final stats.\n");
  243.    printf("   'delay' is in ms\n");
  244. }
  245. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  246.  
  247. main(int argc, char *argv[]) {
  248.  
  249.    int count;
  250.  
  251.  
  252.    for(count = 1; count < argc;)
  253.    {
  254.       switch (argv[count][1])
  255.       {
  256.          case 'S' :                                        /*  Shin  */
  257.                    standardshin = 0;
  258.                    count++;
  259.                    break;
  260.          case 's' :                                        /* silent */
  261.                    doprint = 0;
  262.                    count++;
  263.                    break;
  264.          case 'd' :                                     /* set delay */
  265.                    count++;
  266.                    delaytime = 1000 * atoi(argv[count]);
  267.                    count++;
  268.                    break;
  269.          case 'p' :                                 /*set numplayers */
  270.                    count++;
  271.                    numplayers = atoi(argv[count]);
  272.                    count++;
  273.                    break;
  274.          case 'm' :                                   /*set start $$ */
  275.                    count++;
  276.                    initbankroll = atoi(argv[count]);
  277.                    count++;
  278.                    break;
  279.          case 'h' :                                            /*help*/
  280.          case '?' :
  281.                    usage(argv[0]);
  282.                    exit(0);
  283.          default  :
  284.                    usage(argv[0]);
  285.                    exit(1);
  286.       }
  287.    }
  288.  
  289.    srand(time(0));
  290.    if (doprint) initscr();
  291.    signal(SIGINT, exitwell);
  292.  
  293.    if (doprint) clear();
  294.  
  295.    if(doprint)
  296.    mvprintw(1,5, "Game results for $%d and %d players:",
  297.                                          initbankroll,numplayers);
  298.  
  299.    bankroll = malloc( sizeof(int) * numplayers+1);
  300.  
  301.    if(doprint) mvprintw(3,10, "Pot:");
  302.    for(count = 1; count <= numplayers; count++)
  303.    {
  304.       bankroll[count] = initbankroll;
  305.       if(doprint) mvprintw(5+( (count - 1) * 2 ), 10,"Player %d",count);
  306.    }
  307.    
  308.    playthegame();
  309.  
  310.    exitwell();
  311. }
  312.  
  313.  
  314. ----------------8<-----CUT HERE-----8<--------------------
  315.  
  316. -- 
  317. qp HumanNet: Jonathan Katz          |   Computer Aided Design Laboratory     qp
  318. db AT&T-Net: (310)-825-7821         |_________     ---(*)---                 db
  319. qp Internet: jonathan@kanga.cad.ucla.edu      |______Do you read rec.humor?  qp
  320. db SnailNet: 38-138 Engineering IV, UCLA, LA, Ca, 90024|  No, frayed knot... db
  321.