Class: ChessValidator::BoardLogic
- Inherits:
-
Object
- Object
- ChessValidator::BoardLogic
- Defined in:
- lib/board_logic.rb
Class Method Summary collapse
- .build_board(fen) ⇒ Object
- .build_board_from_string(board_string) ⇒ Object
- .empty_square?(char) ⇒ Boolean
- .find_turn(current_turn) ⇒ Object
- .handle_castle(castling, piece, board) ⇒ Object
- .handle_en_passant(piece, move) ⇒ Object
- .handle_half_move_clock(previous_fen, piece_type, captured) ⇒ Object
- .handle_position(board) ⇒ Object
- .to_fen_notation(board, previous_fen, piece, move, captured) ⇒ Object
Class Method Details
.build_board(fen) ⇒ Object
5 6 7 |
# File 'lib/board_logic.rb', line 5 def self.build_board(fen) build_board_from_string(fen.board_string) end |
.build_board_from_string(board_string) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/board_logic.rb', line 9 def self.build_board_from_string(board_string) board = {} square_index = 1 board_string.chars.each do |char| if empty_square?(char) square_index += char.to_i elsif char != '/' board[square_index] = Piece.new(char, square_index) square_index += 1 end end board end |
.empty_square?(char) ⇒ Boolean
91 92 93 |
# File 'lib/board_logic.rb', line 91 def self.empty_square?(char) ('1'..'8').include?(char) end |
.find_turn(current_turn) ⇒ Object
87 88 89 |
# File 'lib/board_logic.rb', line 87 def self.find_turn(current_turn) current_turn == 'w' ? ' b ' : ' w ' end |
.handle_castle(castling, piece, board) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/board_logic.rb', line 42 def self.handle_castle(castling, piece, board) return castling if castling == '-' castling.delete!('K') if board[64].nil? || board[64].piece_type != 'R' castling.delete!('KQ') if board[61].nil? || board[61].piece_type != 'K' castling.delete!('Q') if board[57].nil? || board[57].piece_type != 'R' castling.delete!('k') if board[8].nil? || board[8].piece_type != 'r' castling.delete!('kq') if board[5].nil? || board[5].piece_type != 'k' castling.delete!('q') if board[1].nil? || board[1].piece_type != 'r' castling.size == 0 ? '-' : castling end |
.handle_en_passant(piece, move) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/board_logic.rb', line 53 def self.handle_en_passant(piece, move) en_passant = ' - ' if (piece.piece_type.downcase == 'p' && (piece.position[1].to_i - move[1].to_i).abs > 1) column = piece.color == 'w' ? '3' : '6' ' ' + piece.position[0] + column + ' ' else ' - ' end end |
.handle_half_move_clock(previous_fen, piece_type, captured) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/board_logic.rb', line 34 def self.handle_half_move_clock(previous_fen, piece_type, captured) if piece_type.downcase == 'p' || captured '0 ' else previous_fen.halfmove.next + ' ' end end |
.handle_position(board) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/board_logic.rb', line 63 def self.handle_position(board) notation = '' square_gap = 0 64.times do |n| if n > 0 && n % 8 == 0 notation += square_gap.to_s if square_gap > 0 notation += '/' square_gap = 0 end piece = board[n + 1] if piece notation += square_gap.to_s if square_gap > 0 notation += piece.piece_type square_gap = 0 elsif n < 63 square_gap += 1 else notation += (square_gap + 1).to_s end end notation end |
.to_fen_notation(board, previous_fen, piece, move, captured) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/board_logic.rb', line 24 def self.to_fen_notation(board, previous_fen, piece, move, captured) notation = handle_position(board) notation += find_turn(previous_fen.active) notation += handle_castle(previous_fen.castling, piece, board) notation += handle_en_passant(piece, move) notation += handle_half_move_clock(previous_fen, piece.piece_type, captured) notation += piece.color == 'b' ? previous_fen.fullmove&.next.to_s : previous_fen.fullmove.to_s notation end |