Last active
May 9, 2022 20:40
-
-
Save vzsoares/2fdb753ecdbc111b5ad054d7bd8ca6b6 to your computer and use it in GitHub Desktop.
caca-palavras0.01.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// backup1 | |
// encontre a palavra "Azul" | |
const grid = [ | |
["A", "B", "A", "C", "A", "T", "E", "A", "Z", "U", "L", "P", "E"], | |
["P", "P", "L", "U", "L", "A", "O", "Z", "Q", "E", "E", "F", "S"], | |
["D", "O", "T", "R", "E", "S", "K", "U", "W", "Q", "O", "M", "L"], | |
["R", "I", "O", "V", "A", "M", "P", "L", "T", "A", "D", "U", "A"], | |
["A", "D", "K", "A", "L", "S", "A", "O", "D", "S", "Z", "D", "Z"], | |
["Z", "D", "K", "S", "Z", "S", "A", "O", "D", "A", "K", "D", "U"], | |
["U", "O", "T", "S", "L", "U", "A", "O", "L", "U", "Z", "A", "L"], | |
["L", "O", "A", "Z", "U", "L", "Z", "L", "P", "S", "K", "D", "D"], | |
]; | |
const findMatch = (word, grid) => { | |
const result = []; | |
for (let i = 0; i < grid.length; i++) { | |
for (let j = 0; j < grid[i].length; j++) { | |
if (hasWords(word, i, j, grid)) { | |
result.push([i, j] + "vertical"); | |
} | |
if (hasWords2(word, i, j, grid)) { | |
result.push([i, j] + "horizontal"); | |
} | |
} | |
} | |
return result; | |
}; | |
const hasWords = (word, i, j, grid) => { | |
const myWord = word.split(""); | |
for (let x = 0; x < myWord.length; x++) { | |
if (i + x >= grid.length) { | |
return false; | |
} | |
if (myWord[x] !== grid[i + x][j]) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
const hasWords2 = (word, i, j, grid) => { | |
const myWord = word.split(""); | |
for (let x = 0; x < myWord.length; x++) { | |
if (j + x >= grid[i].length) { | |
return false; | |
} | |
if (myWord[x] !== grid[i][j + x]) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
console.log(findMatch("AZUL", grid)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment