WHITE=7
};
+/* Enum to define terminal attributes */
enum ATTR {
NORMAL = 23,
BOLD=1
};
+/* Enum to describe current movement orientation in puzzle grid */
enum ORIENTATION {
ACROSS=1,
DOWN=2
/* reset the entire grid */
void init_puzzle (Puzzle *p, int grid_size)
{
- p->grid_size = grid_size;
+ /* check for bounds */
+ if (p->grid_size > MAX_PUZZLE_SIZE)
+ p->grid_size = MAX_PUZZLE_SIZE;
+ else
+ p->grid_size = grid_size;
+
+ /* grid is always unfrozen for a new puzzle */
p->grid_frozen = false;
+
+ /* initialize all the puzzle data - characters, start of words (across/down)
+ and the clues to null */
for (int i = 0; i < p->grid_size; i ++)
{
for (int j = 0; j < p->grid_size; j ++)
p->chars[i][j] = ' ';
p->start_across_word[i][j] = -1;
p->start_down_word[i][j] = -1;
- strcpy (p->clue_across[i][j], "");
- strcpy (p->clue_down[i][j], "");
+ strcpy (p->clue_across[i][j], "\0");
+ strcpy (p->clue_down[i][j], "\0");
}
}
+ /* reset the master password and solution password */
strcpy (p->hashed_master_password, "\0");
strcpy (p->hashed_solution_password, "\0");
for (int i = 0; i < puzzle->grid_size; i ++)
{
char encrypted[256] = { '\0' };
+ /* encrypt the grid characters at row i with master password to
+ generate the key and iv */
encrypt_data (encrypted, puzzle->chars[i],
puzzle->hashed_master_password);
/* read each character of the grid */
for (int i = 0; i < p.grid_size; i ++ )
{
- char encoded[256];
- fgets (encoded, MAX_CLUE_LENGTH + 10, infile);
- decrypt_data (line, encoded, p.hashed_master_password);
+ char encrypted[256];
+ /* get a line from the file - each line is a grid row */
+ fgets (encrypted, MAX_CLUE_LENGTH + 10, infile);
+ /* decrypt each line from the file and put the decrypted chars
+ into the grid array */
+ decrypt_data (line, encrypted, p.hashed_master_password);
+ /* finally read the decrypted data into the array */
for (int j = 0; j < p.grid_size; j ++)
p.chars[i][j] = line[j];
/* read the word numbers */
for (int i = 0; i < p.grid_size; i ++)
{
+ /* get a line from the file - each file represents a row */
+ /* the word numbers are started as n1<space>n2 where n1 is
+ the across word number and n2 is the down word number.
+ Though both across and down word numbers will be the same
+ in a given cell, we use separate number to determine whether
+ there is an across or down word or both in a given cell. */
fgets (line, MAX_CLUE_LENGTH + 10, infile);
+ /* split the line into tokens with space as the separating character */
char *token = strtok (line, " ");
for (int j = 0; j < p.grid_size; j ++)
{
+ /* so long as token is valid, read the first token as across
+ word number */
if (token != NULL)
p.start_across_word[i][j] = atoi (token);
+ /* similarly get the next token as the down word number */
token = strtok (NULL, " ");
if (token != NULL)
p.start_down_word[i][j] = atoi (token);
+ /* get the next token, it should be the across word number format
+ the next cell or NULL if we have read all the tokens */
token = strtok (NULL, " ");
}
}
while (1)
{
fgets (line, MAX_CLUE_LENGTH + 10, infile);
- /* if reached the end of across clues */
+ /* the word DOWN indicates that we reached the end of across clues */
if (strcmp (line, "DOWN\n") == 0)
break;
word_num[c] = atoi (strtok (line, "\t"));
/* print the clues if set */
if (p->grid_frozen == true)
{
- printf ("\x1B[1mACROSS - CLUES\x1B[0m\n");
+ printf ("\x1B[1m%s\x1B[0m\n", ACROSS_CLUES);
for (int i = 0; i < p->grid_size; i ++)
{
for (int j = 0; j < p->grid_size; j ++)
}
}
}
- printf ("\n\x1B[1mDOWN - CLUES\x1B[0m\n");
+ printf ("\n\x1B[1m%s\x1B[0m\n", DOWN_CLUES);
for (int i = 0; i < p->grid_size; i ++)
{
for (int j = 0; j < p->grid_size; j ++)