Class: Move

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

Instance Method Summary collapse

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

#colorObject (readonly)

Returns the value of attribute color.



7
8
9
# File 'lib/bangkok/move.rb', line 7

def color
  @color
end

#from_rank_or_fileObject (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

#modifierObject (readonly)

Returns the value of attribute modifier.



7
8
9
# File 'lib/bangkok/move.rb', line 7

def modifier
  @modifier
end

#pieceObject (readonly)

Returns the value of attribute piece.



7
8
9
# File 'lib/bangkok/move.rb', line 7

def piece
  @piece
end

#squareObject (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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


103
104
105
# File 'lib/bangkok/move.rb', line 103

def blunder?
  has_modifier?('??')
end

#capture?Boolean

Returns true if this is a capture

Returns:

  • (Boolean)


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)

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


68
69
70
# File 'lib/bangkok/move.rb', line 68

def queenside_castle?
  has_modifier?('O-O-O')
end

#to_sObject



47
48
49
# File 'lib/bangkok/move.rb', line 47

def to_s
  return @orig_text
end