class TowersApp
Attributes
app_difficulty[RW]
destination_tower[RW]
player_name[RW]
source_tower[RW]
Public Class Methods
new(target_object)
click to toggle source
# File lib/TowersApp_Class.rb, line 4 def initialize(target_object) @target = target_object @messages = [] puts "\nWelcome to the Tower of Hanoi game!" sleep 1.5 puts "This is a game based on the Tower of Hanoi problem." sleep 1.5 puts "Type 'render' after selecting a difficulty to display the towers." sleep 1.5 puts "Type 'quit' anytime to leave the game." sleep 1.5 puts "Ready? Please type in your name!"\ "\nNo spaces, numbers, or special characters!" end
Public Instance Methods
choice_prompt_check(option)
click to toggle source
Checking for choice or render.
# File lib/TowersApp_Class.rb, line 74 def choice_prompt_check (option) while true prompt_text(option) tower = gets.chomp.downcase.to_sym !!(/render/i.match(tower.to_s)) ? self.render : break end tower end
destination_tower_choice()
click to toggle source
Picking a destination tower.
# File lib/TowersApp_Class.rb, line 104 def destination_tower_choice tower_choice_prompt(:destination) end
difficulty_prompt()
click to toggle source
# File lib/TowersApp_Class.rb, line 32 def difficulty_prompt puts "\nHello, #{@player_name}. Select a difficulty level!"\ "\n[EASY || MEDIUM || HARD]" begin @app_difficulty = gets.chomp.downcase quit?(@app_difficulty.to_s) raise StandardError if !(/easy|medium|hard/i.match(@app_difficulty)) @app_difficulty = @app_difficulty.to_sym rescue puts "\nERROR: did not correctly enter the difficulty."\ " Please try again." retry end self.set_difficulty(@app_difficulty) end
method_missing(method_name, *args, &block)
click to toggle source
# File lib/TowersApp_Class.rb, line 108 def method_missing(method_name, *args, &block) @messages << method_name @target.send(method_name, *args, &block) end
player_name_prompt()
click to toggle source
# File lib/TowersApp_Class.rb, line 19 def player_name_prompt begin @player_name = gets.chomp.downcase.capitalize quit?(@player_name) raise StandardError if !!(/\s+|\W|\d/.match(@player_name)) rescue puts "\nERROR: did not correctly enter the player's name."\ " Please try again." retry end self.set_player_name(@player_name) end
prompt_text(option)
click to toggle source
Text output for prompts.
# File lib/TowersApp_Class.rb, line 61 def prompt_text (option) puts "\nWhose disc will you remove?" if option == :source puts "\nWhere would you like to place the disc?" if option == :destination puts "[FIRST || SECOND || THIRD]" end
set_choice(choice,option)
click to toggle source
Store choice in instance variables
# File lib/TowersApp_Class.rb, line 68 def set_choice (choice,option) @source_tower = choice if option == :source @destination_tower = choice if option == :destination end
source_tower_choice()
click to toggle source
Picking a source tower.
# File lib/TowersApp_Class.rb, line 99 def source_tower_choice tower_choice_prompt(:source) end
tower_choice_error(tower_choice, option = {})
click to toggle source
Check for whether a valid choice of tower was made.
# File lib/TowersApp_Class.rb, line 50 def tower_choice_error(tower_choice, option = {}) topmost_choice = self.towers[tower_choice][-1] topmost_source = self.towers[@source_tower][-1] raise StandardError if\ !(/first|second|third/i.match(tower_choice))\ ||(option == :source && self.towers[tower_choice].empty?)\ ||((option == :destination && topmost_choice < topmost_source) \ unless self.towers[tower_choice].empty?) end
tower_choice_prompt(option)
click to toggle source
Prompt for tower choice.
# File lib/TowersApp_Class.rb, line 84 def tower_choice_prompt (option) begin tower = choice_prompt_check(option) quit?(tower.to_s) set_choice(tower,option) self.tower_choice_error(tower,option) rescue puts "ERROR: did not correctly enter a tower choice."\ " Please try again." retry end tower end