Class: ChessRB::Move

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#boardObject (readonly)

Returns the value of attribute board.



4
5
6
# File 'lib/chess_rb/move.rb', line 4

def board
  @board
end

#fromObject (readonly)

Returns the value of attribute from.



4
5
6
# File 'lib/chess_rb/move.rb', line 4

def from
  @from
end

#promotionObject (readonly)

Returns the value of attribute promotion.



4
5
6
# File 'lib/chess_rb/move.rb', line 4

def promotion
  @promotion
end

#sanObject (readonly)

Returns the value of attribute san.



4
5
6
# File 'lib/chess_rb/move.rb', line 4

def san
  @san
end

#squareObject (readonly)

Returns the value of attribute square.



4
5
6
# File 'lib/chess_rb/move.rb', line 4

def square
  @square
end

#toObject (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.

Returns:

  • (Boolean)


52
53
54
# File 'lib/chess_rb/move.rb', line 52

def capture?
  return valid? && @board.piece_on(@to).desc != 'E'
end

#from_fileObject

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_rankObject

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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_fileObject

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_rankObject

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.

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/chess_rb/move.rb', line 34

def valid?
  return @valid if !@valid.nil?
  return true
end