+/* get a number from the user */
+int get_num ()
+{
+ char s[5];
+ fgets (s, 5, stdin);
+ int n = atoi (s);
+ return n;
+}
+
+/* verify password */
+bool verify_password (Puzzle *p, const char* password)
+{
+ /* no password set */
+ if (strcmp (p->hashed_password, "\0") == 0)
+ return true;
+
+ /* hash the user input password and compare it with the stored password */
+ char* hashed_password = crypt (password, (const char *)p->salt);
+
+ if (strcmp (p->hashed_password, hashed_password) == 0)
+ return true;
+
+ return false;
+}
+
+/* Set or reset password for puzzle */
+void set_puzzle_password (Puzzle *p, const char *password)
+{
+ /* if it is a null string, reset the password */
+ if (strcmp (password, "\0") == 0)
+ {
+ strcpy (p->hashed_password, "\0");
+ strcpy (p->salt, "\0");
+ }
+ else
+ {
+ srand (time(NULL));
+ char salt[256];
+ sprintf (salt, "puzzle%d", rand()%1000);
+ char* hashedpwd = crypt (password, (const char*)salt);
+ strcpy (p->hashed_password, hashedpwd);
+ strcpy (p->salt, salt);
+ }
+}
+
+/* Output the clues to text file */
+void export_clues (Puzzle *p, const char *filename)
+{
+ FILE *outfile = fopen (filename, "w");
+ if (outfile == NULL)
+ {
+ fprintf (stderr, "%s\n", ERROR_WRITING_FILE);
+ exit (1);
+ }
+ /* first the across clues */
+ fprintf (outfile, "ACROSS CLUES\n");
+ for (int i = 0; i < p->grid_size; i ++)
+ {
+ for (int j = 0; j < p->grid_size; j ++)
+ {
+ if (p->start_across_word[i][j] != -1)
+ fprintf (outfile, "%d - %s\n", p->start_across_word[i][j],
+ p->clue_across[i][j]);
+ }
+ }
+ /* now the down clues */
+ fprintf (outfile, "DOWN CLUES\n");
+ for (int i = 0; i < p->grid_size; i ++)
+ {
+ for (int j = 0; j < p->grid_size; j ++)
+ {
+ if (p->start_down_word[i][j] != -1)
+ fprintf (outfile, "%d - %s\n", p->start_down_word[i][j],
+ p->clue_down[i][j]);
+ }
+ }
+ fclose (outfile);
+}
+
+/* Output the grid to image - if answerkey is true export filled grid */
+void export_grid_image (Puzzle *p, const char *filename, bool answerkey)
+{
+ int img_size = p->grid_size * GRID_PIXELS;
+ FILE * outfile = fopen (filename, "wb");
+ if (outfile == NULL)
+ {
+ fprintf (stderr, "%s\n", ERROR_WRITING_FILE);
+ exit (1);
+ }
+
+ gdImagePtr img = gdImageCreate (img_size, img_size);
+ gdImageColorAllocate (img, 255,255,255);
+ int black = gdImageColorAllocate (img, 0, 0, 0);
+ int blue = gdImageColorAllocate (img, 0, 0, 216);
+ gdFontPtr sm_fnt = gdFontGetMediumBold ();
+ gdFontPtr lg_fnt = gdFontGetGiant ();
+
+ for (int i = 0; i < p->grid_size; i ++)
+ {
+ for (int j = 0; j < p->grid_size; j++)
+ {
+ /* if it is a block, draw the black square */
+ if (p->chars[i][j] == '#')
+ gdImageFilledRectangle (img, j*GRID_PIXELS, i*GRID_PIXELS,
+ j*GRID_PIXELS+GRID_PIXELS,
+ i*GRID_PIXELS+GRID_PIXELS,black);
+ else
+ {
+ /* draw a regular square */
+ gdImageRectangle (img, j*GRID_PIXELS, i*GRID_PIXELS,
+ j*GRID_PIXELS+GRID_PIXELS,
+ i*GRID_PIXELS+GRID_PIXELS, black);
+
+ /* print the numers, if it is either start across word or
+ a down word */
+ if (p->start_across_word[i][j] != -1 ||
+ p->start_down_word[i][j] != -1)
+ {
+ if (p->start_across_word[i][j] != -1)
+ {
+ char str[5];
+ sprintf (str, "%d", p->start_across_word[i][j]);
+ gdImageString (img, sm_fnt, j*GRID_PIXELS+2,
+ i*GRID_PIXELS+2,
+ (unsigned char *)str, blue);
+ }
+ else
+ {
+ char str[5];
+ sprintf (str, "%d", p->start_down_word[i][j]);
+ gdImageString (img, sm_fnt, j*GRID_PIXELS+2,
+ i*GRID_PIXELS+2,
+ (unsigned char *)str, blue);
+ }
+ }
+ /* if answerkey is true, draw the character in the cell */
+ if (answerkey)
+ {
+ gdImageChar (img, lg_fnt, j*GRID_PIXELS+15,
+ i*GRID_PIXELS+10, p->chars[i][j], black);
+ }
+ }
+ }
+ }
+
+ gdImagePng (img, outfile);
+ gdImageDestroy (img);
+ fclose (outfile);
+}
+