9 #include "constantstrings.h"
11 void set_clue_word (Puzzle
*p
, enum ORIENTATION orient
)
14 if (p
->grid_frozen
== false)
16 printf (UNFROZEN_GRID
);
25 fgets (clue
, MAX_CLUE_LENGTH
, stdin
);
26 char* cl
= strtok (clue
, "\n");
29 res
= set_clue (p
, cl
, index
, orient
);
32 printf (NO_WORD_INDEX
);
35 /* clear a cell in the grid */
36 void clear_cell (Puzzle
*p
)
39 if (p
->grid_frozen
== true)
50 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
52 printf (EXCEED_GRID_SIZE
);
56 p
->chars
[row
][col
] = ' ';
62 /* add a down word to the grid */
63 void add_down_word (Puzzle
*p
)
66 if (p
->grid_frozen
== true)
72 char wd
[MAX_PUZZLE_SIZE
];
75 fgets (wd
, MAX_PUZZLE_SIZE
, stdin
);
76 char *word
= strtok (wd
, "\n");
77 if (! is_valid_word (word
))
79 printf (INVALID_WORD
);
87 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
89 printf (EXCEED_GRID_SIZE
);
93 if (strlen (word
) > (p
->grid_size
- row
))
95 printf (WORD_TOO_LONG
);
100 for (int i
= row
; i
< row
+ strlen(word
); i
++)
101 p
->chars
[i
][col
] = toupper(word
[i
- row
]);
104 char ch
= getchar ();
107 /* add an across word to the grid */
108 void add_across_word (Puzzle
*p
)
111 if (p
->grid_frozen
== true)
113 printf (FROZEN_GRID
);
114 char ch
= getchar ();
117 char wd
[MAX_PUZZLE_SIZE
];
120 fgets (wd
, p
->grid_size
, stdin
);
121 char *word
= strtok (wd
, "\n");
122 if (! is_valid_word (word
))
124 printf (INVALID_WORD
);
125 char ch
= getchar ();
132 if (row
>= p
->grid_size
|| col
>= p
->grid_size
)
134 printf (EXCEED_GRID_SIZE
);
135 char ch
= getchar ();
139 if (strlen (word
) > (p
->grid_size
- col
))
141 printf (WORD_TOO_LONG
);
142 char ch
= getchar ();
146 for (int i
= col
; i
< col
+strlen (word
); i
++)
147 p
->chars
[row
][i
] = toupper(word
[i
- col
]);
150 char ch
= getchar ();
155 printf (INPUT_CONFIRM_EXIT
);
157 fgets (res
, 2, stdin
);
158 if (toupper(res
[0]) == 'Y')
164 /* main loop for the puzzle editor */
165 void puzzle_editor_loop (Puzzle
*p
, const char *filename
)
170 print_menu (WHITE
, RED
, PUZZLE_MENU_TITLE
, PUZZLE_EDIT_MENU
, 10, 50);
171 printf (INPUT_CHOICE
);
175 case 1: print_puzzle (p
);
176 char ch
= getchar ();
178 case 2: add_across_word (p
);
180 case 3: add_down_word (p
);
182 case 4: clear_cell (p
);
184 case 5: freeze_puzzle (p
);
188 case 6: unfreeze_puzzle (p
);
192 case 7: set_clue_word (p
, ACROSS
);
196 case 8: set_clue_word (p
, DOWN
);
200 case 9: save_puzzle (p
, filename
);
201 printf ("%s\n",FILE_SAVED
);
204 case 10: loop
= !confirm_exit ();
210 /* open an existing puzzle */
216 fgets(fname
, 256, stdin
);
217 char* filename
= strtok (fname
, "\n");
218 p
= load_puzzle (filename
);
219 puzzle_editor_loop (&p
, filename
);
222 /* create a new blank puzzle */
228 fgets (fname
, 256, stdin
);
229 char* filename
= strtok (fname
, "\n");
230 printf (INPUT_GRID_SIZE
);
233 if (size
> MAX_PUZZLE_SIZE
)
235 printf (EXCEED_MAX_GRID_SIZE
);
240 init_puzzle (&p
, size
);
241 puzzle_editor_loop (&p
, filename
);
245 /* The main loop of the program */
248 /* Print the main menu */
251 print_menu (WHITE
, BLUE
, MAIN_MENU_TITLE
, MAIN_MENU
, 3, 50);
252 printf (INPUT_CHOICE
);
256 case 1: new_puzzle ();
258 case 2: open_puzzle ();
265 int main (int argc
, char* argv
[])
267 return (main_loop ());