class Mastermind::Message

Attributes

message[R]
status[R]

Public Class Methods

new(input = nil) click to toggle source
# File lib/mastermind/message.rb, line 6
def initialize(input = nil)
  if input
    set_attr input
  else
    start
  end
end

Public Instance Methods

analyzed_guess(matched_position, included) click to toggle source
# File lib/mastermind/message.rb, line 65
def analyzed_guess(matched_position, included)
  set_attr(message: "You had #{(matched_position.to_s + ' position'.pluralize(matched_position)).colorize(:green)} exactly matched and #{(included.to_s + ' near match'.pluralize(included)).colorize(:blue)}", status: :running)
end
cheat(color) click to toggle source
# File lib/mastermind/message.rb, line 20
def cheat(color)
  set_attr(message: "Hmm! You just cheated. The colors generated were: #{color}.", status: :cheated)
end
continue_later() click to toggle source
# File lib/mastermind/extensions/continue_later.rb, line 52
def continue_later
  set_attr(message: "Can't play again?\nYou can continue later", status: :paused)
end
exit_game() click to toggle source
# File lib/mastermind/message.rb, line 50
def exit_game
  set_attr(message: "Thank you for playing Mastermind!\nGoodbye!".colorize(:red), status: :quitted)
end
gameplay_instructions(color_count = 4) click to toggle source
# File lib/mastermind/message.rb, line 84
def gameplay_instructions(color_count = 4)
  set_attr(message: "Enter a sequence of #{color_count} colors containing the generated colors e.g RYBG or YGRB.\nIf you enter fewer than #{color_count} or more than #{color_count} colors, you would receive an error message", status: :main_instructions)
end
instructions(colors = ['(r)ed', '(g)reen', '(b)lue', '(y)ellow']) click to toggle source
# File lib/mastermind/message.rb, line 28
def instructions(colors = ['(r)ed', '(g)reen', '(b)lue', '(y)ellow'])
  color_count = colors.length
  colors_generated_to_word = turn_array_to_string_list(colors, color_count)
  example_gameplay = sample_instructions_arrangement(colors)
  set_attr(message: "I have generated a beginner sequence with #{color_count.humanize + ' element'.pluralize(color_count)} made up of:\n#{colors_generated_to_word}. You are to guess the sequence in which these colors appeared e.g #{example_gameplay.colorize(:white)} for #{colors_generated_to_word}. You have #{Game::ALLOWED_TRIALS} guesses to get these colors or you lose the game. Use #{'(q)uit'.colorize(:red)} at any time to end the game.\nReady to play? \nWhat's your guess? ", status: :instructions)
end
level_select() click to toggle source
# File lib/mastermind/extensions/extensions.rb, line 81
def level_select
  set_attr(message: "To start the game select a level you would like to play:\nEnter (1) for Beginner,\nEnter (2) for Intermediate,\nEnter (3) for Advanced.", status: :level_select)
end
longer_input() click to toggle source
# File lib/mastermind/message.rb, line 42
def longer_input
  set_attr(message: "Your input is too long.".colorize(:red), status: :longer_input)
end
main_message() click to toggle source
# File lib/mastermind/message.rb, line 88
    def main_message
      message = <<-EOS
      #{%q{Just a little background on MASTERMIND}.colorize(:red)} Mastermind is a board game with an interesting history (or rather a legend?). Some game books report that it was invented in 1971 by Mordecai Meirowitz, an Israeli postmaster and telecommunications expert. After many rejections by leading toy companies, the rights were obtained by a small British firm, Invicta Plastics Ltd. The firm originally manufactured the game itself, though it has since licensed its manufacture to Hasbro in most of the world. However, Mastermind is just a clever readaptation of an old similar game called 'Bulls and cows' in English, and 'Numerello' in Italian... Actually, the old British game 'Bulls and cows' was somewhat different from the commercial version. It was played on paper, not on a board... Over 50 million copies later, Mastermind is still marketed today!
      The idea of the game is for one player (the code-breaker) to guess the secret code chosen by the other player (the code-maker). The code is a sequence of 4 colored pegs chosen from six colors available. The code-breaker makes a serie of pattern guesses - after each guess the code-maker gives feedback in the form of 2 numbers, the number of pegs that are of the right color and in the correct position, and the number of pegs that are of the correct color but not in the correct position - these numbers are usually represented by small black and white pegs.
      In 1977, the mathematician Donald Knuth demonstrated that the code-breaker can solve the pattern in five moves or less, using an algorithm that progressively reduced the number of possible patterns.
