1 /*****************************************************************************
6 * A patience game by T.A.Lister *
7 * Integral screen support by Eric S. Raymond *
9 *****************************************************************************/
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
16 * $Id: blue.c,v 1.18 1999/01/17 00:11:56 tom Exp $
19 #include <test.priv.h>
31 #define SUIT_LENGTH 13
39 #define GRID_WIDTH 14 /* 13+1 */
40 #define GRID_LENGTH 56 /* 4*(13+1) */
46 #define RED_ON_WHITE 1
47 #define BLACK_ON_WHITE 2
48 #define BLUE_ON_WHITE 3
50 static RETSIGTYPE die(int onsig) GCC_NORETURN;
52 static int deck_size = PACK_SIZE; /* initial deck */
53 static int deck[PACK_SIZE];
55 static int grid[GRID_LENGTH]; /* card layout grid */
56 static int freeptr[4]; /* free card space pointers */
58 static int deal_number=0;
60 static chtype ranks[SUIT_LENGTH][2] =
77 /* Please note, that this is a bad example.
78 Color values should not be or'ed in. This
79 only works, because the characters used here
80 are plain and have no color attribute themselves. */
82 #define OR_COLORS(value,pair) ((value) | COLOR_PAIR(pair))
84 #define OR_COLORS(value,pair) (value)
87 #define PC_COLORS(value,pair) (OR_COLORS(value,pair) | A_ALTCHARSET)
89 static chtype letters[4] =
91 OR_COLORS('h', RED_ON_WHITE), /* hearts */
92 OR_COLORS('s', BLACK_ON_WHITE), /* spades */
93 OR_COLORS('d', RED_ON_WHITE), /* diamonds */
94 OR_COLORS('c', BLACK_ON_WHITE), /* clubs */
98 static chtype glyphs[] =
100 PC_COLORS('\003', RED_ON_WHITE), /* hearts */
101 PC_COLORS('\006', BLACK_ON_WHITE), /* spades */
102 PC_COLORS('\004', RED_ON_WHITE), /* diamonds */
103 PC_COLORS('\005', BLACK_ON_WHITE), /* clubs */
105 #endif /* __i386__ */
107 static chtype *suits = letters; /* this may change to glyphs below */
109 static RETSIGTYPE die(int onsig)
111 (void) signal(onsig, SIG_IGN);
116 static void init_vars(void)
120 deck_size = PACK_SIZE;
121 for (i=0; i < PACK_SIZE; i++)
123 for (i = 0; i < 4; i++)
124 freeptr[i]=i * GRID_WIDTH;
127 static void shuffle(int size)
129 int i,j,numswaps,swapnum,temp;
131 numswaps=size*10; /* an arbitrary figure */
133 for (swapnum=0;swapnum<numswaps;swapnum++)
143 static void deal_cards(void)
145 int ptr, card=0, value, csuit, crank, suit, aces[4];
147 for (suit=HEARTS;suit<=CLUBS;suit++)
150 grid[ptr++]=NOCARD; /* 1st card space is blank */
151 while ((ptr % GRID_WIDTH) != 0)
154 crank=value % SUIT_LENGTH;
155 csuit=value / SUIT_LENGTH;
162 if (deal_number==1) /* shift the aces down to the 1st column */
163 for (suit=HEARTS;suit<=CLUBS;suit++)
165 grid[suit * GRID_WIDTH] = suit * SUIT_LENGTH;
166 grid[aces[suit]]=NOCARD;
167 freeptr[suit]=aces[suit];
171 static void printcard(int value)
178 addch(ranks[value % SUIT_LENGTH][0] | COLOR_PAIR(BLUE_ON_WHITE));
179 addch(ranks[value % SUIT_LENGTH][1] | COLOR_PAIR(BLUE_ON_WHITE));
180 addch(suits[value / SUIT_LENGTH]);
185 static void display_cards(int deal)
191 "Blue Moon 2.1 - by Tim Lister & Eric Raymond - Deal %d.\n",
193 for(row=HEARTS;row<=CLUBS;row++)
195 move(BASEROW + row + row + 2, 1);
196 for(card=0;card<GRID_WIDTH;card++)
197 printcard(grid[row * GRID_WIDTH + card]);
200 move(PROMPTROW + 2, 0); refresh();
201 #define P(x) (void)printw("%s\n", x)
202 P(" This 52-card solitaire starts with the entire deck shuffled and dealt");
203 P("out in four rows. The aces are then moved to the left end of the layout,");
204 P("making 4 initial free spaces. You may move to a space only the card that");
205 P("matches the left neighbor in suit, and is one greater in rank. Kings are");
206 P("high, so no cards may be placed to their right (they create dead spaces).");
207 P(" When no moves can be made, cards still out of sequence are reshuffled");
208 P("and dealt face up after the ends of the partial sequences, leaving a card");
209 P("space after each sequence, so that each row looks like a partial sequence");
210 P("followed by a space, followed by enough cards to make a row of 14. ");
211 P(" A moment's reflection will show that this game cannot take more than 13");
212 P("deals. A good score is 1-3 deals, 4-7 is average, 8 or more is poor. ");
217 static int find(int card)
221 if ((card<0) || (card>=PACK_SIZE))
223 for(i = 0; i < GRID_LENGTH; i++)
229 static void movecard(int src, int dst)
234 move( BASEROW + (dst / GRID_WIDTH)*2+2, (dst % GRID_WIDTH)*5 + 1);
235 printcard(grid[dst]);
237 move( BASEROW + (src / GRID_WIDTH)*2+2, (src % GRID_WIDTH)*5 + 1);
238 printcard(grid[src]);
243 static void play_game(void)
247 int selection[4], card;
254 card=grid[freeptr[i]-1];
256 if ( ((card % SUIT_LENGTH)==KING)
261 selection[i]=find(card+1);
263 if (selection[i]==NOCARD)
269 char live[NSUITS+1], *lp = live;
273 if (selection[i] != NOCARD)
275 move(BASEROW + (selection[i] / GRID_WIDTH)*2+3,
276 (selection[i] % GRID_WIDTH)*5);
277 (void)printw(" %c ", *lp++ = 'a' + i);
282 if (strlen(live) == 1)
286 "Making forced moves... ");
296 "Type [%s] to move, r to redraw, q or INTR to quit: ",
302 move(PROMPTROW, (int)strlen(buf));
306 (((c = getch())<'a' || c>'d') && (c!='r') && (c!='q'));
309 for (j = 0; j < 4; j++)
310 if (selection[j]!=NOCARD)
312 move(BASEROW + (selection[j] / GRID_WIDTH)*2+3,
313 (selection[j] % GRID_WIDTH)*5);
318 display_cards(deal_number);
324 if (selection[i] == NOCARD)
328 movecard(selection[i], freeptr[i]);
329 freeptr[i]=selection[i];
337 (void)printw("Finished deal %d - type any character to continue...", deal_number);
342 static int collect_discards(void)
344 int row, col, cardno=0, finish, gridno;
346 for (row=HEARTS;row<=CLUBS;row++)
349 for (col=1;col<GRID_WIDTH;col++)
351 gridno=row * GRID_WIDTH + col;
353 if ((grid[gridno]!=(grid[gridno-1]+1))&&(finish==0))
359 if ((finish!=0)&&(grid[gridno]!=NOCARD))
360 deck[cardno++]=grid[gridno];
366 static void game_finished(int deal)
369 (void)printw("You finished the game in %d deals. This is ",deal);
372 (void)addstr("excellent");
374 (void)addstr("good");
376 (void)addstr("average");
378 (void)addstr("poor");
384 int main(int argc, char *argv[])
386 (void) signal(SIGINT, die);
390 * We use COLOR_GREEN because COLOR_BLACK is wired to the wrong thing.
393 init_pair(RED_ON_WHITE, COLOR_RED, COLOR_WHITE);
394 init_pair(BLUE_ON_WHITE, COLOR_BLUE, COLOR_WHITE);
395 init_pair(BLACK_ON_WHITE, COLOR_BLACK, COLOR_WHITE);
398 letters[0] = OR_COLORS('h', RED_ON_WHITE); /* hearts */
399 letters[1] = OR_COLORS('s', BLACK_ON_WHITE); /* spades */
400 letters[2] = OR_COLORS('d', RED_ON_WHITE); /* diamonds */
401 letters[3] = OR_COLORS('c', BLACK_ON_WHITE); /* clubs */
402 #if defined(__i386__) && defined(A_ALTCHARSET)
403 glyphs[0] = PC_COLORS('\003', RED_ON_WHITE); /* hearts */
404 glyphs[1] = PC_COLORS('\006', BLACK_ON_WHITE); /* spades */
405 glyphs[2] = PC_COLORS('\004', RED_ON_WHITE); /* diamonds */
406 glyphs[3] = PC_COLORS('\005', BLACK_ON_WHITE); /* clubs */
410 #if defined(__i386__) && defined(A_ALTCHARSET)
411 if (tigetstr("smpch"))
413 #endif /* __i386__ && A_ALTCHARSET */
418 srand((unsigned)atoi(argv[1]));
420 srand((unsigned)time((time_t *)0));
428 display_cards(deal_number);
432 ((deck_size=collect_discards()) != 0);
434 game_finished(deal_number);
440 /* blue.c ends here */