-
-
Save jpsilvashy/0104d04ba17521c2cd42 to your computer and use it in GitHub Desktop.
towers
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
# Towers of Hanoi | |
# | |
# Write a Towers of Hanoi game: | |
# http://en.wikipedia.org/wiki/Towers_of_hanoi | |
# | |
# In a class `TowersOfHanoi`, keep a `towers` instance variable that is an array | |
# of three arrays. Each subarray should represent a tower. Each tower should | |
# store integers representing the size of its discs. Expose this instance | |
# variable with an `attr_reader`. | |
# | |
# You'll want a `#play` method. In a loop, prompt the user using puts. Ask what | |
# pile to select a disc from. The pile should be the index of a tower in your | |
# `@towers` array. Use gets | |
# (http://andreacfm.com/2011/06/11/learning-ruby-gets-and-chomp/) to get an | |
# answer. Similarly, find out which pile the user wants to move the disc to. | |
# Next, you'll want to do different things depending on whether or not the move | |
# is valid. Finally, if they have succeeded in moving all of the discs to | |
# another pile, they win! The loop should end. | |
# | |
# You'll want a `TowersOfHanoi#render` method. Don't spend too much time on | |
# this, just get it playable. | |
# | |
# Think about what other helper methods you might want. Here's a list of all the | |
# instance methods I had in my TowersOfHanoi class: | |
# * initialize | |
# * play | |
# * render | |
# * won? | |
# * valid_move?(from_tower, to_tower) | |
# * move(from_tower, to_tower) | |
# | |
# Make sure that the game works in the console. There are also some specs to | |
# keep you on the right track: | |
# | |
# ```bash | |
# bundle exec rspec spec/towers_of_hanoi_spec.rb | |
# ``` | |
# | |
# Make sure to run bundle install first! The specs assume you've implemented the | |
# methods named above. | |
class TowersOfHanoi | |
attr_reader :towers | |
def initialize(towers = [[3,2,1],[],[]]) | |
@towers = towers | |
end | |
def play | |
select_from_tower | |
select_to_tower | |
if valid_move? | |
move | |
else | |
puts "Please select another move." | |
return play | |
end | |
if won? | |
puts "congratulations, you won!" | |
else | |
return move | |
end | |
end | |
def select_from_tower | |
puts "Please select a tower to choose a disc from - select either 1, 2 or 3." | |
from_tower = gets.chomp.to_i | |
if from_tower.from_tower_valid? | |
return from_tower | |
else | |
puts "Please select a valid tower." | |
end | |
end | |
def from_tower_valid(from_tower) | |
(@towers[from_tower].length != 0) && ([1,2,3].include?(@towers[from_tower])) | |
end | |
def select_to_tower | |
until [0,1,2].include?(to_tower) | |
puts "Please select a tower to move to - select either 1, 2 or 3." | |
to_tower = gets.chomp | |
end | |
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 | |
elsif @towers[to_tower].length == 0 | |
return true | |
end | |
(@towers[to_tower].last > @towers[from_tower].last) || @towers[to_tower].length == 0 | |
end | |
def won? | |
(@towers[0].length == 0 && @towers[1].length == 0) || (@towers[0].length == 0 && @towers[2].length == 0) | |
end | |
end | |
new_game = TowersOfHanoi.new | |
new_game.play |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment