I need to discuss this source code. Each code and line should be discussed.
#!/usr/local/bin/ruby
row1 = row2 = row3 = ""
edge = "+------+"
top = {
"1" => "| |",
"2" => "| []|",
"3" => "| []|",
"4" => "|[] []|",
"5" => "|[] []|",
"6" => "|[][][]|",
}
middle = {
"1" => "| [] |",
"2" => "| |",
"3" => "| [] |",
"4" => "| |",
"5" => "| [] |",
"6" => "|[][][]|",
}
bottom = {
"1" => "| |",
"2" => "|[] |",
"3" => "|[] |",
"4" => "|[] []|",
"5" => "|[] []|",
"6" => "|[][][]|",
}
$set = Array.new
def roll(times)
count = 0
while count < times
temp = rand(7)
$set.push(temp.to_s) if temp != 0
count += 1 if temp != 0
end
end
times = 1
while times != 0
puts "Please enter the number of times to roll."
puts "Type \"0\" and press enter to quit."
times = gets.chomp.to_i
roll(times)
rolled = $set.length
$set.each do |index|
row1 += top.fetch(index) + " "
row2 += middle.fetch(index) + " "
row3 += bottom.fetch(index) + " "
end
rolled.times{ print edge + " " }
print "\n"
puts row1 + "\n" + row2 + "\n" + row3 + "\n"
rolled.times{ print edge + " " }
print "\n"
$set.clear
row1 = row2 = row3 = ""
end
Thank you!
#!/usr/local/bin/ruby
row1 = row2 = row3 = ""
edge = "+------+"
top = {
"1" => "| |",
"2" => "| []|",
"3" => "| []|",
"4" => "|[] []|",
"5" => "|[] []|",
"6" => "|[][][]|",
}
middle = {
"1" => "| [] |",
"2" => "| |",
"3" => "| [] |",
"4" => "| |",
"5" => "| [] |",
"6" => "|[][][]|",
}
bottom = {
"1" => "| |",
"2" => "|[] |",
"3" => "|[] |",
"4" => "|[] []|",
"5" => "|[] []|",
"6" => "|[][][]|",
}
$set = Array.new
def roll(times)
count = 0
while count < times
temp = rand(7)
$set.push(temp.to_s) if temp != 0
count += 1 if temp != 0
end
end
times = 1
while times != 0
puts "Please enter the number of times to roll."
puts "Type \"0\" and press enter to quit."
times = gets.chomp.to_i
roll(times)
rolled = $set.length
$set.each do |index|
row1 += top.fetch(index) + " "
row2 += middle.fetch(index) + " "
row3 += bottom.fetch(index) + " "
end
rolled.times{ print edge + " " }
print "\n"
puts row1 + "\n" + row2 + "\n" + row3 + "\n"
rolled.times{ print edge + " " }
print "\n"
$set.clear
row1 = row2 = row3 = ""
end
Thank you!