Class: Bchess::PGN::GameBody
- Inherits:
-
Object
- Object
- Bchess::PGN::GameBody
- 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
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#moves ⇒ Object
readonly
Returns the value of attribute moves.
Instance Method Summary collapse
- #extract_move(move) ⇒ Object
- #extract_moves ⇒ Object
-
#initialize(body) ⇒ GameBody
constructor
A new instance of GameBody.
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
#board ⇒ Object (readonly)
Returns the value of attribute board.
4 5 6 |
# File 'lib/pgn/game_body.rb', line 4 def board @board end |
#body ⇒ Object (readonly)
Returns the value of attribute body.
4 5 6 |
# File 'lib/pgn/game_body.rb', line 4 def body @body end |
#moves ⇒ Object (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
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/pgn/game_body.rb', line 39 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_moves ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# 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 piece = move_info.fetch(:piece) column, row = piece.column, piece.row board.move(*move_info.values) @moves << move_info.merge( { piece: piece.class.new(piece.color, column, row) }) end end |