d840b6d40b2f10fa54e6ff2648e0d5a9e49e51f4
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
);
70 /* set clue for a word - only for frozen grid */
71 void do_set_clue_word (Puzzle
*p
, enum ORIENTATION orient
)
74 if (p
->grid_frozen
== false)
76 printf (UNFROZEN_GRID
);
85 fgets (clue
, MAX_CLUE_LENGTH
, stdin
);
86 char* cl
= strtok (clue
, "\n");
89 res
= set_clue (p
, cl
, index
, orient
);
93 printf (NO_WORD_INDEX
);
98 /* clear a cell in the grid */
99 void do_clear_cell (Puzzle
*p
)
102 if (p
->grid_frozen
== true)
104 printf (FROZEN_GRID
);
105 char ch
= getchar ();
113 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
115 printf (EXCEED_GRID_SIZE
);
116 char ch
= getchar ();
119 p
->chars
[row
][col
] = ' ';
122 char ch
= getchar ();
125 /* add a down word to the grid */
126 void do_add_down_word (Puzzle
*p
)
129 if (p
->grid_frozen
== true)
131 printf (FROZEN_GRID
);
132 char ch
= getchar ();
135 char wd
[MAX_PUZZLE_SIZE
];
138 fgets (wd
, MAX_PUZZLE_SIZE
, stdin
);
139 char *word
= is_valid_word (wd
);
142 printf (INVALID_WORD
);
143 char ch
= getchar ();
150 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
152 printf (EXCEED_GRID_SIZE
);
153 char ch
= getchar ();
156 if (strlen (word
) > (p
->grid_size
- row
))
158 printf (WORD_TOO_LONG
);
159 char ch
= getchar ();
163 for (int i
= row
; i
< row
+ strlen(word
); i
++)
164 p
->chars
[i
][col
] = toupper(word
[i
- row
]);
167 char ch
= getchar ();
170 /* add an across word to the grid */
171 void do_add_across_word (Puzzle
*p
)
174 if (p
->grid_frozen
== true)
176 printf (FROZEN_GRID
);
177 char ch
= getchar ();
180 char wd
[MAX_PUZZLE_SIZE
];
183 fgets (wd
, MAX_PUZZLE_SIZE
, stdin
);
184 char *word
= is_valid_word (wd
);
187 printf (INVALID_WORD
);
188 char ch
= getchar ();
195 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
197 printf (EXCEED_GRID_SIZE
);
198 char ch
= getchar ();
202 if (strlen (word
) > (p
->grid_size
- col
))
204 printf (WORD_TOO_LONG
);
205 char ch
= getchar ();
209 for (int i
= col
; i
< col
+strlen (word
); i
++)
210 p
->chars
[row
][i
] = toupper(word
[i
- col
]);
213 char ch
= getchar ();
216 bool do_confirm_exit ()
218 printf (INPUT_CONFIRM_EXIT
);
220 fgets (res
, 3, stdin
);
221 if (toupper(res
[0]) == 'Y')
227 /* main loop for the puzzle editor */
228 void puzzle_editor_loop (Puzzle
*p
, const char *filename
)
233 print_menu (WHITE
, BLUE
, PUZZLE_MENU_TITLE
, PUZZLE_EDIT_MENU
, 13, 50);
234 printf (INPUT_CHOICE
);
238 case 1: print_puzzle (p
);
239 char ch
= getchar ();
241 case 2: do_add_across_word (p
);
243 case 3: do_add_down_word (p
);
245 case 4: do_clear_cell (p
);
247 case 5: freeze_puzzle (p
);
251 case 6: unfreeze_puzzle (p
);
255 case 7: do_set_clue_word (p
, ACROSS
);
257 case 8: do_set_clue_word (p
, DOWN
);
259 case 9: save_puzzle (p
, filename
);
260 printf ("%s\n",FILE_SAVED
);
263 case 10: do_reset_puzzle (p
);
267 case 11: do_export_puzzle (p
);
269 case 12: do_export_clues (p
);
271 case 13: loop
= ! do_confirm_exit ();
277 /* open an existing puzzle */
278 void do_open_puzzle ()
283 fgets(fname
, 256, stdin
);
284 if (strlen (fname
) == 1)
286 char* filename
= strtok (fname
, "\n");
287 p
= load_puzzle (filename
);
288 puzzle_editor_loop (&p
, filename
);
291 /* create a new blank puzzle */
292 void do_new_puzzle ()
297 fgets (fname
, 256, stdin
);
298 if (strlen (fname
) == 1)
300 char* filename
= strtok (fname
, "\n");
301 printf (INPUT_GRID_SIZE
);
304 if (size
> MAX_PUZZLE_SIZE
)
306 printf (EXCEED_MAX_GRID_SIZE
);
311 init_puzzle (&p
, size
);
312 puzzle_editor_loop (&p
, filename
);
316 /* The main loop of the program */
319 /* Print the main menu */
322 print_menu (WHITE
, BLUE
, MAIN_MENU_TITLE
, MAIN_MENU
, 3, 50);
323 printf (INPUT_CHOICE
);
327 case 1: do_new_puzzle ();
329 case 2: do_open_puzzle ();
336 int main (int argc
, char* argv
[])
343 case 2 : p
= load_puzzle (argv
[1]);
344 puzzle_editor_loop (&p
, argv
[1]);
346 case 4 : if (strcmp (argv
[2], "new") == 0)
348 int grid_size
= atoi (argv
[3]);
349 init_puzzle (&p
, grid_size
);
350 puzzle_editor_loop (&p
, argv
[1]);
353 default: fprintf (stderr
, USAGE_LINE_1
, argv
[0]);
354 fprintf (stderr
, USAGE_LINE_2
);
355 fprintf (stderr
, USAGE_LINE_3
);
359 return (main_loop ());