我正在尝试为控制台创建一个 ruby 的宾果游戏,并且我有一个板可以为每个用户获取随机生成的数字。但是,对于每个用户,它都会生成相同的随机板。有效地@places,被放入数组@bingo_cards。但它们不是两个不同的数字哈希值,而是相同的。我下面的方法有问题。
第一种方法设置棋盘,第二种方法为它选择数字
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}]