Exemplo n.º 1
0
//return the track of given word
vector<int> BogglePlayer::isOnBoard(const string& word_to_check)
{
    vector<int> track;
    string word = toLowercase(word_to_check);
    
    //search every grid as a start
    for(int i = 0; i < mRows; i++)    {
        for(int j = 0; j < mCols; j++)        {
            if(searchBoard(word, i, j, track) ) {
                return track;
            }
        }
    }
    return track;
}
Exemplo n.º 2
0
//recursively search the word
bool BogglePlayer::searchBoard(const string &word, int row, int col, vector<int> &track)
{
    int index = getIndex(row, col);
    if (index < 0 || index >= board.size()) {
        return false;
    }
    if (isUsed[index]) {
        return false;
    }
    
    //compare length
    string cur = board[index];
    int curLen = (int)cur.length();
    int wordLen = (int)word.length();
    if (curLen > wordLen) {
        return false;
    }
    
    //get the substring and compare them
    if (word.substr(0, curLen) != cur) {
        return false;
    }
    
    track.push_back(index);
    
    if (wordLen == curLen) {
        return true;
    }
    
    isUsed[index] = true;
    string restStr = word.substr(curLen);
    
    //use static array to set the vector of movement
    static int step[8][2] = {-1, -1, -1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 0, 1, 1};
    for (int i = 0; i < 8; i++) {
        bool res = searchBoard(restStr, row + step[i][0], col + step[i][1], track);
        if (res) {
            isUsed[index] = false;
            return true;
        }
    }
    
    //recover the status of isUsed[]
    isUsed[index] = false;
    track.pop_back();
    return false;
}
Exemplo n.º 3
0
int initializeProgram(){

	char **dictionaryArray = NULL;
	char **wordSearchBoard = NULL;
	char c[MAX_STRING_LENGTH];
	int wordCount, boardsToSolve, n = 1;

	// Opens dictionary file
	FILE *fileDictionary = fopen("dictionary.txt", "r");


	// Verifies that the file opened properly. 
	if (fileDictionary != NULL){

		// Reads in the number of words in the 
		// txt file based on the int on the first
		// line.
		fscanf(fileDictionary, "%i", &wordCount);

		// Calls a function that reads the file into a 2D
		// array.
		dictionaryArray = readFile(fileDictionary, wordCount);
	}

	else {
		return 1;
	}

	scanf("%i", &boardsToSolve);

	// Loops based on the number of
	// puzzles to be solved.
	while (boardsToSolve > 0){

		int rows, cols, i, j;

		scanf("%i", &rows);

		scanf("%i", &cols);

		wordSearchBoard = createBoardArray(rows, cols);

		printf("Words Found Grind #%i\n", n);

		for (i = 0; i < rows; i++){
			for (j = 0; j < cols; j++){
				
				// Copies the function finding words to a useable vairbale.
				strcpy(c, searchBoard(i, j, 0, &wordSearchBoard, &dictionaryArray, wordCount, rows, cols, 0));

				// Prints the word found from the board.
				printf("%s\n", c);

			}
		}

		n++;
		boardsToSolve--;
	}

	fclose(fileDictionary);

	// Frees the 2D array holding dicitonary.
	freeArray(dictionaryArray, wordCount);

	return 0;
}