Class: Mastermind::Interact

Inherits:
Object
  • Object
show all
Defined in:
lib/mastermind/interact.rb

Instance Method Summary collapse

Instance Method Details

#blank_lineObject



22
23
24
# File 'lib/mastermind/interact.rb', line 22

def blank_line
  "\n"
end

#color_code(letter) ⇒ Object



255
256
257
# File 'lib/mastermind/interact.rb', line 255

def color_code(letter)
  Mastermind::COLOR_CODES[letter]
end

#color_guess(code) ⇒ Object



248
249
250
251
252
253
# File 'lib/mastermind/interact.rb', line 248

def color_guess(code)
  input = code.kind_of?(Array) ? code : code.chars
  colored = []
  input.map { |char| colored << char.colorize(color_code(char)) }
  colored.join("")
end

#command_promptObject



61
62
63
# File 'lib/mastermind/interact.rb', line 61

def command_prompt
  "Enter command: "
end

#guess_prompt(player) ⇒ Object



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

def guess_prompt(player)
  if player.turn_pos == 0
  "\n"+
  "#{player.name} - Enter your guess: "
  else
  "\n"+
  "                                            #{player.name} - Enter your guess: "
  end
end

#line_breakObject



16
17
18
19
20
# File 'lib/mastermind/interact.rb', line 16

def line_break
  "
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  ".colorize(:green)
end

#multi_player_divObject



69
70
71
72
73
# File 'lib/mastermind/interact.rb', line 69

def multi_player_div
  "\n"+
  "Player 1                                    Player 2\n"+
  "==================================================================================="
end

#name_prompt(player_no) ⇒ Object



151
152
153
# File 'lib/mastermind/interact.rb', line 151

def name_prompt(player_no)
  "Player #{player_no} Name: "
end


189
190
191
192
# File 'lib/mastermind/interact.rb', line 189

def print_are_you_sure
  "
Are you sure you want to quit? (y)es/(n)o".colorize(:red)
end


115
116
117
118
119
# File 'lib/mastermind/interact.rb', line 115

def print_farewell
  "
Thanks for playing
  "
end


143
144
145
# File 'lib/mastermind/interact.rb', line 143

def print_game_info
  ""
end


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

def print_game_options
  line_break +
  blank_line +
  "
                       ====================================
                      |        MASTERMIND GAME OPTS        |
                      |------------------------------------|
                      |                                    |
                      |          (s)ingle player           |
                      |          (m)ulti player            |
                      |          (q)uit                    |
                      |                                    |
                       ====================================
  ".colorize(:green) +
  blank_line
end


164
165
166
167
# File 'lib/mastermind/interact.rb', line 164

def print_get_secret
  "Time to create some secrets. Each player will create the secret
That their opponent will be guessing."
end


206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/mastermind/interact.rb', line 206

def print_guess_stats(number_guesses, correct_pos, correct_color, guess, max_guesses, player)
  if player.turn_pos == 0
  "\n"+
  "----------------------------------------\n"+
  "Guess ##{number_guesses} (#{color_guess(guess)}): #{max_guesses - number_guesses} guesses remaining\n"+
  "Correct position: #{correct_pos}   Correct colors: #{correct_color}\n"+
  "\n"+
  "Nope... sorry #{player.name} let's try that again\n"+
  "----------------------------------------\n"
  else
  "\n"+
  "                                            ----------------------------------------\n"+
  "                                            Guess ##{number_guesses} (#{color_guess(guess)}): #{max_guesses - number_guesses} guesses remaining\n"+
  "                                            Correct position: #{correct_pos}   Correct colors: #{correct_color}\n"+
  "\n"+
  "                                            Nope... sorry #{player.name} let's try that again\n"+
  "                                            ----------------------------------------\n"
  end
end


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/mastermind/interact.rb', line 121

def print_instructions(colors)
  "
Mastermind is a code breaking game for one or two players.\n\n"+
"##SINGLE PLAYER##".colorize(:green)+
"\nIn a single player game, when starting, the computer generates a secret code that you need to
guess. The code will consist of 4 colors out of a possible 6. The possible colors are:
#{colors}

The code that you will be guessing will be something like 'RRGB'. Note that duplicate colors 
may be present in the code.

You will be given 12 guesses to guess the correct answer. If you guess correctly, you will see
the number of guesses it took you to guess the correct answer along with the amount of
time that it took you to guess correctly.\n\n"+
"##MULTI PLAYER##".colorize(:green)+
"\nIn a multi player game, the game play is the same as single player, but each player chooses
the code that their opponent must guess. After each player has chosen a secret code, players
will alternate guessing their code until either, each player finishes, or the players run out
of guesses.
  "
end


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mastermind/interact.rb', line 26

