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