Class: ChessValidator::BoardLogic
- Inherits:
-
Object
- Object
- ChessValidator::BoardLogic
- Defined in:
- lib/board_logic.rb
Class Method Summary collapse
- .build_board(fen) ⇒ Object
- .empty_square?(char) ⇒ Boolean
- .find_turn(current_turn) ⇒ Object
- .handle_castle(castling, piece) ⇒ Object
- .handle_en_passant(piece, move) ⇒ Object
- .handle_half_move_clock(board_size, previous_fen, piece) ⇒ Object
- .handle_position(board) ⇒ Object
- .to_fen_notation(board, previous_fen, piece, move) ⇒ Object
Class Method Details
.build_board(fen) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/board_logic.rb', line 5 def self.build_board(fen) board = {} square_index = 1 fen.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
89 90 91 |
# File 'lib/board_logic.rb', line 89 def self.empty_square?(char) ('1'..'8').include?(char) end |
.find_turn(current_turn) ⇒ Object
85 86 87 |
# File 'lib/board_logic.rb', line 85 def self.find_turn(current_turn) current_turn == 'w' ? ' b ' : ' w ' end |
.handle_castle(castling, piece) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/board_logic.rb', line 38 def self.handle_castle(castling, piece) return castling if castling == '-' if ['K', 'Q', 'k', 'q'].include?(piece.piece_type) castling.size == 1 ? '-' : castling.delete(piece.piece_type) elsif piece.piece_type.downcase == 'r' castle_side = piece.position[0] == 'a' ? 'q' : 'k' castle_side = castle_side.capitalize if piece.color == 'w' castling.size == 1 ? '-' : castling.delete(castle_side) else castling end end |
.handle_en_passant(piece, move) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/board_logic.rb', line 51 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(board_size, previous_fen, piece) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/board_logic.rb', line 30 def self.handle_half_move_clock(board_size, previous_fen, piece) if piece.piece_type == 'pawn' || build_board(previous_fen).size > board_size '0 ' else previous_fen.halfmove.next + ' ' end end |
.handle_position(board) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/board_logic.rb', line 61 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) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/board_logic.rb', line 20 def self.to_fen_notation(board, previous_fen, piece, move) notation = handle_position(board) notation += find_turn(previous_fen.active) notation += handle_castle(previous_fen.castling, piece) notation += handle_en_passant(piece, move) notation += handle_half_move_clock(board.size, previous_fen, piece) notation += piece.color == 'b' ? previous_fen.fullmove.next : previous_fen.fullmove notation end |