-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_words.c
188 lines (139 loc) · 4.16 KB
/
find_words.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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include "find_words.h"
extern uint16_t rows, cols;
extern char** game, **solutions;
extern char* words;
extern size_t words_len;
// diagonal find
char d_upRight(const char* word, const uint16_t row, const uint16_t col){
// check if word is there
for (uint16_t i = 0; i < strlen(word); i++)
if (row - i < 0 || col + i >= cols)
return 0;
else if (CHAR_AT(game, row - i, col + i) != *(word + i))
return 0;
// paint word on board
for (uint16_t i = 0; i < strlen(word); i++)
CHAR_AT(solutions, row - i, col + i) = 1;
return 1;
}
char d_downRight(const char* word, const uint16_t row, const uint16_t col){
// check if word is there
for (uint16_t i = 0; i < strlen(word); i++)
if (row + i >= rows || col + i >= cols)
return 0;
else if (CHAR_AT(game, row + i, col + i) != *(word + i))
return 0;
// paint word on board
for (uint16_t i = 0; i < strlen(word); i++)
CHAR_AT(solutions, row + i, col + i) = 1;
return 1;
}
char d_upLeft(const char* word, const uint16_t row, const uint16_t col){
// check if word is there
for (uint16_t i = 0; i < strlen(word); i++)
if (row - i < 0 || col - i < 0)
return 0;
else if (CHAR_AT(game, row - i, col - i) != *(word + i))
return 0;
// paint word on board
for (uint16_t i = 0; i < strlen(word); i++)
CHAR_AT(solutions, row - i, col - i) = 1;
return 1;
}
char d_downLeft(const char* word, const uint16_t row, const uint16_t col){
// check if word is there
for (uint16_t i = 0; i < strlen(word); i++)
if (row + i >= rows || col - i < 0)
return 0;
else if (CHAR_AT(game, row + i, col - i) != *(word + i))
return 0;
// paint word on board
for (uint16_t i = 0; i < strlen(word); i++)
CHAR_AT(solutions, row + i, col - i) = 1;
return 1;
}
// horizontal find
char h_up(const char* word, const uint16_t row, const uint16_t col){
// check if word is there
for (uint16_t i = 0; i < strlen(word); i++)
if (row - i < 0)
return 0;
else if (CHAR_AT(game, row - i, col) != *(word + i))
return 0;
// paint word on board
for (uint16_t i = 0; i < strlen(word); i++)
CHAR_AT(solutions, row - i, col) = 1;
return 1;
}
char h_down(const char* word, const uint16_t row, const uint16_t col){
// check if word is there
for (uint16_t i = 0; i < strlen(word); i++)
if (row + i >= rows)
return 0;
else if (CHAR_AT(game, row + i, col) != *(word + i))
return 0;
// paint word on board
for (uint16_t i = 0; i < strlen(word); i++)
CHAR_AT(solutions, row + i, col) = 1;
return 1;
}
char h_left(const char* word, const uint16_t row, const uint16_t col){
// check if word is there
for (uint16_t i = 0; i < strlen(word); i++)
if (col - i < 0)
return 0;
else if (CHAR_AT(game, row, col - i) != *(word + i))
return 0;
// paint word on board
for (uint16_t i = 0; i < strlen(word); i++)
CHAR_AT(solutions, row, col - i) = 1;
return 1;
}
char h_right(const char* word, const uint16_t row, const uint16_t col){
// check if word is there
for (uint16_t i = 0; i < strlen(word); i++)
if (col + i >= cols)
return 0;
else if (CHAR_AT(game, row, col + i) != *(word + i))
return 0;
// paint word on board
for (uint16_t i = 0; i < strlen(word); i++)
CHAR_AT(solutions, row, col + i) = 1;
return 1;
}
void findWords(void){
char* word = strtok(words, " ,");
while (word && *word) {
//std::cout <<"Finding word - \"" <<word <<"\"\n";
for (uint16_t r = 0; r < rows; r++)
for (uint16_t c = 0; c < cols; c++) {
// if the first letter of the word is found
if (CHAR_AT(game, r, c) == *word) {
// see if the rest of the word is there
if (d_upRight(word, r, c))
goto next_word;
else if (d_downRight(word, r, c))
goto next_word;
else if (d_upLeft(word, r, c))
goto next_word;
else if (d_downLeft(word, r, c))
goto next_word;
else if (h_up(word, r, c))
goto next_word;
else if (h_down(word, r, c))
goto next_word;
else if (h_left(word, r, c))
goto next_word;
else if (h_right(word, r, c))
goto next_word;
}
}
printf("\aWarning: couldn\'t find word \"%s\"\n", word);
next_word:
word = strtok(NULL, " ,");
}
}