int readPuzzle(WordSearchPuzzle *puzzle) { int row, col; scanf("%d %d", &puzzle->height, &puzzle->width); //malloc() 1st dimension of grid[][] puzzle->grid = malloc(sizeof(char *) * (puzzle->height)); //catch any failed malloc() if (puzzle->grid == NULL) { destroyPuzzle(puzzle); return 0; } //malloc() 2nd dimension of grid[][] for (row = 0; row < puzzle->height; row++) { puzzle->grid[row] = malloc(sizeof(char) * (puzzle->width + 1)); //catch any failed malloc() calls if (puzzle->grid[row] == NULL) { destroyPuzzle(puzzle); return 0; } } //populate grid[][] with puzzle for (row = 0; row < puzzle->height; row++) { scanf("%s", puzzle->grid[row]); } //print puzzle if (DEBUG) { printf("____________PUZZLE____________\n"); for (row = 0; row < puzzle->height; row++) { printf("%s\n", puzzle->grid[row]); } printf("_______________________________\n\n"); } return 1; }
void interfaceLoop() { Puzzle *puzzle = NULL; char command = '\0'; showSolution = false; createDirectionKeys(); createOptionKeys(); do { puzzle = createPuzzle(getPuzzleSize()); shuffleCount = getShuffleCount(); shufflePuzzle(puzzle, shuffleCount); showSolution = false; while( !isSolved(puzzle) ){ printHeader(); printPuzzle(puzzle); command = getNextCommand(puzzle); if(command == INVERT_KEY) { puzzle->inverted = (puzzle->inverted) ? false : true; invertSolution(puzzle); setDirectionVectors(puzzle); } else if(command == SOLVE_KEY) showSolution = (showSolution) ? false : true; else if(command == QUIT_KEY || command == RESTART_KEY) break; else applyDirection(puzzle, getData(directionKeys, command)); } if( isSolved(puzzle) ) { printPuzzle(puzzle); printWinner(); } destroyPuzzle(&puzzle); } while(command == RESTART_KEY); destroyOptionKeys(); destroyDirectionKeys(); }
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; }