Advertisement
Guest User

Untitled

a guest
May 24th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. class Piece < ActiveRecord::Base
  2. belongs_to :game
  3. self.inheritance_column = :piece_type
  4.  
  5. def self.piece_types
  6. %w(Pawn Rook Knight Bishop Queen King)
  7. end
  8.  
  9. scope :pawns, -> { where(piece_type: 'Pawn') }
  10. scope :rooks, -> { where(piece_type: 'Rook') }
  11. scope :knights, -> { where(piece_type: 'Knight') }
  12. scope :bishops, -> { where(piece_type: 'Bishop') }
  13. scope :queens, -> { where(piece_type: 'Queen') }
  14. scope :kings, -> { where(piece_type: 'King') }
  15.  
  16. def valid_move?
  17. # Implement this method in each subclass.
  18. # Keep this method here for the parent class, in case things go awry.
  19. raise 'Abstract method'
  20. end
  21.  
  22. # Note: the find_all_pieces helper relies on the current game existing in the
  23. # instance variable @game. If it ends up being somewhere else, please change
  24. # that method's second line accordingly.
  25. def obstructed?(destination_x, destination_y)
  26. # Sanity-check the prospective move.
  27. return true if !linear_move?(destination_x, destination_y)
  28.  
  29. # Set delta_x and delta_y to -1, 0, or 1, such that adding them to
  30. # x_coord and y_coord will move one square toward the destination.
  31. delta_x = destination_x <=> x_coord
  32. delta_y = destination_y <=> y_coord
  33.  
  34. # Move toward the destination one square at a time, stopping and returning
  35. # true if an obstructing piece is present. Return false upon successfully
  36. # reaching the destination.
  37.  
  38. piece_locations = find_all_pieces
  39.  
  40. iterative_x = x_coord
  41. iterative_y = y_coord
  42.  
  43. loop do
  44. iterative_x += delta_x
  45. iterative_y += delta_y
  46.  
  47. return false if iterative_x == destination_x && iterative_y == destination_y
  48.  
  49. return true if piece_locations.include? [iterative_x, iterative_y]
  50. end
  51. end
  52.  
  53. def linear_move?(destination_x, destination_y)
  54.  
  55. # Determine the magnitude of movement along each axis.
  56. x_movement = (destination_x - x_coord).abs
  57. y_movement = (destination_y - y_coord).abs
  58.  
  59. return false if x_movement == 0 && y_movement == 0
  60.  
  61. # If a move is along a rank or file, x_movement or y_movement will be zero.
  62. # If a move is along a diagonal, x_movement and y_movement will be equal.
  63. return false unless x_movement == 0 || y_movement == 0 || x_movement == y_movement
  64.  
  65. true
  66. end
  67.  
  68. def square_on_board?(destination_x, destination_y)
  69.  
  70. return false unless (1..8).cover?(x_coord) && (1..8).cover?(y_coord)
  71.  
  72. return false unless (1..8).cover?(destination_x) && (1..8).cover?(destination_y)
  73.  
  74. true
  75. end
  76.  
  77. def find_all_pieces
  78. locations = []
  79. game.pieces.each do |piece| #REVIEW
  80. locations << [piece.x_coord, piece.y_coord]
  81. end
  82. locations
  83. end
  84.  
  85.  
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement