@@ -391,18 +391,10 @@ var ChessGame = {
391
391
var start_time = Date . now ( ) ;
392
392
var possibleMove = this . state . findmove ( this . level ) ;
393
393
var delta = Date . now ( ) - start_time ;
394
- // game over
395
- if ( possibleMove . length === 0 ) {
396
- this . game_won = true ;
397
- return ;
398
- }
399
- if ( 1 / possibleMove [ 2 ] == 1 / this . state . stalemate_scores [ this . state . to_play ] ) {
400
- this . game_draw = true ;
401
- return ;
402
- }
403
394
var move = this . state . move ( possibleMove [ 0 ] , possibleMove [ 1 ] ) ;
404
395
405
396
if ( ! ( move . flags & ( 1 << 0 ) ) ) {
397
+ // this is where the king can't move or any valid move aren't available, usually happens on stalemate and checkmate
406
398
var depth = this . level ;
407
399
depth ++ ;
408
400
//find at higher depths until it runs out of time
@@ -414,28 +406,26 @@ var ChessGame = {
414
406
delta = Date . now ( ) - start_time ;
415
407
}
416
408
}
417
- if ( possibleMove . length === 0 ) {
418
- this . game_won = true ;
419
- return ;
420
- }
421
- if ( possibleMove [ 2 ] == this . state . stalemate_scores [ this . state . to_play ] ) {
422
- this . game_draw = true ;
423
- return ;
424
- }
425
- move = this . state . move ( possibleMove [ 0 ] , possibleMove [ 1 ] ) ;
426
- if ( ! ( move . flags & ( 1 << 0 ) ) ) {
409
+ if ( possibleMove . length === 0 && flags & 2 ) {
427
410
this . game_won = true ;
428
411
return ;
429
412
}
413
+ move = this . state . move ( possibleMove [ 0 ] , possibleMove [ 1 ] ) ;
430
414
}
431
415
432
- if ( move . flags & ( 1 << 1 ) && move . flags & ( 1 << 2 ) ) {
433
- this . game_lost = true ;
434
416
435
- } else if ( move . flags & ( 1 << 1 ) && ! ( move . flags & ( 1 << 2 ) ) ) {
417
+ if ( ( move . flags & 1 << 0 ) && ( move . flags & 1 << 1 ) && ( move . flags & 1 << 2 ) ) {
418
+ this . board . position ( p4_state2fen ( this . state , true ) ) ;
419
+ this . updateMoves ( ) ;
420
+ this . game_lost = true ;
421
+ return ;
422
+ } else if ( move . flags & ( 1 << 1 ) && ! ( move . flags & ( 1 << 2 ) ) ) {
436
423
this . game_check = true ;
437
- } else if ( ( ! ( move . flags & ( 1 << 1 ) ) && move . flags & ( 1 << 2 ) ) || move . flags & ( 1 << 6 ) ) {
424
+ } else if ( ! ( move . flags & 1 << 1 ) && ( ( ( move . flags & 1 << 0 ) && move . flags & 1 << 2 ) || move . flags & ( 1 << 6 ) ) ) {
425
+ this . board . position ( p4_state2fen ( this . state , true ) ) ;
426
+ this . updateMoves ( ) ;
438
427
this . game_draw = true ;
428
+ return ;
439
429
} else {
440
430
this . game_check = false ;
441
431
}
@@ -461,10 +451,12 @@ var ChessGame = {
461
451
// illegal move
462
452
if ( move . flags === 0 ) return 'snapback' ;
463
453
464
- if ( move . flags === 7 ) {
454
+ if ( move . flags === 7 || move . flags === 15 || move . flags === 23 || move . flags === 39 ) {
465
455
this . game_won = true ;
466
- } else if ( ( ! ( move . flags & ( 1 << 1 ) ) && move . flags & ( 1 << 2 ) ) || move . flags & ( 1 << 6 ) ) {
456
+ return ;
457
+ } else if ( ( ! ( move . flags & 1 << 1 ) ) && ( ( ( move . flags & 1 << 0 ) && ( move . flags & 1 << 2 ) ) || move . flags & ( 1 << 6 ) ) ) {
467
458
this . game_draw = true ;
459
+ return ;
468
460
} else {
469
461
this . game_check = false ;
470
462
}
0 commit comments