10 #include "constantstrings.h"
12 /* export the clues to a text file */
13 void do_export_clues (Puzzle
*p
)
15 if (p
->grid_frozen
== false)
17 printf (UNFROZEN_GRID
);
23 fgets (fname
, 256, stdin
);
24 if (strlen(fname
) == 1)
26 char *filename
= strtok (fname
, "\n");
28 export_clues (p
, filename
);
33 /* export the puzzle to a png file */
34 void do_export_puzzle (Puzzle
*p
)
36 if (p
->grid_frozen
== false)
38 printf (UNFROZEN_GRID
);
44 fgets (fname
, 256, stdin
);
45 if (strlen (fname
) == 1)
47 char* filename
= strtok (fname
, "\n");
49 printf (INPUT_EXPORT_ANSWERS
);
51 fgets (ans
, 3, stdin
);
53 solution
= (toupper (ans
[0]) == 'Y') ? true : false;
55 export_grid_image (p
, filename
, solution
);
61 void do_reset_puzzle (Puzzle
*p
)
63 int grid_size
= p
->grid_size
;
64 printf (INPUT_CONFIRM_RESET
);
66 fgets (conf
, 3, stdin
);
67 if (toupper (conf
[0]) == 'Y')
68 init_puzzle (p
, grid_size
);
74 /* set the password for the puzzle */
75 void do_set_password (Puzzle
*p
)
78 password
= getpass (INPUT_PASSWORD
);
79 /* if empty reset the password to nothing */
80 if (strlen(password
) == 0)
82 set_puzzle_password (p
, "\0");
83 printf (PASSWORD_RESET
);
86 /* set the password */
89 set_puzzle_password (p
, (const char* )password
);
90 printf (PASSWORD_SET
);
95 /* set clue for a word - only for frozen grid */
96 void do_set_clue_word (Puzzle
*p
, enum ORIENTATION orient
)
99 if (p
->grid_frozen
== false)
101 printf (UNFROZEN_GRID
);
102 char ch
= getchar ();
107 printf (INPUT_INDEX
);
110 fgets (clue
, MAX_CLUE_LENGTH
, stdin
);
111 char* cl
= strtok (clue
, "\n");
114 res
= set_clue (p
, cl
, index
, orient
);
118 printf (NO_WORD_INDEX
);
119 char ch
= getchar ();
123 /* clear a cell in the grid */
124 void do_clear_cell (Puzzle
*p
)
127 if (p
->grid_frozen
== true)
129 printf (FROZEN_GRID
);
130 char ch
= getchar ();
138 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
140 printf (EXCEED_GRID_SIZE
);
141 char ch
= getchar ();
144 p
->chars
[row
][col
] = ' ';
147 char ch
= getchar ();
150 /* add a down word to the grid */
151 void do_add_down_word (Puzzle
*p
)
154 if (p
->grid_frozen
== true)
156 printf (FROZEN_GRID
);
157 char ch
= getchar ();
160 char wd
[MAX_PUZZLE_SIZE
];
163 fgets (wd
, MAX_PUZZLE_SIZE
, stdin
);
164 char *word
= is_valid_word (wd
);
167 printf (INVALID_WORD
);
168 char ch
= getchar ();
175 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
177 printf (EXCEED_GRID_SIZE
);
178 char ch
= getchar ();
181 if (strlen (word
) > (p
->grid_size
- row
))
183 printf (WORD_TOO_LONG
);
184 char ch
= getchar ();
188 for (int i
= row
; i
< row
+ strlen(word
); i
++)
189 p
->chars
[i
][col
] = toupper(word
[i
- row
]);
192 char ch
= getchar ();
195 /* add an across word to the grid */
196 void do_add_across_word (Puzzle
*p
)
199 if (p
->grid_frozen
== true)
201 printf (FROZEN_GRID
);
202 char ch
= getchar ();
205 char wd
[MAX_PUZZLE_SIZE
];
208 fgets (wd
, MAX_PUZZLE_SIZE
, stdin
);
209 char *word
= is_valid_word (wd
);
212 printf (INVALID_WORD
);
213 char ch
= getchar ();
220 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
222 printf (EXCEED_GRID_SIZE
);
223 char ch
= getchar ();
227 if (strlen (word
) > (p
->grid_size
- col
))
229 printf (WORD_TOO_LONG
);
230 char ch
= getchar ();
234 for (int i
= col
; i
< col
+strlen (word
); i
++)
235 p
->chars
[row
][i
] = toupper(word
[i
- col
]);
238 char ch
= getchar ();
241 bool do_confirm_exit ()
243 printf (INPUT_CONFIRM_EXIT
);
245 fgets (res
, 3, stdin
);
246 if (toupper(res
[0]) == 'Y')
252 /* main loop for the puzzle editor */
253 void puzzle_editor_loop (Puzzle
*p
, const char *filename
)
258 char puzzle_title
[60];
259 sprintf (puzzle_title
, "%s - %s", PUZZLE_MENU_TITLE
, filename
);
260 print_menu (WHITE
, BLUE
, puzzle_title
, PUZZLE_EDIT_MENU
, 14, 50);
261 printf (INPUT_CHOICE
);
265 case 1: print_puzzle (p
);
266 char ch
= getchar ();
268 case 2: do_add_across_word (p
);
270 case 3: do_add_down_word (p
);
272 case 4: do_clear_cell (p
);
274 case 5: freeze_puzzle (p
);
278 case 6: unfreeze_puzzle (p
);
282 case 7: do_set_clue_word (p
, ACROSS
);
284 case 8: do_set_clue_word (p
, DOWN
);
286 case 9: save_puzzle (p
, filename
);
287 printf ("%s\n",FILE_SAVED
);
290 case 10: do_set_password (p
);
292 case 11: do_reset_puzzle (p
);
294 case 12: do_export_puzzle (p
);
296 case 13: do_export_clues (p
);
298 case 14: loop
= ! do_confirm_exit ();
304 /* open an existing puzzle */
305 void do_open_puzzle (const char *filename
)
308 /* if no filename is provided, get it from command line */
309 if (filename
== NULL
)
313 fgets(fname
, 256, stdin
);
314 if (strlen (fname
) == 1)
316 filename
= strtok (fname
, "\n");
319 p
= load_puzzle (filename
);
321 if (strcmp (p
.hashed_password
, "\0") != 0)
324 passwd
= getpass (INPUT_PASSWORD
);
325 if (strlen (passwd
) == 0)
328 if (verify_password (&p
, (const char*) passwd
))
329 puzzle_editor_loop (&p
, filename
);
332 printf (WRONG_PASSWORD
);
333 char ch
= getchar ();
337 puzzle_editor_loop (&p
, filename
);
340 /* create a new blank puzzle */
341 void do_new_puzzle (char *filename
, int size
)
344 /* if filename is not provided get it from command line */
345 if (filename
== NULL
)
349 fgets (fname
, 256, stdin
);
350 if (strlen (fname
) == 1)
352 filename
= strtok (fname
, "\n");
354 /* if no size is specified get it from command line */
357 printf (INPUT_GRID_SIZE
);
361 if (size
> MAX_PUZZLE_SIZE
)
363 printf (EXCEED_MAX_GRID_SIZE
);
368 init_puzzle (&p
, size
);
369 puzzle_editor_loop (&p
, filename
);
373 /* The main loop of the program */
376 /* Print the main menu */
379 print_menu (WHITE
, BLUE
, MAIN_MENU_TITLE
, MAIN_MENU
, 3, 50);
380 printf (INPUT_CHOICE
);
384 case 1: do_new_puzzle (NULL
, -1);
386 case 2: do_open_puzzle (NULL
);
393 int main (int argc
, char* argv
[])
400 case 2 : do_open_puzzle (argv
[1]);
402 case 4 : if (strcmp (argv
[2], "new") == 0)
404 int grid_size
= atoi (argv
[3]);
405 do_new_puzzle (argv
[1], grid_size
);
408 default: fprintf (stderr
, USAGE_LINE_1
, argv
[0]);
409 fprintf (stderr
, USAGE_LINE_2
);
410 fprintf (stderr
, USAGE_LINE_3
);
414 return (main_loop ());