-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
198 lines (153 loc) · 4.88 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define TEST_FILTER 1
#define TEST_FILTER0 0
#define LEN_GRID_ROW 4
#define LEN_GRID_COL 4
extern void dictFileInit ();
extern void dictFileClose();
extern void resetDictOperationPos ();
extern char * findNextWord ();
extern char * matchTargetFromCurrentPos (char *szTarget, int nCurPos);
static char gGridString [LEN_GRID_ROW * LEN_GRID_COL + 1];
void formatHeadShow (void)
{
printf ("\n\n\n\n");
printf ("This program is for test the searching and matching function in word list ! \n");
printf ("Word list target directory : /usr/share/dict/word");
printf ("\n\n\n\n");
}
void guideInput (void)
{
printf ("\n");
printf ("Please input 16 letter and press the Enter key \n");
printf ("All legal letter will be taken to be built into 4X4 grid \n");
}
void filterInputAndBuildGrid (char * strInput)
{
int i = 0, j = 0;
char * tmp;
char * tmp1 = gGridString;
if (strInput == 0)
return ;
if (strlen(strInput) == 0)
return;
tmp = strInput;
while (*tmp != '\0') {
*tmp1 ++ = *tmp;
fputc (*tmp ++, stdout);
fputc (' ', stdout);
if (++ i >= LEN_GRID_COL) {
printf ("\n");
if (++ j >= LEN_GRID_COL) {
printf ("\n");
break;
}
i = 0;
}
}
}
int findAdjacentLetter (char cRow, char cCol, char * strNow, char * mapOccupancy, int nSearchPos)
{
// check position if legal
char tmpWord [LEN_GRID_ROW * LEN_GRID_COL + 1];
char cTmpMapOccu [LEN_GRID_ROW * LEN_GRID_COL];
int nLen, nTempSearchPos = nSearchPos;
char tmpChar;
char * foundWord ;
#if TEST_FILTER0
printf (" row (%d) col (%d) \n", cRow, cCol);
#endif
if ((cRow < 0) || (cRow >= LEN_GRID_ROW) || (cCol < 0) || (cCol >= LEN_GRID_COL)) {
#if TEST_FILTER0
printf (" -1 \n");
#endif
return -1;
}
if (mapOccupancy [cRow * LEN_GRID_COL + cCol] != '\0') {
#if TEST_FILTER0
printf (" -2 %c \n", mapOccupancy [cRow * LEN_GRID_COL + cCol]);
#endif
return -2;
}
// append new letter
strcpy (tmpWord, strNow);
nLen = strlen(tmpWord);
tmpChar = gGridString [cRow * LEN_GRID_COL + cCol];
#if TEST_FILTER0
//fputs (strNow, stdout);
printf ("%s ", strNow);
//fputs (tmpWord, stdout);
printf ("%s \n", tmpWord);
//printf ("\n");
printf (" len %d c %c \n", nLen, tmpChar);
#endif
tmpWord [nLen] = tmpChar;
tmpWord [nLen + 1] = '\0';
#if TEST_FILTER0
printf (" %s \n", tmpWord);
#endif
memcpy (cTmpMapOccu, mapOccupancy, LEN_GRID_ROW * LEN_GRID_COL);
cTmpMapOccu[cRow * LEN_GRID_COL + cCol] = '1';
#if TEST_FILTER0
//printf ("now \n");
fputs (tmpWord, stdout);
printf ("\n");
#endif
// match word in list
#if 1
foundWord = matchTargetFromCurrentPos ((tmpWord), 0);
if (foundWord) {
printf ("Bingo:-> %s \n", foundWord);
}
#endif
findAdjacentLetter (cRow - 1, cCol - 1, tmpWord, cTmpMapOccu, nTempSearchPos); //
findAdjacentLetter (cRow - 1, cCol + 0, tmpWord, cTmpMapOccu, nTempSearchPos); //
findAdjacentLetter (cRow - 1, cCol + 1, tmpWord, cTmpMapOccu, nTempSearchPos); //
findAdjacentLetter (cRow + 0, cCol + 1, tmpWord, cTmpMapOccu, nTempSearchPos); //
findAdjacentLetter (cRow + 1, cCol + 1, tmpWord, cTmpMapOccu, nTempSearchPos); //
findAdjacentLetter (cRow + 1, cCol + 0, tmpWord, cTmpMapOccu, nTempSearchPos); //
findAdjacentLetter (cRow + 1, cCol - 1, tmpWord, cTmpMapOccu, nTempSearchPos); //
findAdjacentLetter (cRow + 0, cCol - 1, tmpWord, cTmpMapOccu, nTempSearchPos); //
//getchar();
return 0;
}
int main(int argc, char** argv)
{
static char value[256];
char * tmpWord;
int nFilePos = 0, i, j;
char initWord[LEN_GRID_ROW * LEN_GRID_COL +1];
char initMap[LEN_GRID_ROW * LEN_GRID_COL];
memset (initWord, 0, LEN_GRID_ROW * LEN_GRID_COL +1); // init all pos to '\0'
memset (initMap, 0, LEN_GRID_ROW * LEN_GRID_COL); // init all pos to 0 //0 no occupancy
// 1 = occupancy
formatHeadShow ();
guideInput ();
// check the input valid and format the 4X4 grid
fgets(value, 256, stdin);
memset (gGridString, 0, LEN_GRID_ROW * LEN_GRID_COL + 1);
filterInputAndBuildGrid (value);
fputs(gGridString, stdout);
dictFileInit ();
getchar ();
initWord [0] = '\0';
initWord [1] = '\0';
initMap [0] = '\0';
resetDictOperationPos ();
for (i = 0; i < LEN_GRID_ROW; i ++) {
for (j = 0; j < LEN_GRID_COL; j ++) {
findAdjacentLetter (i, j, initWord, initMap, nFilePos);
}
}
#if 0
do {
tmpWord = findNextWord ();
} while (tmpWord);
#endif
dictFileClose();
getchar ();
return 0;
}