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