Search in eight directions
Wordpuzzle.js
Window.onload = entry;
var keyword = ' fuck ';
var words = new Array (4);
Words[0] = new Array (' t ', ' h ', ' I ', ' k ');
WORDS[1] = new Array (' W ', ' A ', ' C ', ' s ');
WORDS[2] = new Array (' O ', ' u ', ' H ', ' J ');
WORDS[3] = new Array (' F ', ' G ', ' d ', ' t ');
function entry () {
Wordpuzzle (words, keyword);
}
function wordpuzzle (words, keyword) {
Exhaustive method, eight directions to find
Right, left, up, down, down, left oblique, opposite left oblique, right oblique under, anti-rightist oblique
var k = 0;
var search = new Array (keyword.length);
for (var i = 0; i < search.length; i++) {
Search[i] = keyword.substring (i, i + 1);
}
Left---->right
for (var i = 0; i < words.length; i++) {
for (var j = 0; J < Words[i].length; J + +) {
if (words[i][j] = = Search[k]) {
k++;
}
else {
k = 0;
Break
}
if (k = = search.length) {
var result = 1;
}
}
k = 0;
}
Right----->left
for (var i = 0; i < words.length; i++) {
for (var j = words[i].length-1; J >= 0; j--) {
if (words[i][j] = = Search[k]) {
k++;
}
else {
k = 0;
Break
}
if (k = = search.length) {
var result = 1;
}
}
k = 0;
}
Top----->bottom
for (var j = 0; J < 4; J + +) {
for (var i = 0; i < words.length; i++) {
if (words[i][j] = = Search[k]) {
k++;
}
else {
k = 0;
Break
}
if (k = = search.length) {
var result = 1;
}
}
k = 0;
}
Bottom----->top
for (var j = 0; J < 4; J + +) {
for (var i = words.length-1; I >= 0; i--) {
if (words[i][j] = = Search[k]) {
k++;
}
else {
k = 0;
Break
}
if (k = = search.length) {
var result = 1;
}
}
k = 0;
}
Top Left-----> bottom Right
var i = j = 0;
while (I < words.length && J < words[i].length) {
if (words[i][j] = = Search[k]) {
k++;
}
else {
k = 0;
Break
}
if (k = = search.length) {
var result = 1;
}
i++;
j + +;
}
k = 0;
Lower right-----> upper Left
var i = words.length-1;
var j = words[i].length-1;
while (i >= 0 && J >= 0) {
if (words[i][j] = = Search[k]) {
k++;
}
else {
k = 0;
Break
}
if (k = = search.length) {
var result = 1;
}
i--;
j--;
}
k = 0;
Lower left-----> upper Right
var i = words.length-1;
var j = 0;
while (i >= 0 && J < words[i].length) {
if (words[i][j] = = Search[k]) {
k++;
}
else {
k = 0;
Break
}
if (k = = search.length) {
var result = 1;
}
i--;
j + +;
}
k = 0;
Top right-----> lower Left
var i = 0;
var j = words[i].length-1;
while (I < words.length && J >= 0) {
if (words[i][j] = = Search[k]) {
k++;
}
else {
k = 0;
Break
}
if (k = = search.length) {
var result = 1;
}
i++;
j--;
}
k = 0;
if (result = = 1) {
Window.alert (' The word exists! ');
}
else {
Window.alert (' The word does not exist! ');
}
}
Data structure and algorithms puzzle game problem