[英]How can I get a random number in Ruby (v2.6.3)
I'm doing an assignment in which I have to make a simple card game.我正在做一项任务,我必须制作一个简单的纸牌游戏。 The tutor has provided me with a method to generate a random card - which I'm not allowed to change:
导师为我提供了一种生成随机卡片的方法 - 我不允许更改:
def random_card
cards = ["two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten",
"jack", "queen", "king", "ace"]
cards[rand(13)]
end
And I have tried to make a method that adds cards to the players hand, calling the random_card
method each time:我尝试制作一种方法,将卡片添加到玩家手中,每次调用
random_card
方法:
def move(random_card)
player_hand = []
while true do
puts "Make a move. Enter 'hit' or 'stick' "
choice = gets.chomp
if choice == "stick"
break
elsif choice == "hit"
# this is currently giving me the same card for each "hit"
# but a different one each time I run the code.
player_hand.push(random_card)
end
end
# TODO change this to return
puts player_hand
end
Each time I `hit' the same card gets added to the hand, My question is how to prevent this and for the method to return a "new" random card each time we run through the loop.每次我“击中”同一张牌时,我的问题是如何防止这种情况发生,以及每次运行循环时返回“新”随机牌的方法。
Thanks.谢谢。
You've used the name random_card
for two different things: 1) the method given you by your instructor;您已将
random_card
名称用于两种不同的random_card
:1) 教师给您的方法; and 2) the name of the argument to your move
method.和 2)
move
方法的参数名称。
The second usage defines a local variable, which masks (or shadows) the method name.第二种用法定义了一个局部变量,它屏蔽(或隐藏)了方法名称。 In other words, within the
move
method random_card
refers to whatever value you passed in when you called move
.换句话说,在
move
方法中random_card
指的是您在调用move
时传入的任何值。 It does not call the method.它不调用该方法。
Here's a simple illustration:这是一个简单的说明:
def foo
%w(hi hey howdy)[rand 3]
end
def bar1
puts foo
end
def bar2(foo)
puts foo
end
5.times { bar1 } # produces a random selection of hi's, hey's, and howdy's
puts
5.times { bar2 "Help! I'm stuck!" } # prints the passed message 5 times
puts
5.times { srand 12345; bar1 } # produces "howdy" 5 times
Given what you've shown, you probably don't need an argument to move
.鉴于您所展示的内容,您可能不需要参数来
move
。 If you do, give it its own name.如果你这样做,给它自己的名字。 Also make sure you aren't messing with
srand
somewhere else in your program.还要确保您没有在程序中的其他地方弄乱
srand
。
Yet another alternative would be to indicate that you want to use the method rather than the argument with the same name by using parentheses.另一种选择是通过使用括号来指示您要使用该方法而不是具有相同名称的参数。 With the example above, change
bar2
to:对于上面的示例,将
bar2
更改为:
def bar2(foo)
puts foo()
end
The empty parentheses make it clear this is an invocation of the method foo
, not a reference to the (now unused) argument foo
.空括号清楚地表明这是对方法
foo
的调用,而不是对(现在未使用的)参数foo
的引用。
here is the simple solution to your problem这是您问题的简单解决方案
def random_card
cards = ["two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten",
"jack", "queen", "king", "ace"]
cards[rand(13)]
end
def move(callback)
player_hand = []
while true do
puts "Make a move. Enter 'hit' or 'stick' "
choice = gets.chomp
if choice == "stick"
break
elsif choice == "hit"
player_hand.push(callback.call)
end
end
puts player_hand
end
and you can call this method as你可以调用这个方法作为
move(method(:random_card))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.