class TowersOfHanoi::Controller

Public Class Methods

new() click to toggle source
# File lib/towers_of_hanoi/controller.rb, line 3
def initialize
  @view = TowersOfHanoi::View.new
end

Public Instance Methods

play(game) click to toggle source
# File lib/towers_of_hanoi/controller.rb, line 26
def play(game)
  until game.over? do
    game.move(
      from: get_input(@view.ask_origin).to_i,
      to:   get_input(@view.ask_destination).to_i
    )
    puts @view.board_template(game.board)
  end
end
run() click to toggle source
# File lib/towers_of_hanoi/controller.rb, line 7
def run
  puts @view.introduction
  loop do
    if tiles = select_number_of_tiles
      game = TowersOfHanoi::Game.new(bricks: tiles)
      puts @view.board_template(game.board)
      play(game)
      puts @view.victory_message(game)
    else
      puts @view.invalid_input_message
    end
  end
end
select_number_of_tiles() click to toggle source
# File lib/towers_of_hanoi/controller.rb, line 21
def select_number_of_tiles
  number = get_input(@view.ask_for_number_of_tiles).to_i
  (1..10) === number && number
end

Private Instance Methods

get_input(label = nil) click to toggle source
# File lib/towers_of_hanoi/controller.rb, line 39
def get_input(label = nil)
  puts label if label
  print '> '
  input = gets.chomp
  exit if input.start_with?('q') || input == "exit"
  input
end