Class: ChessEngine::Move
- Inherits:
-
Object
- Object
- ChessEngine::Move
- Defined in:
- lib/chess_engine/move.rb
Overview
This class is made to make move canceling easier if something goes wrong.
Instance Method Summary collapse
-
#commit ⇒ Object
Applies the move to the board.
-
#initialize(board, from, to) ⇒ Move
constructor
A new instance of Move.
-
#rollback ⇒ Object
Moves pieces back and returns the board to the previous state.
Constructor Details
#initialize(board, from, to) ⇒ Move
Returns a new instance of Move.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/chess_engine/move.rb', line 6 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 |
Instance Method Details
#commit ⇒ Object
Applies the move to the board
22 23 24 25 26 27 |
# File 'lib/chess_engine/move.rb', line 22 def commit if en_passant? @board.set_at(@en_passant_coord, nil) end @board.move_piece(@from, @to) end |
#rollback ⇒ Object
Moves pieces back and returns the board to the previous state
32 33 34 35 36 |
# File 'lib/chess_engine/move.rb', line 32 def rollback @original_squares.each do |square| @board.set_at(square[:coord], square[:piece]) end end |