class Mastermind::Game

Constants

ALLOWED_TRIALS

Attributes

character_count[R]
colors[R]
game_save_data[R]
level[R]
player[R]
response[R]
top_ten_record[R]
trial_count[R]

Public Class Methods

new(response, character_count = 4, top_ten_record: TopTen.new) click to toggle source
# File lib/mastermind/game.rb, line 13
def initialize(response, character_count = 4, top_ten_record: TopTen.new)
  @response = response
  @character_count = character_count
  @top_ten_record = top_ten_record
end

Public Instance Methods

actions() click to toggle source
# File lib/mastermind/extensions/continue_later.rb, line 27
def actions
  new_actions = { 'pause' => 'play_later',
    'pl' => 'play_later'
  }
  action_s = old_actions
  action_s = old_actions.merge(new_actions) unless @trial_count >= ALLOWED_TRIALS
  action_s
end
Also aliased as: old_actions
analyze_guess(current_guess) click to toggle source
# File lib/mastermind/game.rb, line 56
def analyze_guess(current_guess)
  current_guess = current_guess.upcase.split('')
  current_sprint = Hash.new(0)
  current_guess.each_with_index{ |value, index|
    if value == @colors[index]
      current_sprint[:match_position] += 1
    elsif @colors.include? value
      # near matches should be a counter that
      # counts the unique elements in the colors array
      current_sprint[:almost_match] += 1
    end
  }
  current_sprint
end
cheat() click to toggle source
# File lib/mastermind/game.rb, line 178
def cheat
  send_message(@response.cheat(@colors.join).message)
  @response.message
end
check_correct?(analysis) click to toggle source
# File lib/mastermind/game.rb, line 121
def check_correct?(analysis)
  if analysis[:match_position] == @character_count
    won(@time_started)
    true
  end
end
check_if_user_has_saved_game() click to toggle source
# File lib/mastermind/extensions/continue_later.rb, line 13
def check_if_user_has_saved_game
  @game_save_data ||= SaveGame.new
  player = @game_save_data.fetch_player(@player.name)
  if player
    input = get_input(@response.message_if_user_has_saved_game.message)
    continue_saved_game(player) if ['y', 'yes'].include? input
  end
end
continue_saved_game(player) click to toggle source
# File lib/mastermind/extensions/continue_later.rb, line 22
def continue_saved_game(player)
  @player = player
  load_saved_game
end
convert_level(level = 1) click to toggle source
# File lib/mastermind/extensions/extensions.rb, line 40
def convert_level(level = 1)
  # @character_count = 4 + (2 * (level - 1))
  color_count = 4 + (1 * (level - 1))
  #this ought not be, however useful for the simple gem
  #which basically uses the generates colors based on the arrays
  #if colors were repeated the commented method above would have been better

  @character_count = color_count
  additional_colors = {'O' => '(o)range',
                      'P' => '(p)urple',
                      'C' => '(c)yan',
                      'V' => '(v)iolet',
                      'I' => '(i)ndigo',
                      'A' => '(a)mber'
                      }
  @@all_colors_hash.merge!(additional_colors)
  @@color_array = @@all_colors_hash.keys.sample(color_count)
end
game_process() click to toggle source
# File lib/mastermind/game.rb, line 39
def game_process
  until @response.status == :won || @response.status == :lost || @trial_count >= ALLOWED_TRIALS
    input = get_game_input
    if actions.keys.include? input
      method(actions[input]).call
      break unless actions[input] =~ /instructions/
    else
      next if the_input_is_too_long_or_too_short?(input)
      @trial_count += 1
      analyzed = analyze_guess(input)
      break if check_correct?(analyzed)
      send_message(@response.analyzed_guess(analyzed[:match_position], analyzed[:almost_match]).message)
    end
  end
  what_do_you_want_to_do_next
end
generate_colors() click to toggle source
# File lib/mastermind/extensions/extensions.rb, line 59
def generate_colors
  @colors = @@color_array.sample(@character_count)
  @color_values_from_all_colors_array = @colors.map{|color| @@all_colors_hash[color] }
  @color_values_from_all_colors_array.shuffle!
end
get_game_input() click to toggle source
# File lib/mastermind/extensions/extensions.rb, line 71
def get_game_input
  input = get_input(@response.trial_count(@trial_count, @colors.join ,@color_values_from_all_colors_array).message)
  input
