Export clues to Text file completed
authorHarishankar <v.harishankar@gmail.com>
Thu, 30 Apr 2020 15:37:18 +0000 (21:07 +0530)
committerHarishankar <v.harishankar@gmail.com>
Thu, 30 Apr 2020 15:37:18 +0000 (21:07 +0530)
Completed the feature to export clues to text file

constantstrings.h
wordblox.c
wordblox.h

index 050c890..76fede7 100644 (file)
@@ -24,6 +24,8 @@
 #define NO_WORD_INDEX "No such word with specified index" 
 #define INPUT_CONFIRM_EXIT "Are you sure you wish to exit? \
 Unsaved changes will be lost [y/N]"
 #define NO_WORD_INDEX "No such word with specified index" 
 #define INPUT_CONFIRM_EXIT "Are you sure you wish to exit? \
 Unsaved changes will be lost [y/N]"
+#define INPUT_CONFIRM_RESET "Are you sure? This will destroy the entire grid\
+and is irreversible (y/N): "
 
 #define USAGE_LINE_1 "Usage: %s [<filename> [new <nn>]]\n"
 #define USAGE_LINE_2 "<filename> - puzzle file name\n"
 
 #define USAGE_LINE_1 "Usage: %s [<filename> [new <nn>]]\n"
 #define USAGE_LINE_2 "<filename> - puzzle file name\n"
@@ -45,7 +47,9 @@ char *PUZZLE_EDIT_MENU[] =
                                        "7. Set Clue - Across Word",
                                        "8. Set Clue - Down Word",
                                        "9. Save puzzle",
                                        "7. Set Clue - Across Word",
                                        "8. Set Clue - Down Word",
                                        "9. Save puzzle",
-                                       "10.Export puzzle as PNG image",
-                                       "11.Return to main menu" };
+                                       "10.Reset entire grid",
+                                       "11.Export puzzle as PNG image",
+                                       "12.Export clues to text file",
+                                       "13.Return to main menu" };
 
 #endif
 
 #endif
index 223dde7..d840b6d 100644 (file)
@@ -8,8 +8,29 @@
 #include "wordblox.h"
 #include "constantstrings.h"
 
 #include "wordblox.h"
 #include "constantstrings.h"
 
+/* export the clues to a text file */
+void do_export_clues (Puzzle *p)
+{
+       if (p->grid_frozen == false)
+       {
+               printf (UNFROZEN_GRID);
+               char ch = getchar ();
+               return;
+       }
+       char fname[256];
+       printf (INPUT_FILE);
+       fgets (fname, 256, stdin);
+       if (strlen(fname) == 1)
+               return;
+       char *filename = strtok (fname, "\n");
+       
+       export_clues (p, filename);
+       printf (FILE_SAVED);
+       char ch = getchar ();
+}
+
 /* export the puzzle to a png file */
 /* export the puzzle to a png file */
