Perl中,801 733个字符
我相信这是一个非常简单的实现。基本上,对于每只手,我们分别将西服和面孔分类。然后我们再制作一张ace数低的面孔副本,以便我们检查ace数低的直线。然后我们确定我们是否有同花或顺牌,以及高牌是什么。然后,我们只按得分顺序检查比赛(首先检查同花顺,然后检查四种),等等。实际得分只是手的类型的串联,其后依次是牌的面值(它们在重要版本中为_s(),在高尔夫球版本中为u())。这里是:
@l{2..9,qw(T J Q K A)}=2..14;sub u{join"",map{$_>9?$_:"0$_"}shift,ref$_[0]?$$_[0]:map{$h[$_]}@_}sub e{$p[$_[0]-1]-1==$p[$_[0]]}sub f{@p=@_;e(1)&&e(2)&&e(3)&&e 4}sub h{$h[$_[0]]==$h[$_[1]]}sub i{h(@_[0,1])&&h @_[2,3]}sub t{@s=sort map{substr($_,1)}@_;$f=$s[0]eq$s[4];@l=@h=sort{$b<=>$a}map{$l{substr($_,0,1)}}@_;@l=(@l[1..4],1)while$l[0]==14;$s=0;if(f@l){$s=1;$h=$l[0]}else{$h=$h[0];$s=1 if f@h}$f&&$s?u 9,\$h:h(4,1)?u 7,4,0:h(3,0)?u 7,3,4:i(4,3,2,0)?u 6,0,4:i(4,2,1,0)?u 6,4,0:$f?u 5,0:$s?u 4,\$h:h(4,2)?u 3,4,0,1:h(3,1)?u 3,3,0,4:h(2,0)?u 3,2..4:i(4,3,2,1)?u 2,2,4,0:i(4,3,1,0)?u 2,1,4,2:i(3,2,1,0)?u 2,1,3,4:h(4,3)?u 1,4,0,1,2:h(3,2)?u 1,3,0,1,4:h(2,1)?u 1,2,0,3,4:h(1,0)?u 1,1..4:u 0,0..4}print t(@ARGV[0..4])gt t(@ARGV[5..9])?1:2
这是高尔夫球程度较低的等效项:
use strict;
use warnings;
# ace high or low in straights, otherwise high
# T = ten, J = jack, Q = queen, K = king, A = ace
# 0 high card
# 1 one pair
# 2 two pair
# 3 3 of a kind
# 4 straight
# 5 flush
# 6 full house
# 7 four of a kind
# 9 straight flush (royal flush a subclass of straight flush)
my %l;@l{2..9,qw(T J Q K A)}=2..14;
sub score {
my @suits = sort map { substr($_,1) } @_;
my @faces_h = sort { $b <=> $a } map { $l{substr($_,0,1)} } @_;
my @faces_l = @faces_h;
@faces_l = (@faces_l[1..4], 1) while $faces_l[0] eq 14;
my $is_flush = $suits[0] eq $suits[4];
my ($is_straight, $high_card);
if($faces_l[0]-1==$faces_l[1] &&
$faces_l[1]-1==$faces_l[2] &&
$faces_l[2]-1==$faces_l[3] &&
$faces_l[3]-1==$faces_l[4]) {
$is_straight=1;
$high_card = $faces_l[0];
} else {
$high_card = $faces_h[0];
if($faces_h[0]-1==$faces_h[1] &&
$faces_h[1]-1==$faces_h[2] &&
$faces_h[2]-1==$faces_h[3] &&
$faces_h[3]-1==$faces_h[4]) {
$is_straight=1;
}
}
return _s(9, \$high_card) if $is_flush && $is_straight;
return _s(7, 4,0) if $faces_h[4] == $faces_h[1];
return _s(7, 3,4) if $faces_h[3] == $faces_h[0];
return _s(6, 0,4) if $faces_h[4] == $faces_h[3] && $faces_h[2] == $faces_h[0];
return _s(6, 4,0) if $faces_h[4] == $faces_h[2] && $faces_h[1] == $faces_h[0];
return _s(5, 0) if $is_flush;
return _s(4, \$high_card) if $is_straight;
return _s(3, 4,0,1) if $faces_h[4] == $faces_h[2];
return _s(3, 3,0,4) if $faces_h[3] == $faces_h[1];
return _s(3, 2,3,4) if $faces_h[2] == $faces_h[0];
return _s(2, 2,4,0) if $faces_h[4] == $faces_h[3] && $faces_h[2] == $faces_h[1];
return _s(2, 1,4,2) if $faces_h[4] == $faces_h[3] && $faces_h[1] == $faces_h[0];
return _s(2, 1,3,4) if $faces_h[3] == $faces_h[2] && $faces_h[1] == $faces_h[0];
return _s(1, 4,0,1,2) if $faces_h[4] == $faces_h[3];
return _s(1, 3,0,1,4) if $faces_h[3] == $faces_h[2];
return _s(1, 2,0,3,4) if $faces_h[2] == $faces_h[1];
return _s(1, 1,2,3,4) if $faces_h[1] == $faces_h[0];
return _s(0, 0..4);
}
sub _s {
join "", map { $_ > 9 ? $_ : "0$_" } shift,
ref $_[0] ? $$_[0] : map { $faces_h[$_] } @_
# my @a=@_;
# if(ref $a[1]) {
# $a[1]=${$a[1]};
# } else {
# $a[$_]=$faces_h[$a[$_]] for 1..$#a;
# }
# join "", map { $_ < 10 ? "0$_" : $_ } @a;
}
my @p1 = @ARGV[0..4];
my @p2 = @ARGV[5..9];
my $s1 = score(@p1);
my $s2 = score(@p2);
print $s1 gt $s2 ? 1 : 2;