Class: Pawn
Class Method Summary collapse
- .dest_occupied?(dest, board) ⇒ Boolean
- .en_passant(orig, dest, board, en_passant) ⇒ Object
- .move_is_valid?(orig, dest, board, en_passant) ⇒ Boolean
Methods inherited from Piece
capturable?, destination_occupied?, obstructed?
Class Method Details
.dest_occupied?(dest, board) ⇒ Boolean
54 55 56 57 58 |
# File 'lib/pieces/pawn.rb', line 54 def self.dest_occupied?(dest, board) dest_y = dest[0] dest_x = dest[1] !board[dest_y][dest_x].nil? end |
.en_passant(orig, dest, board, en_passant) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/pieces/pawn.rb', line 36 def self.en_passant(orig, dest, board, en_passant) orig_y, orig_x = orig dest_y, dest_x = dest piece_type = board[orig_y][orig_x] opposite_color = piece_type[0].downcase == 'w' ? :black : :white direction = opposite_color == :white ? -1 : 1 return false if en_passant[opposite_color].nil? if en_passant[opposite_color] == [dest[0] + direction, dest[1]] if (orig_x - dest_x).abs == 1 && (orig_y - dest_y) * direction == 1 return true end end false end |
.move_is_valid?(orig, dest, board, en_passant) ⇒ Boolean
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 |
# File 'lib/pieces/pawn.rb', line 6 def self.move_is_valid?(orig, dest, board, en_passant) return true if en_passant(orig, dest, board, en_passant) orig_y, orig_x = orig dest_y, dest_x = dest piece_type = board[orig_y][orig_x] piece_color = piece_type[0].downcase if piece_color == 'w' direction = 1 elsif piece_color == 'b' direction = -1 else return false end if capturable?(orig, dest, board) && (orig_x - dest_x).abs == 1 && (orig_y - dest_y) * direction == 1 return true end not_obstructed = !obstructed?(orig, dest, board) && !dest_occupied?(dest, board) basic_move = ((orig_y - dest_y) * direction == 1 && orig_x == dest_x) move_double_on_first_turn = (orig_y - dest_y == (2 * direction)) && (orig_x == dest_x) not_obstructed && (move_double_on_first_turn || basic_move) end |