// Main Function is below void main(void) { char puzzle[82]; // holds the puzzle int indexes[82]; // holds the location of each empty cell int maxIndex; // total count of empty cells while(1) { readPuzzle(puzzle); show(puzzle); maxIndex = indexfinder(puzzle, indexes); solve(0,1, puzzle, indexes, maxIndex); show(puzzle); } }
int main (void) { int row, col, i, max; char *str = malloc(sizeof(char) * (MAX_WORD_LENGTH + 1)); char temp[2]; Dictionary *dict = malloc(sizeof(Dictionary)); WordSearchPuzzle *puzzle = malloc(sizeof(WordSearchPuzzle)); readDictionary(dict); readPuzzle(puzzle); //set bigger dimension of puzzle, use for determining //length of diagonal for() loops if (puzzle->height > puzzle->width) { max = puzzle->height; } else { max = puzzle->width; } temp[1] = '\0'; for (row = 0; row < puzzle->height; row++) { for (col = 0; col < puzzle->width; col++) { //clear array, except for char at index 0 for (i = 1; i < MAX_WORD_LENGTH + 1; i++) { str[i] = '\0'; } //single char strings *str = puzzle->grid[row][col]; checkString(dict, str); //build strings horizontally-right for (i = 1; i < puzzle->width; i++) { //make sure not to go too far! if (col + i < puzzle->width) { temp[0] = puzzle->grid[row][col + i]; str = strcat(str, temp); checkString(dict, str); } } //clear array, except for char at index 0 for (i = 1; i < MAX_WORD_LENGTH + 1; i++) { str[i] = '\0'; } //build strings horizontally-left for (i = 1; i < puzzle->width; i++) { //make sure not to go too far! if (col - i > 0) { temp[0] = puzzle->grid[row][col - i]; str = strcat(str, temp); checkString(dict, str); } } //clear array, except for char at index 0 for (i = 1; i < MAX_WORD_LENGTH + 1; i++) { str[i] = '\0'; } //build strings vertically-down for (i = 1; i < puzzle->height; i++) { if (row + i < puzzle->height) { temp[0] = puzzle->grid[row + i][col]; str = strcat(str, temp); checkString(dict, str); } } //clear array, except for char at index 0 for (i = 1; i < MAX_WORD_LENGTH + 1; i++) { str[i] = '\0'; } //build strings vertically-up for (i = 1; i < puzzle->height; i++) { //why is this >=? top row of puzzle (height/width ints) hindering correct reading? if (row - i >= 0) { temp[0] = puzzle->grid[row - i][col]; str = strcat(str, temp); checkString(dict, str); } } //clear array, except for char at index 0 for (i = 1; i < MAX_WORD_LENGTH + 1; i++) { str[i] = '\0'; } //build string diagonally up-left for (i = 1; i <= max; i++) { if (row - i >= 0 && col - i >= 0) { temp[0] = puzzle->grid[row - i][col - i]; str = strcat(str, temp); checkString(dict, str); } } //clear array, except for char at index 0 for (i = 1; i < MAX_WORD_LENGTH + 1; i++) { str[i] = '\0'; } //build string diagonally up-right for (i = 1; i < max; i++) { if (row - i >= 0 && col + i < puzzle->width) { temp[0] = puzzle->grid[row - i][col + i]; str = strcat(str, temp); checkString(dict, str); } } //clear array, except for char at index 0 for (i = 1; i < MAX_WORD_LENGTH + 1; i++) { str[i] = '\0'; } //build string diagonally down-left for (i = 1; i < max; i++) { if (row + i < puzzle->height && col - i >= 0) { temp[0] = puzzle->grid[row + i][col - i]; str = strcat(str, temp); checkString(dict, str); } } //clear array, except for char at index 0 for (i = 1; i < MAX_WORD_LENGTH + 1; i++) { str[i] = '\0'; } //build string diagonally down-right for (i = 1; i < max; i++) { if (row + i < puzzle->height && col + i < puzzle->width) { temp[0] = puzzle->grid[row + i][col + i]; str = strcat(str, temp); checkString(dict, str); } } }//end [col] for loop }//end [row] for loop for (i = 0; i < dict->size; i++) { if (dict->counts[i] > 0) { printf("%s (%d)\n", dict->words[i], dict->counts[i]); } } destroyDictionary(dict); destroyPuzzle(puzzle); free(str); str = NULL; return 0; }