Module: Chess::BoardFromFenCode
- Included in:
- Board
- Defined in:
- lib/chess/board/board_from_fen.rb
Overview
create board from fen_code
Instance Method Summary collapse
-
#black_piece(letter) ⇒ Object
create black color piece from the letter.
-
#create_board(board_data) ⇒ Hash
creates board data.
-
#create_piece(letter) ⇒ Object
create piece from the letter.
-
#fill_board(ranks, board) ⇒ Hash
fills board with rank wise data.
-
#fill_rank(board, rank, ranks_array_index) ⇒ void
fills a rank.
-
#generate_data(fen_code) ⇒ Hash
generate game’s data from FEN code.
-
#insert_piece(board, index, ranks_array_index, letter) ⇒ void
insert a piece on given board.
-
#white_piece(letter) ⇒ Object
create white color piece from the letter.
Instance Method Details
#black_piece(letter) ⇒ Object
create black color piece from the letter
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/chess/board/board_from_fen.rb', line 118 def black_piece(letter) # rubocop:disable Metrics/MethodLength color = 'black' case letter when 'r' Chess::Pieces::Rook.new(color) when 'n' Chess::Pieces::Knight.new(color) when 'b' Chess::Pieces::Bishop.new(color) when 'q' Chess::Pieces::Queen.new(color) when 'k' @black_king = Chess::Pieces::King.new(color) when 'p' Chess::Pieces::Pawn.new(color) end end |
#create_board(board_data) ⇒ Hash
creates board data
25 26 27 28 29 30 31 32 |
# File 'lib/chess/board/board_from_fen.rb', line 25 def create_board(board_data) ranks = board_data.split('/') files = ('a'..'h') # value for 'a' to 'h' board = files.to_h { |file| [file, Array.new(8) { '' }] } # empty board board = fill_board(ranks, board) @data = board end |
#create_piece(letter) ⇒ Object
create piece from the letter
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/chess/board/board_from_fen.rb', line 83 def create_piece(letter) if letter == letter.upcase piece = white_piece(letter) @white_pieces << piece else piece = black_piece(letter) @black_pieces << piece end piece end |
#fill_board(ranks, board) ⇒ Hash
fills board with rank wise data.
38 39 40 41 42 43 44 45 |
# File 'lib/chess/board/board_from_fen.rb', line 38 def fill_board(ranks, board) ranks.reverse! ranks.each_index do |ranks_array_index| rank = ranks[ranks_array_index].chars fill_rank(board, rank, ranks_array_index) end board end |
#fill_rank(board, rank, ranks_array_index) ⇒ void
This method returns an undefined value.
fills a rank
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/chess/board/board_from_fen.rb', line 52 def fill_rank(board, rank, ranks_array_index) index = 0 rank.each do |letter| if letter.to_i.zero? index += 1 insert_piece(board, index, ranks_array_index, letter) else index += letter.to_i end end end |
#generate_data(fen_code) ⇒ Hash
generate game’s data from FEN code.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/chess/board/board_from_fen.rb', line 10 def generate_data(fen_code) fen_parts_array = fen_code.split board_data = fen_parts_array[0] @current_player = fen_parts_array[1] @castling_rights = fen_parts_array[2] @possible_en_passant_target = fen_parts_array[3] @half_move = fen_parts_array[4].to_i @full_move = fen_parts_array[5].to_i create_board(board_data) end |
#insert_piece(board, index, ranks_array_index, letter) ⇒ void
This method returns an undefined value.
insert a piece on given board
70 71 72 73 74 75 76 77 78 |
# File 'lib/chess/board/board_from_fen.rb', line 70 def insert_piece(board, index, ranks_array_index, letter) shift_ord = 96 file = (shift_ord + index).chr rank = ranks_array_index board[file][rank] = create_piece(letter) piece_pos = [file, rank] board[file][rank].pos = piece_pos end |
#white_piece(letter) ⇒ Object
create white color piece from the letter
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/chess/board/board_from_fen.rb', line 97 def white_piece(letter) # rubocop:disable Metrics/MethodLength color = 'white' case letter when 'R' Chess::Pieces::Rook.new(color) when 'N' Chess::Pieces::Knight.new(color) when 'B' Chess::Pieces::Bishop.new(color) when 'Q' Chess::Pieces::Queen.new(color) when 'K' @white_king = Chess::Pieces::King.new(color) when 'P' Chess::Pieces::Pawn.new(color) end end |