To Examples
# Source code for Dice class.
# Test code is at the bottom.
class Dice
# Initialize dice by giving them random values.
def initialize
roll
end
# Roll dice to give them new random values.
def roll
@die1 = 1 + rand(6)
@die2 = 1 + rand(6)
end
# Show current values of dice.
def show_dice
print "Die1: ", @die1, " Die2:", @die2
end
# Show current sum of dice.
def show_sum
print "Sum of dice is ", @die1 + @die2, ".\n"
end
end
# Test Dice class.
d = Dice.new
d.show_dice
d.show_sum
d.roll
d.show_dice
d.show_sum
d.roll
d.show_dice
d.show_sum
d.roll
d.show_dice
d.show_sum
d.roll
d.show_dice
d.show_sum