4 #include "constantstrings.h"
6 #define MAX_PUZZLE_SIZE 20
7 #define MAX_CLUE_LENGTH 150
9 /* Enum to define terminal colours */
21 typedef char String
[MAX_CLUE_LENGTH
];
23 /* The main puzzle struct type */
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
];
34 /* Set the terminal colour */
35 void set_color (enum COLOR fg
, enum COLOR bg
) {
36 printf ("\x1B[%d;%dm", fg
+30, bg
+40);
39 /* Reset the terminal colour */
44 /* reset the entire grid */
45 void init_puzzle (Puzzle
*p
, int grid_size
)
47 p
->grid_size
= grid_size
;
48 p
->grid_frozen
= false;
49 for (int i
= 0; i
< p
->grid_size
; i
++)
51 for (int j
= 0; j
< p
->grid_size
; 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
], "");
63 void save_puzzle (Puzzle
*puzzle
, const char* file
) {
65 outfile
= fopen (file
, "wb");
68 fprintf (stderr
, "%s\n", ERROR_WRITING_FILE
);
71 fwrite (puzzle
, sizeof (*puzzle
), 1, outfile
);
76 Puzzle
load_puzzle (const char* file
) {
79 infile
= fopen (file
, "rb");
82 fprintf (stderr
, "%s\n", ERROR_READING_FILE
);
85 fread (&p
, sizeof(p
), 1, infile
);
90 /* display the puzzle */
91 void print_puzzle (Puzzle
*p
)
93 set_color (WHITE
, CYAN
);
95 for (int i
= 0; i
< p
->grid_size
; i
++)
99 for (int i
= 0; i
< p
->grid_size
; i
++)
101 set_color (WHITE
, CYAN
);
103 for (int j
= 0; j
< p
->grid_size
; j
++)
105 if (p
->chars
[i
][j
] == '#') {
106 set_color (WHITE
, BLACK
);
111 if (p
->start_across_word
[i
][j
] != -1)
113 set_color (BLUE
, WHITE
);
114 printf ("%2d", p
->start_across_word
[i
][j
]);
118 set_color (BLACK
, WHITE
);
122 set_color (BLACK
, WHITE
);
123 printf ("%c", p
->chars
[i
][j
]);
131 /* function to check if a word is valid or not */
132 bool is_valid_word (const char *word
)
134 for (int i
= 0; i
< strlen (word
); i
++)
135 if (! isalpha (word
[i
]))
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
)
146 printf ("\e[1;1H\e[2J");
149 for (int i
= 0; i
< padding
; i
++)
152 reset_color (); printf ("\n");
154 printf ("\u2551%-*s\u2551", padding
, title
);
155 reset_color (); printf ("\n");
158 for (int i
= 0; i
< padding
; i
++)
161 reset_color (); printf ("\n");
162 for (int i
= 0; i
< num_items
; i
++)
165 printf ("\u2551%-*s\u2551", padding
, items
[i
]);
166 reset_color (); printf ("\n");
170 for (int i
= 0; i
< padding
; i
++)
173 reset_color (); printf ("\n");