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