From: Harishankar Date: Fri, 22 May 2020 14:24:17 +0000 (+0530) Subject: Minor change: Added constant for strings X-Git-Url: https://harishankar.org/repos/?p=wordblah.git;a=commitdiff_plain;h=740b4fb3719b66e5cabff56871914336e1daedc3 Minor change: Added constant for strings Added constant for some hard-coded values --- diff --git a/constantstrings.h b/constantstrings.h index 8dd4582..f5fa059 100644 --- a/constantstrings.h +++ b/constantstrings.h @@ -38,6 +38,8 @@ longer password protected!" Unsaved changes will be lost [y/N]" #define INPUT_CONFIRM_RESET "Are you sure? This will destroy the entire grid\ and is irreversible (y/N): " +#define ACROSS_CLUES "ACROSS - CLUES" +#define DOWN_CLUES "DOWN - CLUES" #define USAGE_LINE_1 "Usage: %s [ [new ]]\n" #define USAGE_LINE_2 " - puzzle file name\n" diff --git a/wordblah.h b/wordblah.h index f818a7e..864b238 100644 --- a/wordblah.h +++ b/wordblah.h @@ -30,11 +30,13 @@ enum COLOR { 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 @@ -579,8 +581,17 @@ void freeze_puzzle (Puzzle *p) /* 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 ++) @@ -588,10 +599,11 @@ void init_puzzle (Puzzle *p, int grid_size) 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"); @@ -621,6 +633,8 @@ void save_puzzle (Puzzle *puzzle, const char* file) 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); @@ -773,9 +787,13 @@ Puzzle load_puzzle (const char* file) /* 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]; @@ -783,15 +801,27 @@ Puzzle load_puzzle (const char* file) /* 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 n1n2 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, " "); } } @@ -806,7 +836,7 @@ Puzzle load_puzzle (const char* file) 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")); @@ -901,7 +931,7 @@ void print_puzzle (Puzzle *p) /* 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 ++) @@ -913,7 +943,7 @@ void print_puzzle (Puzzle *p) } } } - 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 ++)