git.mcksp
    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
module Fork
  extend self

  struct Move
    getter from : Int64
    getter to : Int64
    getter type : Int32
    getter promote_to : Int32
    getter castle : Int32
    getter double_push : Bool
    getter en_passant : Bool

    def initialize(@from : Int64, @to : Int64, @type : Int32, @promote_to : Int32 = 0, @castle = 0, @double_push = false, @en_passant = false)
    end

    def to_s
      "#{Fork.square_to_text(from)}#{Fork.square_to_text(to)}"
    end

    def set_promotion_piece(piece)
      @promote_to = piece
    end
  end

  module Moves
    extend self

    ROW  = 0x00000000000000FF_u64
    ROWS = Array(UInt64).new(8) { |i| ROW << (8 * i) }

    COL  = 0x0101010101010101_u64
    COLS = Array(UInt64).new(8) { |i| COL << i }

    BORDER = ROW[0] | ROW[7] | COL[0] | COL[7]

    WHITE_PAWN_MOVES = Array(UInt64).new(64) { |i| generate_pawn_moves(i, WHITE) }
    BLACK_PAWN_MOVES = Array(UInt64).new(64) { |i| generate_pawn_moves(i, BLACK) }

    WHITE_PAWN_ATTACKS = Array(UInt64).new(64) { |i| generate_pawn_attacks(i, WHITE) }
    BLACK_PAWN_ATTACKS = Array(UInt64).new(64) { |i| generate_pawn_attacks(i, BLACK) }

    EN_PASSANT_PHANTOM = Array(UInt64).new(64) { |i| en_passant_phantom(i) }

    KNIGHT_MOVES = Array(UInt64).new(64) { |i| generate_knight_moves(i) }

    KING_MOVES = Array(UInt64).new(64) { |i| generate_king_moves(i) }

    private def generate_pawn_moves(index, side)
      side == WHITE ? 1u64 << (index + 8) : 1u64 << (index - 8)
    end

    private def generate_pawn_attacks(index, side)
      sign = 1
      sign = -1 if side == BLACK
      row = index // 8
      return 0u64 if row == 7 && side == WHITE
      return 0u64 if row == 0 && side == BLACK
      b = 1_u64 << index
      (((b << (sign * 7)) | (b << (sign * 9))) & ROWS[row + (sign * 1)])
    end

    private def en_passant_phantom(i)
      row = i // 8
      if row == 1
        1u64 << i + 8
      elsif row == 6
        1u64 << i - 8
      else
        0u64
      end
    end

    private def generate_king_moves(index)
      row, col = index.divmod(8)
      bit = (1u64 << index)
      bitboard = 0u64
      bitboard = bitboard | (bit << 9) if col < 7
      bitboard = bitboard | (bit << 1) if col < 7
      bitboard = bitboard | (bit << -7) if col < 7

      bitboard = bitboard | (bit << 7) if col > 0
      bitboard = bitboard | (bit << -1) if col > 0
      bitboard = bitboard | (bit << -9) if col > 0

      bitboard = bitboard | (bit << 8)
      bitboard = bitboard | (bit << -8)

      bitboard
    end

    private def generate_knight_moves(index)
      row, col = index.divmod(8)
      bit = (1u64 << index)
      bitboard = 0u64
      bitboard = bitboard | (bit << 17) if col < 7
      bitboard = bitboard | (bit << 10) if col < 6
      bitboard = bitboard | (bit << 15) if col > 0
      bitboard = bitboard | (bit << 6) if col > 1

      bitboard = bitboard | (bit >> 17) if col > 0
      bitboard = bitboard | (bit >> 10) if col > 1
      bitboard = bitboard | (bit >> 15) if col < 7
      bitboard = bitboard | (bit >> 6) if col < 6

      bitboard
    end

    def generate(board)
      side = board.side
      player = board.player(side)
      enemy = board.player(Fork.enemy(side))
      player_occ = Fork.occupy(player)
      enemy_occ = Fork.occupy(enemy)
      both_occ = player_occ | enemy_occ
      free_occ = ~both_occ
      generated_moves = Array(Move).new(220)

      Fork.iterate_bitboard(player[PAWN]) do |start_index|
        moves = pawn_moves(side, start_index) & free_occ
        attacks = pawn_attacks(side, start_index)
        Fork.iterate_bitboard(moves | (attacks & enemy_occ)) do |end_index|
          row = end_index // 8
          if row == 0 || row == 7
            generated_moves << Move.new(start_index, end_index, PAWN, promote_to: KNIGHT)
            generated_moves << Move.new(start_index, end_index, PAWN, promote_to: BISHOP)
            generated_moves << Move.new(start_index, end_index, PAWN, promote_to: ROOK)
            generated_moves << Move.new(start_index, end_index, PAWN, promote_to: QUEEN)
          else
            generated_moves << Move.new(start_index, end_index, PAWN)
          end
        end
        if moves != 0
          row = start_index // 8
          if side == WHITE && row == 1
            if (1u64 << (start_index + 16)) & free_occ != 0
              generated_moves << Move.new(start_index, start_index + 16, PAWN, double_push: true)
            end
          elsif side == BLACK && row == 6
            if (1u64 << (start_index - 16)) & free_occ != 0
              generated_moves << Move.new(start_index, start_index - 16, PAWN, double_push: true)
            end
          end
        end
        if attacks & board.en_passant != 0
          generated_moves << Move.new(start_index, Fork.lsb(board.en_passant), PAWN, en_passant: true)
        end
      end

      Fork.iterate_bitboard(player[BISHOP]) do |start_index|
        moves = bishop_moves(start_index, player_occ, enemy_occ)
        Fork.iterate_bitboard(moves) do |end_index|
          generated_moves << Move.new(start_index, end_index, BISHOP)
        end
      end

      Fork.iterate_bitboard(player[KNIGHT]) do |start_index|
        moves = KNIGHT_MOVES[start_index] & (~player_occ)
        Fork.iterate_bitboard(moves) do |end_index|
          generated_moves << Move.new(start_index, end_index, KNIGHT)
        end
      end

      Fork.iterate_bitboard(player[ROOK]) do |start_index|
        moves = rook_moves(start_index, player_occ, enemy_occ)
        Fork.iterate_bitboard(moves) do |end_index|
          generated_moves << Move.new(start_index, end_index, ROOK)
        end
      end

      Fork.iterate_bitboard(player[QUEEN]) do |start_index|
        moves = rook_moves(start_index, player_occ, enemy_occ) | bishop_moves(start_index, player_occ, enemy_occ)
        Fork.iterate_bitboard(moves) do |end_index|
          generated_moves << Move.new(start_index, end_index, QUEEN)
        end
      end

      Fork.iterate_bitboard(player[KING]) do |start_index|
        moves = KING_MOVES[start_index] & (~player_occ)
        Fork.iterate_bitboard(moves) do |end_index|
          generated_moves << Move.new(start_index, end_index, KING)
        end
      end

      if side == WHITE
        if (board.castle & WHITE_KING_CASTLE != 0) &&
           !board.attacked?(E1, WHITE) && !board.attacked?(F1, WHITE) && !board.attacked?(G1, WHITE) &&
           (both_occ & (F1_BIT | G1_BIT)) == 0
          generated_moves << Move.new(E1, G1, KING, castle: WHITE_KING_CASTLE)
        end
        if (board.castle & WHITE_QUEEN_CASTLE != 0) &&
           !board.attacked?(C1, WHITE) && !board.attacked?(D1, WHITE) && !board.attacked?(E1, WHITE) &&
           (both_occ & (B1_BIT | C1_BIT | D1_BIT)) == 0
          generated_moves << Move.new(E1, C1, KING, castle: WHITE_QUEEN_CASTLE)
        end
      else
        if (board.castle & BLACK_KING_CASTLE != 0) &&
           !board.attacked?(E8, BLACK) && !board.attacked?(F8, BLACK) && !board.attacked?(G8, BLACK) &&
           (both_occ & (F8_BIT | G8_BIT)) == 0
          generated_moves << Move.new(E8, G8, KING, castle: BLACK_KING_CASTLE)
        end
        if (board.castle & BLACK_QUEEN_CASTLE != 0) &&
           !board.attacked?(C8, BLACK) && !board.attacked?(D8, BLACK) && !board.attacked?(E8, BLACK) &&
           (both_occ & (B8_BIT | C8_BIT | D8_BIT)) == 0
          generated_moves << Move.new(E8, C8, KING, castle: BLACK_QUEEN_CASTLE)
        end
      end

      generated_moves
    end

    def pawn_moves(side, i)
      side == WHITE ? WHITE_PAWN_MOVES[i] : BLACK_PAWN_MOVES[i]
    end

    def pawn_attacks(side, i)
      side == WHITE ? WHITE_PAWN_ATTACKS[i] : BLACK_PAWN_ATTACKS[i]
    end

    def bishop_moves(index, friends, enemies)
      row, col = index.divmod(8)
      bit = (1u64 << index)
      bitboard = 0u64

      1.upto(7) do |i|
        break if row + i > 7
        break if col + i > 7
        b = (bit << (i * 9))
        break if b & friends != 0
        bitboard |= b
        break if b & enemies != 0
      end

      1.upto(7) do |i|
        break if row + i > 7
        break if col - i < 0
        b = (bit << (i * 7))
        break if b & friends != 0
        bitboard |= b
        break if b & enemies != 0
      end

      1.upto(7) do |i|
        break if row - i < 0
        break if col + i > 7
        b = (bit << (i * (-7)))
        break if b & friends != 0
        bitboard |= b
        break if b & enemies != 0
      end

      1.upto(7) do |i|
        break if row - i < 0
        break if col - i < 0
        b = (bit << (i * (-9)))
        break if b & friends != 0
        bitboard |= b
        break if b & enemies != 0
      end

      bitboard
    end

    def rook_moves(index, friends, enemies)
      row, col = index.divmod(8)
      bitboard = 0u64

      (row + 1).upto(7) do |r|
        b = (1u64 << (r * 8 + col))
        break if b & friends != 0
        bitboard |= b
        break if b & enemies != 0
      end

      (row - 1).downto(0) do |r|
        b = (1u64 << (r * 8 + col))
        break if b & friends != 0
        bitboard |= b
        break if b & enemies != 0
      end

      (col - 1).downto(0) do |c|
        b = (1u64 << (row * 8 + c))
        break if b & friends != 0
        bitboard |= b
        break if b & enemies != 0
      end

      (col + 1).upto(7) do |c|
        b = (1u64 << (row * 8 + c))
        break if b & friends != 0
        bitboard |= b
        break if b & enemies != 0
      end

      bitboard
    end
  end
end