Class: TowersApp
- Inherits:
-
Object
- Object
- TowersApp
- Defined in:
- lib/TowersApp_Class.rb
Instance Attribute Summary collapse
-
#app_difficulty ⇒ Object
Returns the value of attribute app_difficulty.
-
#destination_tower ⇒ Object
Returns the value of attribute destination_tower.
-
#player_name ⇒ Object
Returns the value of attribute player_name.
-
#source_tower ⇒ Object
Returns the value of attribute source_tower.
Instance Method Summary collapse
-
#choice_prompt_check(option) ⇒ Object
Checking for choice or render.
-
#destination_tower_choice ⇒ Object
Picking a destination tower.
- #difficulty_prompt ⇒ Object
-
#initialize(target_object) ⇒ TowersApp
constructor
A new instance of TowersApp.
- #method_missing(method_name, *args, &block) ⇒ Object
- #player_name_prompt ⇒ Object
-
#prompt_text(option) ⇒ Object
Text output for prompts.
-
#set_choice(choice, option) ⇒ Object
Store choice in instance variables.
-
#source_tower_choice ⇒ Object
Picking a source tower.
-
#tower_choice_error(tower_choice, option = {}) ⇒ Object
Check for whether a valid choice of tower was made.
-
#tower_choice_prompt(option) ⇒ Object
Prompt for tower choice.
Constructor Details
#initialize(target_object) ⇒ TowersApp
Returns a new instance of TowersApp.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/TowersApp_Class.rb', line 4 def initialize(target_object) @target = target_object = [] 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
108 109 110 111 |
# File 'lib/TowersApp_Class.rb', line 108 def method_missing(method_name, *args, &block) << method_name @target.send(method_name, *args, &block) end |
Instance Attribute Details
#app_difficulty ⇒ Object
Returns the value of attribute app_difficulty.
2 3 4 |
# File 'lib/TowersApp_Class.rb', line 2 def app_difficulty @app_difficulty end |
#destination_tower ⇒ Object
Returns the value of attribute destination_tower.
2 3 4 |
# File 'lib/TowersApp_Class.rb', line 2 def destination_tower @destination_tower end |
#player_name ⇒ Object
Returns the value of attribute player_name.
2 3 4 |
# File 'lib/TowersApp_Class.rb', line 2 def player_name @player_name end |
#source_tower ⇒ Object
Returns the value of attribute source_tower.
2 3 4 |
# File 'lib/TowersApp_Class.rb', line 2 def source_tower @source_tower end |
Instance Method Details
#choice_prompt_check(option) ⇒ Object
Checking for choice or render.
74 75 76 77 78 79 80 81 |
# 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 ⇒ Object
Picking a destination tower.
104 105 106 |
# File 'lib/TowersApp_Class.rb', line 104 def destination_tower_choice tower_choice_prompt(:destination) end |
#difficulty_prompt ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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 |
#player_name_prompt ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# 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) ⇒ Object
Text output for prompts.
61 62 63 64 65 |
# 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) ⇒ Object
Store choice in instance variables
68 69 70 71 |
# 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 ⇒ Object
Picking a source tower.
99 100 101 |
# File 'lib/TowersApp_Class.rb', line 99 def source_tower_choice tower_choice_prompt(:source) end |
#tower_choice_error(tower_choice, option = {}) ⇒ Object
Check for whether a valid choice of tower was made.
50 51 52 53 54 55 56 57 58 |
# 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) ⇒ Object
Prompt for tower choice.
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# 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 |