-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjquery.sokoban.js
executable file
·301 lines (283 loc) · 8.32 KB
/
jquery.sokoban.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
/*
* jQuery Sokoban
* http://galdrar.net/sokoban
*
* Copyright (c) 2009 Borgar Þorsteinsson
* Licensed under the terms of the GPL v3 license.
* http://www.gnu.org/licenses/gpl-3.0.html
*
*/
(function($){
var _R = RegExp;
var _LINE = /^(\s*)(\#|\#[ \.\@\+\$\*\#]*\#)(\s*)$/;
var _CLS = 'soko-';
var _LC = $( '<div class="' + _CLS + 'room"></div>' );
function _obj () { return {}; }
function reMap ( a ) {
return {
dock: "+*.".indexOf( a ) !== -1,
box: "$*".indexOf( a ) !== -1,
man: "+@".indexOf( a ) !== -1,
wall: (a === '#')
}
}
function Sokoban ( level ) {
this.level = $( level );
this.original = this.level.text();
var ok = this.processLevelData();
if ( ok ) {
this.level.bind( 'keydown', function ( e ) { // keypress doesn't work in safari
$( this ).data( 'sokoban' ).keyhandler( e );
return false;
});
}
}
Sokoban.prototype = {
processLevelData: function () {
var pre = [], post = [], level = [], wid = 0;
var lines = this.level.text().split( '\n' );
for ( var line, i=0; i<lines.length; i++ ) {
line = lines[i];
if ( _LINE.test( line ) ) {
wid = Math.max( wid, ( _R.$1 + _R.$2 ).length );
level.push(
$.map( _R.$1.split( '' ), _obj )
.concat( $.map( _R.$2.split(''), reMap ) )
);
}
else {
if ( level.length > 0 ) {
post = lines.slice( i );
break;
}
pre.push( line );
}
}
this.levelData = level;
this.height = level.length;
this.width = wid;
this.indentCleanup();
this.levelContainer = _LC.clone();
this.moves = 0;
this.pushes = 0;
var men = this.count( 'man' ),
docks = this.count( 'dock' ),
boxes = this.count( 'box' );
if ( men !== 1 || docks !== boxes || this.height < 3 || this.width < 3 ) {
// this is an unplayable level
return false;
}
this.level
.empty()
.append( '<div>' + pre.join( '\n' ) + '\n</div>' )
.append( this.levelContainer.empty() )
.append( '<div>' + post.join( '\n' ) + '</div>' );
this.undoBuffer = [];
this.levelContainer
.append( this.renderLevel() )
.attr( 'tabindex', 1 ); // focusable
this.reTitle();
return true;
},
indentCleanup: function () {
this.scanLevel(function ( t, x, y ) {
if ( x === 0 || y === 0 ||
x === this.width -1 || y === this.height -1 ) {
this.floodFill( x, y );
}
});
},
floodFill: function ( x, y ) {
if ( x > -1 && y > -1 && y < this.height && x < this.width ) {
var c = this.levelData[ y ][ x ] || {};
if ( !c.wall && !c.box && !c.man && !c.dock && !c.overflow ) {
c.overflow = true;
this.levelData[ y ][ x ] = c;
this.floodFill( x - 1, y ).floodFill( x, y - 1 )
.floodFill( x + 1, y ).floodFill( x, y + 1 );
}
}
return this;
},
reTitle: function () {
var s = this.boxesLeft ? '' : 'Solved: ';
this.levelContainer.attr( 'title', s + this.moves + ' / ' + this.pushes );
},
renderLevel: function () {
var r = '', P = ' ' + _CLS;
this.boxesLeft = 0;
for ( var y=0; y<this.height; y++ ) {
r += '<div class="' + _CLS + 'line">';
for ( var x=0; x<this.width; x++ ) {
var t = this.levelData[ y ][ x ] || _obj(),
s = ' ',
c = t.overflow ? _CLS + 'indent' : _CLS + 'floor';
if ( t ) {
if ( t.wall ) {
c = P + 'wall';
s = '#';
}
else if ( t.dock ) {
c = P + 'dock';
s = '.';
if ( t.man ) {
c += P + 'worker';
s = '+';
}
else if ( t.box ) {
c += P + 'box';
s = '*';
}
}
else {
if ( t.man ) {
c += P + 'worker';
s = '@';
}
else if ( t.box ) {
c += P + 'box';
s = '$';
this.boxesLeft++;
}
}
}
r += '<span class="' + $.trim( c ) + '">' + s + '</span>';
}
r += '\n</div>';
}
return r;
},
count: function ( attr ) {
var r = 0;
this.scanLevel(function ( t ) { r += t[attr] ? 1 : 0; });
return r;
},
manPos: function () {
var pos;
this.scanLevel(function ( t, x, y ) {
if ( t.man ) {
pos = { x:x, y:y };
return false;
}
});
if ( !pos ) { throw 'man overboard'; }
return pos;
},
scanLevel: function ( callback ) {
var x, y, t, r;
for ( y=0; y<this.height; y++ ) {
for ( x=0; x<this.width; x++ ) {
t = this.levelData[ y ][ x ] || _obj();
r = callback.call( this, t, x, y );
if ( r === false ) {
return r;
}
}
}
return true;
},
move: function ( yofs, xofs ) {
var m = this.manPos(),
u = this.levelData[ m.y + yofs ][ m.x + xofs ];
if ( !u.wall ) {
if ( u.box ) {
var uu = this.levelData[ m.y + (yofs * 2) ][ m.x + (xofs * 2) ];
if ( !uu.wall && !uu.box ) {
// worker may push
this.undo_push();
this.levelData[ m.y ][ m.x ].man = false;
u.man = true;
u.box = false;
uu.box = true;
this.pushes++;
this.levelContainer.html( this.renderLevel() );
this.reTitle();
}
}
else {
// worker may move
this.undo_push();
this.levelData[ m.y ][ m.x ].man = false;
u.man = true;
this.moves++;
this.levelContainer.html( this.renderLevel() );
this.reTitle();
}
}
},
undo_push: function () {
this.undoBuffer.push([ this.levelContainer.text(), this.moves, this.pushes ]);
},
undo_pop: function () {
var undo = this.undoBuffer.pop();
if ( undo ) {
var lines = undo[0].replace( /\n$/, '' ).split( '\n' ),
level = [];
for ( var line, i=0; i<lines.length; i++ ) {
line = lines[i].replace( /\u00A0/g, ' ' );
if ( _LINE.test( line ) ) {
level.push(
$.map( _R.$1.split( '' ), _obj ).concat( $.map( _R.$2.split( '' ), reMap ) )
);
}
else {
throw 'leaky undo buffer';
}
}
this.moves = undo[1];
this.pushes = undo[2];
this.level.removeClass( _CLS + 'solved' );
this.levelData = level;
this.indentCleanup();
this.reTitle();
this.levelContainer.html( this.renderLevel() );
}
},
keyhandler: function ( e ) {
// stop moving on solve
if ( this.boxesLeft ) {
if ( e.keyCode === 38 ) { // up
this.move( -1, 0 );
}
else if ( e.keyCode === 40 ) { // down
this.move( 1, 0 );
}
else if ( e.keyCode === 37 ) { // left
this.move( 0, -1 );
}
else if ( e.keyCode === 39 ) { // right
this.move( 0, 1 );
}
else if ( e.which === 8 ) { // backspace
this.undo_pop();
}
else if ( e.charCode === 122 && ( e.metaKey || e.ctrlKey ) ) { // ctrl-z
this.undo_pop();
}
}
if ( e.keyCode === 27 ) { // esc
this.level
.removeClass( _CLS + 'solved' )
.text( this.original );
this.processLevelData();
var sc = $( window ).scrollTop();
this.levelContainer[0].focus();
$( window ).scrollTop( sc );
}
if ( !this.boxesLeft ) { // room is solved
this.level.trigger( 'solved' );
this.level.addClass( _CLS + 'solved' );
this.undoBuffer = [];
}
}
}
$.fn.sokoban = function () {
return this.each(function(){
var elm = $( this ),
ctl = elm.data( 'sokoban' );
if ( !ctl && !elm.children().length ) {
elm.data( 'sokoban', new Sokoban( this ) );
}
});
};
})(jQuery);