Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
cee low dice game
#!/usr/bin/perl
use strict;
sub rollThree(){
my @array;
foreach my $num (0..2){
$array[$num] = 1 + int(rand(5));
}
return \@array;
}
sub getScore(){
my $array = shift;
my @sortedArray = sort @$array;
my $first = $sortedArray[0];
my $second = $sortedArray[1];
my $third = $sortedArray[2];
if ($first ~~ $second and $second ~~ $third){
return (6 + $first); }
if ($first ~~ $second){
return $third;}
if ($second ~~ $third){
return $first;}
if ($first ~~ 1 and $second ~~ 2 and $third ~~ 3){
return -1; }
if ($first ~~ 4 and $second ~~ 5 and $third ~~ 6){
return 13;}
return 0;
}
sub printDice(){my $a = shift; print $a->[0]." ".$a->[1]." ".$a->[2]."\n"; }
print "player 1 goes..\n";
my $p1;
my $p1Score = 0;
while($p1Score ~~ 0){
$p1 = &rollThree();
$p1Score = &getScore($p1);
}
&printDice($p1);
print "player 2 goes...\n";
my $p2;
my $p2Score = 0;
while($p2Score ~~ 0){
$p2 = &rollThree();
$p2Score=&getScore($p2);
}
&printDice($p2);
if ($p1Score ~~ -1){
print "player 1 loses \n";}
if ($p1Score ~~ 13){
print "player 1 wins\n";}
if ($p1Score ~~ $p2Score){
print "game is tied...\n";exit }
if ($p1Score > $p2Score){
print "player 1 wins!!!\n";}
else { print "player 2 wins!!!\n"; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.