Class: ChessRB::Move
- Inherits:
-
Object
- Object
- ChessRB::Move
- Defined in:
- lib/chess_rb/move.rb
Constant Summary collapse
- FILE_CONV =
[nil, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#promotion ⇒ Object
readonly
Returns the value of attribute promotion.
-
#san ⇒ Object
readonly
Returns the value of attribute san.
-
#square ⇒ Object
readonly
Returns the value of attribute square.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Class Method Summary collapse
-
.file(square) ⇒ Object
Returns the file of a given square as a number (e.g., a = 1, b = 2, …).
-
.rank(square) ⇒ Object
Returns the rank of a given square.
Instance Method Summary collapse
-
#capture? ⇒ Boolean
Returns true if move results in a capture, false otherwise.
-
#from_file ⇒ Object
Returns file of this move’s @from square.
-
#from_rank ⇒ Object
Returns rank of this move’s @from square.
-
#initialize(move, pos, promotion = nil) ⇒ Move
constructor
A new instance of Move.
-
#king_castle? ⇒ Boolean
Returns true if move represents a king-side castle, false otherwise.
-
#queen_castle? ⇒ Boolean
Returns true if move represents a queen-side castle, false otherwise.
-
#to_file ⇒ Object
Returns file of this move’s @to square.
-
#to_rank ⇒ Object
Returns rank of this move’s @to square.
-
#valid? ⇒ Boolean
TODO Returns true if move is a legal move in the given position, false otherwise.
Constructor Details
#initialize(move, pos, promotion = nil) ⇒ Move
Returns a new instance of Move.
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 |
# File 'lib/chess_rb/move.rb', line 6 def initialize(move, pos, promotion = nil) if move.split('-').length == 2 @square = move else @san = move end if pos.is_a?(String) @board = ChessRB::Position.new pos else @board = pos end @promotion = promotion @square = ChessRB::Notation.algebraic_to_square(self) if @square.nil? squares = @square.split('-') @from = squares[0] @to = squares[1] @san = ChessRB::Notation.square_to_algebraic(self) if @san.nil? @valid = valid? end |
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
4 5 6 |
# File 'lib/chess_rb/move.rb', line 4 def board @board end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
4 5 6 |
# File 'lib/chess_rb/move.rb', line 4 def from @from end |
#promotion ⇒ Object (readonly)
Returns the value of attribute promotion.
4 5 6 |
# File 'lib/chess_rb/move.rb', line 4 def promotion @promotion end |
#san ⇒ Object (readonly)
Returns the value of attribute san.
4 5 6 |
# File 'lib/chess_rb/move.rb', line 4 def san @san end |
#square ⇒ Object (readonly)
Returns the value of attribute square.
4 5 6 |
# File 'lib/chess_rb/move.rb', line 4 def square @square end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
4 5 6 |
# File 'lib/chess_rb/move.rb', line 4 def to @to end |
Class Method Details
.file(square) ⇒ Object
Returns the file of a given square as a number (e.g., a = 1, b = 2, …)
72 73 74 |
# File 'lib/chess_rb/move.rb', line 72 def self.file(square) return FILE_CONV.index(square[0]) end |
.rank(square) ⇒ Object
Returns the rank of a given square
57 58 59 |
# File 'lib/chess_rb/move.rb', line 57 def self.rank(square) return square[1].to_i end |
Instance Method Details
#capture? ⇒ Boolean
Returns true if move results in a capture, false otherwise.
52 53 54 |
# File 'lib/chess_rb/move.rb', line 52 def capture? return valid? && @board.piece_on(@to).desc != 'E' end |
#from_file ⇒ Object
Returns file of this move’s @from square
82 83 84 |
# File 'lib/chess_rb/move.rb', line 82 def from_file return self.class.file(@from) end |
#from_rank ⇒ Object
Returns rank of this move’s @from square
67 68 69 |
# File 'lib/chess_rb/move.rb', line 67 def from_rank return self.class.rank(@from) end |
#king_castle? ⇒ Boolean
Returns true if move represents a king-side castle, false otherwise.
46 47 48 49 |
# File 'lib/chess_rb/move.rb', line 46 def king_castle? return valid? && @board.piece_on(@from).type == 'K' && to_file - from_file == 2 end |
#queen_castle? ⇒ Boolean
Returns true if move represents a queen-side castle, false otherwise.
40 41 42 43 |
# File 'lib/chess_rb/move.rb', line 40 def queen_castle? return valid? && @board.piece_on(@from).type == 'K' && from_file - to_file == 2 end |
#to_file ⇒ Object
Returns file of this move’s @to square
77 78 79 |
# File 'lib/chess_rb/move.rb', line 77 def to_file return self.class.file(@to) end |
#to_rank ⇒ Object
Returns rank of this move’s @to square
62 63 64 |
# File 'lib/chess_rb/move.rb', line 62 def to_rank return self.class.rank(@to) end |
#valid? ⇒ Boolean
TODO Returns true if move is a legal move in the given position, false otherwise.
34 35 36 37 |
# File 'lib/chess_rb/move.rb', line 34 def valid? return @valid if !@valid.nil? return true end |