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 if (strlen (clue
) == 1)
112 char* cl
= strtok (clue
, "\n");
115 res
= set_clue (p
, cl
, index
, orient
);
119 printf (NO_WORD_INDEX
);
120 char ch
= getchar ();
124 /* clear a cell in the grid */
125 void do_clear_cell (Puzzle
*p
)
128 if (p
->grid_frozen
== true)
130 printf (FROZEN_GRID
);
131 char ch
= getchar ();
139 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
141 printf (EXCEED_GRID_SIZE
);
142 char ch
= getchar ();
145 p
->chars
[row
][col
] = ' ';
148 char ch
= getchar ();
151 /* add a down word to the grid */
152 void do_add_down_word (Puzzle
*p
)
155 if (p
->grid_frozen
== true)
157 printf (FROZEN_GRID
);
158 char ch
= getchar ();
161 char wd
[MAX_PUZZLE_SIZE
];
164 fgets (wd
, MAX_PUZZLE_SIZE
, stdin
);
165 char *word
= is_valid_word (wd
);
168 printf (INVALID_WORD
);
169 char ch
= getchar ();
176 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
178 printf (EXCEED_GRID_SIZE
);
179 char ch
= getchar ();
182 if (strlen (word
) > (p
->grid_size
- row
))
184 printf (WORD_TOO_LONG
);
185 char ch
= getchar ();
189 for (int i
= row
; i
< row
+ strlen(word
); i
++)
190 p
->chars
[i
][col
] = toupper(word
[i
- row
]);
193 char ch
= getchar ();
196 /* add an across word to the grid */
197 void do_add_across_word (Puzzle
*p
)
200 if (p
->grid_frozen
== true)
202 printf (FROZEN_GRID
);
203 char ch
= getchar ();
206 char wd
[MAX_PUZZLE_SIZE
];
209 fgets (wd
, MAX_PUZZLE_SIZE
, stdin
);
210 char *word
= is_valid_word (wd
);
213 printf (INVALID_WORD
);
214 char ch
= getchar ();
221 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
223 printf (EXCEED_GRID_SIZE
);
224 char ch
= getchar ();
228 if (strlen (word
) > (p
->grid_size
- col
))
230 printf (WORD_TOO_LONG
);
231 char ch
= getchar ();
235 for (int i
= col
; i
< col
+strlen (word
); i
++)
236 p
->chars
[row
][i
] = toupper(word
[i
- col
]);
239 char ch
= getchar ();
242 bool do_confirm_exit ()
244 printf (INPUT_CONFIRM_EXIT
);
246 fgets (res
, 3, stdin
);
247 if (toupper(res
[0]) == 'Y')
253 /* main loop for the puzzle editor */
254 void puzzle_editor_loop (Puzzle
*p
, const char *filename
)
259 char puzzle_title
[60];
260 sprintf (puzzle_title
, "%s - %s", PUZZLE_MENU_TITLE
, filename
);
261 print_menu (WHITE
, BLUE
, puzzle_title
, PUZZLE_EDIT_MENU
, 14, 50);
262 printf (INPUT_CHOICE
);
266 case 1: print_puzzle (p
);
267 char ch
= getchar ();
269 case 2: do_add_across_word (p
);
271 case 3: do_add_down_word (p
);
273 case 4: do_clear_cell (p
);
275 case 5: freeze_puzzle (p
);
279 case 6: unfreeze_puzzle (p
);
283 case 7: do_set_clue_word (p
, ACROSS
);
285 case 8: do_set_clue_word (p
, DOWN
);
287 case 9: save_puzzle (p
, filename
);
288 printf ("%s\n",FILE_SAVED
);
291 case 10: do_set_password (p
);
293 case 11: do_reset_puzzle (p
);
295 case 12: do_export_puzzle (p
);
297 case 13: do_export_clues (p
);
299 case 14: loop
= ! do_confirm_exit ();
305 /* open an existing puzzle */
306 void do_open_puzzle (const char *filename
)
309 /* if no filename is provided, get it from command line */
310 if (filename
== NULL
)
314 fgets(fname
, 256, stdin
);
315 if (strlen (fname
) == 1)
317 filename
= strtok (fname
, "\n");
320 p
= load_puzzle (filename
);
322 if (strcmp (p
.hashed_password
, "\0") != 0)
325 passwd
= getpass (INPUT_PASSWORD
);
326 if (strlen (passwd
) == 0)
329 if (verify_password (&p
, (const char*) passwd
))
330 puzzle_editor_loop (&p
, filename
);
333 printf (WRONG_PASSWORD
);
334 char ch
= getchar ();
338 puzzle_editor_loop (&p
, filename
);
341 /* create a new blank puzzle */
342 void do_new_puzzle (char *filename
, int size
)
345 /* if filename is not provided get it from command line */
346 if (filename
== NULL
)
350 fgets (fname
, 256, stdin
);
351 if (strlen (fname
) == 1)
353 filename
= strtok (fname
, "\n");
355 /* if no size is specified get it from command line */
358 printf (INPUT_GRID_SIZE
);
362 if (size
> MAX_PUZZLE_SIZE
)
364 printf (EXCEED_MAX_GRID_SIZE
);
369 init_puzzle (&p
, size
);
370 puzzle_editor_loop (&p
, filename
);
374 /* The main loop of the program */
377 /* Print the main menu */
380 print_menu (WHITE
, BLUE
, MAIN_MENU_TITLE
, MAIN_MENU
, 3, 50);
381 printf (INPUT_CHOICE
);
385 case 1: do_new_puzzle (NULL
, -1);
387 case 2: do_open_puzzle (NULL
);
394 int main (int argc
, char* argv
[])
401 case 2 : do_open_puzzle (argv
[1]);
403 case 4 : if (strcmp (argv
[2], "new") == 0)
405 int grid_size
= atoi (argv
[3]);
406 do_new_puzzle (argv
[1], grid_size
);
409 default: fprintf (stderr
, USAGE_LINE_1
, argv
[0]);
410 fprintf (stderr
, USAGE_LINE_2
);
411 fprintf (stderr
, USAGE_LINE_3
);
415 return (main_loop ());