package A_Die; sub new{ my $class = shift; my $self = {}; $self->{VALUE} = int(rand(5)) + 1; $self->{NUM_ROLLS} = 1; $self->{HISTORY} = []; bless ($self, $class); return $self; } sub value { my $self = shift; return $self->{VALUE}; } sub num_rolls { my $self = shift; return $self->{NUM_ROLLS}; } sub history { my $self = shift; return @{ $self->{HISTORY} }; } sub roll { my $self = shift; $self->{HISTORY}[$self->num_rolls - 1] = $self->value; $self->{VALUE} = int(rand(5)) + 1; $self->{NUM_ROLLS}++; } sub info { my $self = shift; return sprintf("current value: %s, num_rolls: %d, history: %s", $self->value, $self->num_rolls, join(" ",$self->history) ) } sub toString{ my $self = shift; my $strings; $strings = { "1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five", "6" => "six", }; return $$strings{$self->value}; } sub toTextArt{ my $self = shift; my $art; my $i; @dieFaces[1..6]=( ["/---\\", "| |", "| * |", "| |", "\\---/"], ["/---\\", "| *|", "| |", "|* |", "\\---/"], ["/---\\", "| *|", "| * |", "|* |", "\\---/"], ["/---\\", "|* *|", "| |", "|* *|", "\\---/"], ["/---\\", "|* *|", "| * |", "|* *|", "\\---/"], ["/---\\", "|* *|", "|* *|", "|* *|", "\\---/"] ); for $i (0..$#{$dieFaces[$self->value]}) { $art = $art . $dieFaces[$self->value][$i] . "\n"; } return $art } 1;