Exemple #1
0
static void leftPart(wordRef partialWord, Trie trieNode, int limit,
                     direction expandDir, int anchorRow, int anchorCol) {
    int length = strlen(partialWord);
    extendRight(partialWord, trieNode, expandDir, anchorRow, anchorCol, FALSE);
    if(limit > 0) {
        letter i;
        for(i=FIRST_LETTER;i<=LAST_LETTER;i++) {
            // for each letter, check if it's legal
            if(getChild(trieNode, i) != NULL) {
                if(isInRack(myRack, i)) {
                    deleteSingleLetterFromRack(myRack, i);

                    // recurse!
                    partialWord[length] = i;
                    leftPart(partialWord, getChild(trieNode, i), limit - 1,
                             expandDir, anchorRow, anchorCol);

                    addSingleLetterToRack(myRack, i);
                    
                    // reset the partialWord
                    partialWord[length] = INVALID_LETTER;
                }
            }
        }
    }
}
Exemple #2
0
void Grid::simplify()
{
    extendLeft();
    extendRight();
    extendUp();
    extendDown();
    merge();
}
void BWTTraverse::extractSG(const BWT* pBWT, const BWT* pRevBWT, const unsigned int len)
{
    WARN_ONCE("Skipping bwt[0]");
    // Keep a bool vector marking which entries in pBWT have been visited
    size_t n_elems = pBWT->getBWLen();
    bool_vec visited(n_elems, false);

    size_t count = 0;
    size_t currIdx = 1;
    while(currIdx < n_elems)
    {
        // Invariant: currIdx is the index into pBWT that is the lowest index that has not been visited
        // Left-extend the entry at currIdx into a string of length len
        
        std::string str;
        str.reserve(len);

        // Get the first character of the suffix starting at this position
        char first = pBWT->getF(currIdx);
        str += first;
        BWTInterval range(currIdx, currIdx);

        while(str.length() < len)
        {
            char b = pBWT->getChar(range.lower);
            if(b == '$')
                break;
            str += b;
            BWTAlgorithms::updateInterval(range, b, pBWT);
        }

        // The string was built backwards, reverse it
        str = reverse(str);
        if(str.length() < len)
        {
            visited[currIdx] = true;
        }
        else if(!visited[range.lower])
        {
            markVisited(str, visited, pBWT);
            //std::cout << currIdx << " interval: " << range << " string: " << str << "\n";
            extendLeft(len, str, visited, pBWT, pRevBWT);
            extendRight(len, str, visited, pBWT, pRevBWT, false);
            printf(">%zu %zu 0\n%s\n", count++, str.length(), str.c_str());
        }

        currIdx++;
    }
}
// Extend the string to the left by reversing it and calling extendRight
void BWTTraverse::extendLeft(const unsigned int len, std::string& str, bool_vec& visited, const BWT* pBWT, const BWT* pRevBWT)
{
    str = reverse(str);
    extendRight(len, str, visited, pRevBWT, pBWT, true);
    str = reverse(str);
}
Exemple #5
0
void makeMove(player you, rackRef yourRack, Trie dictionary) {
    strncpy(myRack, yourRack, RACK_SIZE+1);
    me = you;
    bestScore = INITIAL;
    dict = dictionary;

    word emptyWord = {INVALID_LETTER};
    word startingPartialWord = {INVALID_LETTER};

    int i, j;
    direction d;
    for(d=HORIZONTAL;d<=VERTICAL;d++) {
        D("DIRECTION: %d\n",d);
        findAnchors(d);

        for(i=0;i<BOARD_SIZE;i++) {
            for(j=0;j<BOARD_SIZE;j++){
                strncpy(startingPartialWord, emptyWord, BOARD_SIZE);
                if(anchors[i][j]) {
//                if(getCell(i,j) != INVALID_LETTER) {
                    D("finding word from (%d,%d)\n",i,j);
                    int limit = findLimit(i, j, d);
                    if(limit > 0) {
                        leftPart(startingPartialWord, dict,
                                 findLimit(i, j, d), d, i, j);
                    } else {
                        int spwLength = 0;

                        // we already have a left part
                        int row = i;
                        int col = j;

                        int rowChange = 0;
                        int colChange = 0;

                        if(d == HORIZONTAL) {
                            colChange = -1;
                        } else if(d == VERTICAL) {
                            rowChange = -1;
                        }

                        row += rowChange;
                        col += colChange;

                        while(row >= 0 && col >= 0 &&
                              row < BOARD_SIZE && col < BOARD_SIZE) {
                            startingPartialWord[spwLength] = getCell(row, col);
                            spwLength++;

                            row += rowChange;
                            col += colChange;
                        }

                        // reverse the part
                        int k;
                        for(k=0;k<spwLength;k++) {
                            letter temp = startingPartialWord[k];
                            startingPartialWord[k] = startingPartialWord
                                                        [spwLength-k-1];
                            startingPartialWord[spwLength-k-1] = temp;
                        }

                        // extend to the right
                        extendRight(startingPartialWord, dict, d,
                                    i, j, FALSE);
                    }
                }
            }
        }
    }

    if(bestScore == INITIAL) {
        D("DECIDED TO PASS\n");
        playMove(me, PASS, PASS, NULL, HORIZONTAL);
    } else {
        playMove(me, bestRow, bestCol, bestWord, bestDir);
    }
}
Exemple #6
0
static void extendRight(wordRef partialWord, Trie trieNode,
                        direction expandDir, int row, int col,
                        bool coveredAnchor) {
//    D("extendRight: (%d,%d) \"%s\" covered %d\n", row, col, partialWord, coveredAnchor);
    if(row >= 0 && col >= 0 && row < BOARD_SIZE && col < BOARD_SIZE) {
        int length = strlen(partialWord);

        // new co-ordinates, should we need them
        int newRow = row;
        int newCol = col;
        if(expandDir == HORIZONTAL) {
            newCol++;
        } else if(expandDir == VERTICAL) {
            newRow++;
        }

        if(getCell(row, col) == INVALID_LETTER) {
//            D("extendRight: (%d,%d) is empty!\n",row,col);
            if(coveredAnchor != FALSE && isTerminal(trieNode)) {
                int rowChange = 0;
                int colChange = 0;
                if(expandDir == HORIZONTAL) {
                    colChange = -length;
                } else if(expandDir == VERTICAL) {
                    rowChange = -length;
                }

                handlePossibleMove(row + rowChange, col + colChange,
                                   partialWord, expandDir);
            }

            letter i;
            for(i=FIRST_LETTER;i<=LAST_LETTER;i++) {
                if(getChild(trieNode, i) != NULL) {
                    if(isInRack(myRack, i)) {
                        deleteSingleLetterFromRack(myRack, i);

                        // recurse!
                        partialWord[length] = i;

//                        D("going to try '%c' and goto (%d,%d)\n",i,newRow,newCol);
                        extendRight(partialWord, getChild(trieNode, i), 
                                    expandDir, newRow, newCol, TRUE);

                        addSingleLetterToRack(myRack, i);
                        
                        // reset the partialWord
                        partialWord[length] = INVALID_LETTER;
                    }
                }
            }
        } else {
            if(getChild(trieNode, getCell(row, col)) != NULL) {
                // recurse!
                partialWord[length] = getCell(row, col);

//                D("filled!\n");
                extendRight(partialWord, getChild(trieNode, getCell(row, col)), 
                            expandDir, newRow, newCol, TRUE);

                // reset the partialWord
                partialWord[length] = INVALID_LETTER;
            }
        }
    }
}