def print_intro
  line_break +
  blank_line +
  "
                       ====================================
                      |        MASTERMIND MAIN MENU        |
                      | ---------------------------------- |
                      |                                    |
                      |          (i)nstructions            |
                      |          (p)lay                    |
                      |          (q)uit                    |
                      |                                    |
                       ====================================
  ".colorize(:green) +
  blank_line
end


85
86
87
88
89
# File 'lib/mastermind/interact.rb', line 85

def print_invalid(input)
  "
'#{input}' is not a valid command, please enter a valid command.
  "
end


194
195
196
197
198
199
200
201
202
203
204
# File 'lib/mastermind/interact.rb', line 194

def print_invalid_guess(player)
  if player.turn_pos == 0
  "\n"+
  "'#{player.command}' is not a valid guess,\n"+
  "please guess again."
  else
  "\n"+
  "                                            '#{player.command}' is not a valid guess,\n"+
  "                                            please guess again."
  end
end


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/mastermind/interact.rb', line 91

def print_out_of_guesses(number_guesses, correct_pos, correct_color, guess, max_guesses, player)
  if player.turn_pos == 0
  "\n"+
  "----------------------------------------\n"+
  "Guess ##{number_guesses} (#{color_guess(guess)}): #{max_guesses - number_guesses} guesses remaining\n"+
  "Correct position: #{correct_pos}   Correct colors: #{correct_color}\n"+
  "\n"+
  "Nope... sorry #{player.name}, that was your last\n"+
  "guess and you still didn't get it right....\n"+
  "The secret code was: #{color_guess(player.secret)}\n"+
  "----------------------------------------\n"
  else
  "\n"+
  "                                            ----------------------------------------\n"+
  "                                            Guess ##{number_guesses} (#{color_guess(guess)}): #{max_guesses - number_guesses} guesses remaining\n"+
  "                                            Correct position: #{correct_pos}   Correct colors: #{correct_color}\n"+
  "\n"+
  "                                            Nope... sorry #{player.name}, that was your last\n"+
  "                                            guess and you still didn't get it right....\n"+
  "                                            The secret code was: #{color_guess(player.secret)}\n"+
  "                                            ----------------------------------------\n"
  end
end


147
148
149
# File 'lib/mastermind/interact.rb', line 147

def print_player_intro
  "Player(s) need to enter their name."
end


155
156
157
158
# File 'lib/mastermind/interact.rb', line 155

def print_player_secret_intro(player_name)
  "
#{player_name} - Pick the secret code for your opponent"
end


169
170
171
172
173
174
175
176
177
178
179
# File 'lib/mastermind/interact.rb', line 169

def print_round_intro(colors)
  "
A random code consisting of 4 colors has been generated for you.

The valid color options are:
#{colors}

Enter your guess in the form of '#{color_guess("RGBY")}':
------------------------------
  "
end


181
182
183
184
185
186
187
# File 'lib/mastermind/interact.rb', line 181

def print_round_over
  "
                      **************************************
                      *             Round over             *
                      **************************************
  "
end


6
7
8
9
10
11
12
13
14
# File 'lib/mastermind/interact.rb', line 6

def print_title
  %q(
_/      _/                        _/                                          _/                  _/
   _/_/  _/_/    _/_/_/    _/_/_/  _/_/_/_/    _/_/    _/  _/_/  _/_/_/  _/_/        _/_/_/      _/_/_/
  _/  _/  _/  _/    _/  _/_/        _/      _/_/_/_/  _/_/      _/    _/    _/  _/  _/    _/  _/    _/
 _/      _/  _/    _/      _/_/    _/      _/        _/        _/    _/    _/  _/  _/    _/  _/    _/
_/      _/    _/_/_/  _/_/_/        _/_/    _/_/_/  _/        _/    _/    _/  _/  _/    _/    _/_/_/
  ).colorize(:green)
end


226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/mastermind/interact.rb', line 226

def print_win(number_guesses, time, secret, player)
  if player.turn_pos == 0
  "\n"+
  "******************************\n"+
  "       CONGRATULATIONS\n"+
  "******************************\n"+
  " You guessed the code (#{color_guess(secret)})\n"+
  "    It took you #{number_guesses} guesses\n"+
  "       over #{time} seconds!\n"+
  "******************************\n"
  else
  "\n"+
  "                                            ******************************\n"+
  "                                                   CONGRATULATIONS\n"+
  "                                            ******************************\n"+
  "                                             You guessed the code (#{color_guess(secret)})\n"+
  "                                                It took you #{number_guesses} guesses\n"+
  "                                                   over #{time} seconds!\n"+
  "                                            ******************************\n"
  end
end

#screen_clearObject



65
66
67
# File 'lib/mastermind/interact.rb', line 65

def screen_clear
  "\e[H\e[2J"
end

#secret_guess_promptObject



160
161
162
# File 'lib/mastermind/interact.rb', line 160

def secret_guess_prompt
  "Enter secret code: "
end