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