-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
315 lines (310 loc) · 7.33 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// main.c
// Word Search Solver
//
// Created by MD Saifur Rahman on 20/03/20.
// Copyright © 2020 MD Saifur Rahman. All rights reserved.
#include <stdio.h>
#include <string.h>
void openFile();
void readData();
void game();
void readWords();
int check(int a, int b);
int serch(int a, int n, int b, char test[]);
int serch1(int a, int n, int b, char test[]);
int serch2(int a, int n, int b, char test[]);
int serch3(int a, int n, int b, char test[]);
int serch4(int a, int n, int b, char test[]);
int serch5(int a, int n, int b, char test[]);
int serch6(int a, int n, int b, char test[]);
int serch7(int a, int n, int b, char test[]);
char word[30][30];
char puzzle[40][40];
int p, h;
int main()
{
system("COLOR 4F");
coverPage();
openFile();
readData();
printf("Try First!\n\nThese are the words:\n\n");
readWords();
game();
return 0;
}
void openFile()
{
FILE *fp;
fp = fopen("file.txt", "r");
if (fp == NULL)
{
printf("Error in opening file\n");
}
fscanf(fp, "%d %d", &p, &h);
char line[40], line2[40];
int i, rows = 0;
while (fgets(line, sizeof(line), fp) != NULL && rows < p)
{
for (i = 0; i < (strlen(line) - 1); i++)
{
puzzle[rows - 1][i] = line[i];
}
rows++;
}
for (i = 0; i < (strlen(line) - 1); i++)
puzzle[rows - 1][i] = line[i];
rows = 0;
while (fgets(line2, sizeof(line2), fp) != NULL)
{
for (i = 0; i < (strlen(line2) - 1); i++)
{
word[rows][i] = line2[i];
}
rows++;
}
}
void readData()
{
int i = 0;
for (i = 0; i < p; i++)
printf("%s\n", puzzle[i]);
printf("\n\n");
}
void readWords()
{
int i = 0;
for (i = 0; i < h; i++)
{
printf("%10s", word[i]);
if ((i + 1) % 4 == 0)
printf("\n");
}
printf("\n____________________________________________________________________\n");
}
void game()
{
int i = 0, j = 0;
int leter, x = 0;
char choose[5];
printf("\nTo see the solution enter 'Show'\n");
scanf("%s", choose);
system("COLOR 0A");
printf("\n");
if (strcmp(choose, "show") == 0 || strcmp(choose, "SHOW") == 0 || strcmp(choose, "Show") == 0)
{
printf("Word\t\tRow,Column \tDirection\t\n");
printf("-----------------------------------------------\n");
for (x = 0; x < h; x++)
{
for (i = 0; i < 40; i++)
{
for (j = 0; j < 40; j++)
{
if (puzzle[i][j] == word[x][0])
{
leter = strlen(word[x]);
if (check(i + leter - 1, j) == 1 && serch(i, leter, j, word[x]) == 1)
{
printf("\t%d,%d \t\t Down \n", i + 1, j + 1);
}
else if (check(i, j + leter - 1) == 1 && serch1(i, leter, j, word[x]) == 2)
{
printf("\t%d,%d \t\t Right\n", i + 1, j + 1);
}
else if (check(i + leter - 1, j + leter - 1) == 1 && serch2(i, leter, j, word[x]) == 3)
{
printf("\t%d ,%d \t\t Down,Right \n", i + 1, j + 1);
}
else if (check(i, j - leter + 1) == 1 && serch3(i, leter, j, word[x]) == 4)
{
printf("\t%d,%d \t\t Left\n", i + 1, j + 1);
}
else if (check(i - leter + 1, j) == 1 && serch4(i, leter, j, word[x]) == 5)
{
printf("\t%d,%d \t\t Up\n", i + 1, j + 1);
}
else if (check(i - leter + 1, j + leter - 1) == 1 && serch5(i, leter, j, word[x]) == 6)
{
printf("\t%d,%d \t\t Up,Right \n", i + 1, j + 1);
}
else if (check(i - leter + 1, j - leter + 1) == 1 && serch6(i, leter, j, word[x]) == 7)
{
printf("\t%d,%d \t\t Up,Left \n", i + 1, j + 1);
}
else if (check(i + leter - 1, j - leter + 1) == 1 && serch7(i, leter, j, word[x]) == 8)
{
printf("\t%d,%d \t\t Down,Left \n", i + 1, j + 1);
}
}
}
}
}
}
else
{
printf("Invalid input\n ");
game();
}
}
int check(int a, int b)
{
if (puzzle[a][b] != 0)
return 1;
else
return -1;
}
int serch(int a, int n, int b, char test[])
{
char test2[n];
int i = 0;
for (i = 0; i < n; i++)
{
test2[i] = puzzle[a][b];
a++;
}
test2[n] = '\0';
if (strcmp(test2, test) == 0)
{
printf("%10s", test2);
return 1;
}
else
return -1;
}
int serch1(int a, int n, int b, char test[])
{
char test2[n];
int i = 0;
for (i = 0; i < n; i++)
{
test2[i] = puzzle[a][b];
b++;
}
test2[n] = '\0';
if (strcmp(test2, test) == 0)
{
printf("%10s", test2);
return 2;
}
else
return -1;
}
int serch2(int a, int n, int b, char test[])
{
char test2[n];
int i = 0;
for (i = 0; i < n; i++)
{
test2[i] = puzzle[a][b];
b++;
a++;
}
test2[n] = '\0';
if (strcmp(test2, test) == 0)
{
printf("%10s", test2);
return 3;
}
else
return -1;
}
int serch3(int a, int n, int b, char test[])
{
char test2[n];
int i = 0;
for (i = 0; i < n; i++)
{
test2[i] = puzzle[a][b];
b--;
}
test2[n] = '\0';
if (strcmp(test2, test) == 0)
{
printf("%10s", test2);
return 4;
}
else
return -1;
}
int serch4(int a, int n, int b, char test[])
{
char test2[n];
int i = 0;
for (i = 0; i < n; i++)
{
test2[i] = puzzle[a][b];
a--;
}
test2[n] = '\0';
if (strcmp(test2, test) == 0)
{
printf("%10s", test2);
return 5;
}
else
return -1;
}
int serch5(int a, int n, int b, char test[])
{
char test2[n];
int i = 0;
for (i = 0; i < n; i++)
{
test2[i] = puzzle[a][b];
a--;
b++;
}
test2[n] = '\0';
if (strcmp(test2, test) == 0)
{
printf("%10s", test2);
return 6;
}
else
return -1;
}
int serch6(int a, int n, int b, char test[])
{
char test2[n];
int i = 0;
for (i = 0; i < n; i++)
{
test2[i] = puzzle[a][b];
a--;
b--;
}
test2[n] = '\0';
if (strcmp(test2, test) == 0)
{
printf("%10s", test2);
return 7;
}
else
return -1;
}
int serch7(int a, int n, int b, char test[])
{
char test2[n];
int i = 0;
for (i = 0; i < n; i++)
{
test2[i] = puzzle[a][b];
a++;
b--;
}
test2[n] = '\0';
if (strcmp(test2, test) == 0)
{
printf("%10s", test2);
return 8;
}
else
return -1;
}
void coverPage()
{
printf(" ==================WELCOME============================\n");
printf("\n Press any key to continue ...\n");
getchar();
system("cls");
}