

times do | col | print grid, " " end puts end I initialize it like this ( words is the list of vocabulary words that will be used to build the puzzle): stack = [ Īll that’s left, then, is to display the puzzle: rows.

I could implement this algorithm via recursive function calls, but I opted to use a stack, instead. The algorithm itself begins by initializing a list of possible directions that each word might be drawn in (right-to-left, top-to-bottom, etc.), as well as building a list of all possible positions in the grid: directions = % i ( right down ) directions += % i ( rightdown ) if directions += % i ( left up ) if directions += % i ( leftup leftdown rightup ) if & positions = ( 0. It also makes it really easy to duplicate the grid, so that the state of the grid can be saved and restored as the algorithm backtracks: def dup self. new ( * ) end def index ( row_or_pos, column = nil ) if column row_or_pos * + column else row_or_pos end end def at ( position ) row = position / col = position % end def ( row_or_pos, column = nil ) end #. module WordSearch class Grid attr_reader :rows, :columns, :size def initialize ( rows, columns, grid = nil ), = rows, columns = * = grid || Array. This way I can represent each location as a single integer, which is cleaner than trying to juggle (row, column) tuples. Since the algorithm requires that I be able to try a word against every possible location in the grid, I chose to implement the grid as a one-dimensional array. Visually, it’s a two-dimensional grid of rows and columns: grid = WordSearch :: Grid. The first thing to do was to represent the grid itself.
#Wordsearch puzzle maker generator
After spending ten minutes looking online and being fairly disappointed in the quality of what we found, I decided to take a stab at writing a word search puzzle generator myself.įortunately for me it wasn’t too hard at all, though I’m sure my implementation is far from optimal. I create an empty letter-grid for the current letter-packer.My daughter (age 11) was writing an article this week for a local student newsletter, and had the idea to include a word search puzzle. I return the words that were packed into the letter-grid. I return the words that could not be packed into the letter-grid. I finalize the two-dimensional letter-grid, filling-in any remaining spaces withįor ( var rowIndex = 0 rowIndex row.slice() ) ) Import ] because it could not fit into the current letter-grid.` ) Here's the App component - the generateSearch() method is invoked by the user when they've populated the textarea with a list of words: And then, instantiates the WordPacker class, populates it, and renders the resultant letter-grid.
#Wordsearch puzzle maker code
The Angular code provides a textarea element for a list of words (which is pre-populated for the demo). As you will see below, this demo is mostly JavaScript - the "Angular" part of this Angular application just provides the rendering logic and some small degree of interactions.īefore we look at what the WordPacker class is doing, let's just get the Angular code out of the way. RANT: It's demos like this that showcase how ridiculous it is when inexperienced programmers try to claim that their framework of choice (ex, React) somehow forces them to learn "more JavaScript" than other frameworks. The bulk of the logic is codified within my WordPacker class, which is the class that populates the letter-grid that the Angular application eventually renders. This is an Angular demo but, it doesn't actually contain much Angular code. Instead, this Angular application just takes a list of words and attempts to pack as many of them as it can into a letter-grid of the desired dimensions. For this Angular demo, I'm not providing any interactivity with the letter-grid itself (perhaps that can be a follow-up demo). The goal of the puzzle is to locate and highlight those words. View this code in my JavaScript Demos project on GitHub.Ī Word Search Puzzle is composed of a grid of letters in which connected strings of letters, in different directions, form words from a given list. Run this demo in my JavaScript Demos project on GitHub.
