First commit
[wordblah.git] / wordblox.h
1 #ifndef __WORDBLOX_H
2 #define __WORDBLOX_H
3
4 #include "constantstrings.h"
5
6 #define MAX_PUZZLE_SIZE 20
7 #define MAX_CLUE_LENGTH 150
8
9 /* Enum to define terminal colours */
10 enum COLOR {
11 BLACK = 0,
12 RED= 1,
13 GREEN=2,
14 YELLOW=3,
15 BLUE=4,
16 MAGENTA=5,
17 CYAN=6,
18 WHITE=7
19 };
20
21 typedef char String[MAX_CLUE_LENGTH];
22
23 /* The main puzzle struct type */
24 typedef struct {
25 char chars[MAX_PUZZLE_SIZE][MAX_PUZZLE_SIZE];
26 int start_across_word[MAX_PUZZLE_SIZE][MAX_PUZZLE_SIZE];
27 int start_down_word[MAX_PUZZLE_SIZE][MAX_PUZZLE_SIZE];
28 String clue_across[MAX_PUZZLE_SIZE][MAX_PUZZLE_SIZE];
29 String clue_down[MAX_PUZZLE_SIZE][MAX_PUZZLE_SIZE];
30 int grid_size;
31 bool grid_frozen;
32 } Puzzle;
33
34 /* Set the terminal colour */
35 void set_color (enum COLOR fg, enum COLOR bg) {
36 printf ("\x1B[%d;%dm", fg+30, bg+40);
37 }
38
39 /* Reset the terminal colour */
40 void reset_color () {
41 printf ("\x1B[0m");
42 }
43
44 /* reset the entire grid */
45 void init_puzzle (Puzzle *p, int grid_size)
46 {
47 p->grid_size = grid_size;
48 p->grid_frozen = false;
49 for (int i = 0; i < p->grid_size; i ++)
50 {
51 for (int j = 0; j < p->grid_size; j ++)
52 {
53 p->chars[i][j] = ' ';
54 p->start_across_word[i][j] = -1;
55 p->start_down_word[i][j] = -1;
56 strcpy (p->clue_across[i][j], "");
57 strcpy (p->clue_down[i][j], "");
58 }
59 }
60 }
61
62 /* save the puzzle */
63 void save_puzzle (Puzzle *puzzle, const char* file) {
64 FILE *outfile;
65 outfile = fopen (file, "wb");
66 if (outfile == NULL)
67 {
68 fprintf (stderr, "%s\n", ERROR_WRITING_FILE);
69 exit (1);
70 }
71 fwrite (puzzle, sizeof (*puzzle), 1, outfile);
72 fclose (outfile);
73 }
74
75 /* read the puzzle */
76 Puzzle load_puzzle (const char* file) {
77 FILE *infile;
78 Puzzle p;
79 infile = fopen (file, "rb");
80 if (infile == NULL)
81 {
82 fprintf (stderr, "%s\n", ERROR_READING_FILE);
83 exit (1);
84 }
85 fread (&p, sizeof(p), 1, infile);
86 fclose (infile);
87 return p;
88 }
89
90 /* display the puzzle */
91 void print_puzzle (Puzzle *p)
92 {
93 set_color (WHITE, CYAN);
94 printf (" ");
95 for (int i = 0; i < p->grid_size; i ++)
96 printf ("%3d", i);
97 reset_color ();
98 printf("\n");
99 for (int i = 0; i < p->grid_size; i ++)
100 {
101 set_color (WHITE, CYAN);
102 printf ("%3d ", i);
103 for (int j = 0; j < p->grid_size; j ++)
104 {
105 if (p->chars[i][j] == '#') {
106 set_color (WHITE, BLACK);
107 printf (" ");
108 }
109 else
110 {
111 if (p->start_across_word[i][j] != -1)
112 {
113 set_color (BLUE, WHITE);
114 printf ("%2d", p->start_across_word[i][j]);
115 }
116 else
117 {
118 set_color (BLACK, WHITE);
119 printf (" ");
120 }
121
122 set_color (BLACK, WHITE);
123 printf ("%c", p->chars[i][j]);
124 }
125 reset_color ();
126 }
127 printf ("\n");
128 }
129 }
130
131 /* function to check if a word is valid or not */
132 bool is_valid_word (const char *word)
133 {
134 for (int i = 0; i < strlen (word); i ++)
135 if (! isalpha (word[i]))
136 return false;
137
138 return true;
139 }
140
141 /* function to print a menu */
142 void print_menu (enum COLOR fg, enum COLOR bg, const char* title,
143 char **items, int num_items, int padding)
144 {
145 /* clear screen */
146 printf ("\e[1;1H\e[2J");
147 set_color (fg, bg);
148 printf ("\u2554");
149 for (int i = 0; i < padding; i ++)
150 printf ("\u2550");
151 printf ("\u2557");
152 reset_color (); printf ("\n");
153 set_color (fg, bg);
154 printf ("\u2551%-*s\u2551", padding, title);
155 reset_color (); printf ("\n");
156 set_color (fg, bg);
157 printf ("\u2560");
158 for (int i = 0; i < padding; i ++)
159 printf ("\u2550");
160 printf ("\u2563");
161 reset_color (); printf ("\n");
162 for (int i = 0; i < num_items; i ++)
163 {
164 set_color (fg, bg);
165 printf ("\u2551%-*s\u2551", padding, items[i]);
166 reset_color (); printf ("\n");
167 }
168 set_color (fg, bg);
169 printf ("\u255A");
170 for (int i = 0; i < padding; i ++)
171 printf ("\u2550");
172 printf ("\u255D");
173 reset_color (); printf ("\n");
174 }
175
176 #endif