EOS
      set_attr(message: message, status: :background_message)
    end
message_if_user_has_saved_game() click to toggle source
# File lib/mastermind/extensions/continue_later.rb, line 56
def message_if_user_has_saved_game
  set_attr(message: "You currently have a game saved.\nWould you like to continue?\nEnter (y) for Yes\nEnter (n) for No!", status: :has_saved_game)
end
old_trial_count(trial_count, correct_sequence, colors = nil)
Alias for: trial_count
player() click to toggle source
# File lib/mastermind/message.rb, line 80
def player
  set_attr(message: "So you would like to play!\nStart by telling me your name: ".colorize(:green), status: :player_name)
end
set_attr(input) click to toggle source
# File lib/mastermind/message.rb, line 14
def set_attr(input)
  @message = input[:message]
  @status = input[:status]
  self
end
shorter_input() click to toggle source

def instructions

set_attr(message: "I have generated a beginner sequence with four elements made up of:\n#{'(r)ed'.colorize(:red)}, #{'(g)reen'.colorize(:green)}, #{'(b)lue'.colorize(:blue)}, and #{'(y)ellow'.colorize(:yellow)}. Use #{'(q)uit'.colorize(:red)} at any time to end the game.\nWhat's your guess? ", status: :instructions)

end

# File lib/mastermind/message.rb, line 38
def shorter_input
  set_attr(message: "Your input is too short.".colorize(:red), status: :shorter_input)
end
start() click to toggle source
# File lib/mastermind/message.rb, line 46
def start
  set_attr(message: "Welcome to MASTERMIND!\nWould you like to #{'(p)lay'.colorize(:green)}, read the #{'(i)nstructions'.colorize(:blue)}, read a little #{'(b)ackground'.colorize(:yellow)} on Mastermind or #{'(q)uit'.colorize(:red)}?", status: :main_start)
end
trial_count(trial_count, correct_sequence, colors = nil) click to toggle source
# File lib/mastermind/extensions/extensions.rb, line 85
def trial_count(trial_count, correct_sequence, colors = nil)
  remaining_trials = Game::ALLOWED_TRIALS - trial_count
  if trial_count == 0
    instructions(colors)
  else
    old_trial_count(trial_count, correct_sequence)
  end
end
Also aliased as: old_trial_count
unsupported_game_action(message: nil, status: nil) click to toggle source
# File lib/mastermind/message.rb, line 54
def unsupported_game_action(message: nil, status: nil)
  set_attr(
    message: message || "You entered an unsupported action, try again! ".colorize(:red),
    status: status || :unsupported_action
    )
end
winner(winner, trials, time_taken) click to toggle source
# File lib/mastermind/message.rb, line 97
def winner(winner, trials, time_taken)
  set_attr(message: "#{winner} completed mastermind in #{trials} #{'guess'.pluralize(trials)} and #{time_taken}", status: :top_players)
end
won(tries, time={}) click to toggle source
# File lib/mastermind/message.rb, line 24
def won(tries, time={})
  set_attr(message: "#{'Congratulations!'.colorize(:green)}\nYou won the game in #{(tries.to_s + ' try'.pluralize(tries)).colorize(:blue)} and #{(time[:mins].to_s + 'm' + time[:secs].to_s + 's').colorize(:blue)}.\nDo you want to (p)lay again or (q)uit or (t)op_players?", status: :won)
end
wrong_guess() click to toggle source
# File lib/mastermind/message.rb, line 61
def wrong_guess
  set_attr(message: "Your guess was wrong! Guess again: ".colorize(:red), status: :wrong)
end

Private Instance Methods

sample_instructions_arrangement(colors) click to toggle source
# File lib/mastermind/message.rb, line 113
def sample_instructions_arrangement colors
  colors.map{|c| c[1]}.join.upcase
end
turn_array_to_string_list(colors, array_count) click to toggle source
# File lib/mastermind/message.rb, line 102
def turn_array_to_string_list colors, array_count
  if array_count >= 2
    string_list = ''
    string_list << colors[0..-2].map{|c| c.colorize(c.scan(/([[:alpha:]]+)/).join.to_sym)}.join(', ')
    string_list << " and #{colors[-1].colorize(colors[-1].scan(/([[:alpha:]]+)/).join.to_sym)}"
    string_list
  else
    colors.join
  end
end