--- /dev/null
+wordblox: wordblox.c
+ clang wordblox.c -lgd -o wordblox
+
#define PUZZLE_MENU_TITLE "Edit Puzzle"
#define MAIN_MENU_TITLE "Main Menu"
#define INPUT_GRID_SIZE "Number of rows/columns: "
+#define INPUT_EXPORT_ANSWERS "Export as solution (y/N): "
#define INPUT_CHOICE "Your Choice: "
#define EXCEED_MAX_GRID_SIZE "Exceeds max puzzle size"
#define ERROR_WRITING_FILE "Error writing file"
#define INPUT_CONFIRM_EXIT "Are you sure you wish to exit? \
Unsaved changes will be lost [y/N]"
+#define USAGE_LINE_1 "Usage: %s [<filename> [new <nn>]]\n"
+#define USAGE_LINE_2 "<filename> - puzzle file name\n"
+#define USAGE_LINE_3 "new <nn> - create new puzzle with <nn> grid \
+columns (warning: existing file name may be overwritten)\n"
+
char *MAIN_MENU[] =
{"1. New puzzle",
"2. Open existing puzzle",
"7. Set Clue - Across Word",
"8. Set Clue - Down Word",
"9. Save puzzle",
- "10.Return to main menu" };
+ "10.Export puzzle as PNG image",
+ "11.Return to main menu" };
#endif
#include "wordblox.h"
#include "constantstrings.h"
+/* export the puzzle to a png file */
+void export_puzzle (Puzzle *p)
+{
+ if (p->grid_frozen == false)
+ {
+ printf (UNFROZEN_GRID);
+ char ch = getchar ();
+ return;
+ }
+ char fname[256];
+ printf (INPUT_FILE);
+ fgets (fname, 256, stdin);
+ char* filename = strtok (fname, "\n");
+
+ printf (INPUT_EXPORT_ANSWERS);
+ char ans[3];
+ fgets (ans, 3, stdin);
+ bool solution;
+ solution = (toupper (ans[0]) == 'Y') ? true : false;
+
+ export_grid_image (p, filename, solution);
+ printf (FILE_SAVED);
+ char ch = getchar ();
+}
+
+/* set clue for a word - only for frozen grid */
void set_clue_word (Puzzle *p, enum ORIENTATION orient)
{
print_puzzle (p);
res = set_clue (p, cl, index, orient);
if (res == false)
+ {
printf (NO_WORD_INDEX);
+ char ch = getchar ();
+ }
}
/* clear a cell in the grid */
char wd[MAX_PUZZLE_SIZE];
int row, col;
printf (INPUT_WORD);
- fgets (wd, p->grid_size, stdin);
+ fgets (wd, MAX_PUZZLE_SIZE, stdin);
char *word = strtok (wd, "\n");
if (! is_valid_word (word))
{
bool confirm_exit ()
{
printf (INPUT_CONFIRM_EXIT);
- char res[2];
- fgets (res, 2, stdin);
+ char res[3];
+ fgets (res, 3, stdin);
if (toupper(res[0]) == 'Y')
return true;
else
bool loop = true;
while (loop)
{
- print_menu (WHITE, RED, PUZZLE_MENU_TITLE, PUZZLE_EDIT_MENU, 10, 50);
+ print_menu (WHITE, RED, PUZZLE_MENU_TITLE, PUZZLE_EDIT_MENU, 11, 50);
printf (INPUT_CHOICE);
int ch = get_num ();
switch (ch)
ch = getchar ();
break;
case 7: set_clue_word (p, ACROSS);
- print_puzzle (p);
- ch = getchar ();
break;
case 8: set_clue_word (p, DOWN);
- print_puzzle (p);
- ch = getchar ();
break;
case 9: save_puzzle (p, filename);
printf ("%s\n",FILE_SAVED);
ch = getchar ();
break;
- case 10: loop = !confirm_exit ();
- break;
+ case 10: export_puzzle (p);
+ break;
+ case 11: loop = !confirm_exit ();
+ break;
}
}
}
}
int main (int argc, char* argv[])
-{
+{
+ if (argc >= 2)
+ {
+ Puzzle p;
+ switch (argc)
+ {
+ case 2 : p = load_puzzle (argv[1]);
+ puzzle_editor_loop (&p, argv[1]);
+ break;
+ case 4 : if (strcmp (argv[2], "new") == 0)
+ {
+ int grid_size = atoi (argv[3]);
+ init_puzzle (&p, grid_size);
+ puzzle_editor_loop (&p, argv[1]);
+ break;
+ }
+ default: fprintf (stderr, USAGE_LINE_1, argv[0]);
+ fprintf (stderr, USAGE_LINE_2);
+ fprintf (stderr, USAGE_LINE_3);
+ exit (3);
+ }
+ }
return (main_loop ());
}
#ifndef __WORDBLOX_H
#define __WORDBLOX_H
+#include <gd.h>
+#include <gdfontmb.h>
+#include <gdfontg.h>
#include "constantstrings.h"
#define MAX_PUZZLE_SIZE 20
bool grid_frozen;
} Puzzle;
+/* get a number from the user */
int get_num ()
{
char s[5];
return n;
}
+/* Output the grid to image - if answerkey is true export filled grid */
+void export_grid_image (Puzzle *p, const char *filename, bool answerkey)
+{
+ int img_size = p->grid_size * 40;
+ FILE * outfile = fopen (filename, "wb");
+ if (outfile == NULL)
+ {
+ fprintf (stderr, "%s\n", ERROR_WRITING_FILE);
+ exit (1);
+ }
+
+ gdImagePtr img = gdImageCreate (img_size, img_size);
+ int white = gdImageColorAllocate (img, 255,255,255);
+ int black = gdImageColorAllocate (img, 0, 0, 0);
+ int blue = gdImageColorAllocate (img, 0, 0, 216);
+ gdFontPtr sm_fnt = gdFontGetMediumBold ();
+ gdFontPtr lg_fnt = gdFontGetGiant ();
+
+ for (int i = 0; i < p->grid_size; i ++)
+ {
+ for (int j = 0; j < p->grid_size; j++)
+ {
+ /* if it is a block, draw the black square */
+ if (p->chars[i][j] == '#')
+ gdImageFilledRectangle (img, j*40, i*40, j*40+40,
+ i*40+40,black);
+ else
+ {
+ /* draw a regular square */
+ gdImageRectangle (img, j*40, i*40, j*40+40,
+ i*40+40, black);
+
+ /* print the numers, if it is either start across word or
+ a down word */
+ if (p->start_across_word[i][j] != -1 ||
+ p->start_down_word[i][j] != -1)
+ {
+ if (p->start_across_word[i][j] != -1)
+ {
+ char str[5];
+ sprintf (str, "%d", p->start_across_word[i][j]);
+ gdImageString (img, sm_fnt, j*40+2, i*40+2,
+ (unsigned char *)str, blue);
+ }
+ else
+ {
+ char str[5];
+ sprintf (str, "%d", p->start_down_word[i][j]);
+ gdImageString (img, sm_fnt, j*40+2, i*40+2,
+ (unsigned char *)str, blue);
+ }
+ }
+ /* if answerkey is true, draw the character in the cell */
+ if (answerkey)
+ {
+ gdImageChar (img, lg_fnt, j*40+15, i*40+15,
+ p->chars[i][j], black);
+ }
+ }
+ }
+ }
+
+ gdImagePng (img, outfile);
+ gdImageDestroy (img);
+ fclose (outfile);
+}
+
/* Set the terminal colour */
void set_color (enum COLOR fg, enum COLOR bg, enum ATTR at) {
printf ("\x1B[%d;%d;%dm", fg+30, bg+40, at);