| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Getopt::Long (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Digest::SHA (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Game::Schnapsen::Bot; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Game::Schnapsen::Terminal; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | my %o = (mode => 'bot', level => 2, seat => 'p1'); | |||||||||||||||||||||||||||||||||||||||||
| 13 | Getopt::Long::GetOptions( | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 'variant=s' => \$o{variant}, | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 'mode=s' => \$o{mode}, | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 'level=i' => \$o{level}, | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 'seat=s' => \$o{seat}, | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 'seed=s' => \$o{seed}, | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 'ascii' => \$o{ascii}, | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 'pick!' => \$o{pick}, | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 'colour!' => \$o{colour}, | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 'color!' => \$o{colour}, | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 'help' => \$o{help}, | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | ▶ | ) or usage(1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 4 | ▶ | usage(0) if $o{help}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | usage(1, "--variant must be schnapsen or sixtysix\n") | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 3 | ▶ | unless defined $o{variant} && $o{variant} =~ /\A(?:schnapsen|sixtysix)\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $o{"variant"} and $o{"variant"} =~ /\A(?:schnapsen|sixtysix)\z/
MC/DC50%1/2
defined $o{"variant"} and $o{"variant"} =~ /\A(?:schnapsen|sixtysix)\z/
defined $o{"variant"}
$o{"variant"} =~ /\A(?:schnapsen|sixtysix)\z/
Truth table66%2/3
A and B
A= defined $o{"variant"}B= $o{"variant"} =~ /\A(?:schnapsen|sixtysix)\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | usage(1, "mode must be bot, hotseat or watch\n") | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 2 | ▶ | unless $o{mode} =~ /\A(?:bot|hotseat|watch)\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 2 | ▶ | usage(1, "seat must be p1 or p2\n") unless $o{seat} =~ /\A p[12] \z/x; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | my $levels = Game::Schnapsen::Bot->levels; | |||||||||||||||||||||||||||||||||||||||||
| 34 | usage(1, "level must be 1 to $levels\n") | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 2 | ▶ | unless $o{level} =~ /\A\d+\z/ && $o{level} >= 1 && $o{level} <= $levels; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$o{"level"} =~ /\A\d+\z/ and $o{"level"} >= 1 and $o{"level"} <= $levels
$o{"level"} =~ /\A\d+\z/ and $o{"level"} >= 1
MC/DC0%0/3
$o{"level"} =~ /\A\d+\z/ and $o{"level"} >= 1 and $o{"level"} <= $levels
$o{"level"} =~ /\A\d+\z/
$o{"level"} >= 1
$o{"level"} <= $levels
Truth table25%1/4
A and B and C
A= $o{"level"} =~ /\A\d+\z/B= $o{"level"} >= 1C= $o{"level"} <= $levels
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | ▶ | my $seed = defined $o{seed} ? Digest::SHA::sha256($o{seed}) : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | my $ui = Game::Schnapsen::Terminal->new( | ||||||||||||||||||||||||||||||||||||||||||
| 40 | variant => $o{variant}, | ||||||||||||||||||||||||||||||||||||||||||
| 41 | mode => $o{mode}, | ||||||||||||||||||||||||||||||||||||||||||
| 42 | level => $o{level}, | ||||||||||||||||||||||||||||||||||||||||||
| 43 | seat => $o{seat}, | ||||||||||||||||||||||||||||||||||||||||||
| 44 | ($o{ascii} ? (ascii => 1) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 45 | (defined $o{colour} ? (colour => $o{colour}) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 2 | ▶ | (defined $o{pick} ? (picking => $o{pick} ? 1 : 0) : ()), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 2 | for my $signal (qw/ INT TERM HUP /) { | |||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | $SIG{$signal} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | $ui->leave_raw; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | $SIG{ $_[0] } = 'DEFAULT'; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | kill $_[0], $$; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 6 | }; | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | ▶ | my $game = eval { $ui->play(defined $seed ? (seed => $seed) : ()) }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | my $died = $@; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | $ui->leave_raw; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | ▶ | die $died if $died; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | ▶ | exit 1 unless ref $game && $game->over; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $game and $game->over
MC/DC0%0/2
ref $game and $game->over
ref $game
$game->over
Truth table33%1/3
A and B
A= ref $gameB= $game->over
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | exit 0 if $o{mode} ne 'bot'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | exit 0 if ($game->winner // '') eq $o{seat}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$game->winner // ""
MC/DC0%0/1
$game->winner // ""
$game->winner
Truth table0%0/2
A
A= $game->winner
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | exit 1; | |||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | ▶ | sub usage { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usage: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 2 | my ($status, $message) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | ▶ | my $fh = $status ? \*STDERR : \*STDOUT; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 2 | ▶ | print {$fh} $message if $message; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 2 | print {$fh} <<'USAGE'; | |||||||||||||||||||||||||||||||||||||||||
| 72 | schnapsen - Schnapsen and Sixty-Six at a prompt | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | schnapsen --variant schnapsen the Austrian game, 20 cards, 7 down to 0 | ||||||||||||||||||||||||||||||||||||||||||
| 75 | schnapsen --variant sixtysix the German parent, 24 cards, 0 up to 7 | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | --seat p2 play the bot as p2 | ||||||||||||||||||||||||||||||||||||||||||
| 78 | --mode hotseat two people at one keyboard | ||||||||||||||||||||||||||||||||||||||||||
| 79 | --mode watch two bots, and you read | ||||||||||||||||||||||||||||||||||||||||||
| 80 | --level 1 a weaker opponent (1 or 2) | ||||||||||||||||||||||||||||||||||||||||||
| 81 | --seed tuesday deal the same match every time | ||||||||||||||||||||||||||||||||||||||||||
| 82 | --ascii letters instead of the suit pips | ||||||||||||||||||||||||||||||||||||||||||
| 83 | --nocolour no colour (NO_COLOR does the same) | ||||||||||||||||||||||||||||||||||||||||||
| 84 | --nopick type the cards out instead of pointing at them | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | --variant has no default, because the two are different games and picking one | ||||||||||||||||||||||||||||||||||||||||||
| 87 | silently would make the other an afterthought. They differ in thirteen places: | ||||||||||||||||||||||||||||||||||||||||||
| 88 | the pack, the card that exchanges for the trump, whether a marriage may be | ||||||||||||||||||||||||||||||||||||||||||
| 89 | declared late, what the last trick is worth, which way the score runs, and more. | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | At the prompt, name a card to play it: KH, TH, or 10H for the same card. Then | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | m H declare a marriage in hearts, and lead one of the two | ||||||||||||||||||||||||||||||||||||||||||
| 94 | x exchange your lowest trump for the turn-up | ||||||||||||||||||||||||||||||||||||||||||
| 95 | c close the talon | ||||||||||||||||||||||||||||||||||||||||||
| 96 | out claim 66 and end the deal | ||||||||||||||||||||||||||||||||||||||||||
| 97 | d draw (sixtysix, where you may close before drawing instead) | ||||||||||||||||||||||||||||||||||||||||||
| 98 | ? what is legal right now | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | Claiming is typed in full. A claim that turns out to be false loses the deal, and | ||||||||||||||||||||||||||||||||||||||||||
| 101 | the screen already shows you your own count, so it is not put a keystroke away | ||||||||||||||||||||||||||||||||||||||||||
| 102 | from anything else. | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | On a terminal the left and right keys walk your own hand instead, and the line | ||||||||||||||||||||||||||||||||||||||||||
| 105 | under it says what the card under the cursor would do to the trick: whether it | ||||||||||||||||||||||||||||||||||||||||||
| 106 | takes it, what the trick is worth, and what that puts you on. Enter plays it. | ||||||||||||||||||||||||||||||||||||||||||
| 107 | The moves that are not cards keep their own keys, x c m o d, because a card is | ||||||||||||||||||||||||||||||||||||||||||
| 108 | a thing in your hand and those are not. t goes back to typing, ? lists the | ||||||||||||||||||||||||||||||||||||||||||
| 109 | keys. Needs Term::ReadKey; without it the cards are typed. | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | The card you drew at the end of the last trick is drawn in a heavier frame, so | ||||||||||||||||||||||||||||||||||||||||||
| 112 | you can see what is new without counting the hand. | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | Exit status is 0 if you won, or if nobody was playing a seat, 1 if you lost or | ||||||||||||||||||||||||||||||||||||||||||
| 115 | stopped early, and 1 for an option the game will not take. | ||||||||||||||||||||||||||||||||||||||||||
| 116 | USAGE | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 2 | exit $status; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||