X-Git-Url: https://harishankar.org/repos/?p=wordblah.git;a=blobdiff_plain;f=wordblox.h;h=bd286e3b2ba5207c20f030db7b1a18f78df18adc;hp=f7cd6d2a4c2bba8b5104b7a20ffeaed34b85543f;hb=762979409c32bd4902716911101a1d1071f11038;hpb=a9394973f79c66abefe200c4a0033cc9e3361e26 diff --git a/wordblox.h b/wordblox.h index f7cd6d2..bd286e3 100644 --- a/wordblox.h +++ b/wordblox.h @@ -1,6 +1,9 @@ #ifndef __WORDBLOX_H #define __WORDBLOX_H +#include +#include +#include #include "constantstrings.h" #define MAX_PUZZLE_SIZE 20 @@ -41,6 +44,7 @@ typedef struct { bool grid_frozen; } Puzzle; +/* get a number from the user */ int get_num () { char s[5]; @@ -49,6 +53,107 @@ int get_num () return n; } +/* 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 * 40; + FILE * outfile = fopen (filename, "wb"); + if (outfile == NULL) + { + fprintf (stderr, "%s\n", ERROR_WRITING_FILE); + exit (1); + } + + gdImagePtr img = gdImageCreate (img_size, img_size); + int white = 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*40, i*40, j*40+40, + i*40+40,black); + else + { + /* draw a regular square */ + gdImageRectangle (img, j*40, i*40, j*40+40, + i*40+40, 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*40+2, i*40+2, + (unsigned char *)str, blue); + } + else + { + char str[5]; + sprintf (str, "%d", p->start_down_word[i][j]); + gdImageString (img, sm_fnt, j*40+2, i*40+2, + (unsigned char *)str, blue); + } + } + /* if answerkey is true, draw the character in the cell */ + if (answerkey) + { + gdImageChar (img, lg_fnt, j*40+15, i*40+15, + p->chars[i][j], black); + } + } + } + } + + gdImagePng (img, outfile); + gdImageDestroy (img); + fclose (outfile); +} + /* Set the terminal colour */ void set_color (enum COLOR fg, enum COLOR bg, enum ATTR at) { printf ("\x1B[%d;%d;%dm", fg+30, bg+40, at); @@ -270,13 +375,15 @@ void print_puzzle (Puzzle *p) } /* function to check if a word is valid or not */ -bool is_valid_word (const char *word) +char* is_valid_word (char *word) { - for (int i = 0; i < strlen (word); i ++) + if (word == NULL || strlen(word) == 0) + return NULL; + for (int i = 0; i < strlen (word) - 1; i ++) if (! isalpha (word[i])) - return false; + return NULL; - return true; + return strtok (word, "\n"); } @@ -320,8 +427,12 @@ void print_menu (enum COLOR fg, enum COLOR bg, const char* title, printf ("\u2550"); printf ("\u2557"); reset_color (); printf ("\n"); + printf ("\u2551"); set_color (fg, bg, BOLD); - printf ("\u2551%-*s\u2551", padding, title); + printf ("%-*s", padding, title); + reset_color (); + set_color (fg, bg, NORMAL); + printf ("\u2551"); reset_color (); printf ("\n"); set_color (fg, bg, NORMAL); printf ("\u2560");