#!/usr/bin/perl -w
use strict;
my @suits=("Hearts" ,"Diamonds" ,"Clubs" ,"Spades");
my @facecards=("Jacks", "Queens", "Kings");
my @dictionary;
my $keys; #
for(my $i=2;$i<=10;$i++)
{
for(my $j=0;$j<4;$j++)
{
$keys= "$i of $suits[$j]";
$keys=$keys.","."$i";
# $dictionary{"$keys"}=$i;
push(@dictionary,$keys);
}
} # 2-10 cards insert into the dictionary.
for(my $i=0;$i<3;$i++)
{
for(my $j=0;$j<4;$j++)
{
$keys= "$facecards[$i] of $suits[$j]";
$keys=$keys.","."10";
# $dictionary{"$keys"}=$i;
push(@dictionary,$keys);
# $dictionary{"$keys"}=10;
}
}# insert the face cards into the dictionary
for(my $j=0;$j<4;$j++)
{
$keys= "Ace of $suits[$j]";
$keys=$keys.","."11";
# $dictionary{"$keys"}=$i;
push(@dictionary,$keys);
# $dictionary{"$keys"}=11;
}# insert the ace into the dictionary
my $count= @dictionary; # keys %dictionary; # 个数
my $key;
my $value;
#print "@dictionary";
#while(($key,$value)=each(%dictionary))
#{print "$key,$value\n";}
#print "******there are $count cards*******\n";
#### generate the random cards :::
my $myrand;
my $low=0;
my @dealer;
my @player;
# 生成cards
sub generate
{
my $forprint;
for ( my $i=0;$i<2;$i++)
{
$myrand=int (rand($count-1) + $low);
push(@dealer,$dictionary[$myrand]);
}
$forprint=(split /,/,$dealer[1])[0];
print "The one cards of the dealer are:\n$forprint\n ";
print "The cards of the players are:\n";
for ( my $i=0;$i<2;$i++)
{
$myrand=int (rand($count-1) + $low);
push(@player,$dictionary[$myrand]);
$forprint=(split /,/,$player[$i])[0];
print "$forprint\n ";
}
}
& generate;
#再次抓牌,在选择抓的情况下。
sub generate_player
{
$myrand=int (rand($count-1) + $low);
push(@player,$dictionary[$myrand]);
}
print "do you want to stay(s) or hit(t)?\n";
my $answer=;
chomp($answer);
until ( $answer ne "t")
{
& generate_player;
print "\n The carda of the players is:\n";
foreach(@player)
{
print "$_\n";
}
print "\nDo you want to stay(s) or hit(t)?\n";
$answer=;
chomp($answer);
}
#计算点数的函数:
sub get_point
{
my $g_point;
my @hello=@_;
my ;
for( my $i=0; $i<$num; $i++ )
{
$g_point+=(split /,/,$hello[$i])[1];
}
# print "the point is ::::$g_point\n";
return $g_point;
}
my $get_mypoint = & get_point(@player);
print " \n You(player) have choose staying this cards ,your point now is :$get_mypoint\n";
if ($get_mypoint > 21)
{
print "\nYour point is bigger than 21,you've lost.\n";
exit;
}
my $get_deal_point = & get_point(@dealer);
print "the get_deal_point is $get_deal_point\n";
#dealer 根据具体情况确定是否要牌
until ( $get_deal_point >= $get_mypoint )
{
for ( my $i=0;$i<3;$i++ )
{
my $rand_int=int(rand(0)+0);
if ( $rand_int == 0 )
{
$myrand=int (rand($count-1) + $low);
push(@dealer,$dictionary[$myrand]);
}
$get_deal_point = & get_point(@dealer);
goto MY_WIN if $get_deal_point >= $get_mypoint;
}
MY_WIN:
print "\n";
}
if ($get_mypoint > $get_deal_point || $get_deal_point >= 21 )
{
print "Congratulations ! you win this game !\n";
}
else
{
print " Your point is $get_mypoint, the dealers' is $get_deal_point.so , sorry ,you lost this game. \n";
}
阅读(899) | 评论(0) | 转发(0) |