# 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 <