]> git.gir.st - solVItaire.git/blob - sol.h
join: in SPIDER, prefer completing a stack
[solVItaire.git] / sol.h
1 #ifndef __SOL_H__
2 #define __SOL_H__
3
4 // enums and constants {{{
5 #define DECK_SIZE 52
6 #ifdef KLONDIKE
7 #define NUM_PILES 7
8 #define MAX_HIDDEN 6 /*how many cards are turned over at most in a tableu pile*/
9 #define MAX_STOCK 24 /*how many cards can be in the stock at most (=@start)*/
10 #define NUM_DECKS 1
11 #define PILE_SIZE MAX_HIDDEN+NUM_RANKS
12 #elif defined SPIDER
13 #define MAX_HIDDEN 5
14 #define NUM_PILES 10
15 #define MAX_STOCK 50 /*how many cards can be dealt onto the piles*/
16 #define NUM_DECKS 2
17 #define PILE_SIZE DECK_SIZE*NUM_DECKS /* no maximum stack size in spider :/ */
18 #endif
19
20 enum cards {
21 NO_CARD,
22 CLU_A, DIA_A, HEA_A, SPA_A,
23 CLU_2, DIA_2, HEA_2, SPA_2,
24 CLU_3, DIA_3, HEA_3, SPA_3,
25 CLU_4, DIA_4, HEA_4, SPA_4,
26 CLU_5, DIA_5, HEA_5, SPA_5,
27 CLU_6, DIA_6, HEA_6, SPA_6,
28 CLU_7, DIA_7, HEA_7, SPA_7,
29 CLU_8, DIA_8, HEA_8, SPA_8,
30 CLU_9, DIA_9, HEA_9, SPA_9,
31 CLU_X, DIA_X, HEA_X, SPA_X,
32 CLU_J, DIA_J, HEA_J, SPA_J,
33 CLU_Q, DIA_Q, HEA_Q, SPA_Q,
34 CLU_K, DIA_K, HEA_K, SPA_K,
35 _NUM_CARDS_internal
36 };
37 enum colors {
38 BLK,
39 RED,
40 NUM_COLORS
41 };
42 enum suits {
43 CLUBS,
44 DIAMONDS,
45 HEARTS,
46 SPADES,
47 NUM_SUITS
48 };
49 enum ranks {
50 RANK_A,
51 RANK_2,
52 RANK_3,
53 RANK_4,
54 RANK_5,
55 RANK_6,
56 RANK_7,
57 RANK_8,
58 RANK_9,
59 RANK_X,
60 RANK_J,
61 RANK_Q,
62 RANK_K,
63 NUM_RANKS
64 };
65
66 enum action_return {
67 OK, /*move successful*/
68 ERR, /*invalid move*/
69 WON, /*game won*/
70 };
71 enum game_states {
72 GAME_NEW,
73 GAME_WON,
74 GAME_QUIT,
75 };
76
77 /* WARN: stock must always follow immediately after `TAB_*`! */
78 #define TAB_MAX (STOCK-1)
79 enum field_places {
80 TAB_1,
81 TAB_2,
82 TAB_3,
83 TAB_4,
84 TAB_5,
85 TAB_6,
86 TAB_7,
87 #ifdef SPIDER
88 TAB_8,
89 TAB_9,
90 TAB_10,
91 STOCK,
92 #define WASTE 0 /* for action[][10] (must be valid index) */
93 #define TABLEU STOCK+1 /* for undo{.t} (value never read) */
94 #define FOUNDATION STOCK+2 /* for undo{.t} (must be unique) */
95 #elif defined KLONDIKE
96 STOCK,
97 WASTE,
98 FOUNDATION,
99 #endif
100 NUM_PLACES,
101 };
102 enum special_cmds {
103 CMD_MOVE,
104 CMD_INVAL,
105 CMD_NONE,
106 CMD_QUIT,
107 CMD_NEW,
108 CMD_AGAIN,
109 CMD_HINT,
110 CMD_JOIN,
111 CMD_UNDO,
112 };
113
114 enum event {
115 /* for getctrlseq() */
116 KEY_NULL = 0,
117 KEY_EOF = -1,
118 KEY_INVAL = -2,
119 MOUSE_ANY = -3,
120 /* for getch() */
121 MOUSE_LEFT = -4,
122 MOUSE_MIDDLE = -5,
123 MOUSE_RIGHT = -6,
124 KEY_LEFT = -7,
125 KEY_DOWN = -8,
126 KEY_UP = -9,
127 KEY_RIGHT = -10,
128 KEY_HOME = -11,
129 KEY_END = -12,
130 KEY_INS = -13,
131 KEY_PGUP = -14,
132 KEY_PGDN = -15,
133 };
134
135 enum difficulty {
136 NORMAL,
137 MEDIUM,
138 EASY,
139 };
140 //}}}
141
142 typedef signed char card_t;
143
144 struct playfield {
145 int z; /* stock size */
146 int w; /* waste; index into stock (occupied foundations in spider) */
147 card_t s[MAX_STOCK]; /* stock */
148 card_t f[NUM_DECKS*NUM_SUITS][PILE_SIZE]; /* foundation */
149 card_t t[NUM_PILES][PILE_SIZE]; /* tableu piles */
150 struct undo {
151 int f; /* pile cards were taken from */
152 int t; /* pile cards were moved to */
153 int n; /* if tableu: number of cards moved */
154 /* else: index into stock/foundation */
155 int o; /* turn_over() fired? */
156 struct undo* prev;
157 struct undo* next;
158 }* u;
159 };
160 struct opts {
161 #ifdef SPIDER
162 int m; /* difficulty mode */
163 #endif
164 unsigned short w[2]; /* terminal window rows/columns */
165 const struct scheme* s;
166 };
167 struct cursor {
168 int pile;
169 int opt; /* klondike: foundation id; spider: move nth movable card */
170 };
171 const struct cursor no_hi = {-1, -1};
172 #define NO_HI &no_hi
173
174 struct undo undo_sentinel;
175
176 // help texts {{{
177 #define SHORTHELP "%s [OPTIONS]\n"
178 #ifdef KLONDIKE
179 #define LONGHELP_SPECIFIC ""
180 #define DIRECT_ADDR_KEYHELP \
181 " 1 .. 7: directly address tableu\n" \
182 " 8,9,0 : directly address stock/waste/foundation\n"
183 #elif defined SPIDER
184 #define LONGHELP_SPECIFIC \
185 " -s(uits) <1, 2 or 4>\n"
186 #define DIRECT_ADDR_KEYHELP \
187 " 1 .. 0: directly address tableu\n"
188 #endif
189 #define LONGHELP \
190 "OPTIONS:\n" \
191 LONGHELP_SPECIFIC \
192 " -b(land colorscheme)\n" \
193 " -c(olorful colorscheme)\n" \
194 " -m(iniature colorscheme)\n" \
195 " -h(elp)\n" \
196 "\n"
197 #define KEYHELP \
198 "Keybindings:\n" \
199 " hjkl : move cursor (or cursor keys)\n" \
200 " H,M,L : move cursor to first/centre/last tableu pile (or home/ins/end)\n" \
201 " J : join to here\n" \
202 /*" K : show hint\n" */\
203 " space : select at cursor\n" \
204 " return: draw from stock\n" \
205 " :n : new game\n" \
206 " :q : quit\n" \
207 DIRECT_ADDR_KEYHELP
208 //}}}
209
210 int sol(void);
211 void quit(void);
212 int find_top(card_t* pile);
213 int first_movable(card_t* pile);
214 int turn_over(card_t* pile);
215 int check_won(void);
216 int rank_next (card_t a, card_t b);
217 int is_consecutive (card_t* pile, int pos);
218 int is_movable(card_t* pile, int n);
219 #ifdef KLONDIKE
220 card_t stack_take(void);
221 int t2f(int from, int to, int opt);
222 int w2f(int from, int to, int opt);
223 int s2w(int from, int to, int opt);
224 int w2s(int from, int to, int opt);
225 int f2t(int from, int to, int opt);
226 int w2t(int from, int to, int opt);
227 int t2t(int from, int to, int opt);
228 #elif defined SPIDER
229 int remove_if_complete (int pileno);
230 int t2t(int from, int to, int opt);
231 int s2t(int from, int to, int opt);
232 int t2f(int from, int to, int opt);
233 #endif
234 int join(int to);
235 int nop(int from, int to, int opt);
236 void cursor_left (struct cursor* cursor);
237 void cursor_down (struct cursor* cursor);
238 void cursor_up (struct cursor* cursor);
239 void cursor_right (struct cursor* cursor);
240 int get_cmd (int* from, int* to, int* opt);
241
242 int getctrlseq(unsigned char* buf);
243 int wait_mouse_up(int l, int c);
244 int getch(unsigned char* buf);
245
246 void deal(long seed);
247 void print_hi(int invert, int grey_bg, int bold, char* str);
248 void print_table(const struct cursor* active, const struct cursor* inactive);
249 void visbell (void);
250 void win_anim(void);
251 void undo_push (int f, int t, int n, int o);
252 void undo_pop (struct undo* u);
253 void free_undo (struct undo* u);
254 void screen_setup (int enable);
255 void raw_mode(int enable);
256 void signal_handler (int signum);
257 void signal_setup(void);
258
259 #endif
Imprint / Impressum