Class: MasterMind::Tobi::SinglePlayer

Inherits:
GameMethods show all
Includes:
TimeHelper
Defined in:
lib/mastermind/tobi/single_game.rb

Direct Known Subclasses

MultiPlayer

Constant Summary collapse

ALLOWED =
['c', 'h', 'q', 'cheat', 'history', 'quit']

Constants included from TimeHelper

TimeHelper::PLURAL, TimeHelper::SINGULAR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TimeHelper

#construct_string, #hour_minute_helper, #second_helper, #time_convert

Methods inherited from GameMethods

#average_string, #difference, #get_name, #get_top_ten, #print_history, #print_top_ten, #store_game, #user_permits_store?, #yes_or_no?

Constructor Details

#initialize(sequence, game_logic) ⇒ SinglePlayer

Returns a new instance of SinglePlayer.



22
23
24
25
26
27
28
# File 'lib/mastermind/tobi/single_game.rb', line 22

def initialize(sequence, game_logic)
  @start_time = Time.now
  @history = []
  @sequence = sequence
  @game_logic = game_logic
  @end_guess = 13
end

Instance Attribute Details

#end_guessObject

Returns the value of attribute end_guess.



18
19
20
# File 'lib/mastermind/tobi/single_game.rb', line 18

def end_guess
  @end_guess
end

#game_logicObject (readonly)

Returns the value of attribute game_logic.



17
18
19
# File 'lib/mastermind/tobi/single_game.rb', line 17

def game_logic
  @game_logic
end

#historyObject (readonly)

Returns the value of attribute history.



15
16
17
# File 'lib/mastermind/tobi/single_game.rb', line 15

def history
  @history
end

#sequenceObject (readonly)

Returns the value of attribute sequence.



16
17
18
# File 'lib/mastermind/tobi/single_game.rb', line 16

def sequence
  @sequence
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



14
15
16
# File 'lib/mastermind/tobi/single_game.rb', line 14

def start_time
  @start_time
end

Instance Method Details

#invalid_length?(input) ⇒ Boolean

check if user’s guess is longer or fewer than the required length

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mastermind/tobi/single_game.rb', line 61

def invalid_length?(input)   
  if input.length < game_logic.length && !(ALLOWED.include?(input))
    print UI::INPUT_SHORT_MESSAGE
    return true
    
  elsif input.length > game_logic.length && !(ALLOWED.include?(input)) 
    print UI::INPUT_LONG_MESSAGE
    return true
  end
  
  return false
end

#process_input(guesses, history) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mastermind/tobi/single_game.rb', line 44

def process_input(guesses, history)
  input = gets.chomp.downcase
  length_or_option = false
  
  length_or_option = invalid_length?(input)
  
  if !length_or_option
    length_or_option = treat_option?(input, history)
  end
  
  if !length_or_option
    guesses = treat_guess(input, guesses, history)
  end
  guesses
end

#right_guess(start_time, sequence, guesses) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/mastermind/tobi/single_game.rb', line 98

def right_guess(start_time, sequence, guesses)
  time_elapsed = (Time.now - start_time).to_i                                       # time used by user in seconds
  current_player = store_game(sequence, guesses, time_elapsed)                      # store user data to top-scores file
  
  puts UI::CONGRATS_MESSAGE % [current_player.name, sequence.join.upcase, guesses, guesses > 1 ? "guesses" : "guess", 
    time_convert(time_elapsed) << '.']
  print_top_ten(current_player)
end

#start_gameObject

generate game sequence and start game play



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mastermind/tobi/single_game.rb', line 31

def start_game
  print UI::GENERATE_MESSAGE % [game_logic.sequence_type, game_logic.length, UI::COLOR_STRINGS[game_logic.level]]
  print UI::INPUT_PROMPT
  
  # allow the user guess up to twelve times before ending game
  guesses = 0
  while guesses < UI::GUESS_MAX
    guesses = process_input(guesses, history)
  end
  puts UI::SORRY_SINGLE_MESSAGE % sequence.join.upcase if guesses == UI::GUESS_MAX
  guesses   
end

#treat_guess(input, guesses, history) ⇒ Object

treat guesses entered by user



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mastermind/tobi/single_game.rb', line 86

def treat_guess(input, guesses, history)
  guesses += 1
  if input == sequence.join                         # right guess entered
    right_guess(start_time, sequence, guesses)
    guesses = end_guess                             # sentinel value to end guess loop
  else
    wrong_guess(sequence, guesses, input, history)  # wrong guess entered
  end
  
  return guesses
end

#treat_option?(input, history) ⇒ Boolean

check if user selects an option

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
# File 'lib/mastermind/tobi/single_game.rb', line 75

def treat_option?(input, history)
  case input
  when "h", "history" then print_history(history)
  when "q", "quit" then exit(0)
  when "c", "cheat" then print UI::SEQUENCE_MESSAGE % sequence.join.upcase
  else return false
  end
  return true
end

#wrong_guess(sequence, guesses, input, history) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/mastermind/tobi/single_game.rb', line 107

def wrong_guess(sequence, guesses, input, history)
  result = GameLogic.check_input(sequence, input)                                       # get results from input
  history << GamePlay.new(input, result[:correct_elements], result[:correct_position])  # add game play to history
  
  puts UI::INFO_MESSAGE % [input.upcase, result[:correct_elements], result[:correct_position]]
  puts UI::GUESSES_MESSAGE % [guesses, guesses > 1 ? "guesses" : "guess"]
  print UI::INPUT_PROMPT
end