文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>高级组10:21点

高级组10:21点

时间:2008-05-15  来源:starB6

#!/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=<STDIN>;
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=<STDIN>;
    chomp($answer);
}
#计算点数的函数:
sub get_point

   my $g_point;
   my @hello=@_;
   my $num=@hello;
   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";
}                      
相关阅读 更多 +
排行榜 更多 +
别惹神枪手安卓版

别惹神枪手安卓版

冒险解谜 下载
坦克战争世界

坦克战争世界

模拟经营 下载
丛林反击战

丛林反击战

飞行射击 下载