- You find every unique set of 3 words.
- You get all 6 possible matrices for those 3 words.
- You do a dictionary check for the 5 words that could be created from those matrices (3 columns and 2 diagonals).
Some JavaScript to illustrate.
//setup a test dictionary
var dict = [
"MAD",
"FAD",
"CAT",
"ADD",
"DOG",
"MOD",
"FAM",
"ADA",
"DDD",
"FDD"
];
for(var i=0; i<dict.length; i++)
dict[dict[i]]=true;
// functions
function find(dict) {
for(var x=0; x<dict.length; x++) {
for(var y=x+1; y<dict.length; y++) {
for(var z=y+1; z<dict.length; z++) {
var a=dict[x];
var b=dict[y];
var c=dict[z];
if(valid(dict,a,b,c)) return [a,b,c];
if(valid(dict,a,c,b)) return [a,c,b];
if(valid(dict,b,a,c)) return [b,a,c];
if(valid(dict,b,c,a)) return [b,c,a];
if(valid(dict,c,a,b)) return [c,a,b];
if(valid(dict,c,b,a)) return [c,b,a];
}
}
}
return null;
}
function valid(dict, row1, row2, row3) {
var words = [];
words.push(row1.charAt(0)+row2.charAt(0)+row3.charAt(0));
words.push(row1.charAt(1)+row2.charAt(1)+row3.charAt(1));
words.push(row1.charAt(2)+row2.charAt(2)+row3.charAt(2));
words.push(row1.charAt(0)+row2.charAt(1)+row3.charAt(2));
words.push(row3.charAt(0)+row2.charAt(1)+row1.charAt(2));
for(var x=0; x<words.length; x++)
if(dict[words[x]] == null) return false;
return true;
}
//test
find(dict);