home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ncurses-1.9.9e-src.tgz / tar.out / fsf / ncurses / test / blue.c next >
C/C++ Source or Header  |  1996-09-28  |  9KB  |  413 lines

  1. /*****************************************************************************
  2.  *                                                                           *
  3.  *                         B l u e   M o o n                                 *
  4.  *                         =================                                 *
  5.  *                               V2.2                                        *
  6.  *                   A patience game by T.A.Lister                           *
  7.  *            Integral screen support by Eric S. Raymond                     *
  8.  *                                                                           *
  9.  *****************************************************************************/
  10.  
  11. /*
  12.  * Compile this with the command `cc -O blue.c -lcurses -o blue'. For best
  13.  * results, use the portable freeware ncurses(3) library.  On non-Intel
  14.  * machines, SVr4 curses is just as good.
  15.  */
  16.  
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <signal.h>
  21. #include <time.h>
  22.  
  23. #if HAVE_TERMIOS_H
  24. #include <sys/termios.h>
  25. #endif
  26.  
  27. #include <curses.h>
  28. #include <term.h>
  29.  
  30. #define NOCARD        (-1)
  31.  
  32. #define ACE        0
  33. #define KING        12
  34. #define SUIT_LENGTH    13
  35.  
  36. #define HEARTS        0
  37. #define SPADES        1
  38. #define DIAMONDS    2
  39. #define CLUBS        3
  40. #define NSUITS        4
  41.  
  42. #define GRID_WIDTH    14    /*    13+1  */
  43. #define GRID_LENGTH    56    /* 4*(13+1) */
  44. #define PACK_SIZE    52
  45.  
  46. #define BASEROW        1
  47. #define PROMPTROW    11
  48.  
  49. static int deck_size = PACK_SIZE;    /* initial deck */
  50. static int deck[PACK_SIZE];
  51.  
  52. static int grid[GRID_LENGTH];    /* card layout grid */
  53. static int freeptr[4];        /* free card space pointers */
  54.  
  55. static int deal_number=0;
  56.  
  57. static chtype ranks[SUIT_LENGTH][2] =
  58. {
  59.     {' ', 'A'},
  60.     {' ', '2'},
  61.     {' ', '3'},
  62.     {' ', '4'},
  63.     {' ', '5'},
  64.     {' ', '6'},
  65.     {' ', '7'},
  66.     {' ', '8'},
  67.     {' ', '9'},
  68.     {'1', '0'},
  69.     {' ', 'J'},
  70.     {' ', 'Q'},
  71.     {' ', 'K'}
  72. };
  73.  
  74. static chtype letters[] =
  75. {
  76.     'h' | COLOR_PAIR(COLOR_RED),    /* hearts */
  77.     's' | COLOR_PAIR(COLOR_GREEN),    /* spades */
  78.     'd' | COLOR_PAIR(COLOR_RED),    /* diamonds */
  79.     'c' | COLOR_PAIR(COLOR_GREEN),    /* clubs */
  80. };
  81.  
  82. #if defined(__i386__)
  83. static chtype glyphs[] =
  84. {
  85.     '\003' | A_ALTCHARSET | COLOR_PAIR(COLOR_RED),    /* hearts */
  86.     '\006' | A_ALTCHARSET | COLOR_PAIR(COLOR_GREEN),    /* spades */
  87.     '\004' | A_ALTCHARSET | COLOR_PAIR(COLOR_RED),    /* diamonds */
  88.     '\005' | A_ALTCHARSET | COLOR_PAIR(COLOR_GREEN),    /* clubs */
  89. };
  90. #endif /* __i386__ */
  91.  
  92. static chtype *suits = letters;    /* this may change to glyphs below */
  93.  
  94. static void die(int onsig)
  95. {
  96.     signal(onsig, SIG_IGN);
  97.     endwin();
  98.     exit(0);
  99. }
  100.  
  101. static void init_vars(void)
  102. {
  103.     int i;
  104.  
  105.     deck_size = PACK_SIZE;
  106.     for (i=0; i < PACK_SIZE; i++)
  107.     deck[i]=i;
  108.     for (i = 0; i < 4; i++)
  109.     freeptr[i]=i * GRID_WIDTH;
  110. }
  111.  
  112. static void shuffle(int size)
  113. {
  114.     int i,j,numswaps,swapnum,temp;
  115.  
  116.     numswaps=size*10;        /* an arbitrary figure */
  117.  
  118.     for (swapnum=0;swapnum<numswaps;swapnum++)
  119.     {
  120.     i=rand() % size;
  121.     j=rand() % size;
  122.     temp=deck[i];
  123.     deck[i]=deck[j];
  124.     deck[j]=temp;
  125.     }
  126. }
  127.  
  128. static void deal_cards(void)
  129. {
  130.     int ptr, card=0, value, csuit, crank, suit, aces[4];
  131.  
  132.     for (suit=HEARTS;suit<=CLUBS;suit++)
  133.     {
  134.     ptr=freeptr[suit];
  135.     grid[ptr++]=NOCARD;    /* 1st card space is blank */
  136.     while ((ptr % GRID_WIDTH) != 0)
  137.     {
  138.         value=deck[card++];
  139.         crank=value % SUIT_LENGTH;
  140.         csuit=value / SUIT_LENGTH;
  141.         if (crank==ACE)
  142.         aces[csuit]=ptr;
  143.         grid[ptr++]=value;
  144.     }
  145.     }
  146.  
  147.     if (deal_number==1)        /* shift the aces down to the 1st column */
  148.     for (suit=HEARTS;suit<=CLUBS;suit++)
  149.     {
  150.         grid[suit * GRID_WIDTH] = suit * SUIT_LENGTH;
  151.         grid[aces[suit]]=NOCARD;
  152.         freeptr[suit]=aces[suit];
  153.     }
  154. }
  155.  
  156. static void printcard(int value)
  157. {
  158.     (void) addch(' ');
  159.     if (value == NOCARD)
  160.     (void) addstr("   ");
  161.     else
  162.     {
  163.     addch(ranks[value % SUIT_LENGTH][0] | COLOR_PAIR(COLOR_BLUE));
  164.     addch(ranks[value % SUIT_LENGTH][1] | COLOR_PAIR(COLOR_BLUE));
  165.     addch(suits[value / SUIT_LENGTH]);
  166.     }
  167.     (void) addch(' ');
  168. }
  169.  
  170. static void display_cards(int deal)
  171. {
  172.     int row, card;
  173.  
  174.     clear();
  175.     (void)printw(
  176.          "Blue Moon 2.1 - by Tim Lister & Eric Raymond - Deal %d.\n",
  177.          deal);
  178.     for(row=HEARTS;row<=CLUBS;row++)
  179.     {
  180.     move(BASEROW + row + row + 2, 1);
  181.     for(card=0;card<GRID_WIDTH;card++)
  182.         printcard(grid[row * GRID_WIDTH + card]);
  183.     }
  184.  
  185.     move(PROMPTROW + 2, 0); refresh();
  186. #define P(x)    (void)printw("%s\n", x)
  187. P("   This 52-card solitaire starts with  the entire deck shuffled and dealt");
  188. P("out in four rows.  The aces are then moved to the left end of the layout,");
  189. P("making 4 initial free spaces.  You may move to a space only the card that");
  190. P("matches the left neighbor in suit, and is one greater in rank.  Kings are");
  191. P("high, so no cards may be placed to their right (they create dead spaces).");
  192. P("  When no moves can be made,  cards still out of sequence are  reshuffled");
  193. P("and dealt face up after the ends of the partial sequences, leaving a card");
  194. P("space after each sequence, so that each row looks like a partial sequence");
  195. P("followed by a space, followed by enough cards to make a row of 14.       ");
  196. P("  A moment's reflection will show that this game cannot take more than 13");
  197. P("deals. A good score is 1-3 deals, 4-7 is average, 8 or more is poor.     ");
  198. #undef P
  199.     refresh();
  200. }
  201.  
  202. static int find(int card)
  203. {
  204.     int i;
  205.  
  206.     if ((card<0) || (card>=PACK_SIZE))
  207.     return(NOCARD);
  208.     for(i = 0; i < GRID_LENGTH; i++)
  209.     if (grid[i] == card)
  210.         return i;
  211.     return(NOCARD);
  212. }
  213.  
  214. static void movecard(int src, int dst)
  215. {
  216.     grid[dst]=grid[src];
  217.     grid[src]=NOCARD;
  218.  
  219.     move( BASEROW + (dst / GRID_WIDTH)*2+2, (dst % GRID_WIDTH)*5 + 1);
  220.     printcard(grid[dst]);
  221.  
  222.     move( BASEROW + (src / GRID_WIDTH)*2+2, (src % GRID_WIDTH)*5 + 1);
  223.     printcard(grid[src]);
  224.  
  225.     refresh();
  226. }
  227.  
  228. static void play_game(void)
  229. {
  230.     int dead=0, i, j;
  231.     char c;
  232.     int select[4], card;
  233.  
  234.     while (dead<4)
  235.     {
  236.     dead=0;
  237.     for (i=0;i<4;i++)
  238.     {
  239.         card=grid[freeptr[i]-1];
  240.  
  241.         if (    ((card % SUIT_LENGTH)==KING)
  242.         ||
  243.         (card==NOCARD)    )
  244.         select[i]=NOCARD;
  245.         else
  246.         select[i]=find(card+1);
  247.  
  248.         if (select[i]==NOCARD)
  249.         dead++;
  250.     };
  251.  
  252.     if (dead < 4)
  253.     {
  254.         char    live[NSUITS+1], *lp = live;
  255.  
  256.         for (i=0;i<4;i++)
  257.         {
  258.         if (select[i] != NOCARD)
  259.         {
  260.             move(BASEROW + (select[i] / GRID_WIDTH)*2+3,
  261.              (select[i] % GRID_WIDTH)*5);
  262.             (void)printw("   %c ", *lp++ = 'a' + i);
  263.         }
  264.         };
  265.         *lp = '\0';
  266.  
  267.         if (strlen(live) == 1)
  268.         {
  269.         move(PROMPTROW,0);
  270.         (void)printw(
  271.             "Making forced moves...                                 ");
  272.         refresh();
  273.         (void) sleep(1);
  274.         c = live[0];
  275.         }
  276.         else
  277.         {
  278.         char    buf[BUFSIZ];
  279.  
  280.         (void)sprintf(buf,
  281.             "Type [%s] to move, r to redraw, q or INTR to quit: ",
  282.             live);
  283.  
  284.         do {
  285.             move(PROMPTROW,0);
  286.             (void) addstr(buf);
  287.             move(PROMPTROW, (int)strlen(buf));
  288.             clrtoeol();
  289.             (void) addch(' ');
  290.         } while
  291.             (((c = getch())<'a' || c>'d') && (c!='r') && (c!='q'));
  292.         }
  293.  
  294.         for (j = 0; j < 4; j++)
  295.         if (select[j]!=NOCARD)
  296.         {
  297.             move(BASEROW + (select[j] / GRID_WIDTH)*2+3,
  298.              (select[j] % GRID_WIDTH)*5);
  299.             (void)printw("     ");
  300.         }
  301.  
  302.         if (c == 'r')
  303.         display_cards(deal_number);
  304.         else if (c == 'q')
  305.         die(SIGINT);
  306.         else
  307.         {
  308.         i = c-'a';
  309.         if (select[i] == NOCARD)
  310.             beep();
  311.         else
  312.         {
  313.             movecard(select[i], freeptr[i]);
  314.             freeptr[i]=select[i];
  315.         }
  316.         }
  317.     }
  318.     }
  319.  
  320.     move(PROMPTROW, 0);
  321.     standout();
  322.     (void)printw("Finished deal %d - type any character to continue...", deal_number);
  323.     standend();
  324.     (void) getch();
  325. }
  326.  
  327. static int collect_discards(void)
  328. {
  329.     int row, col, cardno=0, finish, gridno;
  330.  
  331.     for (row=HEARTS;row<=CLUBS;row++)
  332.     {
  333.     finish=0;
  334.     for (col=1;col<GRID_WIDTH;col++)
  335.     {
  336.         gridno=row * GRID_WIDTH + col;
  337.  
  338.         if ((grid[gridno]!=(grid[gridno-1]+1))&&(finish==0))
  339.         {
  340.         finish=1;
  341.         freeptr[row]=gridno;
  342.         };
  343.  
  344.         if ((finish!=0)&&(grid[gridno]!=NOCARD))
  345.         deck[cardno++]=grid[gridno];
  346.     }
  347.     }
  348.     return cardno;
  349. }
  350.  
  351. static void game_finished(int deal)
  352. {
  353.     clear();
  354.     (void)printw("You finished the game in %d deals. This is ",deal);
  355.     standout();
  356.     if (deal<2)
  357.     (void)addstr("excellent");
  358.     else if (deal<4)
  359.     (void)addstr("good");
  360.     else if (deal<8)
  361.     (void)addstr("average");
  362.     else
  363.     (void)addstr("poor");
  364.     standend();
  365.     (void) addstr(".         ");
  366.     refresh();
  367. }
  368.  
  369. int main(int argc, char *argv[])
  370. {
  371.     (void) signal(SIGINT, die);
  372.     initscr();
  373.  
  374.     /*
  375.      * We use COLOR_GREEN because COLOR_BLACK is wired to the wrong thing.
  376.      */
  377.     start_color();
  378.     init_pair(COLOR_RED,     COLOR_RED,   COLOR_WHITE);
  379.     init_pair(COLOR_BLUE,    COLOR_BLUE,  COLOR_WHITE);
  380.     init_pair(COLOR_GREEN,   COLOR_BLACK, COLOR_WHITE);
  381.  
  382. #if defined(__i386__) && defined(A_ALTCHARSET)
  383.     if (tigetstr("smpch"))
  384.     suits = glyphs;
  385. #endif /* __i386__ && A_ALTCHARSET */
  386.  
  387.     cbreak();
  388.  
  389.     if (argc == 2)
  390.     srand((unsigned)atoi(argv[1]));
  391.     else
  392.     srand((unsigned)time((time_t *)0));
  393.  
  394.     init_vars();
  395.  
  396.     do{
  397.     deal_number++;
  398.     shuffle(deck_size);
  399.     deal_cards();
  400.     display_cards(deal_number);
  401.     play_game();
  402.     }
  403.     while
  404.     ((deck_size=collect_discards()) != 0);
  405.  
  406.     game_finished(deal_number);
  407.  
  408.     die(SIGINT);
  409.     /*NOTREACHED*/
  410. }
  411.  
  412. /* blue.c ends here */
  413.