Skip to content

Instantly share code, notes, and snippets.

Created March 15, 2018 18:37
Show Gist options
  • Save anonymous/8fe83890ffaa04fa72cae355a8d714d8 to your computer and use it in GitHub Desktop.
Save anonymous/8fe83890ffaa04fa72cae355a8d714d8 to your computer and use it in GitHub Desktop.
AngularJs Basic Setup (source: http://jsfiddle.net/vjow04jj/1/)
body{
margin: 10px;
}
.warning {
background: #d56c61;
}
td {
border: 1px solid #ccc;
height: 30px;
vertical-align: middle;
width: 30px;
text-align: center;
}
td.highlight {
background: yellow;
font-weight: bold;
}
<div ng-controller="wordsController">
<h2>Find word</h2>
<input type="text" placeholder="Enter a word" ng-model="word" ng-class="{warning: wordNotFound()}" size="30">
<table>
<tr ng-repeat="line in matrix track by $index" ng-init="lineIndex = $index">
<td ng-repeat="column in line track by $index" ng-init="columnIndex = $index" ng-class="{highlight: isHighlighted(lineIndex, columnIndex)}"
ng-bind="column"></td>
</tr>
</table>
</div>
var app = angular.module("myApp", []);
app.controller('wordsController', function ($scope) {
$scope.word = '';
$scope.matrix = [
['d','e','j','s','y','w','y','i','n','l'],
['g','j','g','j','p','m','y','e','t','y'],
['e','g','w','g','r','e','z','h','g','v'],
['y','a','e','p','m','a','m','y','w','b'],
['f','d','y','o','d','x','u','f','a','z'],
['i','n','s','c','n','w','f','x','t','r'],
['x','b','a','k','t','i','e','f','n','s'],
['j','d','j','o','u','o','w','r','o','q'],
['c','s','w','j','s','e','y','h','y','q'],
['d','l','r','k','n','k','y','v','h','n'],
];
$scope.wordNotFound = function () {
return $scope.word && !$scope.found;
}
$scope.isHighlighted = function (line, column) {
var isHighlighted = false;
if (highlights && highlights.length) {
highlights.forEach(function (letter) {
if (letter.pointX == line && letter.pointY == column) {
isHighlighted = true;
}
});
}
return isHighlighted;
}
var highlights = [];
$scope.$watch('word', function (newValue) {
highlights = [];
if ($scope.word) {
$scope.found = true;
hasWord($scope.word);
}
});
var hasWord = function (word) {
var letters = word.toLowerCase().split('');
var currentLetterIndex = 0;
var isArround = function (pointX, pointY, x, y) {
if (pointX == x && pointY == y) {
return false;
}
if (pointX == x || pointX == x - 1 || pointX == x + 1) {
if (pointY == y || pointY == y - 1 || pointY == y + 1) {
return true;
}
}
return false;
}
var findLetter = function (letter, pointX, pointY) {
var out = null;
$scope.matrix.forEach(function (line, indexLine) {
line.forEach(function (column, indexColumn) {
if (!out && column == letter) {
if (!out && (pointX == null && pointY == null) ||
(pointX >= 0 && pointY >= 0 && isArround(pointX, pointY, indexLine, indexColumn))) {
out = {
letter: letter,
pointX: indexLine,
pointY: indexColumn,
};
}
}
});
});
return out;
}
var lastX, lastY;
letters.forEach(function (currentLetter, indexLetter) {
var hasLetter = findLetter(currentLetter, lastX, lastY);
if (hasLetter) {
lastX = hasLetter.pointX;
lastY = hasLetter.pointY;
highlights.push(hasLetter);
} else {
$scope.found = false;
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment