Building Blocks

Building Software Better

A Taste of Ruby (Part 7)

Posted by Mark on September 3, 2008

This is a multi-part series. The first in the series is here.

Tower of Hanoi β€” Rules on Rails

In part 6 we changed the algorithm of Tower Of Hanoi to work without requiring the stack — it instead “more naturally” put sufficient state and logic into the model that the model could start and stop and continue as needed. I believe with Ruby 1.8.x, this is required to have Rails be able to work with the Tower of Hanoi. So we should try it out and see how well it works πŸ™‚

require 'hanoi_7'

class HanoiController < ApplicationController
  def say
    @hanoi = session&#91;:hanoi&#93;

    if (@hanoi == nil) then
      @hanoi = createHanoi
      session&#91;:count&#93; = 0
      session&#91;:consoleLog&#93; = ""

    @isDone = @hanoi.doNextMove_IsDone()

    #clear the app because otherwise it will be part of the attempted serialization
    #could also override the marshaling code, but this is simpler

    #clear out the session variable, but @hanoi is still valid through
    #the rest of this interaction
    session&#91;:hanoi&#93; = @isDone ? nil : @hanoi
    @count = session&#91;:count&#93; ||= 0
    @count += 1
    session&#91;:count&#93; = @count

    if (@isDone) then
      render(:action => 'say_done')

  def appendLog(outString)"Append to Log: "+outString)
    @consoleLog = session[:consoleLog] ||= "\n"
    @consoleLog = @consoleLog + outString + "\n"
    session[:consoleLog] = @consoleLog

  def noteChange
  def createHanoi
    hanoi =
    return hanoi

  def getAsciiTowers
    result = ""
    lines =
    @hanoi.stacks.each_with_index do | eachStack, i |
      0.upto(10) do | j |
        eachDisk = eachStack[j]
        if (eachDisk == nil) then
          newLine = " "*12
          newLine = eachDisk.to_s

        newLine << " "*(12 - newLine.length)

        currentLine = lines&#91;j&#93; ||= ""
        lines&#91;j&#93; = currentLine+" "+newLine
    lines.reverse.each {|line| result += line + "\n"}

    return result


&#91;sourcecode language="html"&#93;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="" xml:lang="en" lang="en">
<title>Foo <%= @page_title %></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="refresh" content="1">
<%= javascript_include_tag :defaults %>

<body<% if @page_class %> class="<%= @page_class %>"<% end %>>

<h1>Tower of Hanoi (Move <%= @count %>) </h1>

It's <%= %>.
Done = <%= @isDone %>
Render =
<%= controller.getAsciiTowers %>  
Log =
<%= @consoleLog %>


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: