Created
October 9, 2015 18:21
-
-
Save rachaelsalter/4f0cf10a4bfc9c6da554 to your computer and use it in GitHub Desktop.
valid_move? King
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class King < Piece | |
def self.type | |
where(type: 'king') | |
end | |
def valid_move?(x, y) | |
# King can move one square at a time in any direction. Move not valid if current position - new position | |
# is greater than the absolute value of 1. | |
return false if (self.x_position - x).abs > 1 || (self.y_position - y).abs > 1 | |
# Check for obstructions between current position and new position | |
if diagonal_obstruction_array(x, y) || linear_obstruction_array(x, y) | |
return false | |
else | |
return true | |
end | |
end | |
end |
Also, I was using the Console and trying to create pieces, and I think that this portion won't be necessary:
def self.type
where(type: 'king')
end
The class itself indicates the type. This helps with the knight thx!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Rachael, I think you can just call is_obstructed(x,y) will do the trick.