Class: Mastermind::Message
- Inherits:
-
Object
- Object
- Mastermind::Message
- Defined in:
- lib/mastermind/message.rb,
lib/mastermind/extensions/extensions.rb,
lib/mastermind/extensions/continue_later.rb
Instance Attribute Summary collapse
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #analyzed_guess(matched_position, included) ⇒ Object
- #cheat(color) ⇒ Object
- #continue_later ⇒ Object
- #exit_game ⇒ Object
- #gameplay_instructions(color_count = 4) ⇒ Object
-
#initialize(input = nil) ⇒ Message
constructor
A new instance of Message.
- #instructions(colors = ['(r)ed', '(g)reen', '(b)lue', '(y)ellow']) ⇒ Object
- #level_select ⇒ Object
- #longer_input ⇒ Object
- #main_message ⇒ Object
- #message_if_user_has_saved_game ⇒ Object
- #old_trial_count ⇒ Object
- #player ⇒ Object
- #set_attr(input) ⇒ Object
-
#shorter_input ⇒ Object
def instructions set_attr(message: “I have generated a beginner sequence with four elements made up of:n#‘®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.
- #start ⇒ Object
- #trial_count(trial_count, correct_sequence, colors = nil) ⇒ Object
- #unsupported_game_action(message: nil, status: nil) ⇒ Object
- #winner(winner, trials, time_taken) ⇒ Object
- #won(tries, time = {}) ⇒ Object
- #wrong_guess ⇒ Object
Constructor Details
#initialize(input = nil) ⇒ Message
Returns a new instance of Message.
6 7 8 9 10 11 12 |
# File 'lib/mastermind/message.rb', line 6 def initialize(input = nil) if input set_attr input else start end end |
Instance Attribute Details
#message ⇒ Object (readonly)
Returns the value of attribute message.
4 5 6 |
# File 'lib/mastermind/message.rb', line 4 def @message end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
4 5 6 |
# File 'lib/mastermind/message.rb', line 4 def status @status end |
Instance Method Details
#analyzed_guess(matched_position, included) ⇒ Object
65 66 67 |
# 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) ⇒ Object
20 21 22 |
# 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 ⇒ Object
52 53 54 |
# 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 ⇒ Object
50 51 52 |
# 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) ⇒ Object
84 85 86 |
# 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']) ⇒ Object
28 29 30 31 32 33 |
# 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 ⇒ Object
81 82 83 |
# 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 ⇒ Object
42 43 44 |
# 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 ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/mastermind/message.rb', line 88 def = <<-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: , status: :background_message) end |
#message_if_user_has_saved_game ⇒ Object
56 57 58 |
# File 'lib/mastermind/extensions/continue_later.rb', line 56 def 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 ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/mastermind/extensions/extensions.rb', line 79 def trial_count(trial_count, correct_sequence, colors = nil) remaining_trials = Game::ALLOWED_TRIALS - trial_count if trial_count == 0 instructions(colors) elsif(trial_count < Game::ALLOWED_TRIALS) set_attr(message: "You have tried #{trial_count.to_s + ' time'.pluralize(trial_count)}. You have #{remaining_trials.to_s + ' attempt'.pluralize(remaining_trials)} left.\nTry again: ", status: :wrong_guess) else set_attr(message: "You tried, but lost.\nThe colors generated were #{correct_sequence}.\nWant to try again? (p)lay to start again or (q)uit to exit or (t)op_players to view the top ten players. ".colorize(:red), status: :lost) end end |
#player ⇒ Object
80 81 82 |
# 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) ⇒ Object
14 15 16 17 18 |
# File 'lib/mastermind/message.rb', line 14 def set_attr(input) @message = input[:message] @status = input[:status] self end |
#shorter_input ⇒ Object
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
38 39 40 |
# 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 ⇒ Object
46 47 48 |
# 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) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/mastermind/message.rb', line 69 def trial_count(trial_count, correct_sequence, colors = nil) remaining_trials = Game::ALLOWED_TRIALS - trial_count if trial_count == 0 instructions(colors) elsif(trial_count < Game::ALLOWED_TRIALS) set_attr(message: "You have tried #{trial_count.to_s + ' time'.pluralize(trial_count)}. You have #{remaining_trials.to_s + ' attempt'.pluralize(remaining_trials)} left.\nTry again: ", status: :wrong_guess) else set_attr(message: "You tried, but lost.\nThe colors generated were #{correct_sequence}.\nWant to try again? (p)lay to start again or (q)uit to exit or (t)op_players to view the top ten players. ".colorize(:red), status: :lost) end end |
#unsupported_game_action(message: nil, status: nil) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/mastermind/message.rb', line 54 def unsupported_game_action(message: nil, status: nil) set_attr( message: || "You entered an unsupported action, try again! ".colorize(:red), status: status || :unsupported_action ) end |
#winner(winner, trials, time_taken) ⇒ Object
97 98 99 |
# 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 = {}) ⇒ Object
24 25 26 |
# 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 ⇒ Object
61 62 63 |
# File 'lib/mastermind/message.rb', line 61 def wrong_guess set_attr(message: "Your guess was wrong! Guess again: ".colorize(:red), status: :wrong) end |