anchor.diff:                  [_ | _] ->
                       %% is there is check?
                       case is_cell_under_attack(
  -                           Board, KingStart, Enemies) of
  +                           Board, KingStart, Color) of
                           true -> [];
                           _ -> PossibleCastlings
                       end;
  
positive.code:                             _ ->
                                  case is_cell_under_attack(
                                         Board, crd_inc(KingStart, {-1, 0}),
                                         Enemies) of
                                      true -> [];
                                      _ ->
                                          [crd_inc(KingStart, {-2, 0})]
  
negative.code:             end
      end.
  
  knight_steps() ->
      [{1, 2}, {-1, 2}, {1, -2}, {-1, -2},
       {2, 1}, {-2, 1}, {2, -1}, {-2, -1}].
  
  is_cell_under_attack(Board, I, Color) ->
      EnemyColor = not_color(Color),
      lists:any(
        fun(Step) ->
                case cell(Board, crd_inc(I, Step)) of
                    {EnemyColor, ?knight} -> true;
                    _ -> false
                end
        end, knight_steps())
          orelse
          lists:any(
            fun({DC, DR} = Step) ->
                    case find_enemy(
                           Board, I, Step, EnemyColor) of
                        {?pawn, 1}
                          when EnemyColor == ?black
                               andalso abs(DC) == 1
                               andalso DR == 1 ->
                            true;
                        {?pawn, 1}
                          when EnemyColor == ?white
                               andalso abs(DC) == 1
                               andalso DR == -1 ->
                            true;
                        {?king, 1} ->
                            true;
                        {?queen, _} ->
                            true;
                        {?bishop, _}
                          when abs(DC) == abs(DR) ->
                            true;
                        {?rook, _}
                          when abs(DC) /= abs(DR) ->
                            true;
                        _ -> false
                    end
            end,
            [{-1, -1}, {-1, 0}, {-1, 1}, {0, -1},
             {0, 1}, {1, -1}, {1, 0}, {1, 1}]).
  
  find_enemy(Board, I, Step, EnemyColor) ->
      find_enemy(Board, I, Step, EnemyColor, 1).
  find_enemy(Board, I, Step, EnemyColor, Distance) ->
      I2 = crd_inc(I, Step),
      case cell(Board, I2) of
          {EnemyColor, ChessmanType} ->
              {ChessmanType, Distance};
          ?empty ->
              find_enemy(
                Board, I2, Step,
                EnemyColor, Distance + 1);
          _ ->
              %% board end reached or
              %% friendly chessman found
              undefined
      end.
  
  is_king_have_been_moved(History, ?white) ->
      lists:any(fun(Move) -> lists:prefix("e1", Move) end, History);
  