Something wrong with inserting random numbers into an array - user game
I am trying to create a bingo game in ruby for the console, and I have a
board that gets random generated numbers on it for each user. However, for
every user it generates the same random board. Effectively @places, gets
put in array @bingo_cards. But instead of being two different hashes of
numbers, they end up the same. There is something wrong with my method
below.
The first method sets up the board, and the second method picks numbers
for it
def start_game(user_goes_first)
#bingo slots
@places = {
a1:" ",a2:" ",a3:" ", a4:" ", a5:" ",
b1:" ",b2:" ",b3:" ", b4:" ", b5:" ",
c1:" ",c2:" ",c3:" ", c4:" ", c5:" ",
d1:" ",d2:" ",d3:" ", d4:" ", d5:" ",
e1:" ",e2:" ",e3:" ", e4:" ", e5:" "
}
@places_keys = [
:a1,:a2,:a3,:a4,:a5,
:b1,:b2,:b3,:b4,:b5,
:c1,:c2,:c3,:c4,:c5,
:d1,:d2,:d3,:d4,:d5,
:e1,:e2,:e3,:e4,:e5
]
@bingo_cards = []
@user_name.each do |numbers|
@places_keys.each_with_index do |n,i|
@places[n] = pick_number(i)
end
@bingo_cards << @places
p @bingo_cards
end
user_turn
end
def pick_number(num)
#generates random numbers that make up the bingo board(s)
case num
when 0..5
rand(1..15)
when 6..10
rand(16..30)
when 11..12
rand(16..30)
when 13
"X"
when 14..15
rand(16..30)
when 16..20
rand(31..45)
when 21..25
rand(46..60)
else
0
end
end
def draw_game
puts ""
puts ""
puts " B I N G O".gray
puts ""
@bingo_cards.each do |bingo|
puts " 1 #{@places[:a1]} | #{@places[:b1]} | #{@places[:c1]} |
#{@places[:d1]} | #{@places[:e1]}".green
puts " --- --- --- --- ---"
puts " 2 #{@places[:a2]} | #{@places[:b2]} | #{@places[:c2]} |
#{@places[:d2]} | #{@places[:e2]}".green
puts " --- --- --- --- ---"
puts " 3 #{@places[:a3]} | #{@places[:b3]} | #{@places[:c3]} |
#{@places[:d3]} | #{@places[:e3]}".green
puts " --- --- --- --- ---"
puts " 4 #{@places[:a4]} | #{@places[:b4]} | #{@places[:c4]} |
#{@places[:d4]} | #{@places[:e4]}".green
puts " --- --- --- --- ---"
puts " 5 #{@places[:a5]} | #{@places[:b5]} | #{@places[:c5]} |
#{@places[:d5]} | #{@places[:e5]}".green
put_line
end
end
Example output: Michael
1 8 | 13 | 17 | 18 | 31
--- --- --- --- ---
2 3 | 29 | 25 | 40 | 47
--- --- --- --- ---
3 7 | 28 | 30 | 38 | 49
--- --- --- --- ---
4 14 | 28 | X | 41 | 57
--- --- --- --- ---
5 7 | 25 | 27 | 33 | 59
Sean Michael
1 8 | 13 | 17 | 18 | 31
--- --- --- --- ---
2 3 | 29 | 25 | 40 | 47
--- --- --- --- ---
3 7 | 28 | 30 | 38 | 49
--- --- --- --- ---
4 14 | 28 | X | 41 | 57
--- --- --- --- ---
5 7 | 25 | 27 | 33 | 59
I don't think how I am generating the random numbers is the issue: here is
the array of the numbers. For some reason there array of random numbers
one which is random and the other which is not.
Here is the printed @bingo_cards, which is where things appear to go wrong:
[{:a1=>7, :a2=>3, :a3=>8, :a4=>2, :a5=>11, :b1=>1, :b2=>22, :b3=>29,
:b4=>25, :b5=>28, :c1=>29, :c2=>17, :c3=>17, :c4=>"X", :c5=>25,
:d1=>16, :d2=>43, :d3=>31, :d4=>35, :d5=>34, :e1=>44, :e2=>57, :e3=>52,
:e4=>59, :e5=>51}]
[{:a1=>8, :a2=>3, :a3=>7, :a4=>14, :a5=>7, :b1=>13, :b2=>29, :b3=>28,
:b4=>28, :b5=>25, :c1=>17, :c2=>25, :c3=>30, :c4=>"X", :c5=>27,
:d1=>18, :d2=>40, :d3=>38, :d4=>41, :d5=>33, :e1=>31, :e2=>47, :e3=>49,
:e4=>57, :e5=>59}, {:a1=>8, :a2=>3, :a3=>7, :a4=>14, :a5=>7, :b1=>13,
:b2=>29, :b3=>28, :b4=>28, :b5=>25, :c1=>17, :c2=>25, :c3=>30,
:c4=>"X", :c5=>27, :d1=>18, :d2=>40, :d3=>38, :d4=>41, :d5=>33,
:e1=>31, :e2=>47, :e3=>49, :e4=>57, :e5=>59}]
No comments:
Post a Comment