Renamed the project to Wordblah from wordblox
[wordblah.git] / wordblah.h
1 #ifndef __WORDBLAH_H
2 #define __WORDBLAH_H
3 #define _XOPEN_SOURCE
4 #include <unistd.h>
5 #include <stdbool.h>
6 #include <string.h>
7 #include <ctype.h>
8 #include <gd.h>
9 #include <gdfontmb.h>
10 #include <gdfontg.h>
11 #include <zlib.h>
12 #include <openssl/conf.h>
13 #include <openssl/evp.h>
14 #include <openssl/err.h>
15 #include "constantstrings.h"
16
17 #define MAX_PUZZLE_SIZE 25
18 #define MAX_CLUE_LENGTH 150
19 #define GRID_PIXELS 37
20
21 /* Enum to define terminal colours */
22 enum COLOR {
23 BLACK = 0,
24 RED= 1,
25 GREEN=2,
26 YELLOW=3,
27 BLUE=4,
28 MAGENTA=5,
29 CYAN=6,
30 WHITE=7
31 };
32
33 enum ATTR {
34 NORMAL = 23,
35 BOLD=1
36 };
37
38 enum ORIENTATION {
39 ACROSS=1,
40 DOWN=2
41 };
42
43 /* for use with the player */
44 enum DIRECTION {
45 DIR_FORWARD = 1,
46 DIR_BACK = -1
47 };
48
49 typedef char String[MAX_CLUE_LENGTH];
50
51 /* The main puzzle struct type */
52 typedef struct {
53 char chars[MAX_PUZZLE_SIZE][MAX_PUZZLE_SIZE];
54 int start_across_word[MAX_PUZZLE_SIZE][MAX_PUZZLE_SIZE];
55 int start_down_word[MAX_PUZZLE_SIZE][MAX_PUZZLE_SIZE];
56 String clue_across[MAX_PUZZLE_SIZE][MAX_PUZZLE_SIZE];
57 String clue_down[MAX_PUZZLE_SIZE][MAX_PUZZLE_SIZE];
58 int grid_size;
59 bool grid_frozen;
60 char hashed_master_password[256];
61 char hashed_solution_password[256];
62 } Puzzle;
63
64 /* The player data struct type - for the player app */
65 typedef struct {
66 Puzzle puzzle;
67 char filename[65535];
68 bool is_loaded;
69 char char_ans[MAX_PUZZLE_SIZE][MAX_PUZZLE_SIZE];
70 int cur_row;
71 int cur_col;
72 bool solution_revealed;
73 enum ORIENTATION current_movement;
74 } MainPlayerData;
75
76 /* compute the hash of a password */
77 void digest_message(const unsigned char *message,
78 size_t message_len, unsigned char **digest, unsigned int *digest_len)
79 {
80 EVP_MD_CTX *mdctx;
81
82 if((mdctx = EVP_MD_CTX_new()) == NULL)
83 goto err;
84
85 if(1 != EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL))
86 goto err;
87
88 if(1 != EVP_DigestUpdate(mdctx, message, message_len))
89 goto err;
90
91 if((*digest = (unsigned char *)
92 OPENSSL_malloc(EVP_MD_size(EVP_sha256()))) == NULL)
93 goto err;
94
95 if(1 != EVP_DigestFinal_ex(mdctx, *digest, digest_len))
96 goto err;
97
98 EVP_MD_CTX_free(mdctx);
99 return;
100 err:
101 EVP_MD_CTX_free(mdctx);
102 ERR_print_errors_fp(stderr);
103 exit (2);
104 }
105
106 /* convert the hashed binary password to hexadecimal representation and
107 free the hashed binary password */
108 void to_hexadecimal (char *hex, unsigned char *binary_pwd, unsigned int len)
109 {
110 char buf[3];
111 /* keep reference to beginning of the hashed password */
112 unsigned char *binary_pw_begin = binary_pwd;
113 for (int i = 0; i < len; i ++)
114 {
115 sprintf (buf, "%02x", (*binary_pwd)&0xff);
116 strcat (hex, buf);
117 binary_pwd ++;
118 }
119 /* free the hashed password */
120 OPENSSL_free (binary_pw_begin);
121 }
122
123 /* get a number from the user */
124 int get_num ()
125 {
126 char s[5];
127 fgets (s, 5, stdin);
128 int n = atoi (s);
129 return n;
130 }
131
132 /* verify solution password */
133 bool verify_solution_password (Puzzle *p, const char* password)
134 {
135 /* no password set */
136 if (strcmp (p->hashed_solution_password, "\0") == 0)
137 return true;
138
139 /* hash the user input password and compare it with the stored password */
140 unsigned char* hashed_sol_password;
141 unsigned int len;
142 digest_message ((const unsigned char *)password, strlen(password),
143 &hashed_sol_password, &len);
144 char hashed_hex_pwd[256] = { (char) NULL };
145 to_hexadecimal (hashed_hex_pwd, hashed_sol_password, len);
146
147 if (strcmp (p->hashed_solution_password, hashed_hex_pwd) == 0)
148 return true;
149
150 return false;
151 }
152
153
154 /* verify master password */
155 bool verify_master_password (Puzzle *p, const char* password)
156 {
157 /* no password set */
158 if (strcmp (p->hashed_master_password, "\0") == 0)
159 return true;
160
161 /* hash the user input password and compare it with the stored password */
162 unsigned char* hashed_mas_password;
163 unsigned int len;
164 digest_message ((const unsigned char *)password, strlen(password),
165 &hashed_mas_password, &len);
166 char hashed_hex_pwd[256] = { (char) NULL };
167 to_hexadecimal (hashed_hex_pwd, hashed_mas_password, len);
168
169 if (strcmp (p->hashed_master_password, hashed_hex_pwd) == 0)
170 return true;
171
172 return false;
173 }
174
175 /* Set or reset solution password for puzzle */
176 void set_solution_password (Puzzle *p, const char *password)
177 {
178 /* if it is a null string, reset the password */
179 if (strcmp (password, "\0") == 0)
180 strcpy (p->hashed_solution_password, "\0");
181 else
182 {
183
184 unsigned char* hashedpwd;
185 unsigned int len;
186 digest_message ((const unsigned char *)password, strlen(password),
187 &hashedpwd, &len);
188 /* the hashedpwd contains binary data - we will convert it to
189 hexadecimal data and store in file */
190
191 to_hexadecimal (p->hashed_solution_password, hashedpwd, len);
192 }
193 }
194
195 /* Set or reset master password for puzzle */
196 void set_master_password (Puzzle *p, const char *password)
197 {
198 /* if it is a null string, reset the password */
199 if (strcmp (password, "\0") == 0)
200 strcpy (p->hashed_master_password, "\0");
201 else
202 {
203
204 unsigned char* hashedpwd;
205 unsigned int len;
206 digest_message ((const unsigned char *)password, strlen(password),
207 &hashedpwd, &len);
208 /* the hashedpwd contains binary data - we will convert it to
209 hexadecimal data and store in file */
210
211 to_hexadecimal (p->hashed_master_password, hashedpwd, len);
212 }
213 }
214
215 /* Output the clues to text file */
216 void export_clues (Puzzle *p, const char *filename)
217 {
218 FILE *outfile = fopen (filename, "w");
219 if (outfile == NULL)
220 {
221 fprintf (stderr, "%s\n", ERROR_WRITING_FILE);
222 exit (1);
223 }
224 /* first the across clues */
225 fprintf (outfile, "ACROSS CLUES\n");
226 for (int i = 0; i < p->grid_size; i ++)
227 {
228 for (int j = 0; j < p->grid_size; j ++)
229 {
230 if (p->start_across_word[i][j] != -1)
231 fprintf (outfile, "%d - %s\n", p->start_across_word[i][j],
232 p->clue_across[i][j]);
233 }
234 }
235 /* now the down clues */
236 fprintf (outfile, "DOWN CLUES\n");
237 for (int i = 0; i < p->grid_size; i ++)
238 {
239 for (int j = 0; j < p->grid_size; j ++)
240 {
241 if (p->start_down_word[i][j] != -1)
242 fprintf (outfile, "%d - %s\n", p->start_down_word[i][j],
243 p->clue_down[i][j]);
244 }
245 }
246 fclose (outfile);
247 }
248
249 /* Output the grid to image - if answerkey is true export filled grid */
250 void export_grid_image (Puzzle *p, const char *filename, bool answerkey)
251 {
252 int img_size = p->grid_size * GRID_PIXELS;
253 FILE * outfile = fopen (filename, "wb");
254 if (outfile == NULL)
255 {
256 fprintf (stderr, "%s\n", ERROR_WRITING_FILE);
257 exit (1);
258 }
259
260 gdImagePtr img = gdImageCreate (img_size, img_size);
261 gdImageColorAllocate (img, 255,255,255);
262 int black = gdImageColorAllocate (img, 0, 0, 0);
263 int blue = gdImageColorAllocate (img, 0, 0, 216);
264 gdFontPtr sm_fnt = gdFontGetMediumBold ();
265 gdFontPtr lg_fnt = gdFontGetGiant ();
266
267 for (int i = 0; i < p->grid_size; i ++)
268 {
269 for (int j = 0; j < p->grid_size; j++)
270 {
271 /* if it is a block, draw the black square */
272 if (p->chars[i][j] == '#')
273 gdImageFilledRectangle (img, j*GRID_PIXELS, i*GRID_PIXELS,
274 j*GRID_PIXELS+GRID_PIXELS,
275 i*GRID_PIXELS+GRID_PIXELS,black);
276 else
277 {
278 /* draw a regular square */
279 gdImageRectangle (img, j*GRID_PIXELS, i*GRID_PIXELS,
280 j*GRID_PIXELS+GRID_PIXELS,
281 i*GRID_PIXELS+GRID_PIXELS, black);
282
283 /* print the numers, if it is either start across word or
284 a down word */
285 if (p->start_across_word[i][j] != -1 ||
286 p->start_down_word[i][j] != -1)
287 {
288 if (p->start_across_word[i][j] != -1)
289 {
290 char str[5];
291 sprintf (str, "%d", p->start_across_word[i][j]);
292 gdImageString (img, sm_fnt, j*GRID_PIXELS+2,
293 i*GRID_PIXELS+2,
294 (unsigned char *)str, blue);
295 }
296 else
297 {
298 char str[5];
299 sprintf (str, "%d", p->start_down_word[i][j]);
300 gdImageString (img, sm_fnt, j*GRID_PIXELS+2,
301 i*GRID_PIXELS+2,
302 (unsigned char *)str, blue);
303 }
304 }
305 /* if answerkey is true, draw the character in the cell */
306 if (answerkey)
307 {
308 gdImageChar (img, lg_fnt, j*GRID_PIXELS+15,
309 i*GRID_PIXELS+10, p->chars[i][j], black);
310 }
311 }
312 }
313 }
314
315 gdImagePng (img, outfile);
316 gdImageDestroy (img);
317 fclose (outfile);
318 }
319
320 /* Set the terminal colour */
321 void set_color (enum COLOR fg, enum COLOR bg, enum ATTR at) {
322 printf ("\x1B[%d;%d;%dm", fg+30, bg+40, at);
323 }
324
325 /* Reset the terminal colour */
326 void reset_color () {
327 printf ("\x1B[0m");
328 }
329
330 /* check if the prev row has a block or not */
331 bool prev_row_block (Puzzle *p, int r, int c)
332 {
333 if (r == 0)
334 return true;
335 if (p->chars[r-1][c] == '#')
336 return true;
337 return false;
338 }
339
340 /* check if the next row has a block or not */
341 bool next_row_block (Puzzle *p, int r, int c)
342 {
343 if (r == p->grid_size-1)
344 return true;
345 if (p->chars[r+1][c] == '#')
346 return true;
347 return false;
348 }
349
350 /* check if the prev col has a block or not */
351 bool prev_col_block (Puzzle *p, int r, int c)
352 {
353 if (c == 0)
354 return true;
355 if (p->chars[r][c-1] == '#')
356 return true;
357 return false;
358 }
359
360 /* check if the next col has a block or not */
361 bool next_col_block (Puzzle *p, int r, int c)
362 {
363 if (c == p->grid_size - 1)
364 return true;
365 if (p->chars[r][c+1] == '#')
366 return true;
367 return false;
368 }
369
370 /* check if previous row is blank or not */
371 bool prev_row_blank (Puzzle *p, int r, int c)
372 {
373 if (r == 0) return true;
374 if (p->chars[r-1][c] == ' ' || p->chars[r-1][c] == '#') return true;
375 return false;
376 }
377 /* check if next row is blank or not */
378 bool next_row_blank (Puzzle *p, int r, int c)
379 {
380 if (r == p->grid_size - 1) return true;
381 if (p->chars[r+1][c] == ' ' || p->chars[r+1][c] == '#') return true;
382 return false;
383 }
384 /* check if previous col is blank or not */
385 bool prev_col_blank (Puzzle *p, int r, int c)
386 {
387 if (c == 0) return true;
388 if (p->chars[r][c-1] == ' ' || p->chars[r][c-1] == '#') return true;
389 return false;
390 }
391 /* check if the next col is blank or not */
392 bool next_col_blank (Puzzle *p, int r, int c)
393 {
394 if (c == p->grid_size -1) return true;
395 if (p->chars[r][c+1] == ' ' || p->chars[r][c+1] == '#') return true;
396 return false;
397 }
398
399 /* set the current row/col to the beginning of word index (across or down) */
400 void set_selection_to_word_start (MainPlayerData *app_data,
401 enum ORIENTATION orient, int word_index)
402 {
403 for (int i = 0; i < app_data->puzzle.grid_size; i ++)
404 {
405 for (int j = 0; j < app_data->puzzle.grid_size; j ++)
406 {
407 if (orient == ACROSS &&
408 app_data->puzzle.start_across_word[i][j] == word_index)
409 {
410 app_data->current_movement = ACROSS;
411 app_data->cur_row = i;
412 app_data->cur_col = j;
413 break;
414 }
415 else if (orient == DOWN &&
416 app_data->puzzle.start_down_word[i][j] == word_index)
417 {
418 app_data->current_movement = DOWN;
419 app_data->cur_row = i;
420 app_data->cur_col = j;
421 break;
422 }
423 }
424 }
425 }
426
427 /* unfreeze the grid - make editing possible to change words */
428 void unfreeze_puzzle (Puzzle *p)
429 {
430 for (int i = 0; i < p->grid_size; i ++)
431 {
432 for (int j = 0; j < p->grid_size; j ++)
433 {
434 if (p->chars[i][j] == '#')
435 p->chars[i][j] = ' ';
436
437 p->start_across_word[i][j] = -1;
438 p->start_down_word[i][j] = -1;
439 }
440 }
441 p->grid_frozen = false;
442 }
443
444 /* freeze the grid - make editing impossible because it finalizes the
445 across and down words in the grid */
446 void freeze_puzzle (Puzzle *p)
447 {
448 int word_num = 1;
449 bool across_word_start, down_word_start;
450 for (int i = 0; i < p->grid_size; i ++)
451 {
452 for (int j = 0; j < p->grid_size; j++)
453 {
454 across_word_start = false;
455 down_word_start = false;
456 /* if it is a blank cell - cover it with a block */
457 if (p->chars[i][j] == ' ' || p->chars[i][j] == '#')
458 p->chars[i][j] = '#';
459 /* it is not a blank cell - check all possibilities */
460 else
461 {
462 bool prev_row = prev_row_blank (p, i, j);
463 bool next_row = next_row_blank (p, i, j);
464 bool prev_col = prev_col_blank (p, i, j);
465 bool next_col = next_col_blank (p, i, j);
466 if (prev_row && ! next_row)
467 down_word_start = true;
468 if (prev_col && ! next_col)
469 across_word_start = true;
470 }
471
472 if (across_word_start == true)
473 p->start_across_word[i][j] = word_num;
474 else
475 p->start_across_word[i][j] = -1;
476 if (down_word_start == true)
477 p->start_down_word[i][j] = word_num;
478 else
479 p->start_down_word[i][j] = -1;
480 if (across_word_start == true || down_word_start == true)
481 word_num ++;
482 }
483 }
484 p->grid_frozen = true;
485 }
486
487 /* reset the entire grid */
488 void init_puzzle (Puzzle *p, int grid_size)
489 {
490 p->grid_size = grid_size;
491 p->grid_frozen = false;
492 for (int i = 0; i < p->grid_size; i ++)
493 {
494 for (int j = 0; j < p->grid_size; j ++)
495 {
496 p->chars[i][j] = ' ';
497 p->start_across_word[i][j] = -1;
498 p->start_down_word[i][j] = -1;
499 strcpy (p->clue_across[i][j], "");
500 strcpy (p->clue_down[i][j], "");
501 }
502 }
503 strcpy (p->hashed_master_password, "\0");
504 strcpy (p->hashed_solution_password, "\0");
505
506 }
507
508 /* save the puzzle to a file */
509 void save_puzzle (Puzzle *puzzle, const char* file)
510 {
511 FILE *outfile;
512 /* First output the uncompressed contents to a temp file */
513 outfile = tmpfile ();
514 if (outfile == NULL)
515 {
516 fprintf (stderr, "%s\n", ERROR_WRITING_FILE);
517 exit (1);
518 }
519 /* grid size is the first field */
520 fprintf (outfile, "%d\n", puzzle->grid_size);
521 /* whether grid is frozen or not */
522 fprintf (outfile, "%d\n", puzzle->grid_frozen);
523 /* the hashed password */
524 fprintf (outfile, "%s\n", puzzle->hashed_master_password);
525 /* the hashed_solution_password */
526 fprintf (outfile, "%s\n", puzzle->hashed_solution_password);
527
528 /* First output the grid characters columns/rows */
529 for (int i = 0; i < puzzle->grid_size; i ++)
530 {
531 for (int j = 0; j < puzzle->grid_size; j ++)
532 fprintf (outfile, "%c", puzzle->chars[i][j]);
533 fprintf (outfile, "\n");
534 }
535
536 /* Next output the start across/down numbers */
537 for (int i = 0; i < puzzle->grid_size; i ++)
538 {
539 for (int j = 0; j < puzzle->grid_size; j++)
540 {
541 fprintf (outfile, "%d ", puzzle->start_across_word[i][j]);
542 fprintf (outfile, "%d ", puzzle->start_down_word[i][j]);
543 }
544 fprintf (outfile, "\n");
545 }
546
547 /* Output the across clues */
548 fprintf (outfile, "ACROSS\n");
549 /* Search the grid for across words */
550 for (int i = 0; i < puzzle->grid_size; i ++)
551 {
552 for (int j = 0; j < puzzle->grid_size; j++)
553 {
554 /* if it is an across word, then put the word index followed by
555 tab character (as separator) and the clue */
556 if (puzzle->start_across_word[i][j] != -1)
557 fprintf (outfile, "%d\t%s\n", puzzle->start_across_word[i][j],
558 puzzle->clue_across[i][j]);
559 }
560 }
561
562 /* Output the down clues */
563 fprintf (outfile, "DOWN\n");
564 /* Search the grid for down words */
565 for (int i = 0; i < puzzle->grid_size; i ++)
566 {
567 for (int j = 0; j < puzzle->grid_size; j++)
568 {
569 /* same as across word, put the word index followed by the tab
570 character and then the clue */
571 if (puzzle->start_down_word[i][j] != -1)
572 fprintf (outfile, "%d\t%s\n", puzzle->start_down_word[i][j],
573 puzzle->clue_down[i][j]);
574 }
575 }
576
577 /* Flush the buffer and rewind to beginning - to read and save into
578 gzip compressed file */
579 fflush (outfile);
580 fseek (outfile, 0, 0);
581
582 /* now compress the file and save it to destination file */
583 gzFile outdestfile = gzopen (file, "wb");
584 if (outdestfile == NULL)
585 {
586 fprintf (stderr, "%s\n", ERROR_WRITING_FILE);
587 fclose (outfile);
588 exit (1);
589 }
590 char buf[128];
591 int num = fread (buf, sizeof(char), sizeof(char)*128, outfile);
592 while (num > 0)
593 {
594 int res = gzwrite (outdestfile, buf, num*sizeof(char) );
595 if (res == 0)
596 {
597 fprintf (stderr, "%s %s\n", ERROR_WRITING_FILE, COMPRESSED);
598 fclose (outfile);
599 exit (1);
600 }
601 num = fread (buf, sizeof(char), sizeof(char)*128, outfile);
602 }
603 gzclose (outdestfile);
604 fclose (outfile);
605
606 }
607
608 /* read the puzzle from a file */
609 Puzzle load_puzzle (const char* file)
610 {
611 /* First open the GZip file */
612 gzFile insourcefile = gzopen (file, "rb");
613 if (insourcefile == NULL)
614 {
615 fprintf (stderr, "%s %s\n", ERROR_READING_FILE, COMPRESSED);
616 exit (1);
617 }
618 /* Open a temporary file to uncompress the contents */
619 FILE *infile = tmpfile ();
620 if (infile == NULL)
621 {
622 fprintf (stderr, "%s\n", ERROR_READING_FILE);
623 exit (1);
624 }
625 /* Put the uncompressed content to the temp file */
626 char buf[128];
627 int num = 0;
628 num = gzread (insourcefile, buf, 128);
629 while (num > 0)
630 {
631 int res = fwrite (buf, 1, num, infile);
632 if (res == 0)
633 {
634 fprintf (stderr, "%s\n", ERROR_READING_FILE);
635 fclose (infile);
636 gzclose (insourcefile);
637 exit (1);
638 }
639 num = gzread (insourcefile, buf, 128);
640 }
641 /* Close the gzip file */
642 gzclose (insourcefile);
643 /* Flush the temp file buffer and rewind to beginning */
644 fflush (infile);
645 fseek (infile, 0, 0);
646
647 /* Read the temporary file contents to the structure Puzzle */
648 Puzzle p;
649 char line[MAX_CLUE_LENGTH+10];
650 fgets (line, MAX_CLUE_LENGTH + 10, infile);
651 p.grid_size = atoi (line);
652 fgets (line, MAX_CLUE_LENGTH + 10, infile);
653 p.grid_frozen = atoi (line) == 0 ? false : true ;
654 fgets (line, MAX_CLUE_LENGTH + 10, infile);
655 if (strlen (line) != 1)
656 strcpy (p.hashed_master_password, strtok (line, "\n"));
657 else
658 strcpy (p.hashed_master_password, "\0");
659 fgets (line, MAX_CLUE_LENGTH + 10, infile);
660 if (strlen (line) != 1)
661 strcpy (p.hashed_solution_password, strtok (line, "\n"));
662 else
663 strcpy (p.hashed_solution_password, "\0");
664
665 /* read each character of the grid */
666 for (int i = 0; i < p.grid_size; i ++ )
667 {
668 fgets (line, MAX_CLUE_LENGTH + 10, infile);
669 for (int j = 0; j < p.grid_size; j ++)
670 p.chars[i][j] = line[j];
671 }
672 /* read the word numbers */
673 for (int i = 0; i < p.grid_size; i ++)
674 {
675 fgets (line, MAX_CLUE_LENGTH + 10, infile);
676 char *token = strtok (line, " ");
677 for (int j = 0; j < p.grid_size; j ++)
678 {
679 if (token != NULL)
680 p.start_across_word[i][j] = atoi (token);
681 token = strtok (NULL, " ");
682 if (token != NULL)
683 p.start_down_word[i][j] = atoi (token);
684 token = strtok (NULL, " ");
685 }
686 }
687 /* read the clues */
688 fgets (line, MAX_CLUE_LENGTH + 10, infile);
689
690 /* across clues */
691 char clues[100][MAX_CLUE_LENGTH];
692 int word_num[100];
693 int c = 0;
694 /* first read the across clues from file */
695 while (1)
696 {
697 fgets (line, MAX_CLUE_LENGTH + 10, infile);
698 /* if reached the end of across clues */
699 if (strcmp (line, "DOWN\n") == 0)
700 break;
701 word_num[c] = atoi (strtok (line, "\t"));
702 char *cl = strtok (NULL, "\n");
703 if (cl != NULL)
704 strcpy (clues[c], cl);
705 else
706 strcpy (clues[c], "\0");
707 c++;
708 }
709 /* set the clue to the correct cell in grid */
710 for (int i = 0; i < p.grid_size; i ++)
711 {
712 for (int j = 0; j < p.grid_size; j ++)
713 {
714 for (int r = 0; r < c; r ++)
715 if (p.start_across_word[i][j] == word_num[r])
716 strcpy (p.clue_across[i][j], clues[r]);
717 }
718 }
719
720 /* down clues */
721 c = 0;
722 while (fgets (line, MAX_CLUE_LENGTH + 10, infile))
723 {
724 word_num[c] = atoi (strtok (line, "\t"));
725 char* cl = strtok (NULL, "\n");
726 if (cl != NULL)
727 strcpy (clues[c], cl);
728 else
729 strcpy (clues[c], "\0");
730 c++;
731 }
732 for (int i = 0; i < p.grid_size; i ++)
733 {
734 for (int j = 0; j < p.grid_size; j ++)
735 {
736 for (int r = 0; r < c; r ++)
737 if (p.start_down_word[i][j] == word_num[r])
738 strcpy (p.clue_down[i][j], clues[r]);
739 }
740 }
741
742 fclose (infile);
743 return p;
744 }
745
746 /* display the puzzle */
747 void print_puzzle (Puzzle *p)
748 {
749 printf ("\n");
750 set_color (WHITE, CYAN, NORMAL);
751 printf (" ");
752 for (int i = 0; i < p->grid_size; i ++)
753 printf ("%3d", i);
754 reset_color ();
755 printf("\n");
756 for (int i = 0; i < p->grid_size; i ++)
757 {
758 set_color (WHITE, CYAN, NORMAL);
759 printf ("%3d ", i);
760 for (int j = 0; j < p->grid_size; j ++)
761 {
762 if (p->chars[i][j] == '#') {
763 set_color (WHITE, BLACK, NORMAL);
764 printf (" ");
765 }
766 else
767 {
768 if (p->start_across_word[i][j] != -1 ||
769 p->start_down_word[i][j] != -1)
770 {
771 set_color (BLUE, WHITE, NORMAL);
772 if (p->start_across_word[i][j] != -1)
773 printf ("%-2d", p->start_across_word[i][j]);
774 else
775 printf ("%-2d", p->start_down_word[i][j]);
776 }
777 else
778 {
779 set_color (BLACK, WHITE,NORMAL);
780 printf (" ");
781 }
782
783 set_color (BLACK, WHITE, BOLD);
784 printf ("%c", p->chars[i][j]);
785 }
786 reset_color ();
787 }
788 printf ("\n");
789 }
790 /* print the clues if set */
791 if (p->grid_frozen == true)
792 {
793 printf ("\x1B[1mACROSS - CLUES\x1B[0m\n");
794 for (int i = 0; i < p->grid_size; i ++)
795 {
796 for (int j = 0; j < p->grid_size; j ++)
797 {
798 if (p->start_across_word[i][j] != -1)
799 {
800 printf ("%d - %s; ", p->start_across_word[i][j],
801 p->clue_across[i][j]);
802 }
803 }
804 }
805 printf ("\n\x1B[1mDOWN - CLUES\x1B[0m\n");
806 for (int i = 0; i < p->grid_size; i ++)
807 {
808 for (int j = 0; j < p->grid_size; j ++)
809 {
810 if (p->start_down_word[i][j] != -1)
811 {
812 printf ("%d - %s; ", p->start_down_word[i][j],
813 p->clue_down[i][j]);
814 }
815 }
816 }
817 printf ("\n");
818 }
819 }
820
821 /* function to check if a word is valid or not */
822 char* is_valid_word (char *word)
823 {
824 if (word == NULL || strlen(word) == 0)
825 return NULL;
826 for (int i = 0; i < strlen (word) - 1; i ++)
827 if (! isalpha (word[i]))
828 return NULL;
829
830 return strtok (word, "\n");
831 }
832
833
834 /* function to set a clue for an across word */
835 bool set_clue (Puzzle *p, String clue, int index, enum ORIENTATION order)
836 {
837 for (int i = 0; i < p->grid_size; i ++)
838 {
839 for (int j = 0; j < p->grid_size; j ++)
840 {
841 if (order == ACROSS)
842 {
843 if (p->start_across_word[i][j] == index)
844 {
845 strcpy (p->clue_across[i][j], clue);
846 return true;
847 }
848 }
849 else if (order == DOWN)
850 {
851 if (p->start_down_word[i][j] == index)
852 {
853 strcpy (p->clue_down[i][j], clue);
854 return true;
855 }
856 }
857 }
858 }
859 return false;
860 }
861
862 /* function to print a menu */
863 void print_menu (enum COLOR fg, enum COLOR bg, const char* title,
864 char **items, int num_items, int padding)
865 {
866 /* clear screen */
867 printf ("\e[1;1H\e[2J");
868 set_color (fg, bg, NORMAL);
869 printf ("\u2554");
870 for (int i = 0; i < padding; i ++)
871 printf ("\u2550");
872 printf ("\u2557");
873 reset_color (); printf ("\n");
874 set_color (fg, bg, NORMAL);
875 printf ("\u2551");
876 set_color (fg, bg, BOLD);
877 printf ("%-*s", padding, title);
878 reset_color ();
879 set_color (fg, bg, NORMAL);
880 printf ("\u2551");
881 reset_color (); printf ("\n");
882 set_color (fg, bg, NORMAL);
883 printf ("\u2560");
884 for (int i = 0; i < padding; i ++)
885 printf ("\u2550");
886 printf ("\u2563");
887 reset_color (); printf ("\n");
888 for (int i = 0; i < num_items; i ++)
889 {
890 set_color (fg, bg, NORMAL);
891 printf ("\u2551%-*s\u2551", padding, items[i]);
892 reset_color (); printf ("\n");
893 }
894 set_color (fg, bg, NORMAL);
895 printf ("\u255A");
896 for (int i = 0; i < padding; i ++)
897 printf ("\u2550");
898 printf ("\u255D");
899 reset_color (); printf ("\n");
900 }
901
902 /* reset the player data, loading from the puzzle file */
903 void reset_player_data (MainPlayerData *app_data, const char *filename)
904 {
905 app_data->puzzle = load_puzzle (filename);
906
907 app_data->is_loaded = app_data->puzzle.grid_frozen;
908 app_data->cur_col = -1;
909 app_data->cur_row = -1;
910 app_data->solution_revealed = false;
911 strcpy (app_data->filename, filename);
912 /* reset the answer keys */
913 for (int i = 0; i < app_data->puzzle.grid_size; i ++)
914 for (int j = 0; j < app_data->puzzle.grid_size; j ++)
915 app_data->char_ans[i][j] = ' ';
916
917 }
918
919 /* save the user grid to a file */
920 void save_user_data (MainPlayerData *app_data, const char *filename)
921 {
922 FILE *outfile;
923 outfile = fopen (filename, "wb");
924 if (outfile == NULL)
925 {
926 fprintf (stderr, ERROR_WRITING_FILE);
927 return;
928 }
929 fprintf (outfile, "%s\n", app_data->filename);
930 for (int i = 0; i < app_data->puzzle.grid_size; i ++)
931 {
932 for (int j = 0; j < app_data->puzzle.grid_size; j ++)
933 fprintf (outfile, "%c", app_data->char_ans[i][j]);
934 fprintf (outfile, "\n");
935 }
936
937 fclose (outfile);
938 }
939
940 /* load the user grid from a file */
941 void load_user_data (MainPlayerData *app_data, const char *filename)
942 {
943 FILE *infile;
944 infile = fopen (filename, "rb");
945 if (infile == NULL)
946 {
947 fprintf (stderr, "%s\n", ERROR_READING_FILE);
948 return;
949 }
950
951 char puzzle_file_name[65535];
952 fgets (puzzle_file_name, 65535, infile);
953 reset_player_data (app_data, strtok (puzzle_file_name, "\n"));
954
955 char line[MAX_PUZZLE_SIZE+10];
956 for (int i = 0; i < app_data->puzzle.grid_size; i ++)
957 {
958 fgets (line, MAX_PUZZLE_SIZE+10, infile);
959 for (int j = 0; j < app_data->puzzle.grid_size; j ++)
960 app_data->char_ans[i][j] = line[j];
961
962 }
963 fclose (infile);
964 }
965
966 /* in the player app, move the current selection index left or right */
967 void move_current_col (MainPlayerData *app_data, enum DIRECTION dir)
968 {
969 int r = app_data->cur_row;
970 int c = app_data->cur_col;
971 if (dir == DIR_FORWARD)
972 {
973 c ++;
974 while (c < app_data->puzzle.grid_size)
975 {
976 if (app_data->puzzle.chars[r][c] == '#')
977 c ++;
978 else
979 break;
980 }
981 if (c < app_data->puzzle.grid_size)
982 app_data->cur_col = c;
983 }
984 else
985 {
986 c --;
987 while (c >= 0)
988 {
989 if (app_data->puzzle.chars[r][c] == '#')
990 c --;
991 else
992 break;
993 }
994 if (c >= 0)
995 app_data->cur_col = c;
996 }
997 }
998
999 /* in the player app move the current selection index up or down */
1000 void move_current_row (MainPlayerData *app_data, enum DIRECTION dir)
1001 {
1002 int r = app_data->cur_row;
1003 int c = app_data->cur_col;
1004 if (dir == DIR_FORWARD)
1005 {
1006 r ++;
1007 while (r < app_data->puzzle.grid_size)
1008 {
1009 if (app_data->puzzle.chars[r][c] == '#')
1010 r ++;
1011 else
1012 break;
1013 }
1014 if (r < app_data->puzzle.grid_size)
1015 app_data->cur_row = r;
1016 }
1017 else
1018 {
1019 r --;
1020 while (r >= 0)
1021 {
1022 if (app_data->puzzle.chars[r][c] == '#')
1023 r --;
1024 else
1025 break;
1026 }
1027 if (r >= 0)
1028 app_data->cur_row = r;
1029 }
1030 }
1031
1032 #endif