These free mini-courses will give you a strong foundation in web development. Track your progress and access advanced courses on HTML/CSS, Ruby and JavaScript for free inside our student portal.
Scroll down...
Tower of Hanoi is a game that requires players to move a stack of disks from one space (or rod) to another. For this assignment, you'll need to build a simple Tower of Hanoi game in Ruby. Because we haven't covered classes yet (officially), it'll be a "procedural" version -- essentially just a big script. You can (and should) break it up into methods and wrap it all in a class (for code organization), but essentially it'll run from top-to-bottom.
The goal is to get you flexing your Ruby muscles on a more substantial problem and gain an appreciation for where classes might come in handy later.
As before, this unit's assignment will be stored on Github. To get started, follow the same steps as before:
Build a Ruby program that allows a player to play ToH from the command line, specifying the initial height of the tower.
This will give you a chance to create an interactive command-line game. You'll want to:
quit
on the input line) or wins.render
method which prints out the current state of the game board in between turns. START SIMPLE! The render method usually gives people the most frustration. Start by just printing the game state in numeric form before you try to get creative with your output.Before you begin, whiteboard/pseudocode your solution! Start high level and add detail until you're ready to code it up.
> load 'towers.rb'
#=> true
> t = TowerOfHanoi.new(3)
#=> #<TowerOfHanoi:0x007f8ea03c93e0 @towers=3>
> t.play
# Welcome to Tower of Hanoi!
# Instructions:
# Enter where you'd like to move from and to
# in the format '1,3'. Enter 'q' to quit.
# Current Board:
# o
# oo
# ooo
# 1 2 3
# Enter move >
# ...
$ git push origin master
).The solution will be available here on Github once you've submitted your pull request.