Class: Move
- Inherits:
-
Object
- Object
- Move
- Defined in:
- lib/bangkok/move.rb
Overview
A Move is a single piece’s move in a chess match. There are two Move objects created for each chess match move: one for white and one for black.
Instance Attribute Summary collapse
-
#color ⇒ Object
readonly
Returns the value of attribute color.
-
#from_rank_or_file ⇒ Object
readonly
Returns the value of attribute from_rank_or_file.
-
#modifier ⇒ Object
readonly
Returns the value of attribute modifier.
-
#piece ⇒ Object
readonly
Returns the value of attribute piece.
-
#square ⇒ Object
readonly
Returns the value of attribute square.
Instance Method Summary collapse
-
#bad_move? ⇒ Boolean
Returns true if this move was a bad one.
-
#blunder? ⇒ Boolean
Returns true if this move was a blunder.
-
#capture? ⇒ Boolean
Returns true if this is a capture.
-
#castle? ⇒ Boolean
Returns true if this is a castle (either side).
-
#check? ⇒ Boolean
Returns true if this move results in a check.
-
#checkmate? ⇒ Boolean
Returns true if this move results in a checkmate.
-
#good_move? ⇒ Boolean
Returns true if this move was a good one.
-
#has_modifier?(str) ⇒ Boolean
Returns true if @modifier is not null and includes
str
. -
#initialize(color, text) ⇒ Move
constructor
Parse the chess piece move
text
and set piece, square, and modifier. -
#kingside_castle? ⇒ Boolean
Returns true if this is a kingside castle.
-
#pawn_promotion? ⇒ Boolean
Returns true if this move results in a pawn promotion.
-
#queenside_castle? ⇒ Boolean
Returns true if this is a queenside castle.
- #to_s ⇒ Object
Constructor Details
#initialize(color, text) ⇒ Move
Parse the chess piece move text
and set piece, square, and modifier.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/bangkok/move.rb', line 10 def initialize(color, text) @color = color # :white or :black @orig_text = text # Note: I don't have to worry about "e.p." en passant notation; the data # files do not use that. case text when 'O-O-O', 'O-O', '0-0-0', '0-0' @modifier = text.gsub(/0/, 'O') # zeroes to capital o's @piece = 'K' @square = Square::OFF_BOARD # Not really, of course; square is never used when /^([KQRBNa-h1-8])x([a-h][1-8])(.*)$/ piece_or_from_rank_or_file, file_and_rank, @modifier = $1, $2, $3 @square = Square.new(file_and_rank) case piece_or_from_rank_or_file when /[a-h1-8]/ # first char is file or rank; it's a pawn @piece = 'P' @from_rank_or_file = Square.new(piece_or_from_rank_or_file) else # first char is piece name @piece = piece_or_from_rank_or_file end @modifier ||= '' @modifier << 'x' when /^([KQRBN]?)([a-h1-8]?)([a-h][1-8])(.*)$/ @piece, from_rank_or_file, file_and_rank, @modifier = $1, $2, $3, $4 @square = Square.new(file_and_rank) unless from_rank_or_file.empty? @from_rank_or_file = Square.new(from_rank_or_file) end else raise "I can't understand the move \"#{@orig_text}\"" end @piece = 'P' if @piece.empty? @piece = @piece.intern end |
Instance Attribute Details
#color ⇒ Object (readonly)
Returns the value of attribute color.
7 8 9 |
# File 'lib/bangkok/move.rb', line 7 def color @color end |
#from_rank_or_file ⇒ Object (readonly)
Returns the value of attribute from_rank_or_file.
7 8 9 |
# File 'lib/bangkok/move.rb', line 7 def from_rank_or_file @from_rank_or_file end |
#modifier ⇒ Object (readonly)
Returns the value of attribute modifier.
7 8 9 |
# File 'lib/bangkok/move.rb', line 7 def modifier @modifier end |
#piece ⇒ Object (readonly)
Returns the value of attribute piece.
7 8 9 |
# File 'lib/bangkok/move.rb', line 7 def piece @piece end |
#square ⇒ Object (readonly)
Returns the value of attribute square.
7 8 9 |
# File 'lib/bangkok/move.rb', line 7 def square @square end |
Instance Method Details
#bad_move? ⇒ Boolean
Returns true if this move was a bad one
98 99 100 |
# File 'lib/bangkok/move.rb', line 98 def bad_move? has_modifier?('?') && !blunder? end |
#blunder? ⇒ Boolean
Returns true if this move was a blunder
103 104 105 |
# File 'lib/bangkok/move.rb', line 103 def blunder? has_modifier?('??') end |
#capture? ⇒ Boolean
Returns true if this is a capture
58 59 60 |
# File 'lib/bangkok/move.rb', line 58 def capture? has_modifier?('x') end |
#castle? ⇒ Boolean
Returns true if this is a castle (either side)
63 64 65 |
# File 'lib/bangkok/move.rb', line 63 def castle? has_modifier?('O-O') # Also true if O-O-O end |
#check? ⇒ Boolean
Returns true if this move results in a check
83 84 85 |
# File 'lib/bangkok/move.rb', line 83 def check? has_modifier?('+') end |
#checkmate? ⇒ Boolean
Returns true if this move results in a checkmate
88 89 90 |
# File 'lib/bangkok/move.rb', line 88 def checkmate? has_modifier?('#') end |
#good_move? ⇒ Boolean
Returns true if this move was a good one
93 94 95 |
# File 'lib/bangkok/move.rb', line 93 def good_move? has_modifier?('!') end |
#has_modifier?(str) ⇒ Boolean
Returns true if @modifier is not null and includes str
.
52 53 54 55 |
# File 'lib/bangkok/move.rb', line 52 def has_modifier?(str) return false unless @modifier return @modifier.include?(str) end |
#kingside_castle? ⇒ Boolean
Returns true if this is a kingside castle
73 74 75 |
# File 'lib/bangkok/move.rb', line 73 def kingside_castle? has_modifier?('O-O') && !queenside_castle? end |
#pawn_promotion? ⇒ Boolean
Returns true if this move results in a pawn promotion
78 79 80 |
# File 'lib/bangkok/move.rb', line 78 def pawn_promotion? has_modifier?('Q') end |
#queenside_castle? ⇒ Boolean
Returns true if this is a queenside castle
68 69 70 |
# File 'lib/bangkok/move.rb', line 68 def queenside_castle? has_modifier?('O-O-O') end |
#to_s ⇒ Object
47 48 49 |
# File 'lib/bangkok/move.rb', line 47 def to_s return @orig_text end |