X-Git-Url: https://harishankar.org/repos/?p=wordblah.git;a=blobdiff_plain;f=wordblox.h;h=bde57c364d95e88728e9b74ded17db4ae047a87a;hp=50033bc897cc87150774719e0ed0ced70ab53c7d;hb=d29b37aa1b82dbd3c4baa55396521296f4a88ef4;hpb=3657c15588e4b2607d9225659f66158d92a85262 diff --git a/wordblox.h b/wordblox.h index 50033bc..bde57c3 100644 --- a/wordblox.h +++ b/wordblox.h @@ -31,6 +31,14 @@ typedef struct { 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); @@ -41,6 +49,95 @@ void reset_color () { 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) { @@ -90,6 +187,7 @@ Puzzle load_puzzle (const char* file) { /* display the puzzle */ void print_puzzle (Puzzle *p) { + printf ("\n"); set_color (WHITE, CYAN); printf (" "); for (int i = 0; i < p->grid_size; i ++) @@ -108,10 +206,14 @@ void print_puzzle (Puzzle *p) } 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 { @@ -126,6 +228,35 @@ void print_puzzle (Puzzle *p) } 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 */ @@ -138,6 +269,23 @@ bool is_valid_word (const char *word) 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)