]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/blue.c
ncurses 5.6 - patch 20070210
[ncurses.git] / test / blue.c
1 /****************************************************************************
2  * Copyright (c) 1998-2005,2006 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 /*****************************************************************************
29  *                                                                           *
30  *                         B l u e   M o o n                                 *
31  *                         =================                                 *
32  *                               V2.2                                        *
33  *                   A patience game by T.A.Lister                           *
34  *            Integral screen support by Eric S. Raymond                     *
35  *                                                                           *
36  *****************************************************************************/
37
38 /*
39  * Compile this with the command `cc -O blue.c -lcurses -o blue'.  For best
40  * results, use the ncurses(3) library.  On non-Intel machines, SVr4 curses is
41  * just as good.
42  *
43  * $Id: blue.c,v 1.29 2006/05/20 15:38:18 tom Exp $
44  */
45
46 #include <test.priv.h>
47
48 #include <time.h>
49
50 #define NOCARD          (-1)
51
52 #define ACE             0
53 #define KING            12
54 #define SUIT_LENGTH     13
55
56 #define HEARTS          0
57 #define SPADES          1
58 #define DIAMONDS        2
59 #define CLUBS           3
60 #define NSUITS          4
61
62 #define GRID_WIDTH      14      /*    13+1  */
63 #define GRID_LENGTH     56      /* 4*(13+1) */
64 #define PACK_SIZE       52
65
66 #define BASEROW         1
67 #define PROMPTROW       11
68
69 #define RED_ON_WHITE    1
70 #define BLACK_ON_WHITE  2
71 #define BLUE_ON_WHITE   3
72
73 static RETSIGTYPE die(int onsig) GCC_NORETURN;
74
75 static int deck_size = PACK_SIZE;       /* initial deck */
76 static int deck[PACK_SIZE];
77
78 static int grid[GRID_LENGTH];   /* card layout grid */
79 static int freeptr[4];          /* free card space pointers */
80
81 static int deal_number = 0;
82
83 static chtype ranks[SUIT_LENGTH][2] =
84 {
85     {' ', 'A'},
86     {' ', '2'},
87     {' ', '3'},
88     {' ', '4'},
89     {' ', '5'},
90     {' ', '6'},
91     {' ', '7'},
92     {' ', '8'},
93     {' ', '9'},
94     {'1', '0'},
95     {' ', 'J'},
96     {' ', 'Q'},
97     {' ', 'K'}
98 };
99
100 /* Please note, that this is a bad example.
101    Color values should not be or'ed in. This
102    only works, because the characters used here
103    are plain and have no color attribute themselves. */
104 #ifdef COLOR_PAIR
105 #define OR_COLORS(value,pair) ((value) | COLOR_PAIR(pair))
106 #else
107 #define OR_COLORS(value,pair) (value)
108 #endif
109
110 #define PC_COLORS(value,pair) (OR_COLORS(value,pair) | A_ALTCHARSET)
111
112 static chtype letters[4] =
113 {
114     OR_COLORS('h', RED_ON_WHITE),       /* hearts */
115     OR_COLORS('s', BLACK_ON_WHITE),     /* spades */
116     OR_COLORS('d', RED_ON_WHITE),       /* diamonds */
117     OR_COLORS('c', BLACK_ON_WHITE),     /* clubs */
118 };
119
120 #if defined(__i386__) && defined(A_ALTCHARSET) && HAVE_TIGETSTR
121 static chtype glyphs[] =
122 {
123     PC_COLORS('\003', RED_ON_WHITE),    /* hearts */
124     PC_COLORS('\006', BLACK_ON_WHITE),  /* spades */
125     PC_COLORS('\004', RED_ON_WHITE),    /* diamonds */
126     PC_COLORS('\005', BLACK_ON_WHITE),  /* clubs */
127 };
128 #define USE_CP437 1
129 #else
130 #define USE_CP437 0
131 #endif /* __i386__ */
132
133 static chtype *suits = letters; /* this may change to glyphs below */
134
135 static RETSIGTYPE
136 die(int onsig)
137 {
138     (void) signal(onsig, SIG_IGN);
139     endwin();
140     ExitProgram(EXIT_SUCCESS);
141 }
142
143 static void
144 init_vars(void)
145 {
146     int i;
147
148     deck_size = PACK_SIZE;
149     for (i = 0; i < PACK_SIZE; i++)
150         deck[i] = i;
151     for (i = 0; i < 4; i++)
152         freeptr[i] = i * GRID_WIDTH;
153 }
154
155 static void
156 shuffle(int size)
157 {
158     int i, j, numswaps, swapnum, temp;
159
160     numswaps = size * 10;       /* an arbitrary figure */
161
162     for (swapnum = 0; swapnum < numswaps; swapnum++) {
163         i = rand() % size;
164         j = rand() % size;
165         temp = deck[i];
166         deck[i] = deck[j];
167         deck[j] = temp;
168     }
169 }
170
171 static void
172 deal_cards(void)
173 {
174     int ptr, card = 0, value, csuit, crank, suit, aces[4];
175
176     for (suit = HEARTS; suit <= CLUBS; suit++) {
177         ptr = freeptr[suit];
178         grid[ptr++] = NOCARD;   /* 1st card space is blank */
179         while ((ptr % GRID_WIDTH) != 0) {
180             value = deck[card++];
181             crank = value % SUIT_LENGTH;
182             csuit = value / SUIT_LENGTH;
183             if (crank == ACE)
184                 aces[csuit] = ptr;
185             grid[ptr++] = value;
186         }
187     }
188
189     if (deal_number == 1)       /* shift the aces down to the 1st column */
190         for (suit = HEARTS; suit <= CLUBS; suit++) {
191             grid[suit * GRID_WIDTH] = suit * SUIT_LENGTH;
192             grid[aces[suit]] = NOCARD;
193             freeptr[suit] = aces[suit];
194         }
195 }
196
197 static void
198 printcard(int value)
199 {
200     (void) addch(' ');
201     if (value == NOCARD)
202         (void) addstr("   ");
203     else {
204         addch(ranks[value % SUIT_LENGTH][0] | COLOR_PAIR(BLUE_ON_WHITE));
205         addch(ranks[value % SUIT_LENGTH][1] | COLOR_PAIR(BLUE_ON_WHITE));
206         addch(suits[value / SUIT_LENGTH]);
207     }
208     (void) addch(' ');
209 }
210
211 static void
212 display_cards(int deal)
213 {
214     int row, card;
215
216     clear();
217     (void) printw(
218                      "Blue Moon 2.1 - by Tim Lister & Eric Raymond - Deal %d.\n",
219                      deal);
220     for (row = HEARTS; row <= CLUBS; row++) {
221         move(BASEROW + row + row + 2, 1);
222         for (card = 0; card < GRID_WIDTH; card++)
223             printcard(grid[row * GRID_WIDTH + card]);
224     }
225
226     move(PROMPTROW + 2, 0);
227     refresh();
228 #define P(x)    (void)printw("%s\n", x)
229     P("   This 52-card solitaire starts with  the entire deck shuffled and dealt");
230     P("out in four rows.  The aces are then moved to the left end of the layout,");
231     P("making 4 initial free spaces.  You may move to a space only the card that");
232     P("matches the left neighbor in suit, and is one greater in rank.  Kings are");
233     P("high, so no cards may be placed to their right (they create dead spaces).");
234     P("  When no moves can be made,  cards still out of sequence are  reshuffled");
235     P("and dealt face up after the ends of the partial sequences, leaving a card");
236     P("space after each sequence, so that each row looks like a partial sequence");
237     P("followed by a space, followed by enough cards to make a row of 14.       ");
238     P("  A moment's reflection will show that this game cannot take more than 13");
239     P("deals. A good score is 1-3 deals, 4-7 is average, 8 or more is poor.     ");
240 #undef P
241     refresh();
242 }
243
244 static int
245 find(int card)
246 {
247     int i;
248
249     if ((card < 0) || (card >= PACK_SIZE))
250         return (NOCARD);
251     for (i = 0; i < GRID_LENGTH; i++)
252         if (grid[i] == card)
253             return i;
254     return (NOCARD);
255 }
256
257 static void
258 movecard(int src, int dst)
259 {
260     grid[dst] = grid[src];
261     grid[src] = NOCARD;
262
263     move(BASEROW + (dst / GRID_WIDTH) * 2 + 2, (dst % GRID_WIDTH) * 5 + 1);
264     printcard(grid[dst]);
265
266     move(BASEROW + (src / GRID_WIDTH) * 2 + 2, (src % GRID_WIDTH) * 5 + 1);
267     printcard(grid[src]);
268
269     refresh();
270 }
271
272 static void
273 play_game(void)
274 {
275     int dead = 0, i, j;
276     char c;
277     int selection[4], card;
278
279     while (dead < 4) {
280         dead = 0;
281         for (i = 0; i < 4; i++) {
282             card = grid[freeptr[i] - 1];
283
284             if (((card % SUIT_LENGTH) == KING)
285                 ||
286                 (card == NOCARD))
287                 selection[i] = NOCARD;
288             else
289                 selection[i] = find(card + 1);
290
291             if (selection[i] == NOCARD)
292                 dead++;
293         };
294
295         if (dead < 4) {
296             char live[NSUITS + 1], *lp = live;
297
298             for (i = 0; i < 4; i++) {
299                 if (selection[i] != NOCARD) {
300                     move(BASEROW + (selection[i] / GRID_WIDTH) * 2 + 3,
301                          (selection[i] % GRID_WIDTH) * 5);
302                     (void) printw("   %c ", *lp++ = 'a' + i);
303                 }
304             };
305             *lp = '\0';
306
307             if (strlen(live) == 1) {
308                 move(PROMPTROW, 0);
309                 (void) printw(
310                                  "Making forced moves...                                 ");
311                 refresh();
312                 (void) sleep(1);
313                 c = live[0];
314             } else {
315                 char buf[BUFSIZ];
316
317                 (void) sprintf(buf,
318                                "Type [%s] to move, r to redraw, q or INTR to quit: ",
319                                live);
320
321                 do {
322                     move(PROMPTROW, 0);
323                     (void) addstr(buf);
324                     move(PROMPTROW, (int) strlen(buf));
325                     clrtoeol();
326                     (void) addch(' ');
327                 } while
328                     (((c = getch()) < 'a' || c > 'd') && (c != 'r') && (c != 'q'));
329             }
330
331             for (j = 0; j < 4; j++)
332                 if (selection[j] != NOCARD) {
333                     move(BASEROW + (selection[j] / GRID_WIDTH) * 2 + 3,
334                          (selection[j] % GRID_WIDTH) * 5);
335                     (void) printw("     ");
336                 }
337
338             if (c == 'r')
339                 display_cards(deal_number);
340             else if (c == 'q')
341                 die(SIGINT);
342             else {
343                 i = c - 'a';
344                 if (selection[i] == NOCARD)
345                     beep();
346                 else {
347                     movecard(selection[i], freeptr[i]);
348                     freeptr[i] = selection[i];
349                 }
350             }
351         }
352     }
353
354     move(PROMPTROW, 0);
355     standout();
356     (void) printw("Finished deal %d - type any character to continue...", deal_number);
357     standend();
358     (void) getch();
359 }
360
361 static int
362 collect_discards(void)
363 {
364     int row, col, cardno = 0, finish, gridno;
365
366     for (row = HEARTS; row <= CLUBS; row++) {
367         finish = 0;
368         for (col = 1; col < GRID_WIDTH; col++) {
369             gridno = row * GRID_WIDTH + col;
370
371             if ((grid[gridno] != (grid[gridno - 1] + 1)) && (finish == 0)) {
372                 finish = 1;
373                 freeptr[row] = gridno;
374             };
375
376             if ((finish != 0) && (grid[gridno] != NOCARD))
377                 deck[cardno++] = grid[gridno];
378         }
379     }
380     return cardno;
381 }
382
383 static void
384 game_finished(int deal)
385 {
386     clear();
387     (void) printw("You finished the game in %d deals. This is ", deal);
388     standout();
389     if (deal < 2)
390         (void) addstr("excellent");
391     else if (deal < 4)
392         (void) addstr("good");
393     else if (deal < 8)
394         (void) addstr("average");
395     else
396         (void) addstr("poor");
397     standend();
398     (void) addstr(".         ");
399     refresh();
400 }
401
402 int
403 main(int argc, char *argv[])
404 {
405     CATCHALL(die);
406
407     setlocale(LC_ALL, "");
408
409     initscr();
410
411     /*
412      * We use COLOR_GREEN because COLOR_BLACK is wired to the wrong thing.
413      */
414     start_color();
415     init_pair(RED_ON_WHITE, COLOR_RED, COLOR_WHITE);
416     init_pair(BLUE_ON_WHITE, COLOR_BLUE, COLOR_WHITE);
417     init_pair(BLACK_ON_WHITE, COLOR_BLACK, COLOR_WHITE);
418
419 #ifndef COLOR_PAIR
420     letters[0] = OR_COLORS('h', RED_ON_WHITE);  /* hearts */
421     letters[1] = OR_COLORS('s', BLACK_ON_WHITE);        /* spades */
422     letters[2] = OR_COLORS('d', RED_ON_WHITE);  /* diamonds */
423     letters[3] = OR_COLORS('c', BLACK_ON_WHITE);        /* clubs */
424 #if USE_CP437
425     glyphs[0] = PC_COLORS('\003', RED_ON_WHITE);        /* hearts */
426     glyphs[1] = PC_COLORS('\006', BLACK_ON_WHITE);      /* spades */
427     glyphs[2] = PC_COLORS('\004', RED_ON_WHITE);        /* diamonds */
428     glyphs[3] = PC_COLORS('\005', BLACK_ON_WHITE);      /* clubs */
429 #endif
430 #endif
431
432 #if USE_CP437
433     if (tigetstr("smpch"))
434         suits = glyphs;
435 #endif /* USE_CP437 */
436
437     cbreak();
438
439     if (argc == 2)
440         srand((unsigned) atoi(argv[1]));
441     else
442         srand((unsigned) time((time_t *) 0));
443
444     init_vars();
445
446     do {
447         deal_number++;
448         shuffle(deck_size);
449         deal_cards();
450         display_cards(deal_number);
451         play_game();
452     }
453     while
454         ((deck_size = collect_discards()) != 0);
455
456     game_finished(deal_number);
457
458     die(SIGINT);
459     /*NOTREACHED */
460 }
461
462 /* blue.c ends here */