Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Logan Bresnahan
Logan Bresnahan
3,389 Points

Can anyone tell me if this attempted version of the game MasterMind has the possibility of working? regex possible?

class Game
    attr_accessor :player_guess
    def initialize
    puts "Started a game."
    @player_guess = player_guess
    @colors = %w{red yellow green blue}.sample(4).to_s
    puts @colors
    end

    def play
        10.times do |x| 
            @player_guess

            m = /#{@colors}/.match(@answer)

            case m
                when m == @colors
                    puts "You Win!"
                    exit
                when m != @colors
                    puts "Keep Going"
                else
                    puts m[0]                   
            end
        end
    end


    def player_guess
        puts "Go ahead and choose 4 colors in any order from red, yellow, green, and blue. Then hit enter."
        @answer = gets.chomp
        @answer.downcase.split
    end
end

game = Game.new
game.play

1 Answer

Brandon Barrette
Brandon Barrette
20,485 Points

Well for mastermind you need to tell the user if the color is in the right spot. Also in your initialize, I don't see where the variable player_guess is defined. I don't think you would need to initialize a guess when the game is created.