-
Notifications
You must be signed in to change notification settings - Fork 111
/
wordfindgame.js
315 lines (269 loc) · 9.35 KB
/
wordfindgame.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
* Wordfind.js 0.0.1
* (c) 2012 Bill, BunKat LLC.
* Wordfind is freely distributable under the MIT license.
* For all details and documentation:
* http://github.com/bunkat/wordfind
*/
(function (document, $, wordfind) {
'use strict';
/**
* An example game using the puzzles created from wordfind.js. Click and drag
* to highlight words.
*
* WordFindGame requires wordfind.js and jQuery.
*/
/**
* Draws the puzzle by inserting rows of buttons into el.
*
* @param {String} el: The jQuery element to write the puzzle to
* @param {[[String]]} puzzle: The puzzle to draw
*/
var drawPuzzle = function (el, puzzle) {
var output = '';
// for each row in the puzzle
for (var i = 0, height = puzzle.length; i < height; i++) {
// append a div to represent a row in the puzzle
var row = puzzle[i];
output += '<div>';
// for each element in that row
for (var j = 0, width = row.length; j < width; j++) {
// append our button with the appropriate class
output += '<button class="puzzleSquare" x="' + j + '" y="' + i + '">';
output += row[j] || ' ';
output += '</button>';
}
// close our div that represents a row
output += '</div>';
}
$(el).html(output);
};
var getWords = function () {
return $('input.word').toArray().map(wordEl => wordEl.value.toLowerCase()).filter(word => word);
};
/**
* Given two points, ensure that they are adjacent and determine what
* orientation the second point is relative to the first
*
* @param {int} x1: The x coordinate of the first point
* @param {int} y1: The y coordinate of the first point
* @param {int} x2: The x coordinate of the second point
* @param {int} y2: The y coordinate of the second point
*/
var calcOrientation = function (x1, y1, x2, y2) {
for (var orientation in wordfind.orientations) {
var nextFn = wordfind.orientations[orientation];
var nextPos = nextFn(x1, y1, 1);
if (nextPos.x === x2 && nextPos.y === y2) {
return orientation;
}
}
return null;
};
/**
* Initializes the WordFindGame object.
*
* Creates a new word find game and draws the board and words.
*
* Returns the puzzle that was created.
*
* @param {String} puzzleEl: Selector to use when inserting the puzzle
* @param {Options} options: WordFind options to use when creating the puzzle
*/
var WordFindGame = function (puzzleEl, options) {
// Class properties, game initial config:
var wordList, puzzle;
/**
* Game play events.
*
* The following events handle the turns, word selection, word finding, and
* game end.
*
*/
// Game state
var startSquare, selectedSquares = [], curOrientation, curWord = '';
/**
* Event that handles mouse down on a new square. Initializes the game state
* to the letter that was selected.
*
*/
var startTurn = function () {
$(this).addClass('selected');
startSquare = this;
selectedSquares.push(this);
curWord = $(this).text();
};
var touchMove = function(e) {
var xPos = e.originalEvent.touches[0].pageX;
var yPos = e.originalEvent.touches[0].pageY;
var targetElement = document.elementFromPoint(xPos, yPos);
select(targetElement)
};
var mouseMove = function() {
select(this);
};
/**
* Event that handles mouse over on a new square. Ensures that the new square
* is adjacent to the previous square and the new square is along the path
* of an actual word.
*
*/
var select = function (target) {
// if the user hasn't started a word yet, just return
if (!startSquare) {
return;
}
// if the new square is actually the previous square, just return
var lastSquare = selectedSquares[selectedSquares.length-1];
if (lastSquare == target) {
return;
}
// see if the user backed up and correct the selectedSquares state if
// they did
var backTo;
for (var i = 0, len = selectedSquares.length; i < len; i++) {
if (selectedSquares[i] == target) {
backTo = i+1;
break;
}
}
while (backTo < selectedSquares.length) {
$(selectedSquares[selectedSquares.length-1]).removeClass('selected');
selectedSquares.splice(backTo,1);
curWord = curWord.substr(0, curWord.length-1);
}
// see if this is just a new orientation from the first square
// this is needed to make selecting diagonal words easier
var newOrientation = calcOrientation(
$(startSquare).attr('x')-0,
$(startSquare).attr('y')-0,
$(target).attr('x')-0,
$(target).attr('y')-0
);
if (newOrientation) {
selectedSquares = [startSquare];
curWord = $(startSquare).text();
if (lastSquare !== startSquare) {
$(lastSquare).removeClass('selected');
lastSquare = startSquare;
}
curOrientation = newOrientation;
}
// see if the move is along the same orientation as the last move
var orientation = calcOrientation(
$(lastSquare).attr('x')-0,
$(lastSquare).attr('y')-0,
$(target).attr('x')-0,
$(target).attr('y')-0
);
// if the new square isn't along a valid orientation, just ignore it.
// this makes selecting diagonal words less frustrating
if (!orientation) {
return;
}
// finally, if there was no previous orientation or this move is along
// the same orientation as the last move then play the move
if (!curOrientation || curOrientation === orientation) {
curOrientation = orientation;
playTurn(target);
}
};
/**
* Updates the game state when the previous selection was valid.
*
* @param {el} square: The jQuery element that was played
*/
var playTurn = function (square) {
// make sure we are still forming a valid word
for (var i = 0, len = wordList.length; i < len; i++) {
if (wordList[i].indexOf(curWord + $(square).text()) === 0) {
$(square).addClass('selected');
selectedSquares.push(square);
curWord += $(square).text();
break;
}
}
};
/**
* Event that handles mouse up on a square. Checks to see if a valid word
* was created and updates the class of the letters and word if it was. Then
* resets the game state to start a new word.
*
*/
var endTurn = function () {
// see if we formed a valid word
for (var i = 0, len = wordList.length; i < len; i++) {
if (wordList[i] === curWord) {
$('.selected').addClass('found');
wordList.splice(i,1);
$('input.word[value="' + curWord + '"]').addClass('wordFound');
}
if (wordList.length === 0) {
$('.puzzleSquare').addClass('complete');
}
}
// reset the turn
$('.selected').removeClass('selected');
startSquare = null;
selectedSquares = [];
curWord = '';
curOrientation = null;
};
/* Constructor START */
$('input.word').removeClass('wordFound');
// Class properties, game initial config:
wordList = getWords().sort();
puzzle = wordfind.newPuzzleLax(wordList, options);
// Draw all of the words
drawPuzzle(puzzleEl, puzzle);
// attach events to the buttons
// optimistically add events for windows 8 touch
if (window.navigator.msPointerEnabled) {
$('.puzzleSquare').on('MSPointerDown', startTurn);
$('.puzzleSquare').on('MSPointerOver', select);
$('.puzzleSquare').on('MSPointerUp', endTurn);
} else {
$('.puzzleSquare').mousedown(startTurn);
$('.puzzleSquare').mouseenter(mouseMove);
$('.puzzleSquare').mouseup(endTurn);
$('.puzzleSquare').on("touchstart", startTurn);
$('.puzzleSquare').on("touchmove", touchMove);
$('.puzzleSquare').on("touchend", endTurn);
}
/**
* Solves an existing puzzle.
*
* @param {[[String]]} puzzle: The puzzle to solve
*/
this.solve = function() {
var solution = wordfind.solve(puzzle, wordList).found;
for( var i = 0, len = solution.length; i < len; i++) {
var word = solution[i].word,
orientation = solution[i].orientation,
x = solution[i].x,
y = solution[i].y,
next = wordfind.orientations[orientation];
var wordEl = $('input.word[value="' + word + '"]');
if (!wordEl.hasClass('wordFound')) {
for (var j = 0, size = word.length; j < size; j++) {
var nextPos = next(x, y, j);
$('[x="' + nextPos.x + '"][y="' + nextPos.y + '"]').addClass('solved');
}
wordEl.addClass('wordFound');
}
}
};
};
WordFindGame.emptySquaresCount = function () {
var allSquares = $('.puzzleSquare').toArray();
return allSquares.length - allSquares.filter(b => b.textContent.trim()).length;
};
// Static method
WordFindGame.insertWordBefore = function (el, word) {
$('<li><input class="word" value="' + (word || '') + '"></li>').insertBefore(el);
};
/**
* Allow game to be used within the browser
*/
window.WordFindGame = WordFindGame;
}(document, jQuery, wordfind));