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 trialLogan Bresnahan
3,389 PointsI'm trying to build the classic MasterMind game in Ruby but I can't seem to put it all together.
class Game
COLORS = %w{red yellow green blue}.sample(4)
puts COLORS
attr_accessor :guess
def initialize
puts "Started a game."
@answer = []
@guess = guess
end
def rules
10.times do |x| player_guess
if @answer == COLORS
puts "You Win!"
else
puts "try again!"
end
end
def player_answer #Here we put the players answer into the @answer array.
@answer.push(guess)
puts @answer
end
end
def player_guess
puts "Go ahead and choose 4 colors in any order from red, yellow, green, and blue. Then hit enter."
guess = gets.chomp
end
end
game = Game.new
game.rules
```Ruby
#Now I know this doesn't follow all the rules the original game MasterMind but I was hoping there is someone out there who is familiar enough with it to help me get this small example working. If anyone could help you will have my eternal gratitude.
2 Answers
Kevin Mulhern
20,374 PointsThe problem is in your loop, It is checking to see if the answer array is equal to the colours array. But the answer array is always empty because it never gets any of the guesses added to it.
I got it to work with the code below, You will have to enter the exact sequence of colours in to get it to evaluate to win in its current form.
I made a mastermind game in ruby a few weeks ago, You can have a look at here if you like: https://github.com/KevinMulhern/ruby_oop/blob/master/mastermind.rb
class Game
COLORS = %w{red yellow green blue}.sample(4)
puts COLORS
attr_accessor :guess
def initialize
puts "Started a game."
@guess = []
end
def rules
10.times do |x|
@guess.push(player_guess)
if @guess == COLORS
puts "you win"
else
puts "try again!"
end
end
def player_guess
puts "Go ahead and choose 4 colors in any order from red, yellow, green, and blue. Then hit enter."
guess = gets.chomp
end
end
game = Game.new
game.rules
Logan Bresnahan
3,389 PointsWow thanks! This was exactly what I was hoping for. I looked over your project awhile and was very impressed! there are a lot of MasterMind examples out there but none as thorough as yours. Honestly your project looked really good and I noticed you had some methods and pieces I have yet to learn. When you were writing your code what did you use for assistance or as a reference?
Kevin Mulhern
20,374 PointsI am currently going through this course on the odin project: http://www.theodinproject.com/ruby-programming
It is bar none the best resource for learning ruby out there, after learning concepts it has you make a program that use what you have learned. I have learnt far more from doing the projects than I could have imagined and the confidence you get after completing a few projects is a real boost. I went through all of Treehouses ruby courses while going through the first few sections and it really helped to shore up things.
Logan Bresnahan
3,389 PointsOK, great. I have heard of the odin project but never explored that avenue. I have pretty much finished the Ruby courses here but that kind of leaves you with...what do i do from here? I'm completely new to programming in general so it can be quite a challenge doing everything yourself from the start. I appreciate your help and will continue with the projects!
Kevin Mulhern
20,374 PointsHonestly what you do next all depends on your end goal, I assume that you are learning ruby to pick up ruby on rails? Getting comfortable with ruby would put you in a great position to move on and master rails. The best way to do that would be to create some projects to build your skills.
I am part of a facebook page for the odin project, most people on it are going through the odin project but everyone who is learning to code is welcome. We share resources and help each other out on a daily basis. You are more than welcome to join us.
Heres the link: https://www.facebook.com/groups/TOPSTUDYGROUP/