create a word search solver in matlab

To create a word search solver in MATLAB, we can use the following algorithm:

  1. Load the word search grid into MATLAB using the 'textread' function or by loading a CSV file that contains the grid.
  2. Load the list of words to search for into MATLAB using the 'textread' function or by loading a text file that contains the list.
  3. For each word in the list, we will search for it in the grid using the following steps: a. Convert the word to search for into a vector of characters (i.e., a string). b. For each cell in the grid, we will compare the first character of the word to the value of the cell. c. If the values match, then we will search the adjacent cells in all directions until we either find the word or reach the end of the grid. d. If we find the word, we will record the starting and ending coordinates of the word in the grid. e. If we do not find the word, we will move on to the next cell in the grid.
  4. Once we have searched for all of the words in the list, we will output the starting and ending coordinates of each word in the grid.

Here's some sample code that implements this algorithm:

main.m
% Load the word search grid
grid = textread('wordsearch.csv', '%s', 'delimiter', ',');
gridSize = sqrt(length(grid));

% Load the list of words to search for
words = textread('words.txt', '%s');

% Loop over each word and search for it in the grid
for i = 1:length(words)
    word = char(words(i));
    wordSize = length(word);
    found = false;
    
    % Loop over each cell in the grid
    for j = 1:length(grid)
        % Check if the first character of the word matches the current cell
        if strcmp(word(1), grid{j})
            % Search in all directions for the rest of the word
            for dx = -1:1
                for dy = -1:1
                    % Skip the current cell
                    if dx == 0 && dy == 0
                        continue;
                    end
                    
                    % Initialize the search from the current cell
                    x = mod(j - 1, gridSize) + 1;
                    y = floor((j - 1) / gridSize) + 1;
                    found = true;
                    
                    % Search in the current direction
                    for k = 2:wordSize
                        x = x + dx;
                        y = y + dy;
                        
                        % Check if the current cell is out of bounds
                        if x < 1 || x > gridSize || y < 1 || y > gridSize
                            found = false;
                            break;
                        end
                        
                        % Check if the current cell matches the current character of the word
                        if ~strcmp(word(k), grid{(y - 1) * gridSize + x})
                            found = false;
                            break;
                        end
                    end
                    
                    % Record the location of the word if it was found
                    if found
                        startX = mod(j - 1, gridSize) + 1;
                        startY = floor((j - 1) / gridSize) + 1;
                        endX = x;
                        endY = y;
                        fprintf('Found "%s" at (%d,%d) to (%d,%d)\n', word, startX, startY, endX, endY);
                        break;
                    end
                end
                
                if found
                    break;
                end
            end
            
            if found
                break;
            end
        end
    end
    
    if ~found
        fprintf('Did not find "%s"\n', word);
    end
end
2568 chars
75 lines

Note that this code assumes that the word search grid is stored as a CSV file where each cell is separated by a comma. It also assumes that the list of words to search for is stored as a text file where each word is separated by a newline character. You may need to adjust the code to match the format of your input files.

gistlibby LogSnag