end
get_player() click to toggle source
# File lib/mastermind/extensions/continue_later.rb, line 8
def get_player
  old_get_player
  check_if_user_has_saved_game
end
Also aliased as: old_get_player
instructions() click to toggle source
# File lib/mastermind/extensions/extensions.rb, line 65
def instructions
  send_message(@response.instructions(@color_values_from_all_colors_array).message) if @colors
  @response.message
end
load_saved_game() click to toggle source
# File lib/mastermind/extensions/continue_later.rb, line 40
def load_saved_game
  @trial_count = @player.guesses_at_pause_count
  @colors = @player.color_combo
  @character_count = @colors.length
  time_used = @player.time_started - @player.time_at_game_save
  @time_started = Time.now.to_i - time_used
  shuffle_colors_hash
  instructions
end
loser_play_again_or_quit() click to toggle source
# File lib/mastermind/game.rb, line 109
def loser_play_again_or_quit
  input = get_game_input
  method(actions[input]).call if actions.keys.include? input
end
lost() click to toggle source
# File lib/mastermind/game.rb, line 167
def lost

end
new_old_play(level = 1)
Alias for: play
old_actions()
Alias for: actions
old_get_player()
Alias for: get_player
old_play(level = 1)

alias_method :old_initialize, :initialize

Alias for: play
play(level = 1) click to toggle source
# File lib/mastermind/extensions/extensions.rb, line 32
def play(level = 1)
  level = 1 if level < 1
  level = 3 if level > 3
  convert_level(level)

  old_play
end
Also aliased as: new_old_play, old_play
play_later() click to toggle source
# File lib/mastermind/extensions/continue_later.rb, line 36
def play_later
  @player.save_game(@time_started, @colors, @trial_count)
end
quit_game() click to toggle source
# File lib/mastermind/game.rb, line 149
def quit_game
  @trial_count = 0
  @colors = []
  send_message(@response.exit_game.message)
  @response.message
end
save_if_top_ten() click to toggle source
# File lib/mastermind/game.rb, line 171
def save_if_top_ten
  game_attr = {time_taken: @time_taken, guesses: @trial_count, date_played: Date.today}
  @player.set_attr game_attr

  @top_ten_record.add_record(@player)
end
shuffle_colors_hash() click to toggle source
# File lib/mastermind/game.rb, line 34
def shuffle_colors_hash
  @color_values_from_all_colors_array = @colors.map{|color| @@all_colors_hash[color] }
  @color_values_from_all_colors_array.shuffle!
end
the_input_is_too_long_or_too_short?(input) click to toggle source
# File lib/mastermind/game.rb, line 95
def the_input_is_too_long_or_too_short?(input)
  true if too_long?(input) || too_short?(input)
end
too_long?(input) click to toggle source
# File lib/mastermind/game.rb, line 88
def too_long?(input)
  if input.length > @character_count
    send_message(@response.longer_input.message)
    true
  end
end
too_short?(input) click to toggle source
# File lib/mastermind/game.rb, line 81
def too_short?(input)
  if input.length < @character_count
    send_message(@response.shorter_input.message)
    true
  end
end
top_players() click to toggle source
# File lib/mastermind/game.rb, line 183
def top_players
  @top_ten_record.fetch_all.each{ |player|
    send_message(player.winner_response)
  }

  game_process
end
what_do_you_want_to_do_next() click to toggle source
# File lib/mastermind/game.rb, line 99
def what_do_you_want_to_do_next
  loser_play_again_or_quit if @response.status == :lost || @trial_count >= ALLOWED_TRIALS
  winner_play_again_or_quit if @response.status == :won
end
winner_play_again_or_quit() click to toggle source
# File lib/mastermind/game.rb, line 114
def winner_play_again_or_quit
  if @response.status == :won
    input = get_input(@response.message)
    method(actions[input]).call if actions.keys.include? input
  end
end
won(start_time) click to toggle source
# File lib/mastermind/game.rb, line 156
def won(start_time)
  @time_taken = Time.now.to_i - start_time
  time = {}
  time[:mins] = @time_taken/60
  time[:secs] = @time_taken%60

  save_if_top_ten

  @response.won(@trial_count, time)
end