12 #include <openssl/conf.h>
13 #include <openssl/evp.h>
14 #include <openssl/err.h>
15 #include "constantstrings.h"
17 #define MAX_PUZZLE_SIZE 25
18 #define MAX_CLUE_LENGTH 150
19 #define GRID_PIXELS 37
21 /* Enum to define terminal colours */
43 /* for use with the player */
49 typedef char String
[MAX_CLUE_LENGTH
];
51 /* The main puzzle struct type */
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
];
60 char hashed_master_password
[256];
61 char hashed_solution_password
[256];
64 /* The player data struct type - for the player app */
69 char char_ans
[MAX_PUZZLE_SIZE
][MAX_PUZZLE_SIZE
];
72 bool solution_revealed
;
73 enum ORIENTATION current_movement
;
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
)
82 if((mdctx
= EVP_MD_CTX_new()) == NULL
)
85 if(1 != EVP_DigestInit_ex(mdctx
, EVP_sha256(), NULL
))
88 if(1 != EVP_DigestUpdate(mdctx
, message
, message_len
))
91 if((*digest
= (unsigned char *)
92 OPENSSL_malloc(EVP_MD_size(EVP_sha256()))) == NULL
)
95 if(1 != EVP_DigestFinal_ex(mdctx
, *digest
, digest_len
))
98 EVP_MD_CTX_free(mdctx
);
101 EVP_MD_CTX_free(mdctx
);
102 ERR_print_errors_fp(stderr
);
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
)
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
++)
115 sprintf (buf
, "%02x", (*binary_pwd
)&0xff);
119 /* free the hashed password */
120 OPENSSL_free (binary_pw_begin
);
123 /* get a number from the user */
132 /* verify solution password */
133 bool verify_solution_password (Puzzle
*p
, const char* password
)
135 /* no password set */
136 if (strcmp (p
->hashed_solution_password
, "\0") == 0)
139 /* hash the user input password and compare it with the stored password */
140 unsigned char* hashed_sol_password
;
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
);
147 if (strcmp (p
->hashed_solution_password
, hashed_hex_pwd
) == 0)
154 /* verify master password */
155 bool verify_master_password (Puzzle
*p
, const char* password
)
157 /* no password set */
158 if (strcmp (p
->hashed_master_password
, "\0") == 0)
161 /* hash the user input password and compare it with the stored password */
162 unsigned char* hashed_mas_password
;
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
);
169 if (strcmp (p
->hashed_master_password
, hashed_hex_pwd
) == 0)
175 /* Set or reset solution password for puzzle */
176 void set_solution_password (Puzzle
*p
, const char *password
)
178 /* if it is a null string, reset the password */
179 if (strcmp (password
, "\0") == 0)
180 strcpy (p
->hashed_solution_password
, "\0");
184 unsigned char* hashedpwd
;
186 digest_message ((const unsigned char *)password
, strlen(password
),
188 /* the hashedpwd contains binary data - we will convert it to
189 hexadecimal data and store in file */
191 to_hexadecimal (p
->hashed_solution_password
, hashedpwd
, len
);
195 /* Set or reset master password for puzzle */
196 void set_master_password (Puzzle
*p
, const char *password
)
198 /* if it is a null string, reset the password */
199 if (strcmp (password
, "\0") == 0)
200 strcpy (p
->hashed_master_password
, "\0");
204 unsigned char* hashedpwd
;
206 digest_message ((const unsigned char *)password
, strlen(password
),
208 /* the hashedpwd contains binary data - we will convert it to
209 hexadecimal data and store in file */
211 to_hexadecimal (p
->hashed_master_password
, hashedpwd
, len
);
215 /* Output the clues to text file */
216 void export_clues (Puzzle
*p
, const char *filename
)
218 FILE *outfile
= fopen (filename
, "w");
221 fprintf (stderr
, "%s\n", ERROR_WRITING_FILE
);
224 /* first the across clues */
225 fprintf (outfile
, "ACROSS CLUES\n");
226 for (int i
= 0; i
< p
->grid_size
; i
++)
228 for (int j
= 0; j
< p
->grid_size
; j
++)
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
]);
235 /* now the down clues */
236 fprintf (outfile
, "DOWN CLUES\n");
237 for (int i
= 0; i
< p
->grid_size
; i
++)
239 for (int j
= 0; j
< p
->grid_size
; j
++)
241 if (p
->start_down_word
[i
][j
] != -1)
242 fprintf (outfile
, "%d - %s\n", p
->start_down_word
[i
][j
],
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
)
252 int img_size
= p
->grid_size
* GRID_PIXELS
;
253 FILE * outfile
= fopen (filename
, "wb");
256 fprintf (stderr
, "%s\n", ERROR_WRITING_FILE
);
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 ();
267 for (int i
= 0; i
< p
->grid_size
; i
++)
269 for (int j
= 0; j
< p
->grid_size
; j
++)
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
);
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
);
283 /* print the numers, if it is either start across word or
285 if (p
->start_across_word
[i
][j
] != -1 ||
286 p
->start_down_word
[i
][j
] != -1)
288 if (p
->start_across_word
[i
][j
] != -1)
291 sprintf (str
, "%d", p
->start_across_word
[i
][j
]);
292 gdImageString (img
, sm_fnt
, j
*GRID_PIXELS
+2,
294 (unsigned char *)str
, blue
);
299 sprintf (str
, "%d", p
->start_down_word
[i
][j
]);
300 gdImageString (img
, sm_fnt
, j
*GRID_PIXELS
+2,
302 (unsigned char *)str
, blue
);
305 /* if answerkey is true, draw the character in the cell */
308 gdImageChar (img
, lg_fnt
, j
*GRID_PIXELS
+15,
309 i
*GRID_PIXELS
+10, p
->chars
[i
][j
], black
);
315 gdImagePng (img
, outfile
);
316 gdImageDestroy (img
);
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
);
325 /* Reset the terminal colour */
326 void reset_color () {
330 /* check if the prev row has a block or not */
331 bool prev_row_block (Puzzle
*p
, int r
, int c
)
335 if (p
->chars
[r
-1][c
] == '#')
340 /* check if the next row has a block or not */
341 bool next_row_block (Puzzle
*p
, int r
, int c
)
343 if (r
== p
->grid_size
-1)
345 if (p
->chars
[r
+1][c
] == '#')
350 /* check if the prev col has a block or not */
351 bool prev_col_block (Puzzle
*p
, int r
, int c
)
355 if (p
->chars
[r
][c
-1] == '#')
360 /* check if the next col has a block or not */
361 bool next_col_block (Puzzle
*p
, int r
, int c
)
363 if (c
== p
->grid_size
- 1)
365 if (p
->chars
[r
][c
+1] == '#')
370 /* check if previous row is blank or not */
371 bool prev_row_blank (Puzzle
*p
, int r
, int c
)
373 if (r
== 0) return true;
374 if (p
->chars
[r
-1][c
] == ' ' || p
->chars
[r
-1][c
] == '#') return true;
377 /* check if next row is blank or not */
378 bool next_row_blank (Puzzle
*p
, int r
, int c
)
380 if (r
== p
->grid_size
- 1) return true;
381 if (p
->chars
[r
+1][c
] == ' ' || p
->chars
[r
+1][c
] == '#') return true;
384 /* check if previous col is blank or not */
385 bool prev_col_blank (Puzzle
*p
, int r
, int c
)
387 if (c
== 0) return true;
388 if (p
->chars
[r
][c
-1] == ' ' || p
->chars
[r
][c
-1] == '#') return true;
391 /* check if the next col is blank or not */
392 bool next_col_blank (Puzzle
*p
, int r
, int c
)
394 if (c
== p
->grid_size
-1) return true;
395 if (p
->chars
[r
][c
+1] == ' ' || p
->chars
[r
][c
+1] == '#') return true;
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
)
403 for (int i
= 0; i
< app_data
->puzzle
.grid_size
; i
++)
405 for (int j
= 0; j
< app_data
->puzzle
.grid_size
; j
++)
407 if (orient
== ACROSS
&&
408 app_data
->puzzle
.start_across_word
[i
][j
] == word_index
)
410 app_data
->current_movement
= ACROSS
;
411 app_data
->cur_row
= i
;
412 app_data
->cur_col
= j
;
415 else if (orient
== DOWN
&&
416 app_data
->puzzle
.start_down_word
[i
][j
] == word_index
)
418 app_data
->current_movement
= DOWN
;
419 app_data
->cur_row
= i
;
420 app_data
->cur_col
= j
;
427 /* unfreeze the grid - make editing possible to change words */
428 void unfreeze_puzzle (Puzzle
*p
)
430 for (int i
= 0; i
< p
->grid_size
; i
++)
432 for (int j
= 0; j
< p
->grid_size
; j
++)
434 if (p
->chars
[i
][j
] == '#')
435 p
->chars
[i
][j
] = ' ';
437 p
->start_across_word
[i
][j
] = -1;
438 p
->start_down_word
[i
][j
] = -1;
441 p
->grid_frozen
= false;
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
)
449 bool across_word_start
, down_word_start
;
450 for (int i
= 0; i
< p
->grid_size
; i
++)
452 for (int j
= 0; j
< p
->grid_size
; j
++)
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 */
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;
472 if (across_word_start
== true)
473 p
->start_across_word
[i
][j
] = word_num
;
475 p
->start_across_word
[i
][j
] = -1;
476 if (down_word_start
== true)
477 p
->start_down_word
[i
][j
] = word_num
;
479 p
->start_down_word
[i
][j
] = -1;
480 if (across_word_start
== true || down_word_start
== true)
484 p
->grid_frozen
= true;
487 /* reset the entire grid */
488 void init_puzzle (Puzzle
*p
, int grid_size
)
490 p
->grid_size
= grid_size
;
491 p
->grid_frozen
= false;
492 for (int i
= 0; i
< p
->grid_size
; i
++)
494 for (int j
= 0; j
< p
->grid_size
; j
++)
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
], "");
503 strcpy (p
->hashed_master_password
, "\0");
504 strcpy (p
->hashed_solution_password
, "\0");
508 /* save the puzzle to a file */
509 void save_puzzle (Puzzle
*puzzle
, const char* file
) {
511 /* First output the uncompressed contents to a temp file */
512 outfile
= tmpfile ();
515 fprintf (stderr
, "%s\n", ERROR_WRITING_FILE
);
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
);
527 /* First output the grid characters columns/rows */
528 for (int i
= 0; i
< puzzle
->grid_size
; i
++)
530 for (int j
= 0; j
< puzzle
->grid_size
; j
++)
531 fprintf (outfile
, "%c", puzzle
->chars
[i
][j
]);
532 fprintf (outfile
, "\n");
535 /* Next output the start across/down numbers */
536 for (int i
= 0; i
< puzzle
->grid_size
; i
++)
538 for (int j
= 0; j
< puzzle
->grid_size
; j
++)
540 fprintf (outfile
, "%d ", puzzle
->start_across_word
[i
][j
]);
541 fprintf (outfile
, "%d ", puzzle
->start_down_word
[i
][j
]);
543 fprintf (outfile
, "\n");
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
++)
551 for (int j
= 0; j
< puzzle
->grid_size
; j
++)
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
]);
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
++)
566 for (int j
= 0; j
< puzzle
->grid_size
; j
++)
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
]);
576 /* Flush the buffer and rewind to beginning - to read and save into
577 gzip compressed file */
579 fseek (outfile
, 0, 0);
581 /* now compress the file and save it to destination file */
582 gzFile outdestfile
= gzopen (file
, "wb");
583 if (outdestfile
== NULL
)
585 fprintf (stderr
, "%s\n", ERROR_WRITING_FILE
);
590 int num
= fread (buf
, sizeof(char), sizeof(char)*128, outfile
);
593 int res
= gzwrite (outdestfile
, buf
, num
*sizeof(char) );
596 fprintf (stderr
, "%s %s\n", ERROR_WRITING_FILE
, COMPRESSED
);
600 num
= fread (buf
, sizeof(char), sizeof(char)*128, outfile
);
602 gzclose (outdestfile
);
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
)
613 fprintf (stderr
, "%s %s\n", ERROR_READING_FILE
, COMPRESSED
);
616 /* Open a temporary file to uncompress the contents */
617 FILE *infile
= tmpfile ();
620 fprintf (stderr
, "%s\n", ERROR_READING_FILE
);
623 /* Put the uncompressed content to the temp file */
626 num
= gzread (insourcefile
, buf
, 128);
629 int res
= fwrite (buf
, 1, num
, infile
);
632 fprintf (stderr
, "%s\n", ERROR_READING_FILE
);
634 gzclose (insourcefile
);
637 num
= gzread (insourcefile
, buf
, 128);
639 /* Close the gzip file */
640 gzclose (insourcefile
);
641 /* Flush the temp file buffer and rewind to beginning */
643 fseek (infile
, 0, 0);
645 /* Read the temporary file contents to the structure Puzzle */
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"));
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"));
661 strcpy (p
.hashed_solution_password
, "\0");
663 /* read each character of the grid */
664 for (int i
= 0; i
< p
.grid_size
; i
++ )
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
];
670 /* read the word numbers */
671 for (int i
= 0; i
< p
.grid_size
; i
++)
673 fgets (line
, MAX_CLUE_LENGTH
+ 10, infile
);
674 char *token
= strtok (line
, " ");
675 for (int j
= 0; j
< p
.grid_size
; j
++)
678 p
.start_across_word
[i
][j
] = atoi (token
);
679 token
= strtok (NULL
, " ");
681 p
.start_down_word
[i
][j
] = atoi (token
);
682 token
= strtok (NULL
, " ");
686 fgets (line
, MAX_CLUE_LENGTH
+ 10, infile
);
689 char clues
[100][MAX_CLUE_LENGTH
];
692 /* first read the across clues from file */
695 fgets (line
, MAX_CLUE_LENGTH
+ 10, infile
);
696 /* if reached the end of across clues */
697 if (strcmp (line
, "DOWN\n") == 0)
699 word_num
[c
] = atoi (strtok (line
, "\t"));
700 char *cl
= strtok (NULL
, "\n");
702 strcpy (clues
[c
], cl
);
704 strcpy (clues
[c
], "\0");
707 /* set the clue to the correct cell in grid */
708 for (int i
= 0; i
< p
.grid_size
; i
++)
710 for (int j
= 0; j
< p
.grid_size
; j
++)
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
]);
720 while (fgets (line
, MAX_CLUE_LENGTH
+ 10, infile
))
722 word_num
[c
] = atoi (strtok (line
, "\t"));
723 char* cl
= strtok (NULL
, "\n");
725 strcpy (clues
[c
], cl
);
727 strcpy (clues
[c
], "\0");
730 for (int i
= 0; i
< p
.grid_size
; i
++)
732 for (int j
= 0; j
< p
.grid_size
; j
++)
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
]);
744 /* display the puzzle */
745 void print_puzzle (Puzzle
*p
)
748 set_color (WHITE
, CYAN
, NORMAL
);
750 for (int i
= 0; i
< p
->grid_size
; i
++)
754 for (int i
= 0; i
< p
->grid_size
; i
++)
756 set_color (WHITE
, CYAN
, NORMAL
);
758 for (int j
= 0; j
< p
->grid_size
; j
++)
760 if (p
->chars
[i
][j
] == '#') {
761 set_color (WHITE
, BLACK
, NORMAL
);
766 if (p
->start_across_word
[i
][j
] != -1 ||
767 p
->start_down_word
[i
][j
] != -1)
769 set_color (BLUE
, WHITE
, NORMAL
);
770 if (p
->start_across_word
[i
][j
] != -1)
771 printf ("%-2d", p
->start_across_word
[i
][j
]);
773 printf ("%-2d", p
->start_down_word
[i
][j
]);
777 set_color (BLACK
, WHITE
,NORMAL
);
781 set_color (BLACK
, WHITE
, BOLD
);
782 printf ("%c", p
->chars
[i
][j
]);
788 /* print the clues if set */
789 if (p
->grid_frozen
== true)
791 printf ("\x1B[1mACROSS - CLUES\x1B[0m\n");
792 for (int i
= 0; i
< p
->grid_size
; i
++)
794 for (int j
= 0; j
< p
->grid_size
; j
++)
796 if (p
->start_across_word
[i
][j
] != -1)
798 printf ("%d - %s; ", p
->start_across_word
[i
][j
],
799 p
->clue_across
[i
][j
]);
803 printf ("\n\x1B[1mDOWN - CLUES\x1B[0m\n");
804 for (int i
= 0; i
< p
->grid_size
; i
++)
806 for (int j
= 0; j
< p
->grid_size
; j
++)
808 if (p
->start_down_word
[i
][j
] != -1)
810 printf ("%d - %s; ", p
->start_down_word
[i
][j
],
819 /* function to check if a word is valid or not */
820 char* is_valid_word (char *word
)
822 if (word
== NULL
|| strlen(word
) == 0)
824 for (int i
= 0; i
< strlen (word
) - 1; i
++)
825 if (! isalpha (word
[i
]))
828 return strtok (word
, "\n");
832 /* function to set a clue for an across word */
833 bool set_clue (Puzzle
*p
, String clue
, int index
, enum ORIENTATION order
)
835 for (int i
= 0; i
< p
->grid_size
; i
++)
837 for (int j
= 0; j
< p
->grid_size
; j
++)
841 if (p
->start_across_word
[i
][j
] == index
)
843 strcpy (p
->clue_across
[i
][j
], clue
);
847 else if (order
== DOWN
)
849 if (p
->start_down_word
[i
][j
] == index
)
851 strcpy (p
->clue_down
[i
][j
], clue
);
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
)
865 printf ("\e[1;1H\e[2J");
866 set_color (fg
, bg
, NORMAL
);
868 for (int i
= 0; i
< padding
; i
++)
871 reset_color (); printf ("\n");
872 set_color (fg
, bg
, NORMAL
);
874 set_color (fg
, bg
, BOLD
);
875 printf ("%-*s", padding
, title
);
877 set_color (fg
, bg
, NORMAL
);
879 reset_color (); printf ("\n");
880 set_color (fg
, bg
, NORMAL
);
882 for (int i
= 0; i
< padding
; i
++)
885 reset_color (); printf ("\n");
886 for (int i
= 0; i
< num_items
; i
++)
888 set_color (fg
, bg
, NORMAL
);
889 printf ("\u2551%-*s\u2551", padding
, items
[i
]);
890 reset_color (); printf ("\n");
892 set_color (fg
, bg
, NORMAL
);
894 for (int i
= 0; i
< padding
; i
++)
897 reset_color (); printf ("\n");
900 /* reset the player data, loading from the puzzle file */
901 void reset_player_data (MainPlayerData
*app_data
, const char *filename
)
903 app_data
->puzzle
= load_puzzle (filename
);
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
] = ' ';
917 /* in the player app, move the current selection index left or right */
918 void move_current_col (MainPlayerData
*app_data
, enum DIRECTION dir
)
920 int r
= app_data
->cur_row
;
921 int c
= app_data
->cur_col
;
922 if (dir
== DIR_FORWARD
)
925 while (c
< app_data
->puzzle
.grid_size
)
927 if (app_data
->puzzle
.chars
[r
][c
] == '#')
932 if (c
< app_data
->puzzle
.grid_size
)
933 app_data
->cur_col
= c
;
940 if (app_data
->puzzle
.chars
[r
][c
] == '#')
946 app_data
->cur_col
= c
;
950 /* in the player app move the current selection index up or down */
951 void move_current_row (MainPlayerData
*app_data
, enum DIRECTION dir
)
953 int r
= app_data
->cur_row
;
954 int c
= app_data
->cur_col
;
955 if (dir
== DIR_FORWARD
)
958 while (r
< app_data
->puzzle
.grid_size
)
960 if (app_data
->puzzle
.chars
[r
][c
] == '#')
965 if (r
< app_data
->puzzle
.grid_size
)
966 app_data
->cur_row
= r
;
973 if (app_data
->puzzle
.chars
[r
][c
] == '#')
979 app_data
->cur_row
= r
;