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