1 /****************************************************************************
2 * Copyright (c) 1998-2016,2017 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
28 /*****************************************************************************
33 * A patience game by T.A.Lister *
34 * Integral screen support by Eric S. Raymond *
36 *****************************************************************************/
39 * $Id: blue.c,v 1.51 2017/09/30 17:43:18 tom Exp $
42 #include <test.priv.h>
46 #if HAVE_LANGINFO_CODESET
54 #define SUIT_LENGTH 13
62 #define GRID_WIDTH 14 /* 13+1 */
63 #define GRID_LENGTH 56 /* 4*(13+1) */
69 #define RED_ON_WHITE 1
70 #define BLACK_ON_WHITE 2
71 #define BLUE_ON_WHITE 3
73 static void die(int onsig) GCC_NORETURN;
75 static int deck_size = PACK_SIZE; /* initial deck */
76 static int deck[PACK_SIZE];
78 static int grid[GRID_LENGTH]; /* card layout grid */
79 static int freeptr[4]; /* free card space pointers */
81 static int deal_number = 0;
83 static chtype ranks[SUIT_LENGTH][2] =
100 static int letters[4] =
108 #if HAVE_LANGINFO_CODESET
111 static int glyphs[] =
115 '\004', /* diamonds */
120 #if USE_WIDEC_SUPPORT
121 static int uglyphs[] =
125 0x2666, /* diamonds */
129 #endif /* HAVE_LANGINFO_CODESET */
131 static int *suits = letters; /* this may change to glyphs below */
136 (void) signal(onsig, SIG_IGN);
138 ExitProgram(EXIT_SUCCESS);
146 deck_size = PACK_SIZE;
147 for (i = 0; i < PACK_SIZE; i++)
149 for (i = 0; i < 4; i++)
150 freeptr[i] = i * GRID_WIDTH;
156 int i, j, numswaps, swapnum, temp;
158 numswaps = size * 10; /* an arbitrary figure */
160 for (swapnum = 0; swapnum < numswaps; swapnum++) {
172 int ptr, card = 0, value, csuit, crank, suit, aces[4];
174 memset(aces, 0, sizeof(aces));
175 for (suit = HEARTS; suit <= CLUBS; 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;
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];
200 if (value == NOCARD) {
203 int which = (value / SUIT_LENGTH);
204 int isuit = (value % SUIT_LENGTH);
205 chtype color = (chtype) COLOR_PAIR(((which % 2) == 0)
209 AddCh(ranks[isuit][0] | (chtype) COLOR_PAIR(BLUE_ON_WHITE));
210 AddCh(ranks[isuit][1] | (chtype) COLOR_PAIR(BLUE_ON_WHITE));
212 #ifdef NCURSES_VERSION
213 (attron) ((int) color); /* quieter compiler warnings */
215 attron(color); /* PDCurses, etc., either no macro or wrong */
217 #if USE_WIDEC_SUPPORT
220 values[0] = (wchar_t) suits[which];
227 #ifdef NCURSES_VERSION
228 (attroff) ((int) color);
237 display_cards(int deal)
243 "Blue Moon 2.1 - by Tim Lister & Eric Raymond - Deal %d.\n",
245 for (row = HEARTS; row <= CLUBS; row++) {
246 move(BASEROW + row + row + 2, 1);
247 for (card = 0; card < GRID_WIDTH; card++)
248 printcard(grid[row * GRID_WIDTH + card]);
251 move(PROMPTROW + 2, 0);
253 #define P(x) (void)printw("%s\n", x)
254 P(" This 52-card solitaire starts with the entire deck shuffled and dealt");
255 P("out in four rows. The aces are then moved to the left end of the layout,");
256 P("making 4 initial free spaces. You may move to a space only the card that");
257 P("matches the left neighbor in suit, and is one greater in rank. Kings are");
258 P("high, so no cards may be placed to their right (they create dead spaces).");
259 P(" When no moves can be made, cards still out of sequence are reshuffled");
260 P("and dealt face up after the ends of the partial sequences, leaving a card");
261 P("space after each sequence, so that each row looks like a partial sequence");
262 P("followed by a space, followed by enough cards to make a row of 14. ");
263 P(" A moment's reflection will show that this game cannot take more than 13");
264 P("deals. A good score is 1-3 deals, 4-7 is average, 8 or more is poor. ");
274 if ((card < 0) || (card >= PACK_SIZE))
276 for (i = 0; i < GRID_LENGTH; i++)
283 movecard(int src, int dst)
285 grid[dst] = grid[src];
288 move(BASEROW + (dst / GRID_WIDTH) * 2 + 2, (dst % GRID_WIDTH) * 5 + 1);
289 printcard(grid[dst]);
291 move(BASEROW + (src / GRID_WIDTH) * 2 + 2, (src % GRID_WIDTH) * 5 + 1);
292 printcard(grid[src]);
302 int selection[4], card;
306 for (i = 0; i < 4; i++) {
307 card = grid[freeptr[i] - 1];
309 if (((card % SUIT_LENGTH) == KING)
312 selection[i] = NOCARD;
314 selection[i] = find(card + 1);
316 if (selection[i] == NOCARD)
321 char live[NSUITS + 1], *lp = live;
323 for (i = 0; i < 4; i++) {
324 if (selection[i] != NOCARD) {
325 move(BASEROW + (selection[i] / GRID_WIDTH) * 2 + 3,
326 (selection[i] % GRID_WIDTH) * 5);
327 (void) printw(" %c ", (*lp++ = (char) ('a' + i)));
332 if (strlen(live) == 1) {
335 "Making forced moves... ");
342 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf))
343 "Type [%s] to move, r to redraw, q or INTR to quit: ",
349 move(PROMPTROW, (int) strlen(buf));
353 (((c = (char) getch()) < 'a' || c > 'd')
358 for (j = 0; j < 4; j++)
359 if (selection[j] != NOCARD) {
360 move(BASEROW + (selection[j] / GRID_WIDTH) * 2 + 3,
361 (selection[j] % GRID_WIDTH) * 5);
366 display_cards(deal_number);
371 if (selection[i] == NOCARD)
374 movecard(selection[i], freeptr[i]);
375 freeptr[i] = selection[i];
383 (void) printw("Finished deal %d - type any character to continue...", deal_number);
389 collect_discards(void)
391 int row, col, cardno = 0, finish, gridno;
393 for (row = HEARTS; row <= CLUBS; row++) {
395 for (col = 1; col < GRID_WIDTH; col++) {
396 gridno = row * GRID_WIDTH + col;
398 if ((grid[gridno] != (grid[gridno - 1] + 1)) && (finish == 0)) {
400 freeptr[row] = gridno;
403 if ((finish != 0) && (grid[gridno] != NOCARD))
404 deck[cardno++] = grid[gridno];
411 game_finished(int deal)
414 (void) printw("You finished the game in %d deals. This is ", deal);
417 (void) addstr("excellent");
419 (void) addstr("good");
421 (void) addstr("average");
423 (void) addstr("poor");
429 #if HAVE_LANGINFO_CODESET
431 * This program first appeared in ncurses in January 1995. At that point, the
432 * Linux console was able to display CP437 graphic characters, e.g., in the
433 * range 0-31. As of 2016, most Linux consoles are running with the UTF-8
434 * (partial) support. Incidentally, that makes all of the cards diamonds.
439 char *check = nl_langinfo(CODESET);
440 if (!strcmp(check, "UTF-8")) {
441 #if USE_WIDEC_SUPPORT
446 if (!strcmp(check, "IBM437") ||
447 !strcmp(check, "CP437") ||
448 !strcmp(check, "IBM850") ||
449 !strcmp(check, "CP850")) {
450 char *smacs = tigetstr("smacs");
451 char *smpch = tigetstr("smpch");
453 * The ncurses library makes this check to decide whether to allow
454 * the alternate character set for the (normally) nonprinting codes.
456 if (smacs != 0 && smpch != 0 && !strcmp(smacs, smpch)) {
464 #define use_pc_display() /* nothing */
465 #endif /* HAVE_LANGINFO_CODESET */
468 main(int argc, char *argv[])
470 setlocale(LC_ALL, "");
474 InitAndCatch(initscr(), die);
477 init_pair(RED_ON_WHITE, COLOR_RED, COLOR_WHITE);
478 init_pair(BLUE_ON_WHITE, COLOR_BLUE, COLOR_WHITE);
479 init_pair(BLACK_ON_WHITE, COLOR_BLACK, COLOR_WHITE);
484 srand((unsigned) atoi(argv[1]));
486 srand((unsigned) time((time_t *) 0));
494 display_cards(deal_number);
498 ((deck_size = collect_discards()) != 0);
500 game_finished(deal_number);
506 /* blue.c ends here */