9 #include "constantstrings.h"
11 /* export the clues to a text file */
12 void do_export_clues (Puzzle
*p
)
14 if (p
->grid_frozen
== false)
16 printf (UNFROZEN_GRID
);
22 fgets (fname
, 256, stdin
);
23 if (strlen(fname
) == 1)
25 char *filename
= strtok (fname
, "\n");
27 export_clues (p
, filename
);
32 /* export the puzzle to a png file */
33 void do_export_puzzle (Puzzle
*p
)
35 if (p
->grid_frozen
== false)
37 printf (UNFROZEN_GRID
);
43 fgets (fname
, 256, stdin
);
44 if (strlen (fname
) == 1)
46 char* filename
= strtok (fname
, "\n");
48 printf (INPUT_EXPORT_ANSWERS
);
50 fgets (ans
, 3, stdin
);
52 solution
= (toupper (ans
[0]) == 'Y') ? true : false;
54 export_grid_image (p
, filename
, solution
);
60 void do_reset_puzzle (Puzzle
*p
)
62 int grid_size
= p
->grid_size
;
63 printf (INPUT_CONFIRM_RESET
);
65 fgets (conf
, 3, stdin
);
66 if (toupper (conf
[0]) == 'Y')
67 init_puzzle (p
, grid_size
);
73 /* set the solution password for the puzzle */
74 void do_set_solution_password (Puzzle
*p
)
77 password
= getpass (INPUT_PASSWORD
);
78 /* if empty reset the password to nothing */
79 if (strlen(password
) == 0)
81 set_solution_password (p
, "\0");
82 printf (SOLUTION_PASSWORD_RESET
);
85 /* set the password */
88 set_solution_password (p
, (const char* )password
);
89 printf (PASSWORD_SET
);
93 /* set the master (editing) password for the puzzle */
94 void do_set_master_password (Puzzle
*p
)
97 password
= getpass (INPUT_PASSWORD
);
98 /* if empty reset the password to nothing */
99 if (strlen(password
) == 0)
101 set_master_password (p
, "\0");
102 printf (MASTER_PASSWORD_RESET
);
103 char ch
= getchar ();
105 /* set the password */
108 set_master_password (p
, (const char* )password
);
109 printf (PASSWORD_SET
);
110 char ch
= getchar ();
114 /* set clue for a word - only for frozen grid */
115 void do_set_clue_word (Puzzle
*p
, enum ORIENTATION orient
)
118 if (p
->grid_frozen
== false)
120 printf (UNFROZEN_GRID
);
121 char ch
= getchar ();
126 printf (INPUT_INDEX
);
129 fgets (clue
, MAX_CLUE_LENGTH
, stdin
);
130 if (strlen (clue
) == 1)
132 char* cl
= strtok (clue
, "\n");
135 res
= set_clue (p
, cl
, index
, orient
);
139 printf (NO_WORD_INDEX
);
140 char ch
= getchar ();
144 /* clear a cell in the grid */
145 void do_clear_cell (Puzzle
*p
)
148 if (p
->grid_frozen
== true)
150 printf (FROZEN_GRID
);
151 char ch
= getchar ();
159 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
161 printf (EXCEED_GRID_SIZE
);
162 char ch
= getchar ();
165 p
->chars
[row
][col
] = ' ';
168 char ch
= getchar ();
171 /* add a down word to the grid */
172 void do_add_down_word (Puzzle
*p
)
175 if (p
->grid_frozen
== true)
177 printf (FROZEN_GRID
);
178 char ch
= getchar ();
181 char wd
[MAX_PUZZLE_SIZE
];
184 fgets (wd
, MAX_PUZZLE_SIZE
, stdin
);
185 char *word
= is_valid_word (wd
);
188 printf (INVALID_WORD
);
189 char ch
= getchar ();
196 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
198 printf (EXCEED_GRID_SIZE
);
199 char ch
= getchar ();
202 if (strlen (word
) > (p
->grid_size
- row
))
204 printf (WORD_TOO_LONG
);
205 char ch
= getchar ();
209 for (int i
= row
; i
< row
+ strlen(word
); i
++)
210 p
->chars
[i
][col
] = toupper(word
[i
- row
]);
213 char ch
= getchar ();
216 /* add an across word to the grid */
217 void do_add_across_word (Puzzle
*p
)
220 if (p
->grid_frozen
== true)
222 printf (FROZEN_GRID
);
223 char ch
= getchar ();
226 char wd
[MAX_PUZZLE_SIZE
];
229 fgets (wd
, MAX_PUZZLE_SIZE
, stdin
);
230 char *word
= is_valid_word (wd
);
233 printf (INVALID_WORD
);
234 char ch
= getchar ();
241 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
243 printf (EXCEED_GRID_SIZE
);
244 char ch
= getchar ();
248 if (strlen (word
) > (p
->grid_size
- col
))
250 printf (WORD_TOO_LONG
);
251 char ch
= getchar ();
255 for (int i
= col
; i
< col
+strlen (word
); i
++)
256 p
->chars
[row
][i
] = toupper(word
[i
- col
]);
259 char ch
= getchar ();
262 bool do_confirm_exit ()
264 printf (INPUT_CONFIRM_EXIT
);
266 fgets (res
, 3, stdin
);
267 if (toupper(res
[0]) == 'Y')
273 /* main loop for the puzzle editor */
274 void puzzle_editor_loop (Puzzle
*p
, const char *filename
)
279 char puzzle_title
[60];
280 sprintf (puzzle_title
, "%s - %s", PUZZLE_MENU_TITLE
, filename
);
281 print_menu (WHITE
, BLUE
, puzzle_title
, PUZZLE_EDIT_MENU
, 15, 50);
282 printf (INPUT_CHOICE
);
286 case 1: print_puzzle (p
);
287 char ch
= getchar ();
289 case 2: do_add_across_word (p
);
291 case 3: do_add_down_word (p
);
293 case 4: do_clear_cell (p
);
295 case 5: freeze_puzzle (p
);
299 case 6: unfreeze_puzzle (p
);
303 case 7: do_set_clue_word (p
, ACROSS
);
305 case 8: do_set_clue_word (p
, DOWN
);
307 case 9: save_puzzle (p
, filename
);
308 printf ("%s\n",FILE_SAVED
);
311 case 10: do_set_master_password (p
);
313 case 11: do_set_solution_password (p
);
315 case 12: do_reset_puzzle (p
);
317 case 13: do_export_puzzle (p
);
319 case 14: do_export_clues (p
);
321 case 15: loop
= ! do_confirm_exit ();
327 /* open an existing puzzle */
328 void do_open_puzzle (const char *filename
)
331 /* if no filename is provided, get it from command line */
332 if (filename
== NULL
)
336 fgets(fname
, 256, stdin
);
337 if (strlen (fname
) == 1)
339 filename
= strtok (fname
, "\n");
342 p
= load_puzzle (filename
);
343 if (p
.grid_size
== 0)
345 printf (INVALID_PUZZLE
);
346 char ch
= getchar ();
350 if (strcmp (p
.hashed_master_password
, "\0") != 0)
353 passwd
= getpass (INPUT_PASSWORD
);
354 if (strlen (passwd
) == 0)
357 if (verify_master_password (&p
, (const char*) passwd
))
358 puzzle_editor_loop (&p
, filename
);
361 printf (WRONG_PASSWORD
);
362 char ch
= getchar ();
366 puzzle_editor_loop (&p
, filename
);
369 /* create a new blank puzzle */
370 void do_new_puzzle (char *filename
, int size
)
373 /* if filename is not provided get it from command line */
374 if (filename
== NULL
)
378 fgets (fname
, 256, stdin
);
379 if (strlen (fname
) == 1)
381 filename
= strtok (fname
, "\n");
383 /* if no size is specified get it from command line */
386 printf (INPUT_GRID_SIZE
);
390 if (size
> MAX_PUZZLE_SIZE
)
392 printf (EXCEED_MAX_GRID_SIZE
);
397 init_puzzle (&p
, size
);
398 puzzle_editor_loop (&p
, filename
);
402 /* The main loop of the program */
405 /* Print the main menu */
408 print_menu (WHITE
, BLUE
, MAIN_MENU_TITLE
, MAIN_MENU
, 3, 50);
409 printf (INPUT_CHOICE
);
413 case 1: do_new_puzzle (NULL
, -1);
415 case 2: do_open_puzzle (NULL
);
422 int main (int argc
, char* argv
[])
429 case 2 : do_open_puzzle (argv
[1]);
431 case 4 : if (strcmp (argv
[2], "new") == 0)
433 int grid_size
= atoi (argv
[3]);
434 do_new_puzzle (argv
[1], grid_size
);
437 default: fprintf (stderr
, USAGE_LINE_1
, argv
[0]);
438 fprintf (stderr
, USAGE_LINE_2
);
439 fprintf (stderr
, USAGE_LINE_3
);
443 return (main_loop ());