Sha256: a6001e29fb18ba8cef5793421ab45484ffb640c43086ce61a45bc024aca1c43e

Contents?: true

Size: 846 Bytes

Versions: 3

Compression:

Stored size: 846 Bytes

Contents

module ChessEngine
  class Move
    def initialize(board, from, to)
      @board = board
      @from = from
      @to = to
      @original_squares = []
      @original_squares << {coord: from, piece: board.at(from)}
      @original_squares << {coord: to, piece: board.at(to)}
      if en_passant?
        @en_passant_coord = [to[0], from[1]]
        @original_squares << {coord: @en_passant_coord, piece: board.at(@en_passant_coord)}
      end
    end

    def commit
      if en_passant?
        @board.set_at(@en_passant_coord, nil)
      end
      @board.move_piece(@from, @to)
    end

    def rollback
      @original_squares.each do |square|
        @board.set_at(square[:coord], square[:piece])
      end
    end

    private

    def en_passant?
      @board.at(@from).pawn? && @from[0] != @to[0] && @board.at(@to).nil?
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
chess_engine-0.0.8 lib/chess_engine/move.rb
chess_engine-0.0.2 lib/chess_engine/move.rb
chess_engine-0.0.1 lib/chess_engine/move.rb