1 /****************************************************************************
2 * Copyright (c) 1998-2013,2016 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.46 2016/09/05 00:24:27 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 attr_t color = (attr_t) 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));
213 #if USE_WIDEC_SUPPORT
216 values[0] = (wchar_t) suits[which];
221 addch((chtype) suits[which]);
229 display_cards(int deal)
235 "Blue Moon 2.1 - by Tim Lister & Eric Raymond - Deal %d.\n",
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]);
243 move(PROMPTROW + 2, 0);
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. ");
266 if ((card < 0) || (card >= PACK_SIZE))
268 for (i = 0; i < GRID_LENGTH; i++)
275 movecard(int src, int dst)
277 grid[dst] = grid[src];
280 move(BASEROW + (dst / GRID_WIDTH) * 2 + 2, (dst % GRID_WIDTH) * 5 + 1);
281 printcard(grid[dst]);
283 move(BASEROW + (src / GRID_WIDTH) * 2 + 2, (src % GRID_WIDTH) * 5 + 1);
284 printcard(grid[src]);
294 int selection[4], card;
298 for (i = 0; i < 4; i++) {
299 card = grid[freeptr[i] - 1];
301 if (((card % SUIT_LENGTH) == KING)
304 selection[i] = NOCARD;
306 selection[i] = find(card + 1);
308 if (selection[i] == NOCARD)
313 char live[NSUITS + 1], *lp = live;
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)));
324 if (strlen(live) == 1) {
327 "Making forced moves... ");
334 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf))
335 "Type [%s] to move, r to redraw, q or INTR to quit: ",
341 move(PROMPTROW, (int) strlen(buf));
345 (((c = (char) getch()) < 'a' || c > 'd')
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);
358 display_cards(deal_number);
363 if (selection[i] == NOCARD)
366 movecard(selection[i], freeptr[i]);
367 freeptr[i] = selection[i];
375 (void) printw("Finished deal %d - type any character to continue...", deal_number);
381 collect_discards(void)
383 int row, col, cardno = 0, finish, gridno;
385 for (row = HEARTS; row <= CLUBS; row++) {
387 for (col = 1; col < GRID_WIDTH; col++) {
388 gridno = row * GRID_WIDTH + col;
390 if ((grid[gridno] != (grid[gridno - 1] + 1)) && (finish == 0)) {
392 freeptr[row] = gridno;
395 if ((finish != 0) && (grid[gridno] != NOCARD))
396 deck[cardno++] = grid[gridno];
403 game_finished(int deal)
406 (void) printw("You finished the game in %d deals. This is ", deal);
409 (void) addstr("excellent");
411 (void) addstr("good");
413 (void) addstr("average");
415 (void) addstr("poor");
421 #if HAVE_LANGINFO_CODESET
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.
431 char *check = nl_langinfo(CODESET);
432 if (!strcmp(check, "UTF-8")) {
433 #if USE_WIDEC_SUPPORT
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");
445 * The ncurses library makes this check to decide whether to allow
446 * the alternate character set for the (normally) nonprinting codes.
448 if (smacs != 0 && smpch != 0 && !strcmp(smacs, smpch)) {
456 #define use_pc_display() /* nothing */
457 #endif /* HAVE_LANGINFO_CODESET */
460 main(int argc, char *argv[])
464 setlocale(LC_ALL, "");
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);
478 srand((unsigned) atoi(argv[1]));
480 srand((unsigned) time((time_t *) 0));
488 display_cards(deal_number);
492 ((deck_size = collect_discards()) != 0);
494 game_finished(deal_number);
500 /* blue.c ends here */