Skip to content

Instantly share code, notes, and snippets.

@pdbradley
Created October 16, 2017 19:30
Show Gist options
  • Save pdbradley/8dcb2d23d54d1a626954ce620d8b1bc6 to your computer and use it in GitHub Desktop.
Save pdbradley/8dcb2d23d54d1a626954ce620d8b1bc6 to your computer and use it in GitHub Desktop.
ali king
class King < ChessPiece
# King specific methods ...
def valid_move?(x_target, y_target)
return false if same_location?(x_target, y_target)
return false if !in_board?(x_target, y_target)
return true if move_single_step?(x_target, y_target)
return false
end
def is_castle_move?(x_target, y_target)
byebug
castling_target_valid?(x_target, y_target) &&
target_unoccupied?(x_target, y_target) &&
rook_in_right_place?(x_target, y_target) &&
rook_target_free?(x_target, y_target)
# clear_path_for_castling?
# return false if move_single_step?(x_target, y_target)
# return true if horizontal_move?(x_target, y_target)
end
def castle_move
#after king moves, move rook
end
def valid_castle_move?
!self.moved?
#which rook?
#has rook moved?
#is there an obstruction?
end
def find_rook
#find rook based on direction of move?
end
private
def target_unoccupied?(x_target, y_target)
!occupied?(x_target, y_target)
end
def castling_target_valid?(x_target, y_target)
white_valid_targets = [[0,2], [0,6]]
black_valid_targets = [[7,2], [7,6]]
if self.white?
return white_valid_targets.include? [x_target, y_target]
else
return black_valid_targets.include? [x_target, y_target]
end
end
def rook_in_right_place?(x_target, y_target)
case [x_target, y_target]
when [0,2]
rook_for_this_target = game.chess_pieces.find_by_x_and_y(0,0)
return rook_for_this_target && rook_for_this_target.type == 'rook'
when [0,6]
rook_for_this_target = game.chess_pieces.find_by_x_and_y(0,7)
return rook_for_this_target && rook_for_this_target.type == 'rook'
when [7,2]
rook_for_this_target = game.chess_pieces.find_by_x_and_y(7,0)
return rook_for_this_target && rook_for_this_target.type == 'rook'
when [7,6]
rook_for_this_target = game.chess_pieces.find_by_x_and_y(7,7)
return rook_for_this_target && rook_for_this_target.type == 'rook'
end
end
def rook_target_free?(x_target, y_target)
case [x_target, y_target]
when [0,2]
rook_target = game.chess_pieces.find_by_x_and_y(3,0)
return rook_for_this_target == nil
when [0,6]
rook_target = game.chess_pieces.find_by_x_and_y(5,0)
return rook_for_this_target == nil
when [7,2]
rook_target = game.chess_pieces.find_by_x_and_y(3,7)
return rook_for_this_target == nil
when [7,6]
rook_target = game.chess_pieces.find_by_x_and_y(5,7)
return rook_for_this_target == nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment