/
Game.js
307 lines (221 loc) · 8.36 KB
/
Game.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
/*
* Copyright (C) 2012 André jr Steenveld all rights reserved
* Licensed under the MIT public license for the full license see the LICENSE file
*
*/
define( [ ".", "lib" ], function( chess, lib ){
chess.Game = lib.declare( [ lib.Evented ], {
counter: 0,
color: null,
board: null,
white: null,
black: null,
constructor: function( _game_ ){
var board = this.board = _game_.board;
lib.aspect.after( board, "place", this._placeHandler.bind( this ), true );
board.piecesInPlay.forEach(
Function.bind( this, function( piece ){
lib.aspect.around( piece, "move", this._moveHandler.bind( this, piece ) );
})
);
this.whiteEmitter = lib.delegate( this );
this.blackEmitter = lib.delegate( this );
"white" in _game_ && _game_.white.join( this, "white" );
"black" in _game_ && _game_.black.join( this, "black" );
"color" in _game_ && ( this.color = _game_.color );
},
turn: function( ){
return this.color && this.color === "white"
? ( this.color = "black" )
: ( this.color = "white" );
},
start: function( ){
if(
this.white && this.white.isInstanceOf( chess.Player )
&& this.black && this.black.isInstanceOf( chess.Player )
){
// Flip the color, hackish but effective
!this.color || this.color === "white"
? ( this.color = "white" )
: ( this.color = "black" );
this.emit.onIdle( this, [ "Start", { color: this.color } ] );
} else {
console.warn( "Game#start :: Not enough players to start the game" );
}
},
end: function( conditions ){
if( conditions.result === "Surrender" ){
this.emit.onIdle( this, [ "Surrender", conditions ] );
}
},
draw: function( ){
},
//
// Handlers for the place and occupy methods from the board
//
_placeHandler: function( field, piece ){
this.emit.onIdle( this, [ "Placed", {
field: field,
piece: piece
}]);
},
_moveHandler: function( piece, move ){
return Function.bind( this, function( toField ){
var occupant = toField.piece || null,
field = piece.field;
if( move.call( piece, toField ) ){
var _moved_ = {
counter: this.counter++,
color: this.color,
occupant: occupant, // The current puece on the field
piece: piece, // The piece we are moving
from: field, // Where did the moving piece come from
to: toField // Where are we going?
};
piece.type === "Pawn" && piece.y === 7
? this.emit.onIdle( this, [ "Promotion", lib.delegate( _moved_, { color: piece.color } ) ] )
.then( this.emit.async( this, [ "Moved", _moved_ ] ) )
: this.emit.onIdle( this, [ "Moved", _moved_ ] );
return true;
} else {
this.emit.onIdle( this, [ "IllegalMove", {
counter: this.counter,
piece: piece,
color: this.color,
from: field,
to: toField
}]);
return false;
}
});
},
player: function( args, both, name ){
var event = name || args.callee.nom.slice( 2 ),
player = !both && this[ args[ 0 ].color || args[ 0 ].loser ];
return !both
? player.emit.onIdle( player, [ event, args[ 0 ] ] )
: lib.Deferred.all(
this.white.emit.onIdle( this.white, [ event, args[ 0 ] ] ),
this.black.emit.onIdle( this.black, [ event, args[ 0 ] ] )
);
},
//
// Game events
//
onPlaced: function( _placed_ ){ /* When a piece is placed on the board (setup or pawn promotion) */
return this.player( arguments, true );
},
onMoved: function( _moved_ ){
var color = this.turn( ),
turn = color === "white" ? "black" : "white",
data = lib.mixin( { }, _moved_ );
//
// We do want to make sure that the check event is triggered befire the
// actual turn event. This is becuase a player has to know he is check
// before considering any moves. Using the deferred returned from the
// onIdle will gurantee that.
//
if( !this.board.isCheck( turn ) && this.board.isStaleMate( turn ) ){
return this.player( arguments, true )
.then( this.emit.async( this, [ "StaleMate", { result: "StaleMate" } ] ) );
} else if( !this.board.isCheckMate( turn ) && this.board.isCheck( turn ) ){
return this.player( arguments, true )
.then( this.emit.async( this, [ "Check", data ] ) )
.then( this.emit.async( this, [ "Turn", data ] ) );
} else if( this.board.isCheckMate( turn ) ){
return this.player( arguments, true )
.then(
this.emit.async( this, [ "CheckMate",
{
result: "CheckMate",
winner: color,
loser: turn
}
])
);
} else {
return this.player( arguments, true )
.then( this.emit.async( this, [ "Turn", data ]) );
}
},
onIllegalMove: function( _illegal_ ){ /* When a player makes an illegal move */
return this.player( arguments );
},
onTurn: function( _turn_ ){ // The event that is triggered when te turn is passed to the other player
return this.player( arguments );
},
onPromotion: function( _turn_ ){ // When a promotion has happended on the board
return this.player( arguments );
},
onCheck: function( _turn_ ){ /* Fired when any player is checked */
return this.player( arguments );
},
onStaleMate: function( _result_ ){ /* Fired when there is a stale mate, fired before the onEnd event */
return this.player( arguments, true )
.then( this.emit.async( this, [ "Draw", _result_ ] ) );
},
onCheckMate: function( _result_ ){ /* Fired when eiter player is mated, fired before the onEnd event */
return this.player( arguments )
.then( this.emit.async( this, [ "End", _result_ ] ) );
},
onSurrender: function( _result_ ){ /* Fired when a player surrenders the match, fired before the onEnd event */
return this.player( arguments, true )
.then( this.emit.async( this, [ "End", _result_ ] ) );
},
onStart: function( _start_ ){
this.player( arguments, true )
.then(
this.emit.async( this,
[
"Moved",
{
counter: this.counter++,
color: _start_.color
}
]
)
);
},
onDraw: function( _result_ ){ /* Fired when a game ended in a draw, fired before the onEnd event */
this.player( arguments, true )
.then( this.emit.async( this, [ "End", _result_ ] ) );
},
onEnd: function( _result_ ){ /* Fired when the game has eneded */
if( _result_.result === "Draw" || _result_.result === "StaleMate" ){
return this.player( arguments, true );
} else {
var winner = this[ _result_.winner ],
loser = this[ _result_.loser ];
return (
lib.Deferred.all(
winner.emit.onIdle( winner, [ "Win", _result_ ] ),
loser.emit.onIdle( loser, [ "Lose", _result_ ] )
)
).then( this.player.async( this, [ arguments, true, "End" ] ) );
// For reasons that are still unclear to me it seemt that it is not possible to
// bind an arguments object with additional properties attached to it.
}
},
onPlayerJoin: function( _newPlayer_ ){ /* When a player joins the game */},
onPlayerLeave: function( ){ /* When a player leaves and the the game wasn't started */ },
//
// Actions
//
join: function( player, color ){
// We already have 2 players, or the preferred color is already taken...
if( ( color && this[ color ] ) || ( this.black && this.white ) ){
return null;
}
// Decide what color we are going to use
color || ( color = ~~( Math.random( ) * 10 ) % 2 ? "white" : "black" );
this[ color ] = player;
this.emit.onIdle( this, [ "PlayerJoin", { color: color, player: player } ] );
return color;
},
leave: function( player ){
this[ player.color ] = null;
this.emit.onIdle( this, [ "PlayerLeave", { player: player } ] );
}
});
return chess.Game;
});