Skip to content

Instantly share code, notes, and snippets.

@jpkalbacher
Created August 5, 2015 01:26
Show Gist options
  • Save jpkalbacher/9e8cf997e8f312ce34f6 to your computer and use it in GitHub Desktop.
Save jpkalbacher/9e8cf997e8f312ce34f6 to your computer and use it in GitHub Desktop.
towers_of_hanoi
class TowersOfHanoi
attr_reader :towers
def initialize(towers = [[3,2,1],[],[]])
@towers = towers
end
def move(from_tower, to_tower)
@towers[to_tower] << @towers[from_tower].pop
end
def valid_move?(from_tower, to_tower)
if @towers[from_tower].length == 0
return false
end
(@towers[to_tower].last < @towers[from_tower].last) || @towers[to_tower].length == 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment