3 * $Id: trackword.c,v 1.1 2001/02/04 17:14:42 mdw Exp $
7 * (c) 2001 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Anag: a simple wordgame helper.
14 * Anag is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Anag is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Anag; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
31 * $Log: trackword.c,v $
32 * Revision 1.1 2001/02/04 17:14:42 mdw
37 /*----- Header files ------------------------------------------------------*/
41 /*----- Data structures ---------------------------------------------------*/
43 enum { N, NE, E, SE, S, SW, W, NW, NDIR };
45 typedef struct tcell {
46 struct tcell *hop[NDIR];
51 typedef struct node_track {
58 /*----- Utility functions -------------------------------------------------*/
62 * Arguments: @long a@ = the target value
64 * Returns: %$\lfloor \sqrt{a} \rfloor$%
66 * Use: Computes an integer square root. This algorithm is
67 * Newton-Raphson with integers. I've stolen this more-or-less
68 * wholesale fom the multiprecision integer square-root function
76 /* --- Initial guess is half the input length --- */
78 for (i = q = a; q; q >>= 2)
81 /* --- Do the main series iteration --- */
85 if (!q || (q < 0 && -q <= 2 * i))
95 /*----- Main code ---------------------------------------------------------*/
99 * Arguments: @tcell *c@ = pointer to start cell
100 * @const char *p@ = string to match
102 * Returns: Nonzero for a match, zero if no match was found.
104 * Use: Searches for a match in a trackword.
107 static int track(tcell *c, const char *p)
116 for (i = 0; i < NDIR; i++) {
117 if (!c->hop[i] || c->hop[i]->f)
119 if (track(c->hop[i], p)) {
128 /* --- Node matcher --- */
130 static int n_track(node *nn, const char *p, size_t sz)
132 node_track *n = (node_track *)nn;
137 for (c = n->c; c->ch; c++) {
144 /* --- Node creation --- */
146 node *trackword(const char *const *av)
151 const char *p = av[0], *q, *l;
152 size_t sz = strlen(p);
155 /* --- Work out the dimensions --- */
161 die("bad trackword `%s': not square, and no line markers", p);
166 die("bad trackword `%s': no columns", p);
177 die("bad trackword `%s': inconsistent line lengths", p);
183 die("bad trackword `%s': no rows", p);
185 /* --- Build the match node --- */
187 n = xmalloc(sizeof(*n));
190 n->c = xmalloc((x * y + 1) * sizeof(tcell));
194 for (j = 0; j < y; j++) {
197 for (i = 0; i < x; i++) {
202 #define SOK (j < y - 1)
204 #define EOK (i < x - 1)
206 c->hop[N] = NOK ? c - x : 0;
207 c->hop[S] = SOK ? c + x : 0;
208 c->hop[W] = WOK ? c - 1 : 0;
209 c->hop[E] = EOK ? c + 1 : 0;
211 c->hop[NW] = NOK && WOK ? c - x - 1 : 0;
212 c->hop[NE] = NOK && EOK ? c - x + 1 : 0;
213 c->hop[SW] = SOK && WOK ? c + x - 1 : 0;
214 c->hop[SE] = SOK && EOK ? c + x + 1 : 0;
229 for (j = 0; j < y; j++) {
230 for (i = 0; i < x; i++) {
231 printf("%c ", c->ch);
240 /*----- That's all, folks -------------------------------------------------*/