8ab98915766d33bce666a51a0ab0baafc95a6b2e
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 password for the puzzle */
74 void do_set_password (Puzzle
*p
)
77 password
= getpass (INPUT_PASSWORD
);
78 /* if empty reset the password to nothing */
79 if (strlen(password
) == 0)
81 set_puzzle_password (p
, "\0");
82 printf (PASSWORD_RESET
);
85 /* set the password */
88 set_puzzle_password (p
, (const char* )password
);
89 printf (PASSWORD_SET
);
94 /* set clue for a word - only for frozen grid */
95 void do_set_clue_word (Puzzle
*p
, enum ORIENTATION orient
)
98 if (p
->grid_frozen
== false)
100 printf (UNFROZEN_GRID
);
101 char ch
= getchar ();
106 printf (INPUT_INDEX
);
109 fgets (clue
, MAX_CLUE_LENGTH
, stdin
);
110 char* cl
= strtok (clue
, "\n");
113 res
= set_clue (p
, cl
, index
, orient
);
117 printf (NO_WORD_INDEX
);
118 char ch
= getchar ();
122 /* clear a cell in the grid */
123 void do_clear_cell (Puzzle
*p
)
126 if (p
->grid_frozen
== true)
128 printf (FROZEN_GRID
);
129 char ch
= getchar ();
137 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
139 printf (EXCEED_GRID_SIZE
);
140 char ch
= getchar ();
143 p
->chars
[row
][col
] = ' ';
146 char ch
= getchar ();
149 /* add a down word to the grid */
150 void do_add_down_word (Puzzle
*p
)
153 if (p
->grid_frozen
== true)
155 printf (FROZEN_GRID
);
156 char ch
= getchar ();
159 char wd
[MAX_PUZZLE_SIZE
];
162 fgets (wd
, MAX_PUZZLE_SIZE
, stdin
);
163 char *word
= is_valid_word (wd
);
166 printf (INVALID_WORD
);
167 char ch
= getchar ();
174 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
176 printf (EXCEED_GRID_SIZE
);
177 char ch
= getchar ();
180 if (strlen (word
) > (p
->grid_size
- row
))
182 printf (WORD_TOO_LONG
);
183 char ch
= getchar ();
187 for (int i
= row
; i
< row
+ strlen(word
); i
++)
188 p
->chars
[i
][col
] = toupper(word
[i
- row
]);
191 char ch
= getchar ();
194 /* add an across word to the grid */
195 void do_add_across_word (Puzzle
*p
)
198 if (p
->grid_frozen
== true)
200 printf (FROZEN_GRID
);
201 char ch
= getchar ();
204 char wd
[MAX_PUZZLE_SIZE
];
207 fgets (wd
, MAX_PUZZLE_SIZE
, stdin
);
208 char *word
= is_valid_word (wd
);
211 printf (INVALID_WORD
);
212 char ch
= getchar ();
219 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
221 printf (EXCEED_GRID_SIZE
);
222 char ch
= getchar ();
226 if (strlen (word
) > (p
->grid_size
- col
))
228 printf (WORD_TOO_LONG
);
229 char ch
= getchar ();
233 for (int i
= col
; i
< col
+strlen (word
); i
++)
234 p
->chars
[row
][i
] = toupper(word
[i
- col
]);
237 char ch
= getchar ();
240 bool do_confirm_exit ()
242 printf (INPUT_CONFIRM_EXIT
);
244 fgets (res
, 3, stdin
);
245 if (toupper(res
[0]) == 'Y')
251 /* main loop for the puzzle editor */
252 void puzzle_editor_loop (Puzzle
*p
, const char *filename
)
257 char puzzle_title
[60];
258 sprintf (puzzle_title
, "%s - %s", PUZZLE_MENU_TITLE
, filename
);
259 print_menu (WHITE
, BLUE
, puzzle_title
, PUZZLE_EDIT_MENU
, 14, 50);
260 printf (INPUT_CHOICE
);
264 case 1: print_puzzle (p
);
265 char ch
= getchar ();
267 case 2: do_add_across_word (p
);
269 case 3: do_add_down_word (p
);
271 case 4: do_clear_cell (p
);
273 case 5: freeze_puzzle (p
);
277 case 6: unfreeze_puzzle (p
);
281 case 7: do_set_clue_word (p
, ACROSS
);
283 case 8: do_set_clue_word (p
, DOWN
);
285 case 9: save_puzzle (p
, filename
);
286 printf ("%s\n",FILE_SAVED
);
289 case 10: do_set_password (p
);
291 case 11: do_reset_puzzle (p
);
293 case 12: do_export_puzzle (p
);
295 case 13: do_export_clues (p
);
297 case 14: loop
= ! do_confirm_exit ();
303 /* open an existing puzzle */
304 void do_open_puzzle (const char *filename
)
307 /* if no filename is provided, get it from command line */
308 if (filename
== NULL
)
312 fgets(fname
, 256, stdin
);
313 if (strlen (fname
) == 1)
315 filename
= strtok (fname
, "\n");
318 p
= load_puzzle (filename
);
320 if (strcmp (p
.hashed_password
, "\0") != 0)
323 passwd
= getpass (INPUT_PASSWORD
);
324 if (strlen (passwd
) == 0)
327 if (verify_password (&p
, (const char*) passwd
))
328 puzzle_editor_loop (&p
, filename
);
331 printf (WRONG_PASSWORD
);
332 char ch
= getchar ();
336 puzzle_editor_loop (&p
, filename
);
339 /* create a new blank puzzle */
340 void do_new_puzzle (char *filename
, int size
)
343 /* if filename is not provided get it from command line */
344 if (filename
== NULL
)
348 fgets (fname
, 256, stdin
);
349 if (strlen (fname
) == 1)
351 filename
= strtok (fname
, "\n");
353 /* if no size is specified get it from command line */
356 printf (INPUT_GRID_SIZE
);
360 if (size
> MAX_PUZZLE_SIZE
)
362 printf (EXCEED_MAX_GRID_SIZE
);
367 init_puzzle (&p
, size
);
368 puzzle_editor_loop (&p
, filename
);
372 /* The main loop of the program */
375 /* Print the main menu */
378 print_menu (WHITE
, BLUE
, MAIN_MENU_TITLE
, MAIN_MENU
, 3, 50);
379 printf (INPUT_CHOICE
);
383 case 1: do_new_puzzle (NULL
, -1);
385 case 2: do_open_puzzle (NULL
);
392 int main (int argc
, char* argv
[])
399 case 2 : do_open_puzzle (argv
[1]);
401 case 4 : if (strcmp (argv
[2], "new") == 0)
403 int grid_size
= atoi (argv
[3]);
404 do_new_puzzle (argv
[1], grid_size
);
407 default: fprintf (stderr
, USAGE_LINE_1
, argv
[0]);
408 fprintf (stderr
, USAGE_LINE_2
);
409 fprintf (stderr
, USAGE_LINE_3
);
413 return (main_loop ());