Class: ChessLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/chess_logger.rb

Instance Method Summary collapse

Constructor Details

#initialize(orig, dest, board, en_passant: false, promotion_type: nil, history: nil) ⇒ ChessLogger

Returns a new instance of ChessLogger.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/helpers/chess_logger.rb', line 7

def initialize(orig, dest, board, en_passant: false, promotion_type: nil, history: nil)
  @promotion_type = promotion_type
  @history = history

  return unless orig && dest && board

  @orig = orig
  @dest = dest
  @board = board
  @en_passant = en_passant
  @orig_y, @orig_x = @orig
  @dest_y, @dest_x = @dest
  @piece = @board[@orig_y][@orig_x]
  @piece_color, @piece_type = @piece.chars
end

Instance Method Details

#log_moveObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/helpers/chess_logger.rb', line 23

def log_move
  if @piece_type == 'K' && (@orig_x - @dest_x).abs > 1
    return (@dest_x - @orig_x).positive? ? '0-0' : '0-0-0'
  end

  origin = encode_origin
  capture = @board[@dest_y][@dest_x] || @en_passant ? 'x' : ''
  destination = NotationParser.encode_notation(@dest)

  origin + capture + destination + check_or_mate
end

#log_promotionObject



35
36
37
# File 'lib/helpers/chess_logger.rb', line 35

def log_promotion
  @promotion_type ? "=(#{@promotion_type})" : ''
end