Skip to content

Instantly share code, notes, and snippets.

@rachaelsalter
Created October 9, 2015 18:21
Show Gist options
  • Save rachaelsalter/4f0cf10a4bfc9c6da554 to your computer and use it in GitHub Desktop.
Save rachaelsalter/4f0cf10a4bfc9c6da554 to your computer and use it in GitHub Desktop.
valid_move? King
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
@lantrinhca
Copy link

Hi Rachael, I think you can just call is_obstructed(x,y) will do the trick.

@mattheweves
Copy link

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