class Mastermind::Oscar::Codemaker
Attributes
code[R]
difficulty[R]
guess[R]
recorder[R]
timer[R]
Public Class Methods
new(difficulty, recorder)
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 7 def initialize(difficulty, recorder) @difficulty = difficulty @recorder = recorder end
Public Instance Methods
analyze_input(input)
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 55 def analyze_input(input) @recorder.print_to_file("Guess #{guess}:\t\t#{input}") input = input.split("") return true if input == code exact = exact_match(code, input) partial = partial_match(code, input, exact) exact = exact.size give_guess_feedback(input, exact, partial) false end
cheat()
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 112 def cheat Printer.show_cheat(color_code) end
cheat?(input)
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 108 def cheat?(input) input.upcase == 'C' && input.length == 1 end
color_code()
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 198 def color_code Printer.colour_letters(code.join) end
colors()
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 138 def colors Printer.colors.keys end
congratulation_msg(play_time)
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 179 def congratulation_msg(play_time) Printer.congratulations(@recorder.user,color_code, guess, play_time) end
congratulations()
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 170 def congratulations @timer.stop_timer play_time = @timer.get_time congratulation_msg(play_time) save_game(play_time) :won end
create_color_string()
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 147 def create_color_string count = 0 string = [] Printer.colors.each do |key, colo| break if count == @possible_colours text = colo.to_s text.gsub!(text[0],"(#{text[0]})") string << Printer.colour_text(text, colo) count += 1 end string[0...-1].join(", ") + ", and " + string.last end
difficulties(key)
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 128 def difficulties(key) #key => [characters, colors] specs = { :beginner => [4, 4], :intermediate => [6, 5], :expert => [8, 6] } specs[key] end
exact_match(game_code, input)
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 70 def exact_match(game_code, input) exact = [] input.each_index do |index| exact << index if game_code[index] == input[index] end exact end
game_over()
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 189 def game_over @timer.stop_timer Printer.game_over(@recorder.user, color_code) @recorder.print_to_file("Game over! #{@timer.get_time}") @recorder.close :end end
game_play()
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 23 def game_play max_guess = 12 @guess = 0 while !out_of_guess?(guess, max_guess) input = get_input if quit?(input) return :quit elsif cheat?(input) cheat return elsif is_valid_input?(input) @guess += 1 status = analyze_input(input) return congratulations if status end Printer.output("\t\t\nYou've taken #{guess} out of #{max_guess} guesses.\n") guess_again end #Some one that is out of guesses return game_over end
generate_code()
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 116 def generate_code @code = [] specs = difficulties(difficulty) @possible_colours = specs[1] characters = specs[0] characters.times do index = rand(0...@possible_colours) @code << colors[index] end end
get_input(stream = STDIN)
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 48 def get_input(stream = STDIN) input = stream.gets.chomp input = input.nil? ? " " : input.upcase.strip input end
give_guess_feedback(input, exact, partial)
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 94 def give_guess_feedback(input, exact, partial) feedback = "Hmmm! Your guess, " + Printer.colour_letters(input) + ", has #{exact + partial} of the correct elements with #{exact} in the correct positions." Printer.output(feedback) end
guess_again()
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 202 def guess_again Printer.output "\t\t\tGuess again" end
init_message()
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 142 def init_message a = (difficulty == :beginner) ? 'a' : 'an' Printer.greet_user(@recorder.user, a, difficulty, code.size, create_color_string) end
is_valid_input?(input)
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 160 def is_valid_input?(input) length = code.size return true if input.length == length puts "\t" + Printer.colour_background(" !!!::: Oops! Your input is too short :::!!!", :red) if input.length < length puts "\t" + Printer.colour_background(" !!!::: Oops! Your input is too long :::!!!", :red) if input.length > length false end
out_of_guess?(guess, max_guess)
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 100 def out_of_guess?(guess, max_guess) guess.eql? max_guess end
partial_match(game_code, input,exact)
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 78 def partial_match(game_code, input,exact) partials = [] + game_code exact.each{|elt| partials[elt] = nil} partial = 0 input.each_with_index do |item, index| pIndex = partials.index(item) if(pIndex && !exact.include?(index)) partial += 1 partials[pIndex] = nil end end partial end
quit?(input)
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 104 def quit?(input) input[0].upcase == 'Q' if !input.empty? end
save_game(play_time)
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 183 def save_game(play_time) @recorder.print_to_file("Guessed the sequence in: #{play_time}") @recorder.check_for_top_ten(code.join, guess, @timer.get_seconds, difficulty.to_s) @recorder.close end
start()
click to toggle source
# File lib/mastermind/oscar/codemaker.rb, line 12 def start generate_code @timer = TimeManager.new @recorder.open_save_file(difficulty) @recorder.print_to_file("Sequence: \t\t<#{code.join}>") @timer.start_timer init_message return game_play end