61de6b6991fba50c54baa4d86f48110e6ed9ad7e
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
)
76 printf (INPUT_PASSWORD
);
78 fgets (password
, 256, stdin
);
79 /* if empty reset the password to nothing */
80 if (strlen (password
) == 1)
82 set_puzzle_password (p
, "\0");
83 printf (PASSWORD_RESET
);
86 /* set the password */
89 char *passwd
= strtok (password
, "\n");
91 set_puzzle_password (p
, (const char* )passwd
);
92 printf (PASSWORD_SET
);
97 /* set clue for a word - only for frozen grid */
98 void do_set_clue_word (Puzzle
*p
, enum ORIENTATION orient
)
101 if (p
->grid_frozen
== false)
103 printf (UNFROZEN_GRID
);
104 char ch
= getchar ();
109 printf (INPUT_INDEX
);
112 fgets (clue
, MAX_CLUE_LENGTH
, stdin
);
113 char* cl
= strtok (clue
, "\n");
116 res
= set_clue (p
, cl
, index
, orient
);
120 printf (NO_WORD_INDEX
);
121 char ch
= getchar ();
125 /* clear a cell in the grid */
126 void do_clear_cell (Puzzle
*p
)
129 if (p
->grid_frozen
== true)
131 printf (FROZEN_GRID
);
132 char ch
= getchar ();
140 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
142 printf (EXCEED_GRID_SIZE
);
143 char ch
= getchar ();
146 p
->chars
[row
][col
] = ' ';
149 char ch
= getchar ();
152 /* add a down word to the grid */
153 void do_add_down_word (Puzzle
*p
)
156 if (p
->grid_frozen
== true)
158 printf (FROZEN_GRID
);
159 char ch
= getchar ();
162 char wd
[MAX_PUZZLE_SIZE
];
165 fgets (wd
, MAX_PUZZLE_SIZE
, stdin
);
166 char *word
= is_valid_word (wd
);
169 printf (INVALID_WORD
);
170 char ch
= getchar ();
177 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
179 printf (EXCEED_GRID_SIZE
);
180 char ch
= getchar ();
183 if (strlen (word
) > (p
->grid_size
- row
))
185 printf (WORD_TOO_LONG
);
186 char ch
= getchar ();
190 for (int i
= row
; i
< row
+ strlen(word
); i
++)
191 p
->chars
[i
][col
] = toupper(word
[i
- row
]);
194 char ch
= getchar ();
197 /* add an across word to the grid */
198 void do_add_across_word (Puzzle
*p
)
201 if (p
->grid_frozen
== true)
203 printf (FROZEN_GRID
);
204 char ch
= getchar ();
207 char wd
[MAX_PUZZLE_SIZE
];
210 fgets (wd
, MAX_PUZZLE_SIZE
, stdin
);
211 char *word
= is_valid_word (wd
);
214 printf (INVALID_WORD
);
215 char ch
= getchar ();
222 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
224 printf (EXCEED_GRID_SIZE
);
225 char ch
= getchar ();
229 if (strlen (word
) > (p
->grid_size
- col
))
231 printf (WORD_TOO_LONG
);
232 char ch
= getchar ();
236 for (int i
= col
; i
< col
+strlen (word
); i
++)
237 p
->chars
[row
][i
] = toupper(word
[i
- col
]);
240 char ch
= getchar ();
243 bool do_confirm_exit ()
245 printf (INPUT_CONFIRM_EXIT
);
247 fgets (res
, 3, stdin
);
248 if (toupper(res
[0]) == 'Y')
254 /* main loop for the puzzle editor */
255 void puzzle_editor_loop (Puzzle
*p
, const char *filename
)
260 print_menu (WHITE
, BLUE
, PUZZLE_MENU_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 printf (INPUT_PASSWORD
);
325 fgets (passwd
, 256, stdin
);
326 if (strlen (passwd
) == 1)
328 char *pwd
= strtok (passwd
, "\n");
330 if (verify_password (&p
, (const char*) pwd
))
331 puzzle_editor_loop (&p
, filename
);
334 printf (WRONG_PASSWORD
);
335 char ch
= getchar ();
339 puzzle_editor_loop (&p
, filename
);
342 /* create a new blank puzzle */
343 void do_new_puzzle (char *filename
, int size
)
346 /* if filename is not provided get it from command line */
347 if (filename
== NULL
)
351 fgets (fname
, 256, stdin
);
352 if (strlen (fname
) == 1)
354 filename
= strtok (fname
, "\n");
356 /* if no size is specified get it from command line */
359 printf (INPUT_GRID_SIZE
);
363 if (size
> MAX_PUZZLE_SIZE
)
365 printf (EXCEED_MAX_GRID_SIZE
);
370 init_puzzle (&p
, size
);
371 puzzle_editor_loop (&p
, filename
);
375 /* The main loop of the program */
378 /* Print the main menu */
381 print_menu (WHITE
, BLUE
, MAIN_MENU_TITLE
, MAIN_MENU
, 3, 50);
382 printf (INPUT_CHOICE
);
386 case 1: do_new_puzzle (NULL
, -1);
388 case 2: do_open_puzzle (NULL
);
395 int main (int argc
, char* argv
[])
402 case 2 : do_open_puzzle (argv
[1]);
404 case 4 : if (strcmp (argv
[2], "new") == 0)
406 int grid_size
= atoi (argv
[3]);
407 do_new_puzzle (argv
[1], grid_size
);
410 default: fprintf (stderr
, USAGE_LINE_1
, argv
[0]);
411 fprintf (stderr
, USAGE_LINE_2
);
412 fprintf (stderr
, USAGE_LINE_3
);
416 return (main_loop ());