I want to iterate through a Ruby array by index. I have a game where I want the players to have individual turns of rolling a dice until one gets to 20, each turn adds to their score. So far it will do all the turns for player 1 until they get to 20 and then will do player 2 until they get to 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
I understand it might be with each_with_index
any ideas?
thanks
new code:
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 do |player, idx|, where idx will be the indexplayers_array.each_with_index...?playerin every iteration?