-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboggle.js
73 lines (51 loc) · 1.87 KB
/
boggle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var sampleBoggle = [
['A', 'R', '6', 'B'],
['L', 'P', 'I', 'N'],
['H', 'E', 'V', 'K'],
['O', 'Z', 'R', 'S'],
//['H', 'E', 'V', 'K'],
//['A', 'R', '6', 'B'],
//['H', 'E', 'V', 'K'],
//['L', 'P', 'I', 'N'],
];
//Helper object to calculate indices of adjacent cells.
//Also passes the need of an additional for loop in getAdjascentLetters function
var adjascent = [
{i:-1, j:-1}, {i: 0, j:-1}, {i: 1, j:-1},
{i:-1, j: 0}, {i: 1, j: 0},
{i:-1, j: 1}, {i: 0, j: 1}, {i: 1, j: 1},
];
const rows = sampleBoggle.length; //number of rows
const cols = sampleBoggle[0].length; //number of cols
var visitedCells = new Array(rows).fill(false).map(() => new Array(cols).fill(false)); //Creating matrix to keep track of visited cells
var myWord = "6Rivers";
myWord = myWord.toUpperCase();
var foundString = "";
var start = new Date()
findWordInBoggle();
var end = new Date()-start;
console.log('in '+end/1000+' seconds');
function findWordInBoggle(){
for(let i = 0; i < rows; i++){
for (let j = 0; j < cols; j++){
getAdjascentLetters(i, j);
}
}
}
function getAdjascentLetters(i, j){
visitedCells[i][j] = true; //Set to true, the loop below only checks non-visted cells
foundString += sampleBoggle[i][j];
let iNext, jNext;
if (foundString == myWord)
console.log('Found: '+ foundString);
//Loop through adjacent cells
for(let k = 0; k < 8; k++){ //There is a max of 8 adjacent cells
iNext = i+adjascent[k].i;
jNext = j+adjascent[k].j;
if (iNext >= 0 && jNext >= 0 && iNext < rows && jNext < cols && !visitedCells[iNext][jNext]){
getAdjascentLetters(iNext, jNext);
}
}
foundString = foundString.slice(0, -1); //remove last added character
visitedCells[i][j] = false; // current cell was not visited for next set of recursive calls
}