๐Ÿ“ฆ Haroenv / catsweeper

๐Ÿ“„ catsweeper.js ยท 241 lines
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(function($) {

  // CATSWEEPER CLASS DEFINITION
  // ===========================

  var Catsweeper = function(element, options) {
    this.$element = $(element);
    this.options  = $.extend({}, Catsweeper.DEFAULTS, options);

    this.catdex   = [];   //indices of placed cats
    this.counts   = {};   //neighboring cat counts
    this.$cells   = null; //1-dimensional array representation

    this.$element
      .on('click', 'a.cell', $.proxy(this.click, this))
      .on('start', $.proxy(this.start, this));

    this.render();
  }

  Catsweeper.DEFAULTS = {
    rows: 8,
    columns: 8,
    cats: 10
  };

  // rendering
  // =========

  Catsweeper.prototype.render = function() {
    var size  = this.options.rows * this.options.columns;
    var cells = (new Array(size+1)).join('<a class="cell"></a>');

    this.$element.html('<div class="catgrid">' + cells + '</div>');

    this.$cells = $('a.cell', this.$element);

    return this.resize();
  };

  Catsweeper.prototype.resize = function() {
    var width  = this.$cells[0].offsetWidth;
    var height = this.$cells[0].offsetHeight;

    this.$cells.parent()
      .width(this.options.columns * width)
      .height(this.options.rows * height);

    //force new game upon resize
    this.$element.trigger('start');

    return this;
  };

  Catsweeper.prototype.reset = function() {
    this.$element.removeClass('win fail');
    this.$cells.removeClass('seen fail cat').html('');

    return this;
  };

  // gamedata
  // ========

  Catsweeper.prototype.catify = function() {
    var rows    = this.options.rows;
    var columns = this.options.columns;
    var cats    = this.options.cats;

    var catdex  = [];

    for (var i=0, n=rows*columns; i<n; i++) {
      catdex[i] = i;
    }

    //shuffles
    for (var i=0, n=rows*columns; i<n; i++) {
      var rand = Math.floor(Math.random() * (i+1));
      var value = catdex[i];
      catdex[i] = catdex[rand];
      catdex[rand] = value;
    };

    return catdex.slice(0, Math.max(0, cats));
  };

  Catsweeper.prototype.calculate = function() {
    var rows    = this.options.rows;
    var columns = this.options.columns;

    var counts  = {};

    for (var i=0, n=this.catdex.length; i<n; i++) {
      var neighbors = getNeighbors(this.catdex[i], rows, columns);
      while (neighbors.length) {
        var neighbor = neighbors.pop();
        if (neighbor in counts) counts[neighbor]++;
        else counts[neighbor] = 1;
      }
    }

    return counts;
  };

  // gameplay
  // ========

  Catsweeper.prototype.start = function(e) {
    this.catdex = this.catify();
    this.counts = this.calculate();

    return this.reset();
  };

  Catsweeper.prototype.validate = function() {
    var rows    = this.options.rows;
    var columns = this.options.columns;

    //needs more

    if (this.$cells.not('.seen').length == this.catdex.length) {
      this.gameover(true);
    }

    return this;
  };

  Catsweeper.prototype.gameover = function(win) {
    this.$element.addClass(win ? 'win' : 'fail');

    //reveal cells
    this.$cells.not('.seen').addClass(win ? 'seen' : 'fail');

    //reveal cats
    for (var i=0, n=this.catdex.length; i<n; i++) {
      this.$cells.eq(this.catdex[i]).addClass('cat');
    }

    //new game
    this.$element.one('click', $.proxy(this.start, this));

    return this;
  };

  Catsweeper.prototype.click = function(e) {
    var $cell = $(e.target);
    var index = $cell.index();

    //important
    e.preventDefault();

    if ($cell.hasClass('seen')) {
      //do nothing

    } else if ($.inArray(index, this.catdex) == -1) {
      //no cat, reveal
      this.reveal(index, {});

      //check if won
      this.validate();

    } else {
      //stepped on a cat, game over
      this.gameover();

    }

  };

  Catsweeper.prototype.reveal = function(index, seen) {
    var rows    = this.options.rows;
    var columns = this.options.columns;

    var $cell   = this.$cells.eq(index);

    seen[index] = true;

    if (index in this.counts) {
      $cell.addClass('seen');

      //show count
      $cell.html(this.counts[index]);

    } else if (!$cell.hasClass('seen')) {
      $cell.addClass('seen');

      //expand section
      var neighbors = getNeighbors(index, rows, columns);
      while (neighbors.length) {
        var neighbor = neighbors.pop();
        if (neighbor in seen) continue;
        this.reveal(neighbor, seen);
      }

    }

  }

  // utils
  // =====

  function toIndex(x, y, columns) {
    return y * columns + x;
  }

  function toXY(index, columns) {
    return [index % columns, Math.floor(index / columns)];
  }

  function getNeighbors(index, rows, columns) {
    var xy  = toXY(index, columns);

    var min = [Math.max(0, xy[0]-1), Math.max(0, xy[1]-1)];
    var max = [Math.min(xy[0]+2, columns), Math.min(xy[1]+2, rows)];

    var neighbors = [];

    for (var y=min[1]; y<max[1]; y++) {
      for (var x=min[0]; x<max[0]; x++) {
        var neighbor = toIndex(x, y, columns);
        if (neighbor != index) neighbors.push(neighbor);
      }
    }

    return neighbors;
  }

  // CATSWEEPER PLUGIN DEFINITION
  // ============================

  $.fn.catsweep = function(options) {
    return this.each(function() {
      var $this   = $(this);
      var game    = $this.data('cat.game');

      if (!game) $this.data('cat.game', (game = new Catsweeper(this, options)));
    });
  };

}(jQuery));