perl撲克牌遊戲

編寫一個腳本,從一疊紙牌中隨機抽出10張,並打印結果值。

a:該腳本應當先借助foreach循環構建一套52張牌。

b:外層循環負責按照花色遍歷整套紙牌:梅花,方塊,紅桃,黑桃。內層循環則針對每一種花色遍歷所有數字:A、1到10、J、Q、K。從各種花色獲得的紙牌都將賦值到一個數組中。

c:使用rand()函數從上述數組中隨機獲得一張紙牌。這樣做便能保證獲得的10張紙牌互不重複。

[root@dou shili]# cat 53.pl
#!/usr/bin/perl -w
use strict;

my @cards;
my $how_many = 10;
my @colors = qw/hearts spades diamonds clubs/;
my @nums = qw/A 2 3 4 5 6 7 8 9 10 J Q K/;

foreach my $suit (@colors)
{
      foreach my $num (@nums) {
          push(@cards, [$num, $suit]);
          }
}

foreach (1 .. $how_many)
{
     my $idx = int(rand(scalar(@cards)));
     my $card = splice(@cards, $idx, 1);
     print "The card is $card->[1]:$card->[0]\n";
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章