#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 */
-void export_puzzle (Puzzle *p)
+void do_export_puzzle (Puzzle *p)
{
if (p->grid_frozen == false)
{
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 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 */
-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)
}
/* 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)
}
/* 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)
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 ();
}
/* 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)
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 (INPUT_ROW);
row = get_num ();
printf (INPUT_COL);
char ch = getchar ();
}
/* confirm exit */
-bool confirm_exit ()
+bool do_confirm_exit ()
{
printf (INPUT_CONFIRM_EXIT);
char res[3];
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)
case 1: print_puzzle (p);
char ch = getchar ();
break;
- case 2: add_across_word (p);
+ case 2: do_add_across_word (p);
break;
- case 3: add_down_word (p);
+ case 3: do_add_down_word (p);
break;
- case 4: clear_cell (p);
+ case 4: do_clear_cell (p);
break;
case 5: freeze_puzzle (p);
print_puzzle (p);
print_puzzle (p);
ch = getchar ();
break;
- case 7: set_clue_word (p, ACROSS);
+ case 7: do_set_clue_word (p, ACROSS);
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;
- case 10: export_puzzle (p);
+ case 10: do_reset_puzzle (p);
+ print_puzzle (p);
+ ch = getchar ();
+ break;
+ case 11: do_export_puzzle (p);
break;
- case 11: loop = !confirm_exit ();
+ case 12: do_export_clues (p);
+ break;
+ case 13: loop = ! do_confirm_exit ();
break;
}
}
}
/* open an existing puzzle */
-void open_puzzle ()
+void do_open_puzzle ()
{
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 */
-void new_puzzle ()
+void do_new_puzzle ()
{
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;
int ch = get_num ();
switch (ch)
{
- case 1: new_puzzle ();
+ case 1: do_new_puzzle ();
break;
- case 2: open_puzzle ();
+ case 2: do_open_puzzle ();
break;
case 3: exit (0);
}
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)
{
}
/* 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");
}
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");