Class: Bchess::PGN::GameBody

Inherits:
Object
  • Object
show all
Includes:
BoardHelpers
Defined in:
lib/pgn/game_body.rb

Constant Summary collapse

C_COLUMN =
2
G_COLUMN =
6
WHITE_ROW =
0
BLACK_ROW =
7

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BoardHelpers

#castle_detected?, #en_passant_detected?, #field, #invalid_data?, #kings_present?, #pawn_long_move_detected?, #promotion_detected?, #to_column, #to_row

Constructor Details

#initialize(body) ⇒ GameBody

Returns a new instance of GameBody.



14
15
16
17
18
# File 'lib/pgn/game_body.rb', line 14

def initialize(body)
  @body = body
  @board = Bchess::Board.new
  @moves = []
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



4
5
6
# File 'lib/pgn/game_body.rb', line 4

def board
  @board
end

#bodyObject (readonly)

Returns the value of attribute body.



4
5
6
# File 'lib/pgn/game_body.rb', line 4

def body
  @body
end

#movesObject (readonly)

Returns the value of attribute moves.



4
5
6
# File 'lib/pgn/game_body.rb', line 4

def moves
  @moves
end

Instance Method Details

#extract_move(move) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pgn/game_body.rb', line 37

def extract_move(move)
  move_text = move.text_value
  info = return_move_information(move_text)

  piece = get_moving_piece(board, info)
  {
    piece: piece,
    column: to_column(info[:column]),
    row: to_row(info[:row]),
    promoted_piece: info[:promoted_piece]
  }
end

#extract_movesObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pgn/game_body.rb', line 20

def extract_moves
  @board.read_fen

  body.elements&.each do |move|
    if is_castle?(move)
      move_info = extract_castle(move)
    elsif is_move?(move)
      move_info = extract_move(move)
    else
      next
    end

    board.move(*move_info.values)
    @moves << move_info
  end
end