Module: ChessData::Moves
- Defined in:
- lib/chess_data/moves.rb
Overview
Moves is a collection of regular expressions and methods to recognise how moves are written in PGN files, and to make the moves on a given board.
As the moves are obtained from PGN files, they are assumed to be correct.
Defined Under Namespace
Classes: KingsideCastles, PawnCapture, PieceMove, PromotionPawnCapture, PromotionPawnMove, QueensideCastles, SimplePawnMove
Constant Summary collapse
- Square =
:nodoc: Regular expressions to match each of the move types.
/[a-h][1-8]/
- Piece =
/[KQRBN]/
- MatchKingsideCastles =
/^O-O\+?\Z/
- MatchQueensideCastles =
/^O-O-O\+?\Z/
- MatchPieceMove =
/^(#{Piece})([a-h]?|[1-8]?)x?(#{Square})\+?\Z/
- MatchPawnCapture =
/^([a-h])x(#{Square})\+?\Z/
- MatchPromotionPawnMove =
/^([a-h][18])=([QqRrBbNn])\+?\Z/
- MatchSimplePawnMove =
/^(#{Square})\+?\Z/
- MatchPromotionPawnCapture =
/^([a-h])x([a-h][18])=([QqRrBbNn])\+?\Z/
- LegalMove =
Combined regular expression, to match a legal move.
/#{MatchKingsideCastles}|#{MatchQueensideCastles}|#{MatchPieceMove}|#{MatchPawnCapture}|#{MatchPromotionPawnMove}|#{MatchSimplePawnMove}|#{MatchPromotionPawnCapture}/
Class Method Summary collapse
-
.can_reach(board, piece, start, finish) ⇒ Object
Return true if given piece can move from start square to finish on given board.
-
.new_move(string) ⇒ Object
Returns an instance of the appropriate move type.
Class Method Details
.can_reach(board, piece, start, finish) ⇒ Object
Return true if given piece can move from start square to finish on given board.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/chess_data/moves.rb', line 48 def Moves.can_reach board, piece, start, finish start = start.upcase finish = finish.upcase case piece when "K", "k" then Moves.king_can_reach start, finish when "Q", "q" then Moves.queen_can_reach board, start, finish when "R", "r" then Moves.rook_can_reach board, start, finish when "B", "b" then Moves.bishop_can_reach board, start,finish when "N", "n" then Moves.knight_can_reach start, finish end end |
.new_move(string) ⇒ Object
Returns an instance of the appropriate move type.
- string
-
a move read from a PGN file.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/chess_data/moves.rb', line 34 def Moves.new_move string case string when MatchKingsideCastles then KingsideCastles.new when MatchQueensideCastles then QueensideCastles.new when MatchPieceMove then PieceMove.new string when MatchPawnCapture then PawnCapture.new string when MatchPromotionPawnMove then PromotionPawnMove.new string when MatchSimplePawnMove then SimplePawnMove.new string when MatchPromotionPawnCapture then PromotionPawnCapture.new string else raise InvalidMoveError.new("Invalid move: #{string}") end end |