search.rb
# Generates all moves for the current side
Generates all moves for the current side
Generates all moves for the current side
def generate_moves(only_captures = false)
    @check = in_check?
    ep, castling = @mv[@on_mv[@ply - 1]].ep, @mv[@on_mv[@ply - 1]].castling
    SQ.select { |sq| @colors[sq] == @mx }.each do |from|
      case @squares[from]
      when P
        to = from + DIR[@mx]
        add_move(from, to + 1, P, true) if @colors[to + 1] == @mn || to + 1 == ep
        add_move(from, to - 1, P, true) if @colors[to - 1] == @mn || to - 1 == ep
        if @colors[to] == EMPTY && !only_captures
          add_move(from, to, P)
          if (from > 80 || from < 40) && @colors[to + DIR[@mx]] == EMPTY
            add_move(from, to + DIR[@mx], P)
          end
        end
      when N
        STEP.each do |n|
          if @colors[from + n] == @mn
            add_move(from, from + n, N, true)
          elsif @colors[from + n] == EMPTY && !only_captures
            add_move(from, from + n, N)
          end
        end
      when B
        DIAG.each do |n|
          to = from + n
          while @colors[to] == EMPTY
            add_move(from, to, B) unless only_captures
            to += n
          end
          add_move(from, to, B, true) if @colors[to] == @mn
        end
      when R
        ORTH.each do |n|
          to = from + n
          while @colors[to] == EMPTY
            add_move(from, to, R) unless only_captures
            to += n
          end
          add_move(from, to, R, true) if @colors[to] == @mn
        end
      when Q
        OCTL.each do |n|
          to = from + n
          while @colors[to] == EMPTY
            add_move(from, to, Q) unless only_captures
            to += n
          end
          add_move(from, to, Q, true) if @colors[to] == @mn
        end
      when K
        OCTL.each do |n|
          if @colors[from + n] == @mn
            add_move(from, from + n, K, true)
          elsif @colors[from + n] == EMPTY && !only_captures
            add_move(from, from + n, K)
            next if @check || n.abs != 1
            if n == -1
              if castling[QSC[@mx]] == 1
                if @colors[from - 2] == EMPTY && @colors[from - 3] == EMPTY
                  add_move(from, from - 2, K) if king_can_move?(from, from - 1)
                end
              end
            elsif castling[KSC[@mx]] == 1
              if @colors[from + 2] == EMPTY
                add_move(from, from + 2, K) if king_can_move?(from, from + 1)
              end
            end
          end
        end
      end
    end
    nil
  end
def generate_moves ( only_captures = false ) @check = in_check? ep , castling = @mv [ @on_mv [ @ply - 1 ] ] . ep , @mv [ @on_mv [ @ply - 1 ] ] . castling SQ . select { | sq | @colors [ sq ] == @mx } . each do | from | case @squares [ from ] when P to = from + DIR [ @mx ] add_move ( from , to + 1 , P , true ) if @colors [ to + 1 ] == @mn || to + 1 == ep add_move ( from , to - 1 , P , true ) if @colors [ to - 1 ] == @mn || to - 1 == ep if @colors [ to ] == EMPTY && ! only_captures add_move ( from , to , P ) if ( from > 80 || from < 40 ) && @colors [ to + DIR [ @mx ] ] == EMPTY add_move ( from , to + DIR [ @mx ] , P ) end end when N STEP . each do | n | if @colors [ from + n ] == @mn add_move ( from , from + n , N , true ) elsif @colors [ from + n ] == EMPTY && ! only_captures add_move ( from , from + n , N ) end end when B DIAG . each do | n | to = from + n while @colors [ to ] == EMPTY add_move ( from , to , B ) unless only_captures to += n end add_move ( from , to , B , true ) if @colors [ to ] == @mn end when R ORTH . each do | n | to = from + n while @colors [ to ] == EMPTY add_move ( from , to , R ) unless only_captures to += n end add_move ( from , to , R , true ) if @colors [ to ] == @mn end when Q OCTL . each do | n | to = from + n while @colors [ to ] == EMPTY add_move ( from , to , Q ) unless only_captures to += n end add_move ( from , to , Q , true ) if @colors [ to ] == @mn end when K OCTL . each do | n | if @colors [ from + n ] == @mn add_move ( from , from + n , K , true ) elsif @colors [ from + n ] == EMPTY && ! only_captures add_move ( from , from + n , K ) next if @check || n . abs != 1 if n == - 1 if castling [ QSC [ @mx ] ] == 1 if @colors [ from - 2 ] == EMPTY && @colors [ from - 3 ] == EMPTY add_move ( from , from - 2 , K ) if king_can_move? ( from , from - 1 ) end end elsif castling [ KSC [ @mx ] ] == 1 if @colors [ from + 2 ] == EMPTY add_move ( from , from + 2 , K ) if king_can_move? ( from , from + 1 ) end end end end end end nil end
Generates all moves for the current side
Generates all moves for the current side
only_captures
<not_specific>