Procedural Tower of Hanoi

Building your first game using procedural Ruby.

Scroll down...

Content

Resources

Comments

Towers of Hanoi Animation

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.

Getting Started

As before, this unit's assignment will be stored on Github. To get started, follow the same steps as before:

  1. Fork and clone the project repo on Github.
  2. Add your name to the README file, commit the change, and push to your fork.

Your Task

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:

  • Wrap everything in a class and focus on separating different functionality into methods instead of creating one long run-on game method.
  • Create a game loop that finishes when the user either quits (for instance, by entering quit on the input line) or wins.
  • Check for valid user inputs
  • Create a 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 >
# ...

Finishing Up

  1. When you're finished with all tasks, push your changes up to your fork (aka $ git push origin master).
  2. To submit your assignment, create a pull request from your fork to the main upstream repository.

Solution

The solution will be available here on Github once you've submitted your pull request.

Pull request 300 Octocat 300


Sign up to track your progress for free

There are ( ) additional resources for this lesson. Check them out!

There are no additional resources for this lesson just yet!

Sorry, comments aren't active just yet!

Next Lesson: Wrapping up the Unit