+/* 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;
+}
+