Class: ChessData::Moves::PieceMove
- Inherits:
-
Object
- Object
- ChessData::Moves::PieceMove
- Defined in:
- lib/chess_data/moves.rb
Overview
Methods to support a piece move.
Instance Method Summary collapse
-
#initialize(move) ⇒ PieceMove
constructor
A new instance of PieceMove.
-
#make_move(board) ⇒ Object
Returns a new instance of the board after move is made.
- #to_s ⇒ Object
Constructor Details
#initialize(move) ⇒ PieceMove
Returns a new instance of PieceMove.
459 460 461 462 463 464 465 466 |
# File 'lib/chess_data/moves.rb', line 459 def initialize move @move_string = move move =~ MatchPieceMove @piece = $1 @identifier = $2 @destination = $3 @is_capture = move.include? "x" end |
Instance Method Details
#make_move(board) ⇒ Object
Returns a new instance of the board after move is made.
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
# File 'lib/chess_data/moves.rb', line 473 def make_move board @piece.downcase! if board.to_move == "b" # for given piece type, locate those pieces on board which can reach destination origin = board.locations_of(@piece, @identifier).select do |loc| Moves.can_reach(board, @piece, loc, @destination) end # filter out ambiguities raised by king being left in check if origin.length > 1 origin = origin.delete_if do |loc| Moves.king_left_in_check(board, @piece, loc, @destination) end end # there should only be one unique piece at this point # raise an InvalidMoveError if not unless origin.length == 1 && board[origin.first] == @piece raise InvalidMoveError, "Not a unique/valid choice for #{@piece} to #{@destination}" end # setup a revised board with the move completed revised_board = board.clone revised_board[origin.first] = nil revised_board[@destination] = @piece revised_board.to_move = case board.to_move when "w" then "b" when "b" then "w" end revised_board.enpassant_target = "-" if @is_capture revised_board.halfmove_clock = 0 else revised_board.halfmove_clock += 1 end revised_board.fullmove_number += 1 if board.to_move == "b" return revised_board end |
#to_s ⇒ Object
468 469 470 |
# File 'lib/chess_data/moves.rb', line 468 def to_s @move_string end |