1 /****************************************************************************
2 * Copyright 2019-2021,2022 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
29 /*****************************************************************************
34 * A patience game by T.A.Lister *
35 * Integral screen support by Eric S. Raymond *
37 *****************************************************************************/
40 * $Id: blue.c,v 1.55 2022/12/10 23:31:31 tom Exp $
43 #include <test.priv.h>
47 #if HAVE_LANGINFO_CODESET
55 #define SUIT_LENGTH 13
63 #define GRID_WIDTH 14 /* 13+1 */
64 #define GRID_LENGTH 56 /* 4*(13+1) */
70 #define RED_ON_WHITE 1
71 #define BLACK_ON_WHITE 2
72 #define BLUE_ON_WHITE 3
74 static GCC_NORETURN void die(int onsig);
76 static int deck_size = PACK_SIZE; /* initial deck */
77 static int deck[PACK_SIZE];
79 static int grid[GRID_LENGTH]; /* card layout grid */
80 static int freeptr[4]; /* free card space pointers */
82 static int deal_number = 0;
84 static chtype ranks[SUIT_LENGTH][2] =
101 static int letters[4] =
109 #if HAVE_LANGINFO_CODESET
112 static int glyphs[] =
116 '\004', /* diamonds */
121 #if USE_WIDEC_SUPPORT
122 static int uglyphs[] =
126 0x2666, /* diamonds */
130 #endif /* HAVE_LANGINFO_CODESET */
132 static int *suits = letters; /* this may change to glyphs below */
137 (void) signal(onsig, SIG_IGN);
139 ExitProgram(EXIT_SUCCESS);
147 deck_size = PACK_SIZE;
148 for (i = 0; i < PACK_SIZE; i++)
150 for (i = 0; i < 4; i++)
151 freeptr[i] = i * GRID_WIDTH;
157 int numswaps, swapnum;
159 numswaps = size * 10; /* an arbitrary figure */
161 for (swapnum = 0; swapnum < numswaps; swapnum++) {
162 int i = rand() % size;
163 int j = rand() % size;
173 int card = 0, value, csuit, crank, suit, aces[4];
175 memset(aces, 0, sizeof(aces));
176 for (suit = HEARTS; suit <= CLUBS; suit++) {
177 int 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;
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];
201 if (value == NOCARD) {
204 int which = (value / SUIT_LENGTH);
205 int isuit = (value % SUIT_LENGTH);
206 chtype color = (chtype) COLOR_PAIR(((which % 2) == 0)
210 AddCh(ranks[isuit][0] | (chtype) COLOR_PAIR(BLUE_ON_WHITE));
211 AddCh(ranks[isuit][1] | (chtype) COLOR_PAIR(BLUE_ON_WHITE));
213 #ifdef NCURSES_VERSION
214 (attron) ((int) color); /* quieter compiler warnings */
216 attron(color); /* PDCurses, etc., either no macro or wrong */
218 #if USE_WIDEC_SUPPORT
221 values[0] = (wchar_t) suits[which];
228 #ifdef NCURSES_VERSION
229 (attroff) ((int) color);
238 display_cards(int deal)
244 "Blue Moon 2.1 - by Tim Lister & Eric Raymond - Deal %d.\n",
246 for (row = HEARTS; row <= CLUBS; row++) {
247 move(BASEROW + row + row + 2, 1);
248 for (card = 0; card < GRID_WIDTH; card++)
249 printcard(grid[row * GRID_WIDTH + card]);
252 move(PROMPTROW + 2, 0);
254 #define P(x) (void)printw("%s\n", x)
255 P(" This 52-card solitaire starts with the entire deck shuffled and dealt");
256 P("out in four rows. The aces are then moved to the left end of the layout,");
257 P("making 4 initial free spaces. You may move to a space only the card that");
258 P("matches the left neighbor in suit, and is one greater in rank. Kings are");
259 P("high, so no cards may be placed to their right (they create dead spaces).");
260 P(" When no moves can be made, cards still out of sequence are reshuffled");
261 P("and dealt face up after the ends of the partial sequences, leaving a card");
262 P("space after each sequence, so that each row looks like a partial sequence");
263 P("followed by a space, followed by enough cards to make a row of 14. ");
264 P(" A moment's reflection will show that this game cannot take more than 13");
265 P("deals. A good score is 1-3 deals, 4-7 is average, 8 or more is poor. ");
275 if ((card < 0) || (card >= PACK_SIZE))
277 for (i = 0; i < GRID_LENGTH; i++)
284 movecard(int src, int dst)
286 grid[dst] = grid[src];
289 move(BASEROW + (dst / GRID_WIDTH) * 2 + 2, (dst % GRID_WIDTH) * 5 + 1);
290 printcard(grid[dst]);
292 move(BASEROW + (src / GRID_WIDTH) * 2 + 2, (src % GRID_WIDTH) * 5 + 1);
293 printcard(grid[src]);
303 int selection[4], card;
307 for (i = 0; i < 4; i++) {
308 card = grid[freeptr[i] - 1];
310 if (((card % SUIT_LENGTH) == KING)
313 selection[i] = NOCARD;
315 selection[i] = find(card + 1);
317 if (selection[i] == NOCARD)
322 char live[NSUITS + 1], *lp = live;
324 for (i = 0; i < 4; i++) {
325 if (selection[i] != NOCARD) {
326 move(BASEROW + (selection[i] / GRID_WIDTH) * 2 + 3,
327 (selection[i] % GRID_WIDTH) * 5);
328 (void) printw(" %c ", (*lp++ = (char) ('a' + i)));
333 if (strlen(live) == 1) {
336 "Making forced moves... ");
343 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf))
344 "Type [%s] to move, r to redraw, q or INTR to quit: ",
350 move(PROMPTROW, (int) strlen(buf));
354 (((c = (char) getch()) < 'a' || c > 'd')
359 for (j = 0; j < 4; j++)
360 if (selection[j] != NOCARD) {
361 move(BASEROW + (selection[j] / GRID_WIDTH) * 2 + 3,
362 (selection[j] % GRID_WIDTH) * 5);
367 display_cards(deal_number);
372 if (selection[i] == NOCARD)
375 movecard(selection[i], freeptr[i]);
376 freeptr[i] = selection[i];
384 (void) printw("Finished deal %d - type any character to continue...", deal_number);
390 collect_discards(void)
392 int row, col, cardno = 0, gridno;
394 for (row = HEARTS; row <= CLUBS; row++) {
396 for (col = 1; col < GRID_WIDTH; col++) {
397 gridno = row * GRID_WIDTH + col;
399 if ((grid[gridno] != (grid[gridno - 1] + 1)) && (finish == 0)) {
401 freeptr[row] = gridno;
404 if ((finish != 0) && (grid[gridno] != NOCARD))
405 deck[cardno++] = grid[gridno];
412 game_finished(int deal)
415 (void) printw("You finished the game in %d deals. This is ", deal);
418 (void) addstr("excellent");
420 (void) addstr("good");
422 (void) addstr("average");
424 (void) addstr("poor");
430 #if HAVE_LANGINFO_CODESET
432 * This program first appeared in ncurses in January 1995. At that point, the
433 * Linux console was able to display CP437 graphic characters, e.g., in the
434 * range 0-31. As of 2016, most Linux consoles are running with the UTF-8
435 * (partial) support. Incidentally, that makes all of the cards diamonds.
440 char *check = nl_langinfo(CODESET);
441 if (!strcmp(check, "UTF-8")) {
442 #if USE_WIDEC_SUPPORT
447 if (!strcmp(check, "IBM437") ||
448 !strcmp(check, "CP437") ||
449 !strcmp(check, "IBM850") ||
450 !strcmp(check, "CP850")) {
451 char *smacs = tigetstr("smacs");
452 char *smpch = tigetstr("smpch");
454 * The ncurses library makes this check to decide whether to allow
455 * the alternate character set for the (normally) nonprinting codes.
457 if (smacs != 0 && smpch != 0 && !strcmp(smacs, smpch)) {
465 #define use_pc_display() /* nothing */
466 #endif /* HAVE_LANGINFO_CODESET */
471 static const char *msg[] =
473 "Usage: blue [options]"
479 for (n = 0; n < SIZEOF(msg); n++)
480 fprintf(stderr, "%s\n", msg[n]);
482 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
489 main(int argc, char *argv[])
493 while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) {
497 ExitProgram(EXIT_SUCCESS);
499 usage(ch == OPTS_USAGE);
506 setlocale(LC_ALL, "");
510 InitAndCatch(initscr(), die);
513 init_pair(RED_ON_WHITE, COLOR_RED, COLOR_WHITE);
514 init_pair(BLUE_ON_WHITE, COLOR_BLUE, COLOR_WHITE);
515 init_pair(BLACK_ON_WHITE, COLOR_BLACK, COLOR_WHITE);
520 srand((unsigned) atoi(argv[1]));
522 srand((unsigned) time((time_t *) 0));
530 display_cards(deal_number);
534 ((deck_size = collect_discards()) != 0);
536 game_finished(deal_number);
542 /* blue.c ends here */