How to Create a Word Search Game Board

This example shows how to create a wordsearch game board using JavaScript, HTML, and CSS. This particular method doesn't attempt to populate a predetermined word list after creating the board, which leaves an interesting possibility--to discover whether an algorithm populating squares with random letters can unintentionally encode words into the game board. And if so, how complicated the words can be.
See Also

Let's quickly examine what needs to happen for us to achieve our objective here. A word search board is a matrix of squares "x" columns wide by "y" rows tall. Inside of each square appears a letter of the alphabet. When examined carefully, words can be found by examining letters in adjacent squares to see if they can be used to string something together. This example will differ in that no specific words will be intentionally included--instead leaving the possibility that the algorithm used to select letters at random may unintentionally create words. A more interesting wrinkle still is--how complicated of a word can be created through this random technique? We want the board to be visually appealing enough that someone can examine it at length for patterns, as result we'll want there to be something visually appealing. Because visually appealing is so subjective, we'll hope that our use of fonts, colors, and spacing will appeal. In order to accommodate changing the visual aspects without requiring modifications to the underlying logic building the board, we'll use CSS classes in order to apply the rules for the presentation aspects.

Breaking this further into a specific series of steps here's what we'll need to do:

  1. Determine x and y. How wide and tall do we want the table?
  2. Create a matrix x wide by y tall.
  3. Inside of each square we'll want to place a random letter.
  4. Once the board is rendered, we'll want a user interacting with it to be able to see where their interaction will affect the board.

Let's address each section and decide how we'll accomplish that step.

"Determine x and y". Let's start with a default x and y, we'll say "10" by "10". We'll also explore allowing a user to request a specific number for x and y and use them to regenerate a board using them. To allow users to provide a value, we'll want to include form inputs which are one means HTML provides for soliciting feedback.

"Create a matrix x wide by y tall." The HTML table structure provides an excellent way to create the two-dimensional structure we want for our board. Rather than statically hand-coding a table structure, let's use JavaScript to govern the logic the will allow us not only to dynamically create the structure but also provide a means of getting random letters to populate the table with.

"Inside of each square we'll want to place a random letter." As described above, we can use JavaScript to create a function we can call over and over again as many times as we need in order to get a random letter for each square. To make this easier, we'll handle building the board and populating a letter in a single pass.

"Once the board is rendered, we'll want a user interacting with it to be able to see where their interaction will affect the board." We'll accomplish this by attaching "event listeners" which can detect when the a user's mouse drives over a square, and when the mouse is clicked on a square. We'll differentiate the mouseover and click events using color changes and perhaps a font change.

Functional Example

Code Sample

<style> .gameboard{ border:3px #eef solid; } .gameboard td{ font-family: arial, sans-serif; font-size: 15pt; font-weight: bold; text-align: center; vertical-align: middle; color: #933; background: #ffe; padding: 8px; margin: 2px; border: 2px #eef solid; } td.gameboard_over{ color: #fff; background: #000; } td.gameboard_clicked{ color: #000; background: #ddd; } </style> <div id="game"></div> <form onsubmit="return false"> <input type="button" value="Reset" onclick="main();"> </form> <script type="text/javascript"><!-- function getRandomLetter(){ var data = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; var rnd = parseInt(Math.random()*data.length) return data.charAt(rnd); } function hMouseOver(obj){ if(obj.className.match(/clicked/)) return if(!obj.orgClassName) obj.orgClassName = obj.className; obj.className = 'gameboard_over' } function hMouseOut(obj){ if(obj.className.match(/clicked/)) return obj.className = obj.orgClassName } function hMouseClick(obj){ obj.className = 'gameboard_clicked' } function createBoard(x,y){ var html = '<table border="0" cellpadding="0" cellspacing="0" class="gameboard">' for(var i=0; i<y; i++){ html += '<tr>' for(var j=0; j<x; j++){ html += '<td onmouseover="hMouseOver(this);" onmouseout="hMouseOut(this);" onclick="hMouseClick(this);">' html += getRandomLetter() html += '</td>' } html += '</tr>' } html += '</table>' return html } function main(){ var obj = document.getElementById('game'); obj.innerHTML = createBoard(10,10); } main(); //--></script>

Code Process Review

Here's what's happening... line by line.

More Resources