class TowersOfHanoi::View

Constants

BLANK
CORNER
SIDE
TILE
TOP

Public Instance Methods

ask_destination() click to toggle source
# File lib/towers_of_hanoi/view.rb, line 34
def ask_destination
  "Move to:"
end
ask_for_number_of_tiles() click to toggle source
# File lib/towers_of_hanoi/view.rb, line 18
def ask_for_number_of_tiles
  "How many tiles would you like in your tower?"
end
ask_origin() click to toggle source
# File lib/towers_of_hanoi/view.rb, line 30
def ask_origin
  "Move from:"
end
board_template(board) click to toggle source
# File lib/towers_of_hanoi/view.rb, line 42
def board_template(board)
  number_of_bricks = board.bricks

  CORNER + horizontal_border(number_of_bricks) + CORNER + "\n" +

  number_of_bricks.downto(1).map do |row|
    SIDE + brick_row(row, board) + SIDE + "\n"
  end.join +

  SIDE + column_labels(number_of_bricks) + SIDE + "\n" +
  CORNER + horizontal_border(number_of_bricks) + CORNER + "\n"
end
introduction() click to toggle source
# File lib/towers_of_hanoi/view.rb, line 9
def introduction
  "Welcome to Towers of Hanoi!\n\n" +
  "The object of this simple game is to move all of the tiles\n" +
  "from the first tower (1) to the last tower (3).\n\n" +
  "You must move only one tile at a time, from one tower to another,\n" +
  "without placing a larger tile on top of a smaller tile.\n\n" +
  "Enter 'q' to exit at any time.\n\n"
end
invalid_input_message() click to toggle source
# File lib/towers_of_hanoi/view.rb, line 26
def invalid_input_message
  "Please enter a numeric value between 1 and 10.\n\n"
end
too_many_tiles_message() click to toggle source
# File lib/towers_of_hanoi/view.rb, line 22
def too_many_tiles_message
  "Playing with more than 10 tiles is not recommended due to requiring over 1000 moves to complete.\n\n"
end
victory_message(game) click to toggle source
# File lib/towers_of_hanoi/view.rb, line 38
def victory_message(game)
  "Completed in #{game.turns} moves! (minimum: #{game.minimum_turns})"
end

Private Instance Methods

brick(width, number_of_bricks) click to toggle source
# File lib/towers_of_hanoi/view.rb, line 76
def brick(width, number_of_bricks)
  brick_buffer(width, number_of_bricks) +
  (width > 0 ? TILE * (2 * width - 1) : BLANK) +
  brick_buffer(width, number_of_bricks)
end
brick_buffer(brick_width, number_of_bricks) click to toggle source
# File lib/towers_of_hanoi/view.rb, line 82
def brick_buffer(brick_width, number_of_bricks)
  if brick_width > 0
    BLANK * (number_of_bricks - brick_width)
  else
    BLANK * (number_of_bricks - 1)
  end
end
brick_row(row, board) click to toggle source
# File lib/towers_of_hanoi/view.rb, line 70
def brick_row(row, board)
  (1..3).to_a.map do |column|
    brick(board.tower(column).brick(row).width, board.bricks)
  end.join(BLANK)
end
column_labels(padding) click to toggle source
# File lib/towers_of_hanoi/view.rb, line 62
def column_labels(padding)
  (1..3).map do |column|
    BLANK * (padding - 1) +
    column.to_s +
    BLANK * (padding - 1)
  end.join(BLANK)
end
horizontal_border(number_of_bricks) click to toggle source
# File lib/towers_of_hanoi/view.rb, line 58
def horizontal_border(number_of_bricks)
  TOP * (number_of_bricks * 6 - 1)
end