-
-
Save pererinha/aee98a34a84f02b21dc4b458da811b09 to your computer and use it in GitHub Desktop.
Find word
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
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; | |
} |
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
<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> |
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
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
http://jsfiddle.net/vjow04jj/1/