Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. def in_check?(color)
  2. king = find_king(color)
  3. opponents = opponents_pieces(color)
  4. @enemies_causing_check = []
  5. opponents.each do |piece|
  6. @enemies_causing_check << piece if piece.valid_move?(king.x_position, king.y_position) == true
  7. end
  8. return true if @enemies_causing_check.any?
  9. false
  10. end
  11.  
  12. def i_can_move_out_of_check?(color)
  13. king = find_king(color)
  14. x_start = king.x_position
  15. y_start = king.y_position
  16. state = false
  17. ((king.x_position - 1)..(king.x_position + 1)).each do |x|
  18. ((king.y_position - 1)..(king.y_position + 1)).each do |y|
  19. king.update(x_position: x, y_position: y) if king.valid_move?(x, y)
  20. state = true unless in_check?(color)
  21. king.update(x_position: x_start, y_position: y_start)
  22. end
  23. end
  24. state
  25. end
  26.  
  27. def capture_opponent_causing_check?(color)
  28. friendlies = friendly_pieces(color)
  29. the_liberator = []
  30. friendlies.each do |friend|
  31. @enemies_causing_check.each do |enemy|
  32. the_liberator << friend if friend.valid_move?(enemy.x_position, enemy.y_position) == true
  33. end
  34. end
  35. return true if the_liberator.any?
  36. false
  37. end
  38.  
  39. def stalemate?(color)
  40. your_pieces = friends_on_board(color)
  41. available_moves = []
  42. your_pieces.each do |piece|
  43. 1.upto(8) do |x|
  44. 1.upto(8) do |y|
  45. if piece.valid_move?(x, y) && !piece.move_causes_check?(x, y)
  46. available_moves << [x, y]
  47. end
  48. end
  49. end
  50. end
  51. return false if available_moves.any?
  52. true
  53. end
  54.  
  55. def friendly_pieces(color)
  56. friendly_pieces = if color == 'BLACK'
  57. 'BLACK'
  58. else
  59. 'WHITE'
  60. end
  61. pieces.where(color: friendly_pieces).to_a
  62. end
  63.  
  64. def opponents_pieces(color)
  65. opposing_color = if color == 'BLACK'
  66. 'WHITE'
  67. else
  68. 'BLACK'
  69. end
  70. pieces.where(color: opposing_color).to_a
  71. end
  72.  
  73. def move_to!(x, y)
  74. if occupied_by_mycolor_piece?(x, y)
  75. false
  76. elsif valid_move?(x, y)
  77. if occupied_by_opposing_piece?(x, y)
  78. capture_piece_at!(x, y)
  79. update_attributes(x_position: x, y_position: y)
  80. elsif unoccupied?(x, y)
  81. update_attributes(x_position: x, y_position: y)
  82. end
  83. else
  84. false
  85. end
  86. end
  87.  
  88. def move_causes_check?(next_x, next_y)
  89. state = false
  90. ActiveRecord::Base.transaction do
  91. move!(next_x, next_y)
  92. state = game.check?(color)
  93. raise ActiveRecord::Rollback
  94. end
  95. reload
  96. state
  97. end
  98.  
  99. def available_moves
  100. Game.all_board_coordinates.select do |coordinate_pair|
  101. valid_move?(coordinate_pair[0], coordinate_pair[1]) &&
  102. !is_obstructed?(coordinate_pair[0], coordinate_pair[1]) &&
  103. !occupied_by_mycolor_piece?(coordinate_pair[0], coordinate_pair[1])
  104. end
  105. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement