将随机数插入数组 - 用户游戏有问题

时间:2013-08-18 01:40:29

标签: 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

示例输出: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

肖恩 迈克尔

 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}]

2 个答案:

答案 0 :(得分:2)

1)您发布的代码如何产生您发布的输出?

2)你的start_game方法从不使用它的参数变量。摆脱它。

3)我不认为以下内容可以完成任何事情:

@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 = Hash.new {|hash, key| hash[key] = " "}

3)我甚至不确定你为什么要在你的电路板上使用Hash。如果您使用数组数组似乎会使事情变得更容易:

[
   [......],   #each row is an array
   [......],
   [......],
   [......],
   [......],
]

要创建5x5随机数数组,您可以执行以下操作:

cols = 5
rows = 5

places = []
count = 0

rows.times do |i|
  row = []

  cols.times do |j|
    row << pick_number(count)
    count += 1
  end

  places << row
end

4)对于输出,你真的想将列转换为行吗?如果是的话,

output = places.transpose

然后所有这些行:

 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

可以简化为:

output.each_with_index do |row, i|
  row.unshift i+1

  strs = row.map do |num|
    num.to_s.center(4)
  end

  puts strs.join("|") 
  puts "---  " * (cols+1)

end

...这会让你得到这样的东西:

 1  | 1  | 1  | 28 | 27 | 44 
---  ---  ---  ---  ---  ---  
 2  | 1  | 16 | 16 | 31 | 53 
---  ---  ---  ---  ---  ---  
 3  | 8  | 29 | 17 | 35 | 56 
---  ---  ---  ---  ---  ---  
 4  | 15 | 24 | X  | 37 | 49 
---  ---  ---  ---  ---  ---  
 5  | 10 | 17 | 24 | 37 | 55 
---  ---  ---  ---  ---  ---  

答案 1 :(得分:1)

当您将哈希对象放入数组中,然后更改原始哈希对象时,这些更改会反映在数组中,因为数组中的对象是同一个对象。

:001 > h = {}
 => {} 
:002 > h[:a1] = 1
 => 1 
:003 > h[:a2] = 2
 => 2 
:004 > arr = []
 => [] 
:005 > arr << h
 => [{:a1=>1, :a2=>2}] 
:006 > h[:a2] = 4
 => 4 
:007 > arr
 => [{:a1=>1, :a2=>4}] 

您可能希望将哈希对象的副本放入数组中。使用dupclone方法制作浅色副本(这就是您需要的所有内容)。

:016 >   arr = []
 => [] 
:017 > h
 => {:a1=>1, :a2=>4} 
:018 > arr << h.dup
 => [{:a1=>1, :a2=>4}] 
:019 > h[:a1] = 15
 => 15 
:020 > arr
 => [{:a1=>1, :a2=>4}] 
:021 > h
 => {:a1=>15, :a2=>4}