阵列由索引我想通过一个Ruby阵列由索引进行迭代。我有一个游戏,我希望玩家可以单独轮流掷骰子,直到达到20,每轮都会增加他们的分数。到目前为止,它会为玩家1所有的圈,直到他们到达20,然后会做玩家2,直到他们得到20迭代通过在红宝石
players_array.each do |player|
player = Person.new(player)
until player.players_score.inject(0, :+) >= 20 do
score = player.dice_roll
player.add_to_score(score)
print player.players_score
print "\n"
print player, + player.players_score.inject(0, :+).to_s
print "\n"
end
end
我的理解可能与each_with_index
任何想法?
感谢
新代码:
players_array.each do |player|
while TRUE
player = Person.new(player)
score = player.dice_roll
player.add_to_score(score)
if player.players_score.inject(0, :+) > 20
# puts player.players_score
puts '20 hit'
break
else
next
end
end
end
更改到'players_array.each_with_index模拟做|播放器,IDX |',其中IDX将是索引 – Santhosh
@Santhosh你的意思是'players_array.each_with_index ...'? –
是的,这是一个错字。我的错。谢谢 – Santhosh