1 /****************************************************************************
2 * Copyright (c) 1998-2008,2009 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 * 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
43 * $Id: blue.c,v 1.33 2009/10/24 21:03:35 tom Exp $
46 #include <test.priv.h>
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 RETSIGTYPE
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 /* 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. */
105 #define OR_COLORS(value,pair) ((value) | COLOR_PAIR(pair))
107 #define OR_COLORS(value,pair) (value)
110 #define PC_COLORS(value,pair) (OR_COLORS(value,pair) | A_ALTCHARSET)
112 static chtype letters
[4] =
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 */
120 #if defined(__i386__) && defined(A_ALTCHARSET) && HAVE_TIGETSTR
121 static chtype glyphs
[] =
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 */
131 #endif /* __i386__ */
133 static chtype
*suits
= letters
; /* this may change to glyphs below */
138 (void) signal(onsig
, SIG_IGN
);
140 ExitProgram(EXIT_SUCCESS
);
148 deck_size
= PACK_SIZE
;
149 for (i
= 0; i
< PACK_SIZE
; i
++)
151 for (i
= 0; i
< 4; i
++)
152 freeptr
[i
] = i
* GRID_WIDTH
;
158 int i
, j
, numswaps
, swapnum
, temp
;
160 numswaps
= size
* 10; /* an arbitrary figure */
162 for (swapnum
= 0; swapnum
< numswaps
; swapnum
++) {
174 int ptr
, card
= 0, value
, csuit
, crank
, suit
, aces
[4];
176 memset(aces
, 0, sizeof(aces
));
177 for (suit
= HEARTS
; suit
<= CLUBS
; suit
++) {
179 grid
[ptr
++] = NOCARD
; /* 1st card space is blank */
180 while ((ptr
% GRID_WIDTH
) != 0) {
181 value
= deck
[card
++];
182 crank
= value
% SUIT_LENGTH
;
183 csuit
= value
/ SUIT_LENGTH
;
190 if (deal_number
== 1) /* shift the aces down to the 1st column */
191 for (suit
= HEARTS
; suit
<= CLUBS
; suit
++) {
192 grid
[suit
* GRID_WIDTH
] = suit
* SUIT_LENGTH
;
193 grid
[aces
[suit
]] = NOCARD
;
194 freeptr
[suit
] = aces
[suit
];
205 addch(ranks
[value
% SUIT_LENGTH
][0] | COLOR_PAIR(BLUE_ON_WHITE
));
206 addch(ranks
[value
% SUIT_LENGTH
][1] | COLOR_PAIR(BLUE_ON_WHITE
));
207 addch(suits
[value
/ SUIT_LENGTH
]);
213 display_cards(int deal
)
219 "Blue Moon 2.1 - by Tim Lister & Eric Raymond - Deal %d.\n",
221 for (row
= HEARTS
; row
<= CLUBS
; row
++) {
222 move(BASEROW
+ row
+ row
+ 2, 1);
223 for (card
= 0; card
< GRID_WIDTH
; card
++)
224 printcard(grid
[row
* GRID_WIDTH
+ card
]);
227 move(PROMPTROW
+ 2, 0);
229 #define P(x) (void)printw("%s\n", x)
230 P(" This 52-card solitaire starts with the entire deck shuffled and dealt");
231 P("out in four rows. The aces are then moved to the left end of the layout,");
232 P("making 4 initial free spaces. You may move to a space only the card that");
233 P("matches the left neighbor in suit, and is one greater in rank. Kings are");
234 P("high, so no cards may be placed to their right (they create dead spaces).");
235 P(" When no moves can be made, cards still out of sequence are reshuffled");
236 P("and dealt face up after the ends of the partial sequences, leaving a card");
237 P("space after each sequence, so that each row looks like a partial sequence");
238 P("followed by a space, followed by enough cards to make a row of 14. ");
239 P(" A moment's reflection will show that this game cannot take more than 13");
240 P("deals. A good score is 1-3 deals, 4-7 is average, 8 or more is poor. ");
250 if ((card
< 0) || (card
>= PACK_SIZE
))
252 for (i
= 0; i
< GRID_LENGTH
; i
++)
259 movecard(int src
, int dst
)
261 grid
[dst
] = grid
[src
];
264 move(BASEROW
+ (dst
/ GRID_WIDTH
) * 2 + 2, (dst
% GRID_WIDTH
) * 5 + 1);
265 printcard(grid
[dst
]);
267 move(BASEROW
+ (src
/ GRID_WIDTH
) * 2 + 2, (src
% GRID_WIDTH
) * 5 + 1);
268 printcard(grid
[src
]);
278 int selection
[4], card
;
282 for (i
= 0; i
< 4; i
++) {
283 card
= grid
[freeptr
[i
] - 1];
285 if (((card
% SUIT_LENGTH
) == KING
)
288 selection
[i
] = NOCARD
;
290 selection
[i
] = find(card
+ 1);
292 if (selection
[i
] == NOCARD
)
297 char live
[NSUITS
+ 1], *lp
= live
;
299 for (i
= 0; i
< 4; i
++) {
300 if (selection
[i
] != NOCARD
) {
301 move(BASEROW
+ (selection
[i
] / GRID_WIDTH
) * 2 + 3,
302 (selection
[i
] % GRID_WIDTH
) * 5);
303 (void) printw(" %c ", (*lp
++ = (char) ('a' + i
)));
308 if (strlen(live
) == 1) {
311 "Making forced moves... ");
319 "Type [%s] to move, r to redraw, q or INTR to quit: ",
325 move(PROMPTROW
, (int) strlen(buf
));
329 (((c
= (char) getch()) < 'a' || c
> 'd')
334 for (j
= 0; j
< 4; j
++)
335 if (selection
[j
] != NOCARD
) {
336 move(BASEROW
+ (selection
[j
] / GRID_WIDTH
) * 2 + 3,
337 (selection
[j
] % GRID_WIDTH
) * 5);
342 display_cards(deal_number
);
347 if (selection
[i
] == NOCARD
)
350 movecard(selection
[i
], freeptr
[i
]);
351 freeptr
[i
] = selection
[i
];
359 (void) printw("Finished deal %d - type any character to continue...", deal_number
);
365 collect_discards(void)
367 int row
, col
, cardno
= 0, finish
, gridno
;
369 for (row
= HEARTS
; row
<= CLUBS
; row
++) {
371 for (col
= 1; col
< GRID_WIDTH
; col
++) {
372 gridno
= row
* GRID_WIDTH
+ col
;
374 if ((grid
[gridno
] != (grid
[gridno
- 1] + 1)) && (finish
== 0)) {
376 freeptr
[row
] = gridno
;
379 if ((finish
!= 0) && (grid
[gridno
] != NOCARD
))
380 deck
[cardno
++] = grid
[gridno
];
387 game_finished(int deal
)
390 (void) printw("You finished the game in %d deals. This is ", deal
);
393 (void) addstr("excellent");
395 (void) addstr("good");
397 (void) addstr("average");
399 (void) addstr("poor");
406 main(int argc
, char *argv
[])
410 setlocale(LC_ALL
, "");
415 * We use COLOR_GREEN because COLOR_BLACK is wired to the wrong thing.
418 init_pair(RED_ON_WHITE
, COLOR_RED
, COLOR_WHITE
);
419 init_pair(BLUE_ON_WHITE
, COLOR_BLUE
, COLOR_WHITE
);
420 init_pair(BLACK_ON_WHITE
, COLOR_BLACK
, COLOR_WHITE
);
423 letters
[0] = OR_COLORS('h', RED_ON_WHITE
); /* hearts */
424 letters
[1] = OR_COLORS('s', BLACK_ON_WHITE
); /* spades */
425 letters
[2] = OR_COLORS('d', RED_ON_WHITE
); /* diamonds */
426 letters
[3] = OR_COLORS('c', BLACK_ON_WHITE
); /* clubs */
428 glyphs
[0] = PC_COLORS('\003', RED_ON_WHITE
); /* hearts */
429 glyphs
[1] = PC_COLORS('\006', BLACK_ON_WHITE
); /* spades */
430 glyphs
[2] = PC_COLORS('\004', RED_ON_WHITE
); /* diamonds */
431 glyphs
[3] = PC_COLORS('\005', BLACK_ON_WHITE
); /* clubs */
436 if (tigetstr("smpch"))
438 #endif /* USE_CP437 */
443 srand((unsigned) atoi(argv
[1]));
445 srand((unsigned) time((time_t *) 0));
453 display_cards(deal_number
);
457 ((deck_size
= collect_discards()) != 0);
459 game_finished(deal_number
);
465 /* blue.c ends here */