Class: TTT::InputHelper
- Inherits:
-
Object
- Object
- TTT::InputHelper
- Defined in:
- lib/tictactoe/input_helper.rb
Instance Attribute Summary collapse
-
#io ⇒ Object
Returns the value of attribute io.
-
#keep_going ⇒ Object
Returns the value of attribute keep_going.
Instance Method Summary collapse
- #computer_choosing_graphic ⇒ Object
- #get_computer_difficulty_level ⇒ Object
- #get_number_of_rows_cols_max_3 ⇒ Object
- #get_number_of_rows_cols_max_9 ⇒ Object
- #get_player_1_name ⇒ Object
- #get_player_1_value(player_name = "Player 1") ⇒ Object
- #get_player_2_name ⇒ Object
- #get_player_2_type ⇒ Object
- #get_player_2_value(taken_value) ⇒ Object
- #get_square_to_change(player_name, available_choices) ⇒ Object
- #get_user_input(prompt, reprompt, &block_validation) ⇒ Object
-
#initialize(io) ⇒ InputHelper
constructor
A new instance of InputHelper.
- #marching_dots ⇒ Object
- #new_game_starting_graphic ⇒ Object
Constructor Details
#initialize(io) ⇒ InputHelper
Returns a new instance of InputHelper.
5 6 7 |
# File 'lib/tictactoe/input_helper.rb', line 5 def initialize(io) @io = io end |
Instance Attribute Details
#io ⇒ Object
Returns the value of attribute io.
3 4 5 |
# File 'lib/tictactoe/input_helper.rb', line 3 def io @io end |
#keep_going ⇒ Object
Returns the value of attribute keep_going.
3 4 5 |
# File 'lib/tictactoe/input_helper.rb', line 3 def keep_going @keep_going end |
Instance Method Details
#computer_choosing_graphic ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/tictactoe/input_helper.rb', line 95 def computer_choosing_graphic io.present("Computer choosing.") 3.times do sleep(0.3) io.present(".") end io.present("\n") end |
#get_computer_difficulty_level ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/tictactoe/input_helper.rb', line 61 def get_computer_difficulty_level user_choice = get_user_input("Please enter \"E\" if you would like to play an easy Computer. Enter \"D\" if you would like to play an extremely difficult computer.", "Invalid character. Please enter either E (Easy) or D (Difficult).") do |input| input == 'd' || input == 'D' || input == 'e' || input == 'E' end user_choice = user_choice.upcase if user_choice == "D" :difficult elsif user_choice == "E" :easy end end |
#get_number_of_rows_cols_max_3 ⇒ Object
9 10 11 12 13 |
# File 'lib/tictactoe/input_helper.rb', line 9 def get_number_of_rows_cols_max_3 get_user_input("Please choose how many squares you would like in each row.", "Please choose number between 2 and 3.") do |input| input.to_i >=2 && input.to_i <=3 end end |
#get_number_of_rows_cols_max_9 ⇒ Object
15 16 17 18 19 |
# File 'lib/tictactoe/input_helper.rb', line 15 def get_number_of_rows_cols_max_9 get_user_input("Please choose how many squares you would like in each row.", "Please choose number between 2 and 3.") do |input| input.to_i >=2 && input.to_i <=9 end end |
#get_player_1_name ⇒ Object
35 36 37 38 39 40 |
# File 'lib/tictactoe/input_helper.rb', line 35 def get_player_1_name user_choice = get_user_input("Player 1, please enter your name", "Please re-enter your name, using only letters") do |input| input =~ /^[a-zA-Z]+$/ end user_choice.capitalize end |
#get_player_1_value(player_name = "Player 1") ⇒ Object
21 22 23 24 25 26 |
# File 'lib/tictactoe/input_helper.rb', line 21 def get_player_1_value(player_name = "Player 1") user_choice = get_user_input("#{player_name}, please enter a value of either X or O", "Must be X or O. Please re-enter.") do |input| input == 'x' || input == 'X' || input == 'o' || input == 'O' end user_choice.upcase end |
#get_player_2_name ⇒ Object
42 43 44 45 46 47 |
# File 'lib/tictactoe/input_helper.rb', line 42 def get_player_2_name user_choice = get_user_input("Player 2, please enter your name", "Please re-enter your name, using only letters") do |input| input =~ /^[a-zA-Z]+$/ end user_choice.capitalize end |
#get_player_2_type ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/tictactoe/input_helper.rb', line 49 def get_player_2_type user_choice = get_user_input("Please enter \"C\" if you would like to play the Computer. Enter \"H\" if you would like to play the human sitting next to you.", "Invalid character. Please enter either C(Computer) or H(Human).") do |input| input == 'c' || input == 'C' || input == 'h' || input == 'H' end user_choice = user_choice.upcase if user_choice == "C" :computer elsif user_choice == "H" :human end end |
#get_player_2_value(taken_value) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/tictactoe/input_helper.rb', line 28 def get_player_2_value(taken_value) user_choice = get_user_input("Player 2, please enter your value", "Must not be #{taken_value}, please re-enter") do |input| input.upcase != taken_value end user_choice.upcase end |
#get_square_to_change(player_name, available_choices) ⇒ Object
73 74 75 76 77 |
# File 'lib/tictactoe/input_helper.rb', line 73 def get_square_to_change(player_name, available_choices) get_user_input("#{player_name}, please enter the number of the square that you would like to change.", "Invalid entry. Please try again.") do |input| available_choices.include?(input) end end |
#get_user_input(prompt, reprompt, &block_validation) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/tictactoe/input_helper.rb', line 79 def get_user_input(prompt, reprompt, &block_validation) io.present_with_new_line(prompt) user_choice = nil while true user_choice = io.receive if user_choice.to_s.upcase == "EXIT" #http://blog.honeybadger.io/how-to-exit-a-ruby-program/ exit(0) end #breaks out of input loop if input is valid (the block validation makes sure some rule is true) break if block_validation.call(user_choice) puts reprompt end user_choice end |
#marching_dots ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'lib/tictactoe/input_helper.rb', line 116 def marching_dots sleep(0.4) io.present(".") sleep(0.3) io.present(".") sleep(0.3) io.present(".") end |
#new_game_starting_graphic ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/tictactoe/input_helper.rb', line 104 def new_game_starting_graphic io.present("New game starting in 3") i = 2 2.times do marching_dots io.present("#{i.to_s}") i = i - 1 end marching_dots io.present("\n") end |