# puzzle_nodes / 13 / lib / board.rb

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 # Chess Validator - Board # ======================= # # Part of my solution to PuzzleNode #13. # Replication of a chess board. # a b c d e f g h # 8 bR bN bB bQ bK bB bN bR 8 # 7 bP bP bP bP bP bP bP bP 7 # 6 -- -- -- -- -- -- -- -- 6 # 5 -- -- -- -- -- -- -- -- 5 # 4 -- -- -- -- -- -- -- -- 4 # 3 -- -- -- -- -- -- -- -- 3 # 2 wP wP wP wP wP wP wP wP 2 # 1 wR wN wB wQ wK wB wN wR 1 # a b c d e f g h class Board BOARD_LETTERS ={ 'a' => 0, 'b' => 1, 'c' => 2, 'd' => 3, 'e' => 4, 'f' => 5, 'g' => 6, 'h' => 7 } def initialize input_string @board_matrix = input_string.lines.map &:split end def [] index @board_matrix[index] end def algebraic? symbol symbol.length == 2 and %w[a b c d e f g h].include? symbol[0] and (1..8).include? symbol[1].to_i end # a move is valid when: # 1. [x] There is a piece at the first square # 2. [ ] The last square is a valid destination for the piece. # 3. [ ] There is a clear path to the destination. # 4. [ ] The destination is empty or has a capturable piece. def valid_move? move return false if empty? move.first true end def empty? place send(place) == '--' end def index_from_algebraic symbol [8 - symbol[1].to_i, BOARD_LETTERS[symbol[0]]] end def method_missing symbol, *args, &block if algebraic? symbol row, col = index_from_algebraic symbol @board_matrix[row][col] else super symbol, *args, &block end end end