Sha256: 14d1fdc5cf799d5afade89ebcf337139efd06b5fd242691c627a4cde740f7b1f

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

module TowersOfHanoi
  class Controller
    def initialize
      @view = TowersOfHanoi::View.new
    end

    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

    def select_number_of_tiles
      number = get_input(@view.ask_for_number_of_tiles).to_i
      (1..10) === number && number
    end

    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


    private

    def get_input(label = nil)
      puts label if label
      print '> '
      input = gets.chomp
      exit if input.start_with?('q') || input == "exit"
      input
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
towers_of_hanoi-0.2.0 lib/towers_of_hanoi/controller.rb