# implements the game Petals Around the Rose # no arguments will roll 5 six sided die and report the score # help will report the three things that a human is allowed to tell a human player: # 1) The name of the game is Petals Around the Rose. # 2) The name of the game is important. # 3) The score will always be zero or an even number. # and report that the goal is to work out how to calculate the score use Getopt::Long; my @args; @args = [@ARGV]; Getopt::Long::Configure("prefix_pattern=--|-|\/"); my $opt_help=''; GetOptions('help|?'=>\$opt_help); usage() if $opt_help; =begin comment #example command parsing Getopt::Long::Configure("prefix_pattern=\/|-|--"); my $opt_chas=''; my $opt_help=''; GetOptions('chas'=>\$opt_chas, 'help|?'=>\$opt_help); print "chaswashere\n" if $opt_chas; usage() if $opt_help; =end comment =cut #print "hello, world\n"; my $i; my $roll; my @aDie; my @Dice; my $answer; $answer = 0; for $i (1..5) { $roll= int(rand(6))+1; $answer += $roll -1 if int($roll / 2) != ($roll / 2); $aDie = getDie($roll); #print @aDie; #for $linenum ( 0..$#{$aDie} ){print ${$aDie}[$linenum],"\n";}; for $linenum ( 0..$#{$aDie} ){$Dice[$linenum][++$#{$Dice[$linenum]}] = ${$aDie}[$linenum]; }; } my $line; $line=""; my $DieNum; for $linenum ( 0..$#Dice ){ for $DieNum (0..$#{$Dice[$linenum]}) { $line .= $Dice[$linenum][$DieNum]; } print $line,"\n"; $line=""; } print "\nThe score is ",$answer,"\n"; #exit $answer; sub getDie { my $retrefval; $dicenum = shift(@_); @dice[1..6]=( ["/---\\", "| |", "| * |", "| |", "\\---/"], ["/---\\", "| *|", "| |", "|* |", "\\---/"], ["/---\\", "| *|", "| * |", "|* |", "\\---/"], ["/---\\", "|* *|", "| |", "|* *|", "\\---/"], ["/---\\", "|* *|", "| * |", "|* *|", "\\---/"], ["/---\\", "|* *|", "|* *|", "|* *|", "\\---/"] ); #for $linenum ( 0..$#{$dice[$dicenum]} ){print $dice[$dicenum][$linenum],"\n";}; $retrefval = $dice[$dicenum]; # print "there are ", $#{$retrefval}, " elements in the return array\n"; # for $linenum ( 0..$#{$retrefval} ){print ${$retrefval}[$linenum],"\n";}; # print ${$retrefval}[0],"\n",${$retrefval}[1],"\n",${$retrefval}[2],"\n",${$retrefval}[3],"\n",${$retrefval}[4],"\n"; return $retrefval; #print $dice[1][0],"\n",$dice[1][1],"\n",$dice[1][2],"\n",$dice[1][3],"\n",$dice[1][4],"\n"; } sub usage { print "\nUsage: \n"; print "\t",$0," [/?]\n\n"; print <## 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; #### ####################### # testing single die ####################### use A_Die; my $a_die; $a_die = A_Die->new(); printf " Die value is: %s\n", $a_die->value; printf " Die num_rolls is: %s\n", $a_die->num_rolls; printf " Die history is: %s\n", $a_die->history; print $a_die->info . "\n"; print $a_die->toString . "\n"; print $a_die->toTextArt . "\n"; for $i (0..3) { $a_die->roll(); print $a_die->info . "\n"; print $a_die->toString . "\n"; print $a_die->toTextArt . "\n"; } ####################### # testing group of dice ####################### use Dice; my $some_dice; $some_dice = Dice->new(); printf "Dice num_dice is: %s\n", $some_dice->num_dice; $some_dice->show_dice(); for $i (0..3) { $some_dice->add_die(); printf "Dice num_dice is: %s\n", $some_dice->num_dice; $some_dice->show_dice(); } print $some_dice->toTextArt() . "\n"; print "that's: " . $some_dice->toString() . "\n"; for $i (1..3) { $some_dice->roll_all(); $some_dice->show_dice(); print $some_dice->toTextArt() . "\n"; print "that's: " . $some_dice->toString() . "\n"; } print "Rolling only dice 2 and 4\n"; ${$some_dice->dice}[1]->roll(); # zero offset ${$some_dice->dice}[3]->roll(); # zero offset $some_dice->show_dice(); print $some_dice->toTextArt() . "\n"; print "that's: " . $some_dice->toString() . "\n"; print "\nremoving die from middle and end\n"; for $i (1..2) { print "removing die $i\n"; print $some_dice->rem_die(${$some_dice->dice}[1]) . "\n"; $some_dice->show_dice(); print $some_dice->toTextArt() . "\n"; print "that's: " . $some_dice->toString() . "\n"; } print "\nremoving die from begining and removing last die\n"; for $i (1..2) { print "removing die $i\n"; print $some_dice->rem_die(${$some_dice->dice}[0]) . "\n"; $some_dice->show_dice(); print $some_dice->toTextArt() . "\n"; print "that's: " . $some_dice->toString() . "\n"; } #### package Dice; use A_Die; my $debug = 0; sub new{ my $class = shift; my $self = {}; $self->{NUM_DICE} = 0; $self->{DICE} = []; bless ($self, $class); return $self; } sub num_dice { my $self = shift; return $self->{NUM_DICE}; } sub dice { my $self = shift; return ( $self->{DICE} ); } sub add_die { my $self = shift; my $die = A_Die->new(); ${$self->dice}[$#{$self->dice} + 1] = $die; $self->{NUM_DICE}++; print "new die added: " . $die->info() . "\n" if $debug; return $die; } sub show_dice { my $self = shift; my $i; print "No dice\n" unless @{$self->dice}; for $i (0..$#{$self->dice}) { print ${$self->dice}[$i]->info() . "\n"; } } sub rem_die { my $self = shift; return -1 if @_ != 1; # should be oaoo arg my $arg = shift; my $die; my $i; for ($i=0;$i<=$#{$self->dice};$i++) { last if $arg == ${$self->dice}[$i]; } #assert: $i is the index of the array where die referenced by $arg is located or is 1 greater than $#dice $die = splice(@{$self->dice},$i,1); #should remove that element and return it $self->{NUM_DICE}--; if ($debug) { print ($die == $arg ? "die removed: \$die = $die\n" : "failed to remove die: \$arg = $arg\n"); } $die == $arg ? return $die : return -1; } sub roll_all { my $self = shift; for $i (0..$#{$self->dice}) { ${$self->dice}[$i]->roll(); } } sub toString{ my $self = shift; my $i; my $retstring; if (@{$self->dice} == 0) { $retstring = "No dice\n" } else { for $i (0..$#{$self->dice}) { $retstring = $retstring . ${$self->dice}[$i]->toString(); $retstring = $retstring. " " unless $i == $#{$self->dice}; } } return $retstring; } sub toTextArt{ my $self = shift; my $art; my $textrow; my $NUM_ROWS = 5; @dieFaces[1..6]=( ["/---\\", "| |", "| * |", "| |", "\\---/"], ["/---\\", "| *|", "| |", "|* |", "\\---/"], ["/---\\", "| *|", "| * |", "|* |", "\\---/"], ["/---\\", "|* *|", "| |", "|* *|", "\\---/"], ["/---\\", "|* *|", "| * |", "|* *|", "\\---/"], ["/---\\", "|* *|", "|* *|", "|* *|", "\\---/"] ); if (@{$self->dice} == 0) { $art = "/---\\\n" . "| |\n" . "| |\n" . "| |\n" . "\\---/\n"; } else { for $textrow (0..$NUM_ROWS) { for $die (0..$#{$self->dice}) { $art = $art . $dieFaces[${$self->dice}[$die]->value][$textrow]; } $art = $art . "\n"; } } return $art } 1; #### # implements the game Petals Around the Rose # no arguments will roll 5 six sided die and report the score # help will report the three things that a human is allowed to tell a human player: # 1) The name of the game is Petals Around the Rose. # 2) The name of the game is important. # 3) The score will always be zero or an even number. # and report that the goal is to work out how to calculate the score use Getopt::Long; use A_Die; use Dice; my @args; @args = [@ARGV]; Getopt::Long::Configure("prefix_pattern=--|-|\/"); my $opt_help=''; GetOptions('help|?'=>\$opt_help); usage() if $opt_help; my $i; my $dice; my $die; my $answer; $dice = Dice->new(); $answer = 0; for $i (1..5) { $die = $dice->add_die(); $roll = $die->value; $answer += $roll -1 if int($roll / 2) != ($roll / 2); } print $dice->toTextArt(); print "\nThe score is ",$answer,"\n"; sub usage { print "\nUsage: \n"; print "\t",$0," [/?]\n\n"; print <