Last active
October 1, 2018 04:09
-
-
Save bchase/23124daead533449568a0697c7a12cf5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env ruby | |
class Pawn | |
def move_down?(x,y) | |
can_move_distance?(x,y) | |
end | |
def move_up?(x,y) | |
can_move_distance?(x,y) | |
end | |
private | |
def can_move_distance?(x,y) | |
# actually implemented in reality, | |
# but `true` here to contrast `false` below | |
true | |
end | |
end | |
class WhitePawn < Pawn | |
def move_down?(x,y) | |
false | |
end | |
end | |
class BlackPawn < Pawn | |
def move_up?(x,y) | |
false | |
end | |
end | |
white = WhitePawn.new | |
black = BlackPawn.new | |
puts "WHITE DOWN -- #{ white.move_down?(5,5) }" | |
puts "WHITE UP -- #{ white.move_up?(5,5) }" | |
puts "BLACK DOWN -- #{ black.move_down?(5,5) }" | |
puts "BLACK UP -- #{ black.move_up?(5,5) }" | |
# $ ruby chess-pawn.rb | |
# WHITE DOWN -- false | |
# WHITE UP -- true | |
# BLACK DOWN -- true | |
# BLACK UP -- false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment