I am trying to build a mastermind game, and I am hoping to have my code instance its own objects containing information about the place and color of the step in the passcode. This is so I can vary the length of the passcode without hard coding it. Is there a way to keep track of these objects, seeing as I am not personally assigning a variable? Can my program assign the instances their own variable names, and if so how would I do that?
#Builder for password objects
class ColorPicks
attr_reader :color, :place
def initialize(color, place)
@color = color
@place = place
end
end
# Determines how long the password should be
module StartUp
def self.how_many
@how_many
end
def HowManyColors
p "How long should the computer's code be?"
@how_many = gets.chomp.to_i
end
end
#Instances the password objects from ColorPicks
class Printer
include StartUp
@@i = StartUp.how_many
@@place = 1
COLORS = {1 => 'red', 2 => 'blue', 3 => 'green'}
def self.code_maker
for a in 1..@@i do
number_of_colors = COLORS.length
random_number = rand(number_of_colors)
ColorPicks.new(COLORS[random_number], @@place)
@@place += 1
end
end
end
PS. Sorry, I am new to programming.