]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/blue.c
ncurses 6.0 - patch 20160521
[ncurses.git] / test / blue.c
1 /****************************************************************************
2  * Copyright (c) 1998-2013,2016 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  * $Id: blue.c,v 1.44 2016/04/16 23:20:09 tom Exp $
40  */
41
42 #include <test.priv.h>
43
44 #include <time.h>
45
46 #if HAVE_LANGINFO_CODESET
47 #include <langinfo.h>
48 #endif
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 void 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 static int letters[4] =
101 {
102     'h',                        /* hearts */
103     's',                        /* spades */
104     'd',                        /* diamonds */
105     'c',                        /* clubs */
106 };
107
108 #if HAVE_LANGINFO_CODESET
109
110 #if HAVE_TIGETSTR
111 static int glyphs[] =
112 {
113     '\003',                     /* hearts */
114     '\006',                     /* spades */
115     '\004',                     /* diamonds */
116     '\005',                     /* clubs */
117 };
118 #endif
119
120 #if USE_WIDEC_SUPPORT
121 static int uglyphs[] =
122 {
123     0x2665,                     /* hearts */
124     0x2660,                     /* spades */
125     0x2666,                     /* diamonds */
126     0x2663                      /* clubs */
127 };
128 #endif
129 #endif /* HAVE_LANGINFO_CODESET */
130
131 static int *suits = letters;    /* this may change to glyphs below */
132
133 static void
134 die(int onsig)
135 {
136     (void) signal(onsig, SIG_IGN);
137     endwin();
138     ExitProgram(EXIT_SUCCESS);
139 }
140
141 static void
142 init_vars(void)
143 {
144     int i;
145
146     deck_size = PACK_SIZE;
147     for (i = 0; i < PACK_SIZE; i++)
148         deck[i] = i;
149     for (i = 0; i < 4; i++)
150         freeptr[i] = i * GRID_WIDTH;
151 }
152
153 static void
154 shuffle(int size)
155 {
156     int i, j, numswaps, swapnum, temp;
157
158     numswaps = size * 10;       /* an arbitrary figure */
159
160     for (swapnum = 0; swapnum < numswaps; swapnum++) {
161         i = rand() % size;
162         j = rand() % size;
163         temp = deck[i];
164         deck[i] = deck[j];
165         deck[j] = temp;
166     }
167 }
168
169 static void
170 deal_cards(void)
171 {
172     int ptr, card = 0, value, csuit, crank, suit, aces[4];
173
174     memset(aces, 0, sizeof(aces));
175     for (suit = HEARTS; suit <= CLUBS; suit++) {
176         ptr = freeptr[suit];
177         grid[ptr++] = NOCARD;   /* 1st card space is blank */
178         while ((ptr % GRID_WIDTH) != 0) {
179             value = deck[card++];
180             crank = value % SUIT_LENGTH;
181             csuit = value / SUIT_LENGTH;
182             if (crank == ACE)
183                 aces[csuit] = ptr;
184             grid[ptr++] = value;
185         }
186     }
187
188     if (deal_number == 1)       /* shift the aces down to the 1st column */
189         for (suit = HEARTS; suit <= CLUBS; suit++) {
190             grid[suit * GRID_WIDTH] = suit * SUIT_LENGTH;
191             grid[aces[suit]] = NOCARD;
192             freeptr[suit] = aces[suit];
193         }
194 }
195
196 static void
197 printcard(int value)
198 {
199     (void) addch(' ');
200     if (value == NOCARD) {
201         (void) addstr("   ");
202     } else {
203         int which = (value / SUIT_LENGTH);
204         int isuit = (value % SUIT_LENGTH);
205         attr_t color = (attr_t) COLOR_PAIR(((which % 2) == 0)
206                                            ? RED_ON_WHITE
207                                            : BLACK_ON_WHITE);
208
209         addch(ranks[isuit][0] | (chtype) COLOR_PAIR(BLUE_ON_WHITE));
210         addch(ranks[isuit][1] | (chtype) COLOR_PAIR(BLUE_ON_WHITE));
211
212         attr_on(color, NULL);
213 #if USE_WIDEC_SUPPORT
214         {
215             wchar_t values[2];
216             values[0] = (wchar_t) suits[which];
217             values[1] = 0;
218             addwstr(values);
219         }
220 #else
221         addch((chtype) suits[which]);
222 #endif
223         attr_off(color, NULL);
224     }
225     (void) addch(' ');
226 }
227
228 static void
229 display_cards(int deal)
230 {
231     int row, card;
232
233     clear();
234     (void) printw(
235                      "Blue Moon 2.1 - by Tim Lister & Eric Raymond - Deal %d.\n",
236                      deal);
237     for (row = HEARTS; row <= CLUBS; row++) {
238         move(BASEROW + row + row + 2, 1);
239         for (card = 0; card < GRID_WIDTH; card++)
240             printcard(grid[row * GRID_WIDTH + card]);
241     }
242
243     move(PROMPTROW + 2, 0);
244     refresh();
245 #define P(x)    (void)printw("%s\n", x)
246     P("   This 52-card solitaire starts with  the entire deck shuffled and dealt");
247     P("out in four rows.  The aces are then moved to the left end of the layout,");
248     P("making 4 initial free spaces.  You may move to a space only the card that");
249     P("matches the left neighbor in suit, and is one greater in rank.  Kings are");
250     P("high, so no cards may be placed to their right (they create dead spaces).");
251     P("  When no moves can be made,  cards still out of sequence are  reshuffled");
252     P("and dealt face up after the ends of the partial sequences, leaving a card");
253     P("space after each sequence, so that each row looks like a partial sequence");
254     P("followed by a space, followed by enough cards to make a row of 14.       ");
255     P("  A moment's reflection will show that this game cannot take more than 13");
256     P("deals. A good score is 1-3 deals, 4-7 is average, 8 or more is poor.     ");
257 #undef P
258     refresh();
259 }
260
261 static int
262 find(int card)
263 {
264     int i;
265
266     if ((card < 0) || (card >= PACK_SIZE))
267         return (NOCARD);
268     for (i = 0; i < GRID_LENGTH; i++)
269         if (grid[i] == card)
270             return i;
271     return (NOCARD);
272 }
273
274 static void
275 movecard(int src, int dst)
276 {
277     grid[dst] = grid[src];
278     grid[src] = NOCARD;
279
280     move(BASEROW + (dst / GRID_WIDTH) * 2 + 2, (dst % GRID_WIDTH) * 5 + 1);
281     printcard(grid[dst]);
282
283     move(BASEROW + (src / GRID_WIDTH) * 2 + 2, (src % GRID_WIDTH) * 5 + 1);
284     printcard(grid[src]);
285
286     refresh();
287 }
288
289 static void
290 play_game(void)
291 {
292     int dead = 0, i, j;
293     char c;
294     int selection[4], card;
295
296     while (dead < 4) {
297         dead = 0;
298         for (i = 0; i < 4; i++) {
299             card = grid[freeptr[i] - 1];
300
301             if (((card % SUIT_LENGTH) == KING)
302                 ||
303                 (card == NOCARD))
304                 selection[i] = NOCARD;
305             else
306                 selection[i] = find(card + 1);
307
308             if (selection[i] == NOCARD)
309                 dead++;
310         };
311
312         if (dead < 4) {
313             char live[NSUITS + 1], *lp = live;
314
315             for (i = 0; i < 4; i++) {
316                 if (selection[i] != NOCARD) {
317                     move(BASEROW + (selection[i] / GRID_WIDTH) * 2 + 3,
318                          (selection[i] % GRID_WIDTH) * 5);
319                     (void) printw("   %c ", (*lp++ = (char) ('a' + i)));
320                 }
321             };
322             *lp = '\0';
323
324             if (strlen(live) == 1) {
325                 move(PROMPTROW, 0);
326                 (void) printw(
327                                  "Making forced moves...                                 ");
328                 refresh();
329                 (void) sleep(1);
330                 c = live[0];
331             } else {
332                 char buf[BUFSIZ];
333
334                 (void) sprintf(buf,
335                                "Type [%s] to move, r to redraw, q or INTR to quit: ",
336                                live);
337
338                 do {
339                     move(PROMPTROW, 0);
340                     (void) addstr(buf);
341                     move(PROMPTROW, (int) strlen(buf));
342                     clrtoeol();
343                     (void) addch(' ');
344                 } while
345                     (((c = (char) getch()) < 'a' || c > 'd')
346                      && (c != 'r')
347                      && (c != 'q'));
348             }
349
350             for (j = 0; j < 4; j++)
351                 if (selection[j] != NOCARD) {
352                     move(BASEROW + (selection[j] / GRID_WIDTH) * 2 + 3,
353                          (selection[j] % GRID_WIDTH) * 5);
354                     (void) printw("     ");
355                 }
356
357             if (c == 'r')
358                 display_cards(deal_number);
359             else if (c == 'q')
360                 die(SIGINT);
361             else {
362                 i = c - 'a';
363                 if (selection[i] == NOCARD)
364                     beep();
365                 else {
366                     movecard(selection[i], freeptr[i]);
367                     freeptr[i] = selection[i];
368                 }
369             }
370         }
371     }
372
373     move(PROMPTROW, 0);
374     (void) standout();
375     (void) printw("Finished deal %d - type any character to continue...", deal_number);
376     (void) standend();
377     (void) getch();
378 }
379
380 static int
381 collect_discards(void)
382 {
383     int row, col, cardno = 0, finish, gridno;
384
385     for (row = HEARTS; row <= CLUBS; row++) {
386         finish = 0;
387         for (col = 1; col < GRID_WIDTH; col++) {
388             gridno = row * GRID_WIDTH + col;
389
390             if ((grid[gridno] != (grid[gridno - 1] + 1)) && (finish == 0)) {
391                 finish = 1;
392                 freeptr[row] = gridno;
393             };
394
395             if ((finish != 0) && (grid[gridno] != NOCARD))
396                 deck[cardno++] = grid[gridno];
397         }
398     }
399     return cardno;
400 }
401
402 static void
403 game_finished(int deal)
404 {
405     clear();
406     (void) printw("You finished the game in %d deals. This is ", deal);
407     (void) standout();
408     if (deal < 2)
409         (void) addstr("excellent");
410     else if (deal < 4)
411         (void) addstr("good");
412     else if (deal < 8)
413         (void) addstr("average");
414     else
415         (void) addstr("poor");
416     (void) standend();
417     (void) addstr(".         ");
418     refresh();
419 }
420
421 #if HAVE_LANGINFO_CODESET
422 /*
423  * This program first appeared in ncurses in January 1995.  At that point, the
424  * Linux console was able to display CP437 graphic characters, e.g., in the
425  * range 0-31.  As of 2016, most Linux consoles are running with the UTF-8
426  * (partial) support.  Incidentally, that makes all of the cards diamonds.
427  */
428 static void
429 use_pc_display(void)
430 {
431     char *check = nl_langinfo(CODESET);
432     if (!strcmp(check, "UTF-8")) {
433 #if USE_WIDEC_SUPPORT
434         suits = uglyphs;
435 #endif
436     } else {
437 #if HAVE_TIGETSTR
438         if (!strcmp(check, "IBM437") ||
439             !strcmp(check, "CP437") ||
440             !strcmp(check, "IBM850") ||
441             !strcmp(check, "CP850")) {
442             char *smacs = tigetstr("smacs");
443             char *smpch = tigetstr("smpch");
444             /*
445              * The ncurses library makes this check to decide whether to allow
446              * the alternate character set for the (normally) nonprinting codes.
447              */
448             if (smacs != 0 && smpch != 0 && !strcmp(smacs, smpch)) {
449                 suits = glyphs;
450             }
451         }
452 #endif
453     }
454 }
455 #else
456 #define use_pc_display()        /* nothing */
457 #endif /* HAVE_LANGINFO_CODESET */
458
459 int
460 main(int argc, char *argv[])
461 {
462     CATCHALL(die);
463
464     setlocale(LC_ALL, "");
465
466     use_pc_display();
467
468     initscr();
469
470     start_color();
471     init_pair(RED_ON_WHITE, COLOR_RED, COLOR_WHITE);
472     init_pair(BLUE_ON_WHITE, COLOR_BLUE, COLOR_WHITE);
473     init_pair(BLACK_ON_WHITE, COLOR_BLACK, COLOR_WHITE);
474
475     cbreak();
476
477     if (argc == 2)
478         srand((unsigned) atoi(argv[1]));
479     else
480         srand((unsigned) time((time_t *) 0));
481
482     init_vars();
483
484     do {
485         deal_number++;
486         shuffle(deck_size);
487         deal_cards();
488         display_cards(deal_number);
489         play_game();
490     }
491     while
492         ((deck_size = collect_discards()) != 0);
493
494     game_finished(deal_number);
495
496     die(SIGINT);
497     /*NOTREACHED */
498 }
499
500 /* blue.c ends here */