I am working on the basic mastermind project with random selection of colours and user have to guess it.
The problem is map! is supposed to go via each element and map! edits the values of the original array. But here in the first it does work as intended by changing its numerical values to a hash based value where each number represents a color like this:
module Shapes
CIRCLE = "●"
COLORS = {
1 => :red,
2 => :green,
3 => :magenta,
4 => :blue,
5 => :cyan,
6 => :yellow,
8 => :white,
9 => :black
}
end
There are two codes where one takes values from a random function and the other user inputs.
with the second one the values are [nil,nil,nil,nil]
which doesn’t make sense. Help me fix the issues with what I’m doing wrong with the map! function.
The code in my main.rb
def random_code
arr = Array.new(4) {(rand(1..6).to_i)}
puts arr
arr.map! do |item|
color = COLORS[item]
CIRCLE.colorize(color)
end
puts arr
return arr
end
end
My other code inside another module
p_code.map! do |item|
colors = COLORS[item.to_i]
CIRCLE.colorize(colors)
puts CIRCLE.colorize(colors)
end```