Data structure and algorithms puzzle game problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.