summaryrefslogtreecommitdiff
path: root/js/wordsearch.js
blob: ce85f0e16a9a97c7728f625a1aef013c3fdc908f (plain)
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
(function(){
  'use strict';

  // Extend the element method
  Element.prototype.wordSeach = function(settings) {
    return new WordSeach(this, settings);
  }

  /**
   * Word seach
   *
   * @param {Element} wrapWl the game's wrap element
   * @param {Array} settings
   * constructor
   */
  function WordSeach(wrapEl, settings) {
    this.wrapEl = wrapEl;
    this.settings = settings;

    // Check the words' length if it is overflow the grid
    if (this.parseWords(this.settings.gridSize)) {
      // Add words into the martrix data
      var isWorked = false;

      while (isWorked == false) {
        // initialize the application
        this.initialize();

        isWorked = this.addWords();
      }

      // Fill up the remaining blank items
      this.fillUpFools();

      // Draw the martrix into wrap element
      this.drawMartrix();
    }
  }

  /**
   * Put the words into the martrix
   */
  WordSeach.prototype.addWords = function() {
      var keepGoing = true,
        counter = 0,
        isWorked = true;

      while (keepGoing) {
        var dir = Math.rangeInt(3), // Direction
          result = this.addWord(this.settings.words[counter], dir),
          isWorked = true;

        if (result == false) {
          keepGoing = false;
          isWorked = false;
        }

        counter++;
        if (counter >= this.settings.words.length) {
          keepGoing = false;
        }
      }

      return isWorked;
  }

  /**
   * Add word into the martrix
   */
  WordSeach.prototype.addWord = function(word, direction) {
    var itWorked = true;

    switch (direction) {
      case 0: // Horizontal
        // New row(y) and col(x)
        var row = Math.rangeInt(this.settings.gridSize  - 1),
          col = Math.rangeInt(this.settings.gridSize - word.length);

        for (var i = 0; i < word.length; i++) {
          var origin = this.martrix[row][col + i].letter;
          if (origin == '.' || origin == word[i]) {
            this.martrix[row][col + i].letter = word[i];
          } else {
            itWorked = false;
          }
        }
        break;

      case 1: // vertical
        var row = Math.rangeInt(this.settings.gridSize - word.length),
          col = Math.rangeInt(this.settings.gridSize  - 1);

        for (var i = 0; i < word.length; i++) {
          var origin = this.martrix[row + i][col].letter;
          if (origin == '.' || origin == word[i]) {
            this.martrix[row + i][col].letter = word[i];
          } else {
            itWorked = false;
          }
        }
        break;

      case 2: // From top left to bottom right
        var row = Math.rangeInt(this.settings.gridSize - word.length),
          col = Math.rangeInt(this.settings.gridSize - word.length);
        var col = Math.floor(Math.random() * (this.settings.gridSize - word.length + 1)),
          row = Math.floor(Math.random() * (this.settings.gridSize - word.length + 1));

        for (var i = 0; i < word.length; i++) {
          var origin = this.martrix[row + i][col + i].letter;
          if (origin == '.' || origin == word[i]) {
            this.martrix[row + i][col + i].letter = word[i];
          } else {
            itWorked = false;
          }
        }
        break;

      case 3: // From top right to bottom left
        var row = Math.rangeInt(this.settings.gridSize - word.length),
          col = Math.rangeInt(word.length - 1, this.settings.gridSize - 1);

        for (var i = 0; i < word.length; i++) {
          var origin = this.martrix[row + i][col - i].letter;
          if (origin == '.' || origin == word[i]) {
            this.martrix[row + i][col - i].letter = word[i];
          } else {
            itWorked = false;
          }
        }
        break;
    }

    return itWorked;
  }

  /**
   * Initialize the application
   */
  WordSeach.prototype.initialize = function() {
    /**
     * Store the words
     *
     * param {Array}
     */
    this.martrix = [];

    this.initMartrix(this.settings.gridSize);
  }

  WordSeach.prototype.parseWords = function(maxSize) {
    var itWorked = true;

    for (var i = 0; i < this.settings.words.length; i++) {
      // Convert all the letters to upper case
      this.settings.words[i] = this.settings.words[i].toUpperCase();
      var word = this.settings.words[i];

      if (word.length > maxSize) {
        alert('The length of word `' + word + '` is overflow the gridSize.');
        console.error('The length of word `' + word + '` is overflow the gridSize.');
        itWorked = false;
      }
    }

    return itWorked;
  }

  /**
   * Fill default items into the martrix
   */
  WordSeach.prototype.initMartrix = function(size) {
    for (var row = 0; row < size; row++) {
      for (var col = 0; col < size; col++) {
        var item = {
          'letter': '.' // Default value
        }

        if (!this.martrix[row]) {
          this.martrix[row] = [];
        }

        this.martrix[row][col] = item;
      }
    }
  }

  /**
   * Draw the martrix
   */
  WordSeach.prototype.drawMartrix = function() {
    for (var row = 0; row < this.settings.gridSize; row++) {
      // New row
      var divEl = document.createElement('div');
      this.wrapEl.appendChild(divEl);

      for (var col = 0; col < this.settings.gridSize; col++) {
        var cvEl = document.createElement('canvas');
        cvEl.setAttribute('class', 'ws-letter');
        cvEl.setAttribute('width', 40);
        cvEl.setAttribute('height', 40);

        // Fill text in middle center
        var x = cvEl.width / 2,
          y = cvEl.height / 2;

        var ctx = cvEl.getContext('2d');
        ctx.font = '30px Calibri';
        ctx.textAlign = 'center';
        ctx.fillStyle = '#333'; // Text color
        ctx.fillText(this.martrix[row][col].letter, x, y);

        divEl.appendChild(cvEl);
      }
    }
  }

  /**
   * Fill up the remaining blank items
   */
  WordSeach.prototype.fillUpFools = function() {
    for (var row = 0; row < this.settings.gridSize; row++) {
      for (var col = 0; col < this.settings.gridSize; col++) {
        if (this.martrix[row][col].letter == '.') {
          this.martrix[row][col].letter = String.fromCharCode(Math.rangeInt(65, 90));
        }
      }
    }
  }

})();