Embedded loops in Ruby
I'm trying to write a program that simulates the dice game bones. The idea is to roll five dice and get the lowest score possible--threes have a value of 0. Once the five dice are rolled the player/bot MUST select at least one die (or more) from the five and roll the rest. This is what's posing a problem to me. If there's no threes then the "keeper" array which I push the dice the bot keeps to ends up being empty, which necessitates an embedded loop. Since I'm pretty new to coding I really can't figure out a way to create an embedded loop that will ensure that at least one dice is designated as a keeper. For your sanity's sake I'll say that the "beta" version of this program I'll present now is intended to do the following: Create a bot that tries to get the lowest score on one round of bones. Ie. The dice are rolled once. Then he tries to pick the lowest score possible. If there are no threes (equivalent to 0) he picks ones. The problem I'm trying to solve is creating an embedded loop that ensures at least one die is selected for the keeper array. The code is mainly to demonstrate how ugly my solution is and give an idea for a better solution.
#rolls dice
srand
dice = []
5.times do
dice.push(rand(6)+1)
end
puts dice
puts " "
#initialize keeper and roll again arrays
i = 0
keeper = []
roll_again = []
#select any 3s from the dice roll and put them in keeper
dice.each do |d|
if d == 3
keeper.push(d)
else
i +=1 #d开发者_Python百科ummy operation to keep if statement functioning, tragically ugly code
end
end
#in the case that no threes were rolled, ones are selected
if keeper.length == 0
dice.each do |f|
if f == 1
keeper.push(f)
else
i+= 1
end
end
else
i += 1
end
puts "Keeper:"
puts keeper
puts "Roll Again:"
puts roll_again
For a beginner you are doing fine :-) I'll just show you some things that can help you with this exercise, you learn best by trying out different things and trying to understand them.
>> dice = [5,1,2,2,6,4] #=> [5, 1, 2, 2, 6, 4]
>> if (threes = dice.select { |d| d == 3 }).empty?
.. ones = dice.select { |d| d == 1 }
.. end #=> [1]
>> threes #=> []
>> ones #=> [1]
Another thing you can do is group the dice array:
>> dice.group_by { |d| d } #=> {5=>[5], 1=>[1], 2=>[2, 2], 6=>[6], 4=>[4]}
That way you can instantly see if the array for a given number is empty or not. Note that it's easy to do array differences in Ruby, which might help you with removing dice:
>> [3,1,3,5,1,7] - [3] #=> [1, 5, 1, 7]
In fact array intersection also is a good way to see if a certain element is part of an array:
>> ([3,1,3,5,1,7] & [3]).empty? #=> false
>> ([4,1,2,5,1,7] & [3]).empty? #=> true
Some more tips: You don't need dummy statements for your else
branches, you can just leave them out. I also strongly recommend going through the docs of Array
and Enumerable
, there's lot of useful methods in there.
BTW, Michael Kohl created methodfinder gem. so:
$ sudo gem install methodfinder
Now we can find method we need:
[2,3,2,3,4,3,2].find_method { |x| x.unknown(3) == [3,3,3] }
=> grep
So I'd write your algorithm like this:
def keep dice
threes = dice.grep(3)
threes.empty? ? [dice.min] : threes
end
Of course there are many ways to do this. But have a look: I've made some general modifications to make it a bit more... Rubyish!
# A custom helper method
class Array
def take_while_num # Modeled off take_while, but also pass in the length so far
arr = []
each do |e|
# Call the block with this element and the current length,
# and stop if it returns false
if yield(e, arr.length)
arr << e
else
break
end
end
arr
end
end
dice = Array.new(5) { rand(6)+1 } # 5 random numbers
puts "Original dice: #{dice.inspect}"
# Sort the dice by value (3s are worth 0), then take all 3s
# or if there are no 3s, take whatever is at the front
keep = dice.sort_by {|d| d == 3 ? 0 : d }.take_while_num {|d, n| n.zero? or d == 3 }
# Remove the ones we're keeping from the list of dice
dice -= keep
puts "Dice: #{dice.inspect}"
puts "Keep: #{keep.inspect}"
An example:
Original dice: [3, 2, 4, 3, 1]
Dice: [2, 4, 1]
Keep: [3, 3]
精彩评论