-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordSearch.php
More file actions
303 lines (265 loc) · 9.01 KB
/
WordSearch.php
File metadata and controls
303 lines (265 loc) · 9.01 KB
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
<?php
namespace Jasonmm\WordSearch;
use Twig_Environment;
/**
* Class WordSearch creates a word search grid and can be used to create
* the HTML to display the grid.
* @package Jasonmm\WordSearch
*/
class WordSearch {
private $directions = array(
array(-1, 0, 1, 1, 1, 0, -1, -1), // x
array(-1, -1, -1, 0, 1, 1, 1, 0) // y
);
private $grid = array();
private $rows = 20;
private $cols = 20;
private $title = '';
private $wordList = array();
private $wordListPositions = array();
private $showDate = false;
private $uppercaseWords = true;
private $sortBy = 'alpha';
public function __construct($title = '', $rows = 20, $cols = 20) {
$this->rows = $rows;
$this->cols = $cols;
$this->title = $title;
$this->grid = array();
}
public function isCreated() {
return !empty($this->grid);
}
public function getTitle() {
return $this->title;
}
public function getRows() {
return $this->rows;
}
public function getCols() {
return $this->cols;
}
public function getSortBy() {
return $this->sortBy;
}
public function getShowDate() {
return $this->showDate;
}
/**
* @return array the word list sorted by the current sort criteria.
*/
protected function sortedWordList() {
$wl = $this->wordList;
if( $this->sortBy == 'alpha' ) {
sort($wl);
} else {
usort($wl, function ($a, $b) {
$aLen = strlen($a);
$bLen = strlen($b);
if( $aLen < $bLen ) {
return -1;
}
if( $bLen < $aLen ) {
return 1;
}
return 0;
});
}
return $wl;
}
/**
* @param string $sep
*
* @return string
*/
public function getWordList($sep = ";") {
$wl = $this->sortedWordList();
return implode($sep, $wl);
}
/**
* @param array $wl
* @param string $sortBy
*
* @return int the number of words added to the word list
*/
public function setWordList(array $wl, $sortBy = null) {
if( !is_array($wl) ) {
return 0;
}
if( $sortBy != null ) {
$this->sortBy = $sortBy;
}
$this->wordList = $wl;
$this->wordList = $this->sortedWordList();
return count($this->wordList);
}
/**
* @param string $show
*/
public function setShowDate($show) {
$this->showDate = $show;
}
/**
* @param bool $up
*/
public function setWordsInUppercase($up) {
$this->uppercaseWords = $up;
}
/**
* @param int $maxPlacementTries
*
* @throws \Exception
*/
public function build($maxPlacementTries = 100) {
$alphabet = 'abcdefghijklmnopqrstuvwxyz';
$this->initGrid();
// Loop over each word in the word list.
foreach( $this->wordList as $curWord ) {
$curWord = trim($curWord, "\r\n");
$curWordLen = strlen($curWord);
// Skip blank strings.
if( $curWordLen === 0 ) {
continue;
}
// We loop for the specified number of placement tries. Later on
// we will explicitly break out of the loop if a word was
// successfully placed.
$numTries = 0;
while( $numTries++ < $maxPlacementTries ) {
// Pick a random row, column, and direction.
$randRow = mt_rand(0, $this->rows - 1);
$randCol = mt_rand(0, $this->cols - 1);
$dirIndex = mt_rand(0, 7);
// Get the x and y directions
$dx = $this->directions[0][$dirIndex];
$dy = $this->directions[1][$dirIndex];
// Check to see if the word will fit in the word search grid.
$endRow = $randRow + ($dy * $curWordLen);
$endCol = $randCol + ($dx * $curWordLen);
if( $endRow < 0 || $endRow >= $this->rows ||
$endCol < 0 || $endCol >= $this->cols
) {
continue;
}
// Check to see if placing this word here will work with the
// other words already placed in the grid.
for( $i = 0; $i < $curWordLen; $i++ ) {
$char = substr($curWord, $i, 1);
$cannotBePlaced = $this->gridSquareIsUnacceptable($randRow, $randCol, $char);
if( $cannotBePlaced === true ) {
continue 2;
}
$randRow += $dy;
$randCol += $dx;
}
// Place the word in the grid. The word is placed in
// the grid "backwards". We also record the position of
// the word here so that we always know where this word
// is in the grid.
$end_point = array('x' => $randCol - $dx, 'y' => $randRow - $dy);
for( $i = $curWordLen - 1; $i >= 0; $i-- ) {
$randRow -= $dy;
$randCol -= $dx;
$this->grid[$randRow][$randCol] = strtoupper(substr($curWord, $i, 1));
}
$start_point = array('x' => $randCol, 'y' => $randRow);
$newWordListPosition = array(
'word' => $curWord,
'start_point' => $start_point,
'end_point' => $end_point
);
array_unshift($this->wordListPositions, $newWordListPosition);
break;
}
if( $numTries >= $maxPlacementTries ) {
$msg = 'Error: Unable to place "' . $curWord . '" in word search. Try using fewer words or a larger grid.';
throw new \Exception($msg);
}
}
// Fill in the rest of the grid with random letters.
for( $y = 0; $y < $this->rows; $y++ ) {
for( $x = 0; $x < $this->cols; $x++ ) {
if( $this->grid[$y][$x] == ' ' ) {
$this->grid[$y][$x] = strtoupper(substr($alphabet, mt_rand(0, 25), 1));
}
}
}
}
/**
* @param Twig_Environment $twig
* @param int $version
*
* @return string the HTML used to display the word search grid.
*/
public function getHtml(Twig_Environment $twig, $version) {
$grid = $this->grid;
$wordList = $this->sortedWordList();
// Convert blanks to for display.
foreach( $grid as $y => $row ) {
foreach( $row as $x => $value ) {
if( $value === '' ) {
$grid[$y][$x] = ' ';
}
}
}
// Convert words to uppercase, if necessary.
if( $this->uppercaseWords ) {
foreach( $wordList as &$word ) {
$word = strtoupper($word);
}
}
$params = [
'numRows' => $this->rows,
'numCols' => $this->cols,
'grid' => $grid,
'wordList' => $wordList,
'wordListLen' => count($wordList),
'createdOnString' => $this->showDate ? 'Word Search Created ' . date('r') . '<br>' : '',
'version' => $version,
];
return $twig->render('wordsearch-html.twig', $params);
}
/**
* Initialize this object from the given filename.
*
* @param string $fileName
*
* @return WordSearch|string
*/
public function initFromFile($fileName) {
if( !file_exists($fileName) ) {
return 'File does not exist';
}
$contents = file_get_contents($fileName);
$ws = unserialize(base64_decode($contents));
if( $ws === false ) {
return 'Invalid WordSearcher file';
}
return $ws;
}
/**
* Checks to see if the given character ($ch) can be placed at the given position ($row, $col)
* in the grid.
* @access private
*
* @param int $row the row coordinate in the grid where the character is asking to be
* placed.
* @param int $col the col coordinate in the grid where the character is asking to be
* placed.
* @param string $ch the character asking to be placed in the grid.
*
* @return boolean
*/
private function gridSquareIsUnacceptable($row, $col, $ch) {
return $this->grid[$row][$col] != ' ' && $this->grid[$row][$col] != $ch;
}
/**
* Initialize the grid array with spaces.
* @access private
*/
private function initGrid() {
$this->grid = array_fill(0, $this->rows, ' ');
for( $i = 0; $i < count($this->grid); $i++ ) {
$this->grid[$i] = array_fill(0, $this->cols, ' ');
}
}
}