Class: Shared::InputHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/games/shared/input_helper.rb

Direct Known Subclasses

MM::InputHelper, TTT::InputHelper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ InputHelper



5
6
7
# File 'lib/games/shared/input_helper.rb', line 5

def initialize(io)
  @io = io
end

Instance Attribute Details

#ioObject

Returns the value of attribute io.



3
4
5
# File 'lib/games/shared/input_helper.rb', line 3

def io
  @io
end

Instance Method Details

#computer_choosing_graphicObject



44
45
46
47
48
49
50
51
# File 'lib/games/shared/input_helper.rb', line 44

def computer_choosing_graphic
  io.present("Computer choosing.")
  3.times do
    sleep(0.3)
    io.present(".")
  end
  io.present("\n")
end

#custom_final_message(game) ⇒ Object



74
75
76
# File 'lib/games/shared/input_helper.rb', line 74

def custom_final_message(game)

end

#get_gameObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/games/shared/input_helper.rb', line 9

def get_game
  user_choice = get_user_input("Please enter \"M\" if you would like to play Mastermind. Enter \"T\" if you would like to play Tic Tac Toe.", "Invalid character. Please enter either M (Mastermind) or T (Tic Tac Toe).") do |input|
    input == 'm' || input == 'M' || input == 't' || input == 'T'
  end
  user_choice = user_choice.upcase
  if user_choice == "M"
    :mastermind
  elsif user_choice == "T"
    :tictactoe
  end
end

#get_player_1_nameObject



21
22
23
24
25
26
# File 'lib/games/shared/input_helper.rb', line 21

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_user_input(prompt, reprompt, &block_validation) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/games/shared/input_helper.rb', line 28

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_dotsObject



65
66
67
68
69
70
71
72
# File 'lib/games/shared/input_helper.rb', line 65

def marching_dots
  sleep(0.2)
  io.present(".")
  sleep(0.2)
  io.present(".")
  sleep(0.1)
  io.present(".")
end

#new_game_starting_graphicObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/games/shared/input_helper.rb', line 53

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