Code covered by the BSD License  

Highlights from
Boggle (V2.0)

image thumbnail

Boggle (V2.0)

by

 

08 Aug 2011 (Updated )

The traditional Boggle Master Game from Hasbro

insureWord(G, word)
function [bool xit_flag] = insureWord(G, word)
%Function to make sure the word is in the grid
%SCd 12/01/2010
%

    %Word should be uppercase to equal grid
    word = upper(word);
    qflag = false; %set flag
    
    %If there is a Q in the word and no U in the grid, get rid of the U.
    if ismember('Q',word) &&  ~any(ismember('U',G))
        word(find(word=='Q')+1) = [];  %q shouldn't be last line
    elseif ismember('Q',word)
        %If there's a q and a u, try not removing u first, if it fails try
        %again.  Set try again flag here.
        qflag = true; 
    end
    
    %Failure Possibility 1:
    %Make sure all letters are in the grid    
    if any(~ismember(word,G));  
        bool = false;
        xit_flag = sprintf('There are no: %s',word(~ismember(word,G))); %Not valid letters
        return;
    end    
    
    G3D = makeG3D(G,word);
    
    %Use Bruno's binpath to see if word is present
    [xit_flag] = binpath(G3D);
    if isempty(xit_flag)
        xit_flag = 'The word does not exist';
        bool = false;
        if qflag
            word(find(word=='Q')+1) = [];  %q shouldn't be last line
            G3D = makeG3D(G,word);
            [xit_flag] = binpath(G3D);
            if ~isempty(xit_flag)
                bool = true;
            end    
        end    
    else
        bool = true;
    end
end 
function G3D = makeG3D(G,word)
    %Make 3D matrix with each slice pertaining to that letter of the word
    G3D = false(5,5,length(word));
    for ii = 1:length(word)
        G3D(:,:,ii) = G == word(ii);
    end 
end    

Contact us