1 | /*****************************************************************************
|
---|
2 | * *
|
---|
3 | * B l u e M o o n *
|
---|
4 | * ================= *
|
---|
5 | * V2.2 *
|
---|
6 | * A patience game by T.A.Lister *
|
---|
7 | * Integral screen support by Eric S. Raymond *
|
---|
8 | * *
|
---|
9 | *****************************************************************************/
|
---|
10 |
|
---|
11 | /*
|
---|
12 | * Compile this with the command `cc -O blue.c -lcurses -o blue'. For best
|
---|
13 | * results, use the ncurses(3) library. On non-Intel machines, SVr4 curses is
|
---|
14 | * just as good.
|
---|
15 | *
|
---|
16 | * $Id: blue.c,v 1.25 2005/05/28 21:38:03 tom Exp $
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include <test.priv.h>
|
---|
20 |
|
---|
21 | #include <time.h>
|
---|
22 |
|
---|
23 | #define NOCARD (-1)
|
---|
24 |
|
---|
25 | #define ACE 0
|
---|
26 | #define KING 12
|
---|
27 | #define SUIT_LENGTH 13
|
---|
28 |
|
---|
29 | #define HEARTS 0
|
---|
30 | #define SPADES 1
|
---|
31 | #define DIAMONDS 2
|
---|
32 | #define CLUBS 3
|
---|
33 | #define NSUITS 4
|
---|
34 |
|
---|
35 | #define GRID_WIDTH 14 /* 13+1 */
|
---|
36 | #define GRID_LENGTH 56 /* 4*(13+1) */
|
---|
37 | #define PACK_SIZE 52
|
---|
38 |
|
---|
39 | #define BASEROW 1
|
---|
40 | #define PROMPTROW 11
|
---|
41 |
|
---|
42 | #define RED_ON_WHITE 1
|
---|
43 | #define BLACK_ON_WHITE 2
|
---|
44 | #define BLUE_ON_WHITE 3
|
---|
45 |
|
---|
46 | static RETSIGTYPE die(int onsig) GCC_NORETURN;
|
---|
47 |
|
---|
48 | static int deck_size = PACK_SIZE; /* initial deck */
|
---|
49 | static int deck[PACK_SIZE];
|
---|
50 |
|
---|
51 | static int grid[GRID_LENGTH]; /* card layout grid */
|
---|
52 | static int freeptr[4]; /* free card space pointers */
|
---|
53 |
|
---|
54 | static int deal_number = 0;
|
---|
55 |
|
---|
56 | static chtype ranks[SUIT_LENGTH][2] =
|
---|
57 | {
|
---|
58 | {' ', 'A'},
|
---|
59 | {' ', '2'},
|
---|
60 | {' ', '3'},
|
---|
61 | {' ', '4'},
|
---|
62 | {' ', '5'},
|
---|
63 | {' ', '6'},
|
---|
64 | {' ', '7'},
|
---|
65 | {' ', '8'},
|
---|
66 | {' ', '9'},
|
---|
67 | {'1', '0'},
|
---|
68 | {' ', 'J'},
|
---|
69 | {' ', 'Q'},
|
---|
70 | {' ', 'K'}
|
---|
71 | };
|
---|
72 |
|
---|
73 | /* Please note, that this is a bad example.
|
---|
74 | Color values should not be or'ed in. This
|
---|
75 | only works, because the characters used here
|
---|
76 | are plain and have no color attribute themselves. */
|
---|
77 | #ifdef COLOR_PAIR
|
---|
78 | #define OR_COLORS(value,pair) ((value) | COLOR_PAIR(pair))
|
---|
79 | #else
|
---|
80 | #define OR_COLORS(value,pair) (value)
|
---|
81 | #endif
|
---|
82 |
|
---|
83 | #define PC_COLORS(value,pair) (OR_COLORS(value,pair) | A_ALTCHARSET)
|
---|
84 |
|
---|
85 | static chtype letters[4] =
|
---|
86 | {
|
---|
87 | OR_COLORS('h', RED_ON_WHITE), /* hearts */
|
---|
88 | OR_COLORS('s', BLACK_ON_WHITE), /* spades */
|
---|
89 | OR_COLORS('d', RED_ON_WHITE), /* diamonds */
|
---|
90 | OR_COLORS('c', BLACK_ON_WHITE), /* clubs */
|
---|
91 | };
|
---|
92 |
|
---|
93 | #if defined(__i386__)
|
---|
94 | static chtype glyphs[] =
|
---|
95 | {
|
---|
96 | PC_COLORS('\003', RED_ON_WHITE), /* hearts */
|
---|
97 | PC_COLORS('\006', BLACK_ON_WHITE), /* spades */
|
---|
98 | PC_COLORS('\004', RED_ON_WHITE), /* diamonds */
|
---|
99 | PC_COLORS('\005', BLACK_ON_WHITE), /* clubs */
|
---|
100 | };
|
---|
101 | #endif /* __i386__ */
|
---|
102 |
|
---|
103 | static chtype *suits = letters; /* this may change to glyphs below */
|
---|
104 |
|
---|
105 | static RETSIGTYPE
|
---|
106 | die(int onsig)
|
---|
107 | {
|
---|
108 | (void) signal(onsig, SIG_IGN);
|
---|
109 | endwin();
|
---|
110 | ExitProgram(EXIT_SUCCESS);
|
---|
111 | }
|
---|
112 |
|
---|
113 | static void
|
---|
114 | init_vars(void)
|
---|
115 | {
|
---|
116 | int i;
|
---|
117 |
|
---|
118 | deck_size = PACK_SIZE;
|
---|
119 | for (i = 0; i < PACK_SIZE; i++)
|
---|
120 | deck[i] = i;
|
---|
121 | for (i = 0; i < 4; i++)
|
---|
122 | freeptr[i] = i * GRID_WIDTH;
|
---|
123 | }
|
---|
124 |
|
---|
125 | static void
|
---|
126 | shuffle(int size)
|
---|
127 | {
|
---|
128 | int i, j, numswaps, swapnum, temp;
|
---|
129 |
|
---|
130 | numswaps = size * 10; /* an arbitrary figure */
|
---|
131 |
|
---|
132 | for (swapnum = 0; swapnum < numswaps; swapnum++) {
|
---|
133 | i = rand() % size;
|
---|
134 | j = rand() % size;
|
---|
135 | temp = deck[i];
|
---|
136 | deck[i] = deck[j];
|
---|
137 | deck[j] = temp;
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | static void
|
---|
142 | deal_cards(void)
|
---|
143 | {
|
---|
144 | int ptr, card = 0, value, csuit, crank, suit, aces[4];
|
---|
145 |
|
---|
146 | for (suit = HEARTS; suit <= CLUBS; suit++) {
|
---|
147 | ptr = freeptr[suit];
|
---|
148 | grid[ptr++] = NOCARD; /* 1st card space is blank */
|
---|
149 | while ((ptr % GRID_WIDTH) != 0) {
|
---|
150 | value = deck[card++];
|
---|
151 | crank = value % SUIT_LENGTH;
|
---|
152 | csuit = value / SUIT_LENGTH;
|
---|
153 | if (crank == ACE)
|
---|
154 | aces[csuit] = ptr;
|
---|
155 | grid[ptr++] = value;
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | if (deal_number == 1) /* shift the aces down to the 1st column */
|
---|
160 | for (suit = HEARTS; suit <= CLUBS; suit++) {
|
---|
161 | grid[suit * GRID_WIDTH] = suit * SUIT_LENGTH;
|
---|
162 | grid[aces[suit]] = NOCARD;
|
---|
163 | freeptr[suit] = aces[suit];
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | static void
|
---|
168 | printcard(int value)
|
---|
169 | {
|
---|
170 | (void) addch(' ');
|
---|
171 | if (value == NOCARD)
|
---|
172 | (void) addstr(" ");
|
---|
173 | else {
|
---|
174 | addch(ranks[value % SUIT_LENGTH][0] | COLOR_PAIR(BLUE_ON_WHITE));
|
---|
175 | addch(ranks[value % SUIT_LENGTH][1] | COLOR_PAIR(BLUE_ON_WHITE));
|
---|
176 | addch(suits[value / SUIT_LENGTH]);
|
---|
177 | }
|
---|
178 | (void) addch(' ');
|
---|
179 | }
|
---|
180 |
|
---|
181 | static void
|
---|
182 | display_cards(int deal)
|
---|
183 | {
|
---|
184 | int row, card;
|
---|
185 |
|
---|
186 | clear();
|
---|
187 | (void) printw(
|
---|
188 | "Blue Moon 2.1 - by Tim Lister & Eric Raymond - Deal %d.\n",
|
---|
189 | deal);
|
---|
190 | for (row = HEARTS; row <= CLUBS; row++) {
|
---|
191 | move(BASEROW + row + row + 2, 1);
|
---|
192 | for (card = 0; card < GRID_WIDTH; card++)
|
---|
193 | printcard(grid[row * GRID_WIDTH + card]);
|
---|
194 | }
|
---|
195 |
|
---|
196 | move(PROMPTROW + 2, 0);
|
---|
197 | refresh();
|
---|
198 | #define P(x) (void)printw("%s\n", x)
|
---|
199 | P(" This 52-card solitaire starts with the entire deck shuffled and dealt");
|
---|
200 | P("out in four rows. The aces are then moved to the left end of the layout,");
|
---|
201 | P("making 4 initial free spaces. You may move to a space only the card that");
|
---|
202 | P("matches the left neighbor in suit, and is one greater in rank. Kings are");
|
---|
203 | P("high, so no cards may be placed to their right (they create dead spaces).");
|
---|
204 | P(" When no moves can be made, cards still out of sequence are reshuffled");
|
---|
205 | P("and dealt face up after the ends of the partial sequences, leaving a card");
|
---|
206 | P("space after each sequence, so that each row looks like a partial sequence");
|
---|
207 | P("followed by a space, followed by enough cards to make a row of 14. ");
|
---|
208 | P(" A moment's reflection will show that this game cannot take more than 13");
|
---|
209 | P("deals. A good score is 1-3 deals, 4-7 is average, 8 or more is poor. ");
|
---|
210 | #undef P
|
---|
211 | refresh();
|
---|
212 | }
|
---|
213 |
|
---|
214 | static int
|
---|
215 | find(int card)
|
---|
216 | {
|
---|
217 | int i;
|
---|
218 |
|
---|
219 | if ((card < 0) || (card >= PACK_SIZE))
|
---|
220 | return (NOCARD);
|
---|
221 | for (i = 0; i < GRID_LENGTH; i++)
|
---|
222 | if (grid[i] == card)
|
---|
223 | return i;
|
---|
224 | return (NOCARD);
|
---|
225 | }
|
---|
226 |
|
---|
227 | static void
|
---|
228 | movecard(int src, int dst)
|
---|
229 | {
|
---|
230 | grid[dst] = grid[src];
|
---|
231 | grid[src] = NOCARD;
|
---|
232 |
|
---|
233 | move(BASEROW + (dst / GRID_WIDTH) * 2 + 2, (dst % GRID_WIDTH) * 5 + 1);
|
---|
234 | printcard(grid[dst]);
|
---|
235 |
|
---|
236 | move(BASEROW + (src / GRID_WIDTH) * 2 + 2, (src % GRID_WIDTH) * 5 + 1);
|
---|
237 | printcard(grid[src]);
|
---|
238 |
|
---|
239 | refresh();
|
---|
240 | }
|
---|
241 |
|
---|
242 | static void
|
---|
243 | play_game(void)
|
---|
244 | {
|
---|
245 | int dead = 0, i, j;
|
---|
246 | char c;
|
---|
247 | int selection[4], card;
|
---|
248 |
|
---|
249 | while (dead < 4) {
|
---|
250 | dead = 0;
|
---|
251 | for (i = 0; i < 4; i++) {
|
---|
252 | card = grid[freeptr[i] - 1];
|
---|
253 |
|
---|
254 | if (((card % SUIT_LENGTH) == KING)
|
---|
255 | ||
|
---|
256 | (card == NOCARD))
|
---|
257 | selection[i] = NOCARD;
|
---|
258 | else
|
---|
259 | selection[i] = find(card + 1);
|
---|
260 |
|
---|
261 | if (selection[i] == NOCARD)
|
---|
262 | dead++;
|
---|
263 | };
|
---|
264 |
|
---|
265 | if (dead < 4) {
|
---|
266 | char live[NSUITS + 1], *lp = live;
|
---|
267 |
|
---|
268 | for (i = 0; i < 4; i++) {
|
---|
269 | if (selection[i] != NOCARD) {
|
---|
270 | move(BASEROW + (selection[i] / GRID_WIDTH) * 2 + 3,
|
---|
271 | (selection[i] % GRID_WIDTH) * 5);
|
---|
272 | (void) printw(" %c ", *lp++ = 'a' + i);
|
---|
273 | }
|
---|
274 | };
|
---|
275 | *lp = '\0';
|
---|
276 |
|
---|
277 | if (strlen(live) == 1) {
|
---|
278 | move(PROMPTROW, 0);
|
---|
279 | (void) printw(
|
---|
280 | "Making forced moves... ");
|
---|
281 | refresh();
|
---|
282 | (void) sleep(1);
|
---|
283 | c = live[0];
|
---|
284 | } else {
|
---|
285 | char buf[BUFSIZ];
|
---|
286 |
|
---|
287 | (void) sprintf(buf,
|
---|
288 | "Type [%s] to move, r to redraw, q or INTR to quit: ",
|
---|
289 | live);
|
---|
290 |
|
---|
291 | do {
|
---|
292 | move(PROMPTROW, 0);
|
---|
293 | (void) addstr(buf);
|
---|
294 | move(PROMPTROW, (int) strlen(buf));
|
---|
295 | clrtoeol();
|
---|
296 | (void) addch(' ');
|
---|
297 | } while
|
---|
298 | (((c = getch()) < 'a' || c > 'd') && (c != 'r') && (c != 'q'));
|
---|
299 | }
|
---|
300 |
|
---|
301 | for (j = 0; j < 4; j++)
|
---|
302 | if (selection[j] != NOCARD) {
|
---|
303 | move(BASEROW + (selection[j] / GRID_WIDTH) * 2 + 3,
|
---|
304 | (selection[j] % GRID_WIDTH) * 5);
|
---|
305 | (void) printw(" ");
|
---|
306 | }
|
---|
307 |
|
---|
308 | if (c == 'r')
|
---|
309 | display_cards(deal_number);
|
---|
310 | else if (c == 'q')
|
---|
311 | die(SIGINT);
|
---|
312 | else {
|
---|
313 | i = c - 'a';
|
---|
314 | if (selection[i] == NOCARD)
|
---|
315 | beep();
|
---|
316 | else {
|
---|
317 | movecard(selection[i], freeptr[i]);
|
---|
318 | freeptr[i] = selection[i];
|
---|
319 | }
|
---|
320 | }
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | move(PROMPTROW, 0);
|
---|
325 | standout();
|
---|
326 | (void) printw("Finished deal %d - type any character to continue...", deal_number);
|
---|
327 | standend();
|
---|
328 | (void) getch();
|
---|
329 | }
|
---|
330 |
|
---|
331 | static int
|
---|
332 | collect_discards(void)
|
---|
333 | {
|
---|
334 | int row, col, cardno = 0, finish, gridno;
|
---|
335 |
|
---|
336 | for (row = HEARTS; row <= CLUBS; row++) {
|
---|
337 | finish = 0;
|
---|
338 | for (col = 1; col < GRID_WIDTH; col++) {
|
---|
339 | gridno = row * GRID_WIDTH + col;
|
---|
340 |
|
---|
341 | if ((grid[gridno] != (grid[gridno - 1] + 1)) && (finish == 0)) {
|
---|
342 | finish = 1;
|
---|
343 | freeptr[row] = gridno;
|
---|
344 | };
|
---|
345 |
|
---|
346 | if ((finish != 0) && (grid[gridno] != NOCARD))
|
---|
347 | deck[cardno++] = grid[gridno];
|
---|
348 | }
|
---|
349 | }
|
---|
350 | return cardno;
|
---|
351 | }
|
---|
352 |
|
---|
353 | static void
|
---|
354 | game_finished(int deal)
|
---|
355 | {
|
---|
356 | clear();
|
---|
357 | (void) printw("You finished the game in %d deals. This is ", deal);
|
---|
358 | standout();
|
---|
359 | if (deal < 2)
|
---|
360 | (void) addstr("excellent");
|
---|
361 | else if (deal < 4)
|
---|
362 | (void) addstr("good");
|
---|
363 | else if (deal < 8)
|
---|
364 | (void) addstr("average");
|
---|
365 | else
|
---|
366 | (void) addstr("poor");
|
---|
367 | standend();
|
---|
368 | (void) addstr(". ");
|
---|
369 | refresh();
|
---|
370 | }
|
---|
371 |
|
---|
372 | int
|
---|
373 | main(int argc, char *argv[])
|
---|
374 | {
|
---|
375 | (void) signal(SIGINT, die);
|
---|
376 |
|
---|
377 | setlocale(LC_ALL, "");
|
---|
378 |
|
---|
379 | initscr();
|
---|
380 |
|
---|
381 | /*
|
---|
382 | * We use COLOR_GREEN because COLOR_BLACK is wired to the wrong thing.
|
---|
383 | */
|
---|
384 | start_color();
|
---|
385 | init_pair(RED_ON_WHITE, COLOR_RED, COLOR_WHITE);
|
---|
386 | init_pair(BLUE_ON_WHITE, COLOR_BLUE, COLOR_WHITE);
|
---|
387 | init_pair(BLACK_ON_WHITE, COLOR_BLACK, COLOR_WHITE);
|
---|
388 |
|
---|
389 | #ifndef COLOR_PAIR
|
---|
390 | letters[0] = OR_COLORS('h', RED_ON_WHITE); /* hearts */
|
---|
391 | letters[1] = OR_COLORS('s', BLACK_ON_WHITE); /* spades */
|
---|
392 | letters[2] = OR_COLORS('d', RED_ON_WHITE); /* diamonds */
|
---|
393 | letters[3] = OR_COLORS('c', BLACK_ON_WHITE); /* clubs */
|
---|
394 | #if defined(__i386__) && defined(A_ALTCHARSET)
|
---|
395 | glyphs[0] = PC_COLORS('\003', RED_ON_WHITE); /* hearts */
|
---|
396 | glyphs[1] = PC_COLORS('\006', BLACK_ON_WHITE); /* spades */
|
---|
397 | glyphs[2] = PC_COLORS('\004', RED_ON_WHITE); /* diamonds */
|
---|
398 | glyphs[3] = PC_COLORS('\005', BLACK_ON_WHITE); /* clubs */
|
---|
399 | #endif
|
---|
400 | #endif
|
---|
401 |
|
---|
402 | #if defined(__i386__) && defined(A_ALTCHARSET)
|
---|
403 | if (tigetstr("smpch"))
|
---|
404 | suits = glyphs;
|
---|
405 | #endif /* __i386__ && A_ALTCHARSET */
|
---|
406 |
|
---|
407 | cbreak();
|
---|
408 |
|
---|
409 | if (argc == 2)
|
---|
410 | srand((unsigned) atoi(argv[1]));
|
---|
411 | else
|
---|
412 | srand((unsigned) time((time_t *) 0));
|
---|
413 |
|
---|
414 | init_vars();
|
---|
415 |
|
---|
416 | do {
|
---|
417 | deal_number++;
|
---|
418 | shuffle(deck_size);
|
---|
419 | deal_cards();
|
---|
420 | display_cards(deal_number);
|
---|
421 | play_game();
|
---|
422 | }
|
---|
423 | while
|
---|
424 | ((deck_size = collect_discards()) != 0);
|
---|
425 |
|
---|
426 | game_finished(deal_number);
|
---|
427 |
|
---|
428 | die(SIGINT);
|
---|
429 | /*NOTREACHED */
|
---|
430 | }
|
---|
431 |
|
---|
432 | /* blue.c ends here */
|
---|