@@ -343,6 +343,26 @@ function get_allowed_moves(piece: [Piece, Color], from: Square, ignore_check: bo
343343 case Piece . King :
344344 allowed_movements . push ( [ 1 , 0 ] , [ 0 , 1 ] , [ - 1 , 0 ] , [ 0 , - 1 ] ) ;
345345 allowed_movements . push ( [ 1 , 1 ] , [ - 1 , - 1 ] , [ 1 , - 1 ] , [ - 1 , 1 ] ) ;
346+
347+ switch ( piece [ 1 ] ) {
348+ case Color . White :
349+ if ( ! moved_pieces [ 7 ] [ 7 ] ) {
350+ movement_dirs [ MoveDirection . Right ] . push ( [ 2 , 0 ] ) ;
351+ }
352+ if ( ! moved_pieces [ 7 ] [ 0 ] ) {
353+ movement_dirs [ MoveDirection . Left ] . push ( [ - 2 , 0 ] ) ;
354+ }
355+ break ;
356+
357+ case Color . Black :
358+ if ( ! moved_pieces [ 0 ] [ 7 ] ) {
359+ movement_dirs [ MoveDirection . Right ] . push ( [ 2 , 0 ] ) ;
360+ }
361+ if ( ! moved_pieces [ 0 ] [ 0 ] ) {
362+ movement_dirs [ MoveDirection . Left ] . push ( [ - 2 , 0 ] ) ;
363+ }
364+ break ;
365+ }
346366 break ;
347367 }
348368
@@ -573,8 +593,35 @@ board.addEventListener('click', function (event) {
573593 indicators = [ ] ;
574594 } else if ( squareToMove !== null && ! squares_equal ( tile , empty_location ) && ( ( ! has_moved_piece && move_piece_and_tile ) || ! move_piece_and_tile ) && is_move_allowed ( get_square ( squareToMove ) ! , squareToMove , square ) ) {
575595 const piece = get_square ( squareToMove ) ;
596+
576597 set_square ( square , piece ) ;
577598 set_square ( squareToMove , null ) ;
599+
600+ // alert(`from ${squareToMove} to ${square}`);
601+
602+ if ( piece ! [ 0 ] === Piece . King ) {
603+ switch ( turn ) {
604+ case Color . White :
605+ if ( squares_equal ( squareToMove , [ 4 , 7 ] ) && squares_equal ( square , [ 6 , 7 ] ) ) {
606+ set_square ( [ squareToMove [ 0 ] + 1 , squareToMove [ 1 ] ] , [ Piece . Rook , Color . White ] ) ;
607+ set_square ( [ 7 , 7 ] , null ) ;
608+ } else if ( squares_equal ( squareToMove , [ 4 , 7 ] ) && squares_equal ( square , [ 0 , 7 ] ) ) {
609+ set_square ( [ squareToMove [ 0 ] - 1 , squareToMove [ 1 ] ] , [ Piece . Rook , Color . White ] ) ;
610+ set_square ( [ 0 , 7 ] , null ) ;
611+ }
612+ break ;
613+
614+ case Color . Black :
615+ if ( squares_equal ( squareToMove , [ 4 , 0 ] ) && squares_equal ( square , [ 6 , 0 ] ) ) {
616+ set_square ( [ squareToMove [ 0 ] + 1 , squareToMove [ 1 ] ] , [ Piece . Rook , Color . Black ] ) ;
617+ set_square ( [ 7 , 0 ] , null ) ;
618+ } else if ( squares_equal ( squareToMove , [ 4 , 0 ] ) && squares_equal ( square , [ 0 , 0 ] ) ) {
619+ set_square ( [ squareToMove [ 0 ] - 1 , squareToMove [ 1 ] ] , [ Piece . Rook , Color . Black ] ) ;
620+ set_square ( [ 0 , 0 ] , null ) ;
621+ }
622+ break ;
623+ }
624+ }
578625 squareToMove = null ;
579626 highlight = null ;
580627 //flip_board();
0 commit comments