bool grid_frozen;
} Puzzle;
+int get_num ()
+{
+ char s[5];
+ fgets (s, 5, stdin);
+ int n = atoi (s);
+ return n;
+}
+
/* Set the terminal colour */
void set_color (enum COLOR fg, enum COLOR bg) {
printf ("\x1B[%d;%dm", fg+30, bg+40);
printf ("\x1B[0m");
}
+/* check if previous row is blank or not */
+bool prev_row_blank (Puzzle *p, int r, int c)
+{
+ if (r == 0) return true;
+ if (p->chars[r-1][c] == ' ' || p->chars[r-1][c] == '#') return true;
+ return false;
+}
+/* check if next row is blank or not */
+bool next_row_blank (Puzzle *p, int r, int c)
+{
+ if (r == p->grid_size - 1) return true;
+ if (p->chars[r+1][c] == ' ' || p->chars[r+1][c] == '#') return true;
+ return false;
+}
+/* check if previous col is blank or not */
+bool prev_col_blank (Puzzle *p, int r, int c)
+{
+ if (c == 0) return true;
+ if (p->chars[r][c-1] == ' ' || p->chars[r][c-1] == '#') return true;
+ return false;
+}
+/* check if the next col is blank or not */
+bool next_col_blank (Puzzle *p, int r, int c)
+{
+ if (c == p->grid_size -1) return true;
+ if (p->chars[r][c+1] == ' ' || p->chars[r][c+1] == '#') return true;
+ return false;
+}
+
+/* unfreeze the grid - mak editing possible to change words */
+void unfreeze_puzzle (Puzzle *p)
+{
+ for (int i = 0; i < p->grid_size; i ++)
+ {
+ for (int j = 0; j < p->grid_size; j ++)
+ {
+ if (p->chars[i][j] == '#')
+ p->chars[i][j] = ' ';
+
+ p->start_across_word[i][j] = -1;
+ p->start_down_word[i][j] = -1;
+ }
+ }
+ p->grid_frozen = false;
+}
+
+/* freeze the grid - make editing impossible because it finalizes the
+ across and down words in the grid */
+void freeze_puzzle (Puzzle *p)
+{
+ int word_num = 1;
+ bool across_word_start, down_word_start;
+ for (int i = 0; i < p->grid_size; i ++)
+ {
+ for (int j = 0; j < p->grid_size; j++)
+ {
+ across_word_start = false;
+ down_word_start = false;
+ /* if it is a blank cell - cover it with a block */
+ if (p->chars[i][j] == ' ')
+ p->chars[i][j] = '#';
+ /* it is not a blank cell - check all possibilities */
+ else
+ {
+ bool prev_row = prev_row_blank (p, i, j);
+ bool next_row = next_row_blank (p, i, j);
+ bool prev_col = prev_col_blank (p, i, j);
+ bool next_col = next_col_blank (p, i, j);
+ if (prev_row && ! next_row)
+ down_word_start = true;
+ if (prev_col && ! next_col)
+ across_word_start = true;
+ }
+
+ if (across_word_start == true)
+ p->start_across_word[i][j] = word_num;
+ else
+ p->start_across_word[i][j] = -1;
+ if (down_word_start == true)
+ p->start_down_word[i][j] = word_num;
+ else
+ p->start_down_word[i][j] = -1;
+ if (across_word_start == true || down_word_start == true)
+ word_num ++;
+ }
+ }
+ p->grid_frozen = true;
+}
+
/* reset the entire grid */
void init_puzzle (Puzzle *p, int grid_size)
{
/* display the puzzle */
void print_puzzle (Puzzle *p)
{
+ printf ("\n");
set_color (WHITE, CYAN);
printf (" ");
for (int i = 0; i < p->grid_size; i ++)
}
else
{
- if (p->start_across_word[i][j] != -1)
+ if (p->start_across_word[i][j] != -1 ||
+ p->start_down_word[i][j] != -1)
{
set_color (BLUE, WHITE);
- printf ("%2d", p->start_across_word[i][j]);
+ if (p->start_across_word[i][j] != -1)
+ printf ("%-2d", p->start_across_word[i][j]);
+ else
+ printf ("%-2d", p->start_down_word[i][j]);
}
else
{
}
printf ("\n");
}
+ /* print the clues if set */
+ if (p->grid_frozen == true)
+ {
+ printf ("ACROSS - CLUES\n");
+ for (int i = 0; i < p->grid_size; i ++)
+ {
+ for (int j = 0; j < p->grid_size; j ++)
+ {
+ if (p->start_across_word[i][j] != -1)
+ {
+ printf ("%d - %s; ", p->start_across_word[i][j],
+ p->clue_across[i][j]);
+ }
+ }
+ }
+ printf ("\nDOWN - CLUES\n");
+ for (int i = 0; i < p->grid_size; i ++)
+ {
+ for (int j = 0; j < p->grid_size; j ++)
+ {
+ if (p->start_down_word[i][j] != -1)
+ {
+ printf ("%d - %s; ", p->start_down_word[i][j],
+ p->clue_down[i][j]);
+ }
+ }
+ }
+ printf ("\n");
+ }
}
/* function to check if a word is valid or not */
return true;
}
+/* function set a clue for an across word */
+bool set_across_clue (Puzzle *p, String clue, int index)
+{
+ for (int i = 0; i < p->grid_size; i ++)
+ {
+ for (int j = 0; j < p->grid_size; j ++)
+ {
+ if (p->start_across_word[i][j] == index)
+ {
+ strcpy (p->clue_across[i][j], clue);
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
/* function to print a menu */
void print_menu (enum COLOR fg, enum COLOR bg, const char* title,
char **items, int num_items, int padding)