Class: MasterMind::Tobi::MultiPlayer

Inherits:
SinglePlayer show all
Defined in:
lib/mastermind/tobi/multi_game.rb

Constant Summary

Constants inherited from SinglePlayer

SinglePlayer::ALLOWED

Constants included from TimeHelper

TimeHelper::PLURAL, TimeHelper::SINGULAR

Instance Attribute Summary collapse

Attributes inherited from SinglePlayer

#end_guess, #game_logic, #history, #sequence, #start_time

Instance Method Summary collapse

Methods inherited from SinglePlayer

#invalid_length?, #process_input, #right_guess, #treat_guess, #treat_option?

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, hide_guess) ⇒ MultiPlayer

Returns a new instance of MultiPlayer.



11
12
13
14
# File 'lib/mastermind/tobi/multi_game.rb', line 11

def initialize(sequence, game_logic, hide_guess)
  super(sequence, game_logic)
  @hide_guess = hide_guess  
end

Instance Attribute Details

#hide_guessObject (readonly)

Returns the value of attribute hide_guess.



9
10
11
# File 'lib/mastermind/tobi/multi_game.rb', line 9

def hide_guess
  @hide_guess
end

Instance Method Details

#check_help(input, guesses_hash, history_hash, index) ⇒ Object



72
73
74
# File 'lib/mastermind/tobi/multi_game.rb', line 72

def check_help(input, guesses_hash, history_hash, index)
  treat_guess(input, guesses_hash[index], history_hash[index])  # player enters a guess
end

#get_guess(history_hash, guesses_hash, index) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mastermind/tobi/multi_game.rb', line 55

def get_guess(history_hash, guesses_hash, index)
  input = hide_guess ? STDIN.noecho(&:gets).chomp : gets.chomp
  
  length_or_option = false
  length_or_option = invalid_length?(input)                            # invalid length for entry
  
  if !length_or_option
    length_or_option = treat_option?(input, history_hash[index])             # entry is a game option
  end
  
  if !length_or_option
    guesses_hash[index] = check_help(input, guesses_hash, history_hash, index)
  end
  
  guesses_hash[index]
end

#multi_helper(guesses_hash, index, history_hash) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/mastermind/tobi/multi_game.rb', line 45

def multi_helper(guesses_hash, index, history_hash)
  last_guess = guesses_hash[index]                      # to store player's last guess
  print "************"
  print UI::PLAYER_MESSAGE % index
  while guesses_hash[index] == last_guess               # to counter situations where user enters invalid input, guess would still be same
    guesses_hash[index] = get_guess(history_hash, guesses_hash, index)
    throw(:player_wins) if guesses_hash[index] == end_guess # if there is a win
  end
end

#multi_start_game(number, history_hash, guesses_hash) ⇒ Object



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

def multi_start_game(number, history_hash, guesses_hash)
  total_guesses = 0                                       # total guesses of all players
  catch :player_wins do
    while total_guesses < (UI::GUESS_MAX * number)        # until all players have exhausted their guesses
      for index in (1..number)                                # loop through to allow each player have a guess
        total_guesses += 1
        multi_helper(guesses_hash, index, history_hash)
      end
    end
  end
  puts UI::SORRY_MULTI_MESSAGE % sequence.join.upcase if total_guesses == UI::GUESS_MAX * number # guesses exhausted with no winner
  total_guesses
end

#num_of_playersObject



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

def num_of_players
  print UI::MULTI_START_MESSAGE
  input = gets.chomp
  
  while (input.to_i <= 1)
    print UI::INVALID_MESSAGE
    input = gets.chomp
  end
  
  return input.to_i
end

#start_gameObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mastermind/tobi/multi_game.rb', line 16

def start_game
  number = num_of_players
  end_guess = 12*number + 1
  
  puts UI::GENERATE_MESSAGE % [game_logic.sequence_type, game_logic.length, UI::COLOR_STRINGS[game_logic.level]]
  history_hash = {}
  guesses_hash = {}
  (1..number).each {|x| 
    history_hash[x] = []
    guesses_hash[x] = 0 
  }
  
  multi_start_game(number, history_hash, guesses_hash)    
end

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



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

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 % [hide_guess ? "Your Guess" : input.upcase, result[:correct_elements], result[:correct_position]]
  puts UI::GUESSES_MESSAGE % [guesses, guesses > 1 ? "guesses" : "guess"]
  print UI::INPUT_PROMPT
end