Guest User

Untitled

a guest
Oct 17th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. class King < ChessPiece
  2.  
  3. # King specific methods ...
  4. def valid_move?(x_target, y_target)
  5. return false if same_location?(x_target, y_target)
  6. return false if !in_board?(x_target, y_target)
  7. return true if move_single_step?(x_target, y_target)
  8. return false
  9. end
  10.  
  11. def is_castle_move?(x_target, y_target)
  12. byebug
  13. castling_target_valid?(x_target, y_target) &&
  14. target_unoccupied?(x_target, y_target) &&
  15. rook_in_right_place?(x_target, y_target) &&
  16. rook_target_free?(x_target, y_target)
  17.  
  18.  
  19.  
  20.  
  21. # clear_path_for_castling?
  22. # return false if move_single_step?(x_target, y_target)
  23. # return true if horizontal_move?(x_target, y_target)
  24. end
  25.  
  26. def castle_move
  27. #after king moves, move rook
  28. end
  29.  
  30. def valid_castle_move?
  31. !self.moved?
  32. #which rook?
  33. #has rook moved?
  34. #is there an obstruction?
  35. end
  36.  
  37. def find_rook
  38. #find rook based on direction of move?
  39. end
  40.  
  41. private
  42.  
  43. def target_unoccupied?(x_target, y_target)
  44. !occupied?(x_target, y_target)
  45. end
  46.  
  47. def castling_target_valid?(x_target, y_target)
  48. white_valid_targets = [[0,2], [0,6]]
  49. black_valid_targets = [[7,2], [7,6]]
  50.  
  51. if self.white?
  52. return white_valid_targets.include? [x_target, y_target]
  53. else
  54. return black_valid_targets.include? [x_target, y_target]
  55. end
  56. end
  57.  
  58. def rook_in_right_place?(x_target, y_target)
  59. case [x_target, y_target]
  60. when [0,2]
  61. rook_for_this_target = game.chess_pieces.find_by_x_and_y(0,0)
  62. return rook_for_this_target && rook_for_this_target.type == 'rook'
  63. when [0,6]
  64. rook_for_this_target = game.chess_pieces.find_by_x_and_y(0,7)
  65. return rook_for_this_target && rook_for_this_target.type == 'rook'
  66. when [7,2]
  67. rook_for_this_target = game.chess_pieces.find_by_x_and_y(7,0)
  68. return rook_for_this_target && rook_for_this_target.type == 'rook'
  69. when [7,6]
  70. rook_for_this_target = game.chess_pieces.find_by_x_and_y(7,7)
  71. return rook_for_this_target && rook_for_this_target.type == 'rook'
  72. end
  73. end
  74.  
  75. def rook_target_free?(x_target, y_target)
  76. case [x_target, y_target]
  77. when [0,2]
  78. rook_target = game.chess_pieces.find_by_x_and_y(3,0)
  79. return rook_for_this_target == nil
  80. when [0,6]
  81. rook_target = game.chess_pieces.find_by_x_and_y(5,0)
  82. return rook_for_this_target == nil
  83. when [7,2]
  84. rook_target = game.chess_pieces.find_by_x_and_y(3,7)
  85. return rook_for_this_target == nil
  86. when [7,6]
  87. rook_target = game.chess_pieces.find_by_x_and_y(5,7)
  88. return rook_for_this_target == nil
  89. end
  90. end
  91.  
  92.  
  93.  
  94. end
Add Comment
Please, Sign In to add comment