a355a09acf16e178ae3e46c02dce9c08f28fff98
[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_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 FILE *outfile;
511 /* First output the uncompressed contents to a temp file */
512 outfile = tmpfile ();
513 if (outfile == NULL)
514 {
515 fprintf (stderr, "%s\n", ERROR_WRITING_FILE);
516 exit (1);
517 }
518 /* grid size is the first field */
519 fprintf (outfile, "%d\n", puzzle->grid_size);
520 /* whether grid is frozen or not */
521 fprintf (outfile, "%d\n", puzzle->grid_frozen);
522 /* the hashed password */
523 fprintf (outfile, "%s\n", puzzle->hashed_master_password);
524 /* the hashed_solution_password */
525 fprintf (outfile, "%s\n", puzzle->hashed_solution_password);
526
527 /* First output the grid characters columns/rows */
528 for (int i = 0; i < puzzle->grid_size; i ++)
529 {
530 for (int j = 0; j < puzzle->grid_size; j ++)
531 fprintf (outfile, "%c", puzzle->chars[i][j]);
532 fprintf (outfile, "\n");
533 }
534
535 /* Next output the start across/down numbers */
536 for (int i = 0; i < puzzle->grid_size; i ++)
537 {
538 for (int j = 0; j < puzzle->grid_size; j++)
539 {
540 fprintf (outfile, "%d ", puzzle->start_across_word[i][j]);
541 fprintf (outfile, "%d ", puzzle->start_down_word[i][j]);
542 }
543 fprintf (outfile, "\n");
544 }
545
546 /* Output the across clues */
547 fprintf (outfile, "ACROSS\n");
548 /* Search the grid for across words */
549 for (int i = 0; i < puzzle->grid_size; i ++)
550 {
551 for (int j = 0; j < puzzle->grid_size; j++)
552 {
553 /* if it is an across word, then put the word index followed by
554 tab character (as separator) and the clue */
555 if (puzzle->start_across_word[i][j] != -1)
556 fprintf (outfile, "%d\t%s\n", puzzle->start_across_word[i][j],
557 puzzle->clue_across[i][j]);
558 }
559 }
560
561 /* Output the down clues */
562 fprintf (outfile, "DOWN\n");
563 /* Search the grid for down words */
564 for (int i = 0; i < puzzle->grid_size; i ++)
565 {
566 for (int j = 0; j < puzzle->grid_size; j++)
567 {
568 /* same as across word, put the word index followed by the tab
569 character and then the clue */
570 if (puzzle->start_down_word[i][j] != -1)
571 fprintf (outfile, "%d\t%s\n", puzzle->start_down_word[i][j],
572 puzzle->clue_down[i][j]);
573 }
574 }
575
576 /* Flush the buffer and rewind to beginning - to read and save into
577 gzip compressed file */
578 fflush (outfile);
579 fseek (outfile, 0, 0);
580
581 /* now compress the file and save it to destination file */
582 gzFile outdestfile = gzopen (file, "wb");
583 if (outdestfile == NULL)
584 {
585 fprintf (stderr, "%s\n", ERROR_WRITING_FILE);
586 fclose (outfile);
587 exit (1);
588 }
589 char buf[128];
590 int num = fread (buf, sizeof(char), sizeof(char)*128, outfile);
591 while (num > 0)
592 {
593 int res = gzwrite (outdestfile, buf, num*sizeof(char) );
594 if (res == 0)
595 {
596 fprintf (stderr, "%s %s\n", ERROR_WRITING_FILE, COMPRESSED);
597 fclose (outfile);
598 exit (1);
599 }
600 num = fread (buf, sizeof(char), sizeof(char)*128, outfile);
601 }
602 gzclose (outdestfile);
603 fclose (outfile);
604
605 }
606
607 /* read the puzzle from a file */
608 Puzzle load_puzzle (const char* file) {
609 /* First open the GZip file */
610 gzFile insourcefile = gzopen (file, "rb");
611 if (insourcefile == NULL)
612 {
613 fprintf (stderr, "%s %s\n", ERROR_READING_FILE, COMPRESSED);
614 exit (1);
615 }
616 /* Open a temporary file to uncompress the contents */
617 FILE *infile = tmpfile ();
618 if (infile == NULL)
619 {
620 fprintf (stderr, "%s\n", ERROR_READING_FILE);
621 exit (1);
622 }
623 /* Put the uncompressed content to the temp file */
624 char buf[128];
625 int num = 0;
626 num = gzread (insourcefile, buf, 128);
627 while (num > 0)
628 {
629 int res = fwrite (buf, 1, num, infile);
630 if (res == 0)
631 {
632 fprintf (stderr, "%s\n", ERROR_READING_FILE);
633 fclose (infile);
634 gzclose (insourcefile);
635 exit (1);
636 }
637 num = gzread (insourcefile, buf, 128);
638 }
639 /* Close the gzip file */
640 gzclose (insourcefile);
641 /* Flush the temp file buffer and rewind to beginning */
642 fflush (infile);
643 fseek (infile, 0, 0);
644
645 /* Read the temporary file contents to the structure Puzzle */
646 Puzzle p;
647 char line[MAX_CLUE_LENGTH+10];
648 fgets (line, MAX_CLUE_LENGTH + 10, infile);
649 p.grid_size = atoi (line);
650 fgets (line, MAX_CLUE_LENGTH + 10, infile);
651 p.grid_frozen = atoi (line) == 0 ? false : true ;
652 fgets (line, MAX_CLUE_LENGTH + 10, infile);
653 if (strlen (line) != 1)
654 strcpy (p.hashed_master_password, strtok (line, "\n"));
655 else
656 strcpy (p.hashed_master_password, "\0");
657 fgets (line, MAX_CLUE_LENGTH + 10, infile);
658 if (strlen (line) != 1)
659 strcpy (p.hashed_solution_password, strtok (line, "\n"));
660 else
661 strcpy (p.hashed_solution_password, "\0");
662
663 /* read each character of the grid */
664 for (int i = 0; i < p.grid_size; i ++ )
665 {
666 fgets (line, MAX_CLUE_LENGTH + 10, infile);
667 for (int j = 0; j < p.grid_size; j ++)
668 p.chars[i][j] = line[j];
669 }
670 /* read the word numbers */
671 for (int i = 0; i < p.grid_size; i ++)
672 {
673 fgets (line, MAX_CLUE_LENGTH + 10, infile);
674 char *token = strtok (line, " ");
675 for (int j = 0; j < p.grid_size; j ++)
676 {
677 if (token != NULL)
678 p.start_across_word[i][j] = atoi (token);
679 token = strtok (NULL, " ");
680 if (token != NULL)
681 p.start_down_word[i][j] = atoi (token);
682 token = strtok (NULL, " ");
683 }
684 }
685 /* read the clues */
686 fgets (line, MAX_CLUE_LENGTH + 10, infile);
687
688 /* across clues */
689 char clues[100][MAX_CLUE_LENGTH];
690 int word_num[100];
691 int c = 0;
692 /* first read the across clues from file */
693 while (1)
694 {
695 fgets (line, MAX_CLUE_LENGTH + 10, infile);
696 /* if reached the end of across clues */
697 if (strcmp (line, "DOWN\n") == 0)
698 break;
699 word_num[c] = atoi (strtok (line, "\t"));
700 char *cl = strtok (NULL, "\n");
701 if (cl != NULL)
702 strcpy (clues[c], cl);
703 else
704 strcpy (clues[c], "\0");
705 c++;
706 }
707 /* set the clue to the correct cell in grid */
708 for (int i = 0; i < p.grid_size; i ++)
709 {
710 for (int j = 0; j < p.grid_size; j ++)
711 {
712 for (int r = 0; r < c; r ++)
713 if (p.start_across_word[i][j] == word_num[r])
714 strcpy (p.clue_across[i][j], clues[r]);
715 }
716 }
717
718 /* down clues */
719 c = 0;
720 while (fgets (line, MAX_CLUE_LENGTH + 10, infile))
721 {
722 word_num[c] = atoi (strtok (line, "\t"));
723 char* cl = strtok (NULL, "\n");
724 if (cl != NULL)
725 strcpy (clues[c], cl);
726 else
727 strcpy (clues[c], "\0");
728 c++;
729 }
730 for (int i = 0; i < p.grid_size; i ++)
731 {
732 for (int j = 0; j < p.grid_size; j ++)
733 {
734 for (int r = 0; r < c; r ++)
735 if (p.start_down_word[i][j] == word_num[r])
736 strcpy (p.clue_down[i][j], clues[r]);
737 }
738 }
739
740 fclose (infile);
741 return p;
742 }
743
744 /* display the puzzle */
745 void print_puzzle (Puzzle *p)
746 {
747 printf ("\n");
748 set_color (WHITE, CYAN, NORMAL);
749 printf (" ");
750 for (int i = 0; i < p->grid_size; i ++)
751 printf ("%3d", i);
752 reset_color ();
753 printf("\n");
754 for (int i = 0; i < p->grid_size; i ++)
755 {
756 set_color (WHITE, CYAN, NORMAL);
757 printf ("%3d ", i);
758 for (int j = 0; j < p->grid_size; j ++)
759 {
760 if (p->chars[i][j] == '#') {
761 set_color (WHITE, BLACK, NORMAL);
762 printf (" ");
763 }
764 else
765 {
766 if (p->start_across_word[i][j] != -1 ||
767 p->start_down_word[i][j] != -1)
768 {
769 set_color (BLUE, WHITE, NORMAL);
770 if (p->start_across_word[i][j] != -1)
771 printf ("%-2d", p->start_across_word[i][j]);
772 else
773 printf ("%-2d", p->start_down_word[i][j]);
774 }
775 else
776 {
777 set_color (BLACK, WHITE,NORMAL);
778 printf (" ");
779 }
780
781 set_color (BLACK, WHITE, BOLD);
782 printf ("%c", p->chars[i][j]);
783 }
784 reset_color ();
785 }
786 printf ("\n");
787 }
788 /* print the clues if set */
789 if (p->grid_frozen == true)
790 {
791 printf ("\x1B[1mACROSS - CLUES\x1B[0m\n");
792 for (int i = 0; i < p->grid_size; i ++)
793 {
794 for (int j = 0; j < p->grid_size; j ++)
795 {
796 if (p->start_across_word[i][j] != -1)
797 {
798 printf ("%d - %s; ", p->start_across_word[i][j],
799 p->clue_across[i][j]);
800 }
801 }
802 }
803 printf ("\n\x1B[1mDOWN - CLUES\x1B[0m\n");
804 for (int i = 0; i < p->grid_size; i ++)
805 {
806 for (int j = 0; j < p->grid_size; j ++)
807 {
808 if (p->start_down_word[i][j] != -1)
809 {
810 printf ("%d - %s; ", p->start_down_word[i][j],
811 p->clue_down[i][j]);
812 }
813 }
814 }
815 printf ("\n");
816 }
817 }
818
819 /* function to check if a word is valid or not */
820 char* is_valid_word (char *word)
821 {
822 if (word == NULL || strlen(word) == 0)
823 return NULL;
824 for (int i = 0; i < strlen (word) - 1; i ++)
825 if (! isalpha (word[i]))
826 return NULL;
827
828 return strtok (word, "\n");
829 }
830
831
832 /* function to set a clue for an across word */
833 bool set_clue (Puzzle *p, String clue, int index, enum ORIENTATION order)
834 {
835 for (int i = 0; i < p->grid_size; i ++)
836 {
837 for (int j = 0; j < p->grid_size; j ++)
838 {
839 if (order == ACROSS)
840 {
841 if (p->start_across_word[i][j] == index)
842 {
843 strcpy (p->clue_across[i][j], clue);
844 return true;
845 }
846 }
847 else if (order == DOWN)
848 {
849 if (p->start_down_word[i][j] == index)
850 {
851 strcpy (p->clue_down[i][j], clue);
852 return true;
853 }
854 }
855 }
856 }
857 return false;
858 }
859
860 /* function to print a menu */
861 void print_menu (enum COLOR fg, enum COLOR bg, const char* title,
862 char **items, int num_items, int padding)
863 {
864 /* clear screen */
865 printf ("\e[1;1H\e[2J");
866 set_color (fg, bg, NORMAL);
867 printf ("\u2554");
868 for (int i = 0; i < padding; i ++)
869 printf ("\u2550");
870 printf ("\u2557");
871 reset_color (); printf ("\n");
872 set_color (fg, bg, NORMAL);
873 printf ("\u2551");
874 set_color (fg, bg, BOLD);
875 printf ("%-*s", padding, title);
876 reset_color ();
877 set_color (fg, bg, NORMAL);
878 printf ("\u2551");
879 reset_color (); printf ("\n");
880 set_color (fg, bg, NORMAL);
881 printf ("\u2560");
882 for (int i = 0; i < padding; i ++)
883 printf ("\u2550");
884 printf ("\u2563");
885 reset_color (); printf ("\n");
886 for (int i = 0; i < num_items; i ++)
887 {
888 set_color (fg, bg, NORMAL);
889 printf ("\u2551%-*s\u2551", padding, items[i]);
890 reset_color (); printf ("\n");
891 }
892 set_color (fg, bg, NORMAL);
893 printf ("\u255A");
894 for (int i = 0; i < padding; i ++)
895 printf ("\u2550");
896 printf ("\u255D");
897 reset_color (); printf ("\n");
898 }
899
900 /* reset the player data, loading from the puzzle file */
901 void reset_player_data (MainPlayerData *app_data, const char *filename)
902 {
903 app_data->puzzle = load_puzzle (filename);
904
905 app_data->is_loaded = app_data->puzzle.grid_frozen;
906 app_data->cur_col = -1;
907 app_data->cur_row = -1;
908 app_data->solution_revealed = false;
909 strcpy (app_data->filename, filename);
910 /* reset the answer keys */
911 for (int i = 0; i < app_data->puzzle.grid_size; i ++)
912 for (int j = 0; j < app_data->puzzle.grid_size; j ++)
913 app_data->char_ans[i][j] = ' ';
914
915 }
916
917 /* in the player app, move the current selection index left or right */
918 void move_current_col (MainPlayerData *app_data, enum DIRECTION dir)
919 {
920 int r = app_data->cur_row;
921 int c = app_data->cur_col;
922 if (dir == DIR_FORWARD)
923 {
924 c ++;
925 while (c < app_data->puzzle.grid_size)
926 {
927 if (app_data->puzzle.chars[r][c] == '#')
928 c ++;
929 else
930 break;
931 }
932 if (c < app_data->puzzle.grid_size)
933 app_data->cur_col = c;
934 }
935 else
936 {
937 c --;
938 while (c >= 0)
939 {
940 if (app_data->puzzle.chars[r][c] == '#')
941 c --;
942 else
943 break;
944 }
945 if (c >= 0)
946 app_data->cur_col = c;
947 }
948 }
949
950 /* in the player app move the current selection index up or down */
951 void move_current_row (MainPlayerData *app_data, enum DIRECTION dir)
952 {
953 int r = app_data->cur_row;
954 int c = app_data->cur_col;
955 if (dir == DIR_FORWARD)
956 {
957 r ++;
958 while (r < app_data->puzzle.grid_size)
959 {
960 if (app_data->puzzle.chars[r][c] == '#')
961 r ++;
962 else
963 break;
964 }
965 if (r < app_data->puzzle.grid_size)
966 app_data->cur_row = r;
967 }
968 else
969 {
970 r --;
971 while (r >= 0)
972 {
973 if (app_data->puzzle.chars[r][c] == '#')
974 r --;
975 else
976 break;
977 }
978 if (r >= 0)
979 app_data->cur_row = r;
980 }
981 }
982
983 #endif