-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscramble1.c
446 lines (381 loc) · 10.2 KB
/
scramble1.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/***************************************************************************
* scramble.c
*
* Problem Set 3
*
* Implements Scramble with CS50.
*
* Usage: scramble [#]
*
* where # is an optional grid number.
***************************************************************************/
#include <cs50.h>
#include <ctype.h>
#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
// duration of a game in seconds
#define DURATION 30
// grid's dimensions
#define DIMENSION 4
// maximum number of words in any dictionary
#define WORDS 172806
// maximum number of letters in any word
#define LETTERS 29
// default dictionary
// http://www.becomeawordgameexpert.com/wordlists.htm
#define DICTIONARY "words"
//for scramble
int scramble_count = 0;
// for logging
FILE* log;
// grid
char grid[DIMENSION][DIMENSION];
// flags with which we can mark grid's letters while searching for words
bool marks[DIMENSION][DIMENSION];
// defines a word as having an array of letters plus a flag
// indicating whether word has been found on grid
typedef struct
{
bool found;
char letters[LETTERS + 1];
}
word;
// defines a dictionary as having a size and an array of words
struct
{
int size;
word words[WORDS];
}
dictionary;
// prototypes
void clear(void);
bool crawl(string letters, int x, int y);
void draw(void);
bool find(string s);
void initialize(void);
bool load(string s);
bool lookup(string s);
void scramble(void);
// This is Scramble.
int main(int argc, string argv[])
{
// ensure proper usage
if (argc > 2)
{
printf("Usage: %s [#]\n", basename(argv[0]));
return 1;
}
// seed pseudorandom number generator
if (argc == 2)
{
int seed = atoi(argv[1]);
if (seed <= 0)
{
printf("Invalid grid.\n");
return 1;
}
srand(seed);
}
else
srand(time(NULL));
// determine path to dictionary
string directory = dirname(argv[0]);
char path[strlen(directory) + 1 + strlen(DICTIONARY) + 1];
sprintf(path, "%s/%s", directory, DICTIONARY);
// load dictionary
if (!load(path))
{
printf("Could not open dictionary.\n");
return 1;
}
// initialize the grid
initialize();
// initialize user's score
int score = 0;
// calculate time of game's end
int end = time(NULL) + DURATION;
// open log
log = fopen("log.txt", "w");
if (log == NULL)
{
printf("Could not open log.\n");
return 1;
}
// accept words until timer expires
while (true)
{
// clear the screen
clear();
// draw the current state of the grid
draw();
// log board
for (int row = 0; row < DIMENSION; row++)
{
for (int col = 0; col < DIMENSION; col++)
fprintf(log, "%c", grid[row][col]);
fprintf(log, "\n");
}
// get current time
int now = time(NULL);
// report score
printf("Score: %d\n", score);
fprintf(log, "%d\n", score);
// check for game's end
if (now >= end)
{
printf("\033[31m"); // red
printf("Time: %d\n\n", 0);
printf("\033[39m"); // default
break;
}
// report time remaining
printf("Time: %d\n\n", end - now);
// prompt for word
printf("> ");
string s = GetString();
for (int a = 0, b = strlen(s); a < b; a++)
s[a] = toupper(s[a]);
// quit playing if user hits ctrl-d
if (s == NULL)
break;
// log word
fprintf(log, "%s\n", s);
// check whether to scramble grid
if (strcmp(s, "SCRAMBLE") == 0)
scramble();
// or to look for word on grid and in dictionary
else
{
if (find(s) && lookup(s))
score += strlen(s);
}
}
// close log
fclose(log);
return 0;
}
/**
* Clears screen.
*/
void clear()
{
printf("\033[2J");
printf("\033[%d;%dH", 0, 0);
}
/**
* Crawls grid recursively for letters starting at grid[x][y].
* Returns true if all letters are found.
*/
bool crawl(string letters, int x, int y)
{
// if out of letters, then we must've found them all!
if (strlen(letters) == 0)
return true;
// don't fall off the grid!
if (x < 0 || x >= DIMENSION)
return false;
if (y < 0 || y >= DIMENSION)
return false;
// been here before!
if (marks[x][y])
return false;
// check grid[x][y] for current letter
if (grid[x][y] != letters[0])
return false;
// mark location
marks[x][y] = true;
// look left and right for next letter
for (int i = -1; i <= 1; i++)
{
// look down and up for next letter
for (int j = -1; j <= 1; j++)
{
// check grid[x + i][y + j] for next letter
if (crawl(&letters[1], x + i, y + j))
return true;
}
}
// unmark location
marks[x][y] = false;
// fail
return false;
}
/**
* Prints the grid in its current state.
*/
void draw(void)
{
if (scramble_count == 4)
{
scramble_count = 0;
}
if (scramble_count == 0) //0 or 360 degrees
{
printf("\n %c %c %c %c\n", grid[0][0], grid[0][1], grid[0][2], grid[0][3]);
printf(" %c %c %c %c\n", grid[1][0], grid[1][1], grid[1][2], grid[1][3]);
printf(" %c %c %c %c\n", grid[2][0], grid[2][1], grid[2][2], grid[2][3]);
printf(" %c %c %c %c\n\n", grid[3][0], grid[3][1], grid[3][2], grid[3][3]);
}
if (scramble_count == 1) //90 degrees
{
printf("\n %c %c %c %c\n", grid[3][0], grid[2][0], grid[1][0], grid[0][0]);
printf(" %c %c %c %c\n", grid[3][1], grid[2][1], grid[1][1], grid[0][1]);
printf(" %c %c %c %c\n", grid[3][2], grid[2][2], grid[1][2], grid[0][2]);
printf(" %c %c %c %c\n\n", grid[3][3], grid[2][3], grid[1][3], grid[0][3]);
}
if (scramble_count == 2) //180 dergees
{
printf("\n %c %c %c %c\n", grid[3][3], grid[3][2], grid[3][1], grid[3][0]);
printf(" %c %c %c %c\n", grid[2][3], grid[2][2], grid[2][1], grid[2][0]);
printf(" %c %c %c %c\n", grid[1][3], grid[1][2], grid[1][1], grid[1][0]);
printf(" %c %c %c %c\n\n", grid[0][3], grid[0][2], grid[0][1], grid[0][0]);
}
if (scramble_count == 3) //270 degrees
{
printf("\n %c %c %c %c\n", grid[0][3], grid[1][3], grid[2][3], grid[3][3]);
printf(" %c %c %c %c\n", grid[0][2], grid[1][2], grid[2][2], grid[3][2]);
printf(" %c %c %c %c\n", grid[0][1], grid[1][1], grid[2][1], grid[3][1]);
printf(" %c %c %c %c\n\n", grid[0][0], grid[1][0], grid[2][0], grid[3][0]);
}
}
/**
* Returns true iff word, s, is found in grid.
*/
bool find(string s)
{
// word must be at least 2 characters in length
if (strlen(s) < 2)
return false;
// search grid for word
for (int row = 0; row < DIMENSION; row++)
{
for (int col = 0; col < DIMENSION; col++)
{
// reset marks
for (int i = 0; i < DIMENSION; i++)
for (int j = 0; j < DIMENSION; j++)
marks[i][j] = false;
// search for word starting at grid[i][j]
if (crawl(s, row, col))
return true;
}
}
return false;
}
/**
* Initializes grid with letters.
*/
void initialize(void)
{
// http://en.wikipedia.org/wiki/Letter_frequency
float frequencies[] = {
8.167, // a
1.492, // b
2.782, // c
4.253, // d
12.702, // e
2.228, // f
2.015, // g
6.094, // h
6.966, // i
0.153, // j
0.747, // k
4.025, // l
2.406, // m
6.749, // n
7.507, // o
1.929, // p
0.095, // q
5.987, // r
6.327, // s
9.056, // t
2.758, // u
1.037, // v
2.365, // w
0.150, // x
1.974, // y
0.074 // z
};
int n = sizeof(frequencies) / sizeof(float);
// iterate over grid
for (int row = 0; row < DIMENSION; row++)
{
for (int col = 0; col < DIMENSION; col++)
{
// generate pseudorandom double in [0, 1]
double d = rand() / (double) RAND_MAX;
// map d onto range of frequencies
for (int k = 0; k < n; k++)
{
d -= frequencies[k] / 100;
if (d < 0.0 || k == n - 1)
{
grid[row][col] = 'A' + k;
break;
}
}
}
}
}
/**
* Loads words from dictionary with given filename, s, into a global array.
*/
bool load(string s)
{
// open dictionary
FILE* file = fopen(s, "r");
if (file == NULL)
return false;
// initialize dictionary's size
dictionary.size = 0;
// load words from dictionary
char buffer[LETTERS + 2];
while (fgets(buffer, LETTERS + 2, file))
{
// overwrite \n with \0
buffer[strlen(buffer) - 1] = '\0';
// capitalize word
for (int i = 0, n = strlen(buffer); i < n; i++)
buffer[i] = toupper(buffer[i]);
// ignore SCRAMBLE
if (strcmp(buffer, "SCRAMBLE") == 0)
continue;
// copy word into dictionary
dictionary.words[dictionary.size].found = false;
strncpy(dictionary.words[dictionary.size].letters, buffer, LETTERS + 1);
dictionary.size++;
}
// success!
return true;
}
/**
* Looks up word, s, in dictionary. If found (for the first time), flags word
* as found (so that user can't score with it again) and returns true.
*/
bool lookup(string s)
{
for (int i = 0; i < dictionary.size; i++)
{
if (strcmp(s, dictionary.words[i].letters) == 0)
{
if(dictionary.words[i].found == true)
{
return false;
}
else
dictionary.words[i].found = true;
return true;
}
}
return false;
}
/**
* Scrambles the grid by rotating it 90 degrees clockwise, whereby
* grid[0][0] rotates to grid[0][DIMENSION - 1].
*/
void scramble(void)
{
scramble_count++;
}