コンソール用のルビーでビンゴゲームを作成しようとしています.ユーザーごとにランダムに生成された数字を取得するボードがあります. ただし、すべてのユーザーに対して同じランダム ボードが生成されます。事実上 @places は、配列 @bingo_cards に入れられます。しかし、数値の 2 つの異なるハッシュではなく、最終的には同じになります。以下の私の方法には何か問題があります。
最初の方法でボードをセットアップし、2 番目の方法でボードの番号を選択します。
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
終わり
出力例: マイケル
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
ショーン・マイケル
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
乱数をどのように生成するかが問題だとは思いません。ここに数字の配列があります。何らかの理由で、ランダムな乱数とそうでない乱数の配列があります。
これは印刷された @bingo_cards で、問題が発生しているように見えます。
[{: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}]