-void export_puzzle (Puzzle *p)
+void do_export_puzzle (Puzzle *p)
 {
        if (p->grid_frozen == false)
        {
 {
        if (p->grid_frozen == false)
        {
@@ -20,6 +41,8 @@ void export_puzzle (Puzzle *p)
        char fname[256];
        printf (INPUT_FILE);
        fgets (fname, 256, stdin);
        char fname[256];
        printf (INPUT_FILE);
        fgets (fname, 256, stdin);
+       if (strlen (fname) == 1)
+               return;
        char* filename = strtok (fname, "\n");
 
        printf (INPUT_EXPORT_ANSWERS);
        char* filename = strtok (fname, "\n");
 
        printf (INPUT_EXPORT_ANSWERS);
@@ -33,8 +56,19 @@ void export_puzzle (Puzzle *p)
        char ch = getchar ();
 }
 
        char ch = getchar ();
 }
 
+/* reset the grid */
+void do_reset_puzzle (Puzzle *p)
+{
+       int grid_size = p->grid_size;
+       printf (INPUT_CONFIRM_RESET);
+       char conf[3];
+       fgets (conf, 3, stdin);
+       if (toupper (conf[0]) == 'Y')
+               init_puzzle (p, grid_size);
+}
+
 /* set clue for a word - only for frozen grid */
 /* set clue for a word - only for frozen grid */
-void set_clue_word (Puzzle *p, enum ORIENTATION orient)
+void do_set_clue_word (Puzzle *p, enum ORIENTATION orient)
 {
        print_puzzle (p);
        if (p->grid_frozen == false)
 {
        print_puzzle (p);
        if (p->grid_frozen == false)
@@ -62,7 +96,7 @@ void set_clue_word (Puzzle *p, enum ORIENTATION orient)
 }
 
 /* clear a cell in the grid */
 }
 
 /* clear a cell in the grid */
-void clear_cell (Puzzle *p)
+void do_clear_cell (Puzzle *p)
 {
        print_puzzle (p);
        if (p->grid_frozen == true) 
 {
        print_puzzle (p);
        if (p->grid_frozen == true) 
@@ -89,7 +123,7 @@ void clear_cell (Puzzle *p)
 }
 
 /* add a down word to the grid */
 }
 
 /* add a down word to the grid */
-void add_down_word (Puzzle *p)
+void do_add_down_word (Puzzle *p)
 {
        print_puzzle (p);
        if (p->grid_frozen == true) 
 {
        print_puzzle (p);
        if (p->grid_frozen == true) 
@@ -102,8 +136,8 @@ void add_down_word (Puzzle *p)
        int row, col;
        printf (INPUT_WORD);
        fgets (wd, MAX_PUZZLE_SIZE, stdin);
        int row, col;
        printf (INPUT_WORD);
        fgets (wd, MAX_PUZZLE_SIZE, stdin);
-       char *word = strtok (wd, "\n");
-       if (! is_valid_word (word))
+       char *word = is_valid_word (wd);
+       if (word == NULL)
        {
                printf (INVALID_WORD);
                char ch = getchar ();
        {
                printf (INVALID_WORD);
                char ch = getchar ();
@@ -134,7 +168,7 @@ void add_down_word (Puzzle *p)
 }
 
 /* add an across word to the grid */
 }
 
 /* add an across word to the grid */
-void add_across_word (Puzzle *p)
+void do_add_across_word (Puzzle *p)
 {
        print_puzzle (p);
        if (p->grid_frozen == true) 
 {
        print_puzzle (p);
        if (p->grid_frozen == true) 
@@ -147,13 +181,13 @@ void add_across_word (Puzzle *p)
        int row, col;   
        printf (INPUT_WORD);
        fgets (wd, MAX_PUZZLE_SIZE, stdin);
        int row, col;   
        printf (INPUT_WORD);
        fgets (wd, MAX_PUZZLE_SIZE, stdin);
-       char *word = strtok (wd, "\n");
-       if (! is_valid_word (word))
+       char *word = is_valid_word (wd);
+       if (word == NULL)
        {
                printf (INVALID_WORD);
                char ch = getchar ();
                return;
        {
                printf (INVALID_WORD);
                char ch = getchar ();
                return;
-       }       
+       }
        printf (INPUT_ROW);
        row = get_num ();
        printf (INPUT_COL);
        printf (INPUT_ROW);
        row = get_num ();
        printf (INPUT_COL);
@@ -179,7 +213,7 @@ void add_across_word (Puzzle *p)
        char ch = getchar ();
 }
 /* confirm exit */
        char ch = getchar ();
 }
 /* confirm exit */
-bool confirm_exit ()
+bool do_confirm_exit ()
 {
        printf (INPUT_CONFIRM_EXIT);
        char res[3];
 {
        printf (INPUT_CONFIRM_EXIT);
        char res[3];
@@ -196,7 +230,7 @@ void puzzle_editor_loop (Puzzle *p, const char *filename)
        bool loop = true;
        while (loop) 
        {
        bool loop = true;
        while (loop) 
        {
-               print_menu (WHITE, RED, PUZZLE_MENU_TITLE, PUZZLE_EDIT_MENU, 11, 50);
+               print_menu (WHITE, BLUE, PUZZLE_MENU_TITLE, PUZZLE_EDIT_MENU, 13, 50);
                printf (INPUT_CHOICE);
                int ch = get_num ();
                switch (ch)
                printf (INPUT_CHOICE);
                int ch = get_num ();
                switch (ch)
@@ -204,11 +238,11 @@ void puzzle_editor_loop (Puzzle *p, const char *filename)
                        case 1: print_puzzle (p);
                                        char ch = getchar ();
                                        break;
                        case 1: print_puzzle (p);
                                        char ch = getchar ();
                                        break;
-                       case 2: add_across_word (p);
+                       case 2: do_add_across_word (p);
                                        break;
                                        break;
-                       case 3: add_down_word (p);
+                       case 3: do_add_down_word (p);
                                        break;
                                        break;
-                       case 4: clear_cell (p);
+                       case 4: do_clear_cell (p);
                                        break;
                        case 5: freeze_puzzle (p);
                                        print_puzzle (p);
                                        break;
                        case 5: freeze_puzzle (p);
                                        print_puzzle (p);
@@ -218,41 +252,51 @@ void puzzle_editor_loop (Puzzle *p, const char *filename)
                                        print_puzzle (p);
                                        ch = getchar ();
                                        break;
                                        print_puzzle (p);
                                        ch = getchar ();
                                        break;
-                       case 7: set_clue_word (p, ACROSS);
+                       case 7: do_set_clue_word (p, ACROSS);
                                        break;
                                        break;
-                       case 8: set_clue_word (p, DOWN);
+                       case 8: do_set_clue_word (p, DOWN);
                                        break;
                        case 9: save_puzzle (p, filename);
                                        printf ("%s\n",FILE_SAVED);
                                        ch = getchar ();
                                        break;
                                        break;
                        case 9: save_puzzle (p, filename);
                                        printf ("%s\n",FILE_SAVED);
                                        ch = getchar ();
                                        break;
-                       case 10: export_puzzle (p);
+                       case 10: do_reset_puzzle (p);
+                                        print_puzzle (p);
+                                        ch = getchar ();
+                                        break;
+                       case 11: do_export_puzzle (p);
                                         break;
                                         break;
-                       case 11: loop = !confirm_exit ();
+                       case 12: do_export_clues (p);
+                                        break;
+                       case 13: loop = ! do_confirm_exit ();
                                         break;
                }
        }
 }
 
 /* open an existing puzzle */
                                         break;
                }
        }
 }
 
 /* open an existing puzzle */
-void open_puzzle () 
+void do_open_puzzle () 
 {
        Puzzle p;
        printf (INPUT_FILE);
        char fname[256];
        fgets(fname, 256, stdin);
 {
        Puzzle p;
        printf (INPUT_FILE);
        char fname[256];
        fgets(fname, 256, stdin);
+       if (strlen (fname) == 1)
+               return;
        char* filename = strtok (fname, "\n");
        p = load_puzzle (filename);
        puzzle_editor_loop (&p, filename);
 }
 
 /* create a new blank puzzle */
        char* filename = strtok (fname, "\n");
        p = load_puzzle (filename);
        puzzle_editor_loop (&p, filename);
 }
 
 /* create a new blank puzzle */
-void new_puzzle () 
+void do_new_puzzle () 
 {
        Puzzle p; 
        printf (INPUT_FILE);
        char fname[256];
        fgets (fname, 256, stdin);
 {
        Puzzle p; 
        printf (INPUT_FILE);
        char fname[256];
        fgets (fname, 256, stdin);
+       if (strlen (fname) == 1)
+               return;
        char* filename = strtok (fname, "\n"); 
        printf (INPUT_GRID_SIZE);
        int size;
        char* filename = strtok (fname, "\n"); 
        printf (INPUT_GRID_SIZE);
        int size;
@@ -280,9 +324,9 @@ int main_loop ()
                int ch = get_num ();
                switch (ch)
                {
                int ch = get_num ();
                switch (ch)
                {
-                       case 1: new_puzzle ();
+                       case 1: do_new_puzzle ();
                                        break;
                                        break;
-                       case 2: open_puzzle ();
+                       case 2: do_open_puzzle ();
                                        break;
                        case 3: exit (0);
                }
                                        break;
                        case 3: exit (0);
                }
index 53c522b..bd286e3 100644 (file)
@@ -53,6 +53,40 @@ int get_num ()
        return n;
 }
 
        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) 
 {
 /* Output the grid to image - if answerkey is true export filled grid */
 void export_grid_image  (Puzzle *p, const char *filename, bool answerkey) 
 {
@@ -341,13 +375,15 @@ void print_puzzle (Puzzle *p)
 }
 
 /* function to check if a word is valid or not */
 }
 
 /* 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])) 
                if (! isalpha (word[i])) 
-                       return false;
+                       return NULL;
                        
                        
-       return true;
+       return strtok (word, "\n");
 }
 
 
 }
 
 
@@ -391,8 +427,12 @@ void print_menu (enum COLOR fg, enum COLOR bg, const char* title,
                        printf ("\u2550");
                printf ("\u2557"); 
                reset_color (); printf ("\n");
                        printf ("\u2550");
                printf ("\u2557"); 
                reset_color (); printf ("\n");
+               printf ("\u2551");
                set_color (fg, bg, BOLD);
                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");
                reset_color (); printf ("\n");
                set_color (fg, bg, NORMAL);
                printf ("\u2560");