Word Search Solver

1k Views Asked by At

I tried to make a word search solver. So the purpose of this tool is to count how many times the word given appear in the grid a.k.a array in this project. But I got stuck when I made a code to check vertically, It said "TypeError: Cannot read property '3' of undefined"

Here is the code

var baris = [3];
var kolom = [8];
var kata = ["zpw"];
var puzzle= [
  ["z","x","x","r","r","r","f","z"],
  ["p","l","q","z","h","h","r","p"],
  ["w","o","l","p","p","o","o","w"]
 ];
var i;
var k;
var l;
var x;
var y;
var r=0;//left-right
var s=0;//right-left
var t=0;//up-down
var u=0;//down-up

var z=0;
var a=0;
var n=0;
var p=0;



function vertical() {
for (i=0;i<baris.length;i++) {

  for(k=p;k<(baris[i]+baris[i-1] ||baris[i]);k++ )   {

     for(l=0; l<kolom[i];l++) {  

      if(puzzle[k][l] == kata[i].charAt(0)) {     //up-down CHECKER
        for(y=1;y<kata[i].length ;y++) { 
          if(kata[i].charAt(y) == puzzle[k+y][l]) {
            n=n+1; 
           console.log(n);
          }

        }
         if(n==kata[i].length-1) {
         t=t+1;

          }
         n=0;
      }


    }
    //console.log(t);
  }


 t=0;
 u=0;
 p=p+baris[i];
 }

 }

 vertical();`

Hope you guys can help to find the problem. Thank you very much.

1

There are 1 best solutions below

0
On

I guess what do you need it is make a "clean" code, just think about algorithm, write in on paper, draw it on paper, think again, then create prototype, then refactor, so you will have algorithm divided into autonomous isolated functions. As a reward you will get more-less understanding code, which will be flexible, real to debug, ready to improve and extend.

As I see, you will need something that will:

  1. Create a plain array (or string) from your 2D array.
  2. Check if there is a substring (or validate), it must support words with custom char offset (since original array is 2D).
  3. Check all possibility of substring joinment.

Here is hasty written code, without any "fool protection", will work only on "puzzles" with same rows' sizes, and might be pretty slow:

// Join a 2D array into a plain string.
function Join2DArray(array){
  var val = ""
  for(var i = 0; i < array.length; i++)
    val += array[i].join("")
  return val
}

// Check if substring exists at given position with given letters offset.
function IsSubstring(str, subStr, pos, inc){ 
  var j = 0;
  for(var i = pos; i < str.length, j < subStr.length; i += inc, j++)
    if (str.charAt(i) != subStr.charAt(j)) return false
  if (j < subStr.length) return false
  return true
}

// Find all horizontal left->right and vertical up->down words.
function GetWordCount(array2D, word){ 
  var rowCount = array2D.length
  var rowSize = array2D[0].length
  var string = Join2DArray(array2D)
  var wordLen = word.length
  var wordCount = 0;
  // Search in rows
  for(var j = 0; j < rowCount; j++)
    for (var i = 0; i <= rowSize - wordLen; i++)
      wordCount += IsSubstring(string, word, j * rowSize + i, 1)
  // Search in columns
  for(var i = 0; i < rowSize; i++)
    for(var j = 0; j <= rowCount - wordLen; j++)
      wordCount += IsSubstring(string, word, j * rowSize + i, rowSize)
  return wordLen > 1 ? wordCount : wordCount / 2
}

var puzzle = [['H', 'E', 'L', 'L', 'O'], 
              ['E', ' ', ' ', ' ', ' '],
              ['L', ' ', ' ', 'B', ' '],
              ['L', 'I', 'B', 'R', 'O'],
              ['O', ' ', ' ', 'O', ' ']]

console.log("HELLO: " + GetWordCount(puzzle, "HELLO")); // 2
console.log("BRO: " + GetWordCount(puzzle, "BRO")); // 2
console.log("LIBRO: " + GetWordCount(puzzle, "LIBRO")); // 1
console.log("O: " + GetWordCount(puzzle, "O")